Created
January 2, 2019 11:40
-
-
Save justinyoo/3ae0b1e3d47454b9ed6f9fb4290e1cae to your computer and use it in GitHub Desktop.
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