0

The VaultSharp package seems to contain all I want and it is well-documented.
I tried to use VaultSharp package to read our secrets from a Vault server.
But my rusty C# stopped me at line

Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2
     .ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");

with error message:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VaultSharp;
using VaultSharp.V1.AuthMethods.AppRole;
using VaultSharp.V1.AuthMethods;
using VaultSharp.V1.Commons;
using VaultSharp.V1.AuthMethods.Token;

namespace VaultConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var vaultUrl = "https://vault-server.url.com:443";
            Program.by_token(vaultUrl);
        }
     
        static void by_token(string vaultUrl)
        {
            // Initialize one of the several auth methods.
            IAuthMethodInfo authMethod = new TokenAuthMethodInfo("s.R2gFHDiup5wCeHHksfc2zKUN");

            // Initialize settings. You can also set proxies, custom delegates etc. here.
            var vaultClientSettings = new VaultClientSettings(vaultUrl, authMethod);

            IVaultClient vaultClient = new VaultClient(vaultClientSettings);

            // Use client to read a key-value secret.
            Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");
        }
    }
}

The codes are essentially copied from http://rajanadar.github.io/VaultSharp/

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
user3400152
  • 133
  • 6
  • 13
  • Do what the message tells you to do. – Aluan Haddad Jul 14 '20 at 23:24
  • Microsoft has great documentation about this: [ms async docs](https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/) ; Depending on what you're trying to achieve you'll be better off using a library for loading configuration. Check these out: [stratio.externsions.configuration.vault](https://github.com/stratio-automotive/Stratio.Extensions.Configuration.Vault) [VaultSharp.Extensions.Configuration](https://github.com/MrZoidberg/VaultSharp.Extensions.Configuration) [NetEscapades.Configuration.Vault](https://github.com/andrewlock/NetEscapades.Configuration) – Bernardo Marques Jul 26 '23 at 09:32

2 Answers2

2

The error is telling you exactly what you need to know. Though let's extrapolate:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

static async Task Main(string[] args)
{
    var vaultUrl = "https://vault-server.url.com:443";
    await Program.by_token(vaultUrl);
}

 
static async Task by_token(string vaultUrl)
{
    ...
    Secret<SecretData> kv2Secret = await vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");
}

At this time you should do some research on the async and await pattern:

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks. it worked. I did not write C# for a while and did not use async and await before, – user3400152 Jul 15 '20 at 00:49
  • It runs. But when I tried to see what got returns using: Console.Writeline("data {0}", kv2Secret.Data); then the last line was not reached and I could not get the data. Obviously I still do not understand how the await works. How can I get the data returned after the ReadSecretAsync method execution? – user3400152 Jul 15 '20 at 01:47
  • If you cannot use async for whatever reason, you can try this sync way. Secret kv2Secret = vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds").Result – Raja Nadar Jul 24 '20 at 05:14
  • @RajaNadar there is a good chance this will cause a deadlock in a UI framework – TheGeneral Jul 24 '20 at 05:17
  • Yes. Agree. The op has a console app. And it looked like they are just trying out the snippet. Hence I didn't call the deadlock aspect with the usage of .Result out. The Vault sharp docs do call out sync usage and deadlock risks. VaultSharp also has a continuationcontext flag to help. – Raja Nadar Jul 25 '20 at 06:03
0

Instead of making the method async you can also remove "await" which will make it synchronous and not async.

var kv2Secret = vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync("/secret/my_corp/my_app/dev/db_creds");

The best thing to do for you depends on your use case though so I suggest reading up on it first as suggested in other answer.

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44