|
| 1 | +using Azure; |
| 2 | +using Azure.Core; |
| 3 | +using Azure.Identity; |
| 4 | +using Azure.Security.KeyVault.Certificates; |
| 5 | +using Azure.Security.KeyVault.Secrets; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using System.Security.Cryptography.X509Certificates; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace CertHelper; |
| 15 | + |
| 16 | +public class KeyVaultCert |
| 17 | +{ |
| 18 | + private readonly string _keyVaultUrl = "https://dotnetperfkeyvault.vault.azure.net/"; |
| 19 | + private readonly string _tenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47"; |
| 20 | + private readonly string _clientId = "8c4b65ef-5a73-4d5a-a298-962d4a4ef7bc"; |
| 21 | + |
| 22 | + public X509Certificate2Collection KeyVaultCertificates { get; set; } |
| 23 | + public ILocalCert LocalCerts { get; set; } |
| 24 | + private TokenCredential _credential { get; set; } |
| 25 | + private CertificateClient _certClient { get; set; } |
| 26 | + private SecretClient _secretClient { get; set; } |
| 27 | + |
| 28 | + public KeyVaultCert(TokenCredential? cred = null, CertificateClient? certClient = null, SecretClient? secretClient = null, ILocalCert? localCerts = null) |
| 29 | + { |
| 30 | + LocalCerts = localCerts ?? new LocalCert(); |
| 31 | + _credential = cred ?? GetCertifcateCredentialAsync(_tenantId, _clientId, LocalCerts.Certificates).Result; |
| 32 | + _certClient = certClient ?? new CertificateClient(new Uri(_keyVaultUrl), _credential); |
| 33 | + _secretClient = secretClient ?? new SecretClient(new Uri(_keyVaultUrl), _credential); |
| 34 | + KeyVaultCertificates = new X509Certificate2Collection(); |
| 35 | + } |
| 36 | + |
| 37 | + public async Task LoadKeyVaultCertsAsync() |
| 38 | + { |
| 39 | + KeyVaultCertificates.Add(await FindCertificateInKeyVaultAsync(Constants.Cert1Name)); |
| 40 | + KeyVaultCertificates.Add(await FindCertificateInKeyVaultAsync(Constants.Cert2Name)); |
| 41 | + |
| 42 | + if (KeyVaultCertificates.Where(c => c == null).Count() > 0) |
| 43 | + { |
| 44 | + throw new Exception("One or more certificates not found"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private async Task<ClientCertificateCredential> GetCertifcateCredentialAsync(string tenantId, string clientId, X509Certificate2Collection certCollection) |
| 49 | + { |
| 50 | + ClientCertificateCredential? ccc = null; |
| 51 | + Exception? exception = null; |
| 52 | + foreach (var cert in certCollection) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + ccc = new ClientCertificateCredential(tenantId, clientId, cert); |
| 57 | + await ccc.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" })); |
| 58 | + break; |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + ccc = null; |
| 63 | + exception = ex; |
| 64 | + } |
| 65 | + } |
| 66 | + if(ccc == null) |
| 67 | + { |
| 68 | + throw new Exception("Both certificates failed to authenticate", exception); |
| 69 | + } |
| 70 | + return ccc; |
| 71 | + } |
| 72 | + |
| 73 | + private async Task<X509Certificate2> FindCertificateInKeyVaultAsync(string certName) |
| 74 | + { |
| 75 | + var keyVaultCert = await _certClient.GetCertificateAsync(certName); |
| 76 | + if(keyVaultCert.Value == null) |
| 77 | + { |
| 78 | + throw new Exception("Certificate not found in Key Vault"); |
| 79 | + } |
| 80 | + var secret = await _secretClient.GetSecretAsync(keyVaultCert.Value.Name, keyVaultCert.Value.SecretId.Segments.Last()); |
| 81 | + if(secret.Value == null) |
| 82 | + { |
| 83 | + throw new Exception("Certificate secret not found in Key Vault"); |
| 84 | + } |
| 85 | + var certBytes = Convert.FromBase64String(secret.Value.Value); |
| 86 | +#if NET9_0_OR_GREATER |
| 87 | + var cert = X509CertificateLoader.LoadPkcs12(certBytes, "", X509KeyStorageFlags.Exportable); |
| 88 | +#else |
| 89 | + var cert = new X509Certificate2(certBytes, "", X509KeyStorageFlags.Exportable); |
| 90 | +#endif |
| 91 | + return cert; |
| 92 | + } |
| 93 | + |
| 94 | + public bool ShouldRotateCerts() |
| 95 | + { |
| 96 | + var keyVaultThumbprints = new HashSet<string>(); |
| 97 | + foreach (var cert in KeyVaultCertificates) |
| 98 | + { |
| 99 | + keyVaultThumbprints.Add(cert.Thumbprint); |
| 100 | + } |
| 101 | + foreach(var cert in LocalCerts.Certificates) |
| 102 | + { |
| 103 | + if (!keyVaultThumbprints.Contains(cert.Thumbprint)) |
| 104 | + { |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + return false; |
| 109 | + } |
| 110 | +} |
0 commit comments