33
44using System ;
55using System . Threading . Tasks ;
6- using Microsoft . Azure . KeyVault ;
7- using Microsoft . Azure . Services . AppAuthentication ;
6+ using Azure . Core ;
7+ using Azure . Identity ;
8+ using Azure . Security . KeyVault . Secrets ;
89using Microsoft . Extensions . Logging ;
9- using Microsoft . IdentityModel . Clients . ActiveDirectory ;
10+ using AzureSecurityKeyVaultSecret = Azure . Security . KeyVault . Secrets . KeyVaultSecret ;
1011
1112namespace NuGet . Services . KeyVault
1213{
@@ -17,12 +18,9 @@ namespace NuGet.Services.KeyVault
1718 public class KeyVaultReader : ISecretReader
1819 {
1920 private readonly KeyVaultConfiguration _configuration ;
20- private readonly string _vault ;
21- private readonly Lazy < KeyVaultClient > _keyVaultClient ;
22- private ClientAssertionCertificate _clientAssertionCertificate ;
21+ private readonly Lazy < SecretClient > _keyVaultClient ;
2322
24- protected string VaultBaseUrl => _vault ;
25- protected KeyVaultClient KeyVaultClient => _keyVaultClient . Value ;
23+ protected SecretClient KeyVaultClient => _keyVaultClient . Value ;
2624
2725 public KeyVaultReader ( KeyVaultConfiguration configuration )
2826 {
@@ -32,8 +30,7 @@ public KeyVaultReader(KeyVaultConfiguration configuration)
3230 }
3331
3432 _configuration = configuration ;
35- _vault = $ "https://{ _configuration . VaultName } .vault.azure.net/";
36- _keyVaultClient = new Lazy < KeyVaultClient > ( InitializeClient ) ;
33+ _keyVaultClient = new Lazy < SecretClient > ( InitializeClient ) ;
3734 }
3835
3936 public async Task < string > GetSecretAsync ( string secretName )
@@ -43,7 +40,7 @@ public async Task<string> GetSecretAsync(string secretName)
4340
4441 public async Task < string > GetSecretAsync ( string secretName , ILogger logger )
4542 {
46- var secret = await _keyVaultClient . Value . GetSecretAsync ( _vault , secretName ) ;
43+ AzureSecurityKeyVaultSecret secret = await _keyVaultClient . Value . GetSecretAsync ( secretName ) ;
4744 return secret . Value ;
4845 }
4946
@@ -54,36 +51,36 @@ public async Task<ISecret> GetSecretObjectAsync(string secretName)
5451
5552 public async Task < ISecret > GetSecretObjectAsync ( string secretName , ILogger logger )
5653 {
57- var secret = await _keyVaultClient . Value . GetSecretAsync ( _vault , secretName ) ;
58- return new KeyVaultSecret ( secretName , secret . Value , secret . Attributes . Expires ) ;
54+ AzureSecurityKeyVaultSecret secret = await _keyVaultClient . Value . GetSecretAsync ( secretName ) ;
55+ return new KeyVaultSecret ( secretName , secret . Value , secret . Properties . ExpiresOn ) ;
5956 }
6057
61- private KeyVaultClient InitializeClient ( )
58+ private SecretClient InitializeClient ( )
6259 {
60+ TokenCredential credential = null ;
61+
6362 if ( _configuration . UseManagedIdentity )
6463 {
65- var azureServiceTokenProvider = new AzureServiceTokenProvider ( ) ;
66- return new KeyVaultClient ( new KeyVaultClient . AuthenticationCallback ( azureServiceTokenProvider . KeyVaultTokenCallback ) ) ;
64+ if ( string . IsNullOrEmpty ( _configuration . ClientId ) )
65+ {
66+ credential = new DefaultAzureCredential ( ) ;
67+ }
68+ else
69+ {
70+ credential = new ManagedIdentityCredential ( _configuration . ClientId ) ;
71+ }
6772 }
6873 else
6974 {
70- _clientAssertionCertificate = new ClientAssertionCertificate ( _configuration . ClientId , _configuration . Certificate ) ;
71- return new KeyVaultClient ( GetTokenAsync ) ;
75+ credential = new ClientCertificateCredential ( _configuration . TenantId , _configuration . ClientId , _configuration . Certificate ) ;
7276 }
77+ return new SecretClient ( GetKeyVaultUri ( _configuration ) , credential ) ;
7378 }
7479
75- private async Task < string > GetTokenAsync ( string authority , string resource , string scope )
80+ private Uri GetKeyVaultUri ( KeyVaultConfiguration keyVaultConfiguration )
7681 {
77- var authContext = new AuthenticationContext ( authority ) ;
78- var result = await authContext . AcquireTokenAsync ( resource , _clientAssertionCertificate , _configuration . SendX5c ) ;
79-
80- if ( result == null )
81- {
82- throw new InvalidOperationException ( "Bearer token acquisition needed to call the KeyVault service failed" ) ;
83- }
84-
85- return result . AccessToken ;
82+ var uriString = $ "https://{ keyVaultConfiguration . VaultName } .vault.azure.net/";
83+ return new Uri ( uriString ) ;
8684 }
8785 }
88-
8986}
0 commit comments