|
9 | 9 | using Microsoft.Extensions.Logging; |
10 | 10 | using NHS.Screening.RetrieveMeshFile; |
11 | 11 | using HealthChecks.Extensions; |
12 | | -using Microsoft.Extensions.HealthChecks; |
13 | 12 |
|
14 | 13 |
|
15 | 14 | var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); |
|
20 | 19 | var host = new HostBuilder(); |
21 | 20 |
|
22 | 21 | X509Certificate2 cert = null; |
| 22 | + X509Certificate2Collection caCerts = new X509Certificate2Collection(); |
| 23 | + |
| 24 | + var KeyVaultConnectionString = Environment.GetEnvironmentVariable("KeyVaultConnectionString"); |
23 | 25 |
|
24 | 26 | host.AddConfiguration<RetrieveMeshFileConfig>(out RetrieveMeshFileConfig config); |
25 | 27 |
|
26 | | - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("KeyVaultConnectionString"))) |
| 28 | + if (!string.IsNullOrEmpty(KeyVaultConnectionString)) |
27 | 29 | { |
28 | 30 | logger.LogInformation("Pulling Mesh Certificate from KeyVault"); |
29 | | - var client = new CertificateClient(vaultUri: new Uri(Environment.GetEnvironmentVariable("KeyVaultConnectionString")), credential: new DefaultAzureCredential()); |
| 31 | + var client = new CertificateClient(vaultUri: new Uri(KeyVaultConnectionString), credential: new DefaultAzureCredential()); |
30 | 32 | var certificate = await client.DownloadCertificateAsync(config.MeshKeyName); |
31 | 33 | cert = certificate.Value; |
32 | 34 | } |
|
36 | 38 | cert = new X509Certificate2(config.MeshKeyName, config.MeshKeyPassphrase); |
37 | 39 | } |
38 | 40 |
|
| 41 | + if (!string.IsNullOrEmpty(KeyVaultConnectionString)) |
| 42 | + { |
| 43 | + var client = new CertificateClient(new Uri(KeyVaultConnectionString), new DefaultAzureCredential()); |
| 44 | + var allCertificates = client.GetPropertiesOfCertificates(); |
| 45 | + |
| 46 | + foreach (var certificateProperties in allCertificates) |
| 47 | + { |
| 48 | + if (certificateProperties.Name.StartsWith("CaCert")) |
| 49 | + { |
| 50 | + var certificateWithPolicy = await client.DownloadCertificateAsync(certificateProperties.Name); |
| 51 | + caCerts.Add(certificateWithPolicy.Value); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + else if (!string.IsNullOrEmpty(config.ServerSideCerts)) |
| 56 | + { |
| 57 | + var pemCerts = File.ReadAllText(config.ServerSideCerts) |
| 58 | + .Split(new string[] { "-----END CERTIFICATE-----" }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| 59 | + .Select(pem => pem + "\n-----END CERTIFICATE-----") |
| 60 | + .Select(pem => new X509Certificate2(Convert.FromBase64String( |
| 61 | + pem.Replace("-----BEGIN CERTIFICATE-----", "") |
| 62 | + .Replace("-----END CERTIFICATE-----", "") |
| 63 | + .Replace("\n", "") |
| 64 | + ))) |
| 65 | + .ToArray(); |
| 66 | + |
| 67 | + caCerts.AddRange(pemCerts); |
| 68 | + } |
| 69 | + |
| 70 | + |
39 | 71 | host.ConfigureFunctionsWebApplication(); |
40 | 72 | host.ConfigureServices(services => |
41 | 73 | { |
|
47 | 79 | { |
48 | 80 | Password = config.MeshPassword, |
49 | 81 | SharedKey = config.MeshSharedKey, |
50 | | - Cert = cert |
| 82 | + Cert = cert, |
| 83 | + serverSideCertCollection = caCerts |
51 | 84 | }) |
52 | 85 | .Build(); |
53 | 86 | services.AddSingleton<IBlobStorageHelper, BlobStorageHelper>(); |
|
0 commit comments