Accessing to Key Vault from Azure Functions with Managed Identity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppModule : Module | |
{ | |
public override void Load(IServiceCollection services) | |
{ | |
var azureServiceTokenProvider = new AzureServiceTokenProvider(); | |
var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)); | |
services.AddSingleton<IKeyVaultClient>(kv); | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetSecretFunction : FunctionBase<ILogger>, IGetSecretFunction | |
{ | |
private readonly IKeyVaultClient _kv; | |
... | |
public GetSecretFunction(AppSettings settings, IMapper mapper, IKeyVaultClient kv) | |
{ | |
this._kv = kv ?? throw new ArgumentNullException(nameof(kv)); | |
... | |
} | |
public override async Task<TOutput> InvokeAsync<TInput, TOutput>(TInput input, FunctionOptionsBase options = null) | |
{ | |
... | |
var secret = await this._kv | |
.GetSecretAsync("https://my-keyvault.vault.azure.net/", "[secret key]") | |
.ConfigureAwait(false); | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var provider = new AzureServiceTokenProvider(); | |
var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback)); | |
var secret = await kv.GetSecretAsync("https://my-keyvault.vault.azure.net", "[secret key]") | |
.ConfigureAwait(false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment