user3400152
user3400152

Reputation: 133

Read Vault secret with VaultSharp in C#

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/

Upvotes: 0

Views: 7341

Answers (2)

PostureOfLearning
PostureOfLearning

Reputation: 3541

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.

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81563

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:

Upvotes: 2

Related Questions