33using System . Linq ;
44using System . Security . Cryptography . X509Certificates ;
55using System . Threading . Tasks ;
6- using Microsoft . Azure . KeyVault ;
7- using Microsoft . Azure . KeyVault . Models ;
6+ using Azure . Security . KeyVault . Certificates ;
7+ using Azure . Security . KeyVault . Secrets ;
88using Microsoft . Azure . Services . AppAuthentication ;
99
1010namespace StsServerIdentity . Services . Certificate
@@ -25,46 +25,57 @@ public KeyVaultCertificateService(string keyVaultEndpoint, string certificateNam
2525 _certificateName = certificateName ; // certificateName
2626 }
2727
28- public async Task < ( X509Certificate2 ActiveCertificate , X509Certificate2 SecondaryCertificate ) > GetCertificatesFromKeyVault ( )
28+ public async Task < ( X509Certificate2 ActiveCertificate , X509Certificate2 SecondaryCertificate ) > GetCertificatesFromKeyVault (
29+ SecretClient secretClient , CertificateClient certificateClient )
2930 {
30- ( X509Certificate2 ActiveCertificate , X509Certificate2 SecondaryCertificate ) certs = ( null , null ) ;
31- var azureServiceTokenProvider = new AzureServiceTokenProvider ( ) ;
32- var keyVaultClient = new KeyVaultClient ( new KeyVaultClient . AuthenticationCallback ( azureServiceTokenProvider . KeyVaultTokenCallback ) ) ;
31+ ( X509Certificate2 ActiveCertificate , X509Certificate2 SecondaryCertificate ) certs = ( null , null ) ;
3332
34- var certificateItems = await GetAllEnabledCertificateVersionsAsync ( keyVaultClient ) ;
33+ var certificateItems = GetAllEnabledCertificateVersions ( certificateClient ) ;
3534 var item = certificateItems . FirstOrDefault ( ) ;
3635 if ( item != null )
3736 {
38- certs . ActiveCertificate = await GetCertificateAsync ( item . Identifier . Identifier , keyVaultClient ) ;
37+ certs . ActiveCertificate = await GetCertificateAsync (
38+ secretClient , _certificateName , item . Version ) ;
3939 }
4040
4141 if ( certificateItems . Count > 1 )
4242 {
43- certs . SecondaryCertificate = await GetCertificateAsync ( certificateItems [ 1 ] . Identifier . Identifier , keyVaultClient ) ;
43+ certs . SecondaryCertificate = await GetCertificateAsync (
44+ secretClient , _certificateName , certificateItems [ 1 ] . Version ) ;
4445 }
4546
4647 return certs ;
4748 }
4849
49- private async Task < List < CertificateItem > > GetAllEnabledCertificateVersionsAsync ( KeyVaultClient keyVaultClient )
50- {
51- // Get all the certificate versions (this will also get the currect active version
52- var certificateVersions = await keyVaultClient . GetCertificateVersionsAsync ( _keyVaultEndpoint , _certificateName ) ;
53-
54- // Find all enabled versions of the certificate and sort them by creation date in decending order
55- return certificateVersions
56- . Where ( certVersion => certVersion . Attributes . Enabled . HasValue && certVersion . Attributes . Enabled . Value )
57- . OrderByDescending ( certVersion => certVersion . Attributes . Created )
50+ private List < CertificateProperties > GetAllEnabledCertificateVersions (
51+ CertificateClient certificateClient )
52+ {
53+ var certificateVersions = certificateClient . GetPropertiesOfCertificateVersions ( _certificateName ) ;
54+ var certificateItems = certificateVersions . ToList ( ) ;
55+
56+ // Find all enabled versions of the certificate and sort them by creation date in decending order
57+ return certificateVersions
58+ . Where ( certVersion => certVersion . Enabled . HasValue && certVersion . Enabled . Value )
59+ . OrderByDescending ( certVersion => certVersion . CreatedOn )
5860 . ToList ( ) ;
59- }
60-
61- private async Task < X509Certificate2 > GetCertificateAsync ( string identitifier , KeyVaultClient keyVaultClient )
62- {
63- var certificateVersionBundle = await keyVaultClient . GetCertificateAsync ( identitifier ) ;
64- var certificatePrivateKeySecretBundle = await keyVaultClient . GetSecretAsync ( certificateVersionBundle . SecretIdentifier . Identifier ) ;
65- var privateKeyBytes = Convert . FromBase64String ( certificatePrivateKeySecretBundle . Value ) ;
66- var certificateWithPrivateKey = new X509Certificate2 ( privateKeyBytes , ( string ) null , X509KeyStorageFlags . MachineKeySet ) ;
67- return certificateWithPrivateKey ;
61+ }
62+
63+ private async Task < X509Certificate2 > GetCertificateAsync (
64+ SecretClient secretClient ,
65+ string certName ,
66+ string version )
67+ {
68+ // Create a new secret using the secret client.
69+ var secretName = certName ;
70+ KeyVaultSecret secret = await secretClient . GetSecretAsync ( secretName , version ) ;
71+
72+ var privateKeyBytes = Convert . FromBase64String ( secret . Value ) ;
73+
74+ var certificateWithPrivateKey = new X509Certificate2 ( privateKeyBytes ,
75+ ( string ) null ,
76+ X509KeyStorageFlags . MachineKeySet ) ;
77+
78+ return certificateWithPrivateKey ;
6879 }
6980
7081 }
0 commit comments