forked from dotnet/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (62 loc) · 2.68 KB
/
Program.cs
File metadata and controls
67 lines (62 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Specialized;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace CertHelper;
internal class Program
{
static readonly string TENANT_ID = "72f988bf-86f1-41af-91ab-2d7cd011db47";
static readonly string CERT_CLIENT_ID = "8c4b65ef-5a73-4d5a-a298-962d4a4ef7bc";
static async Task<int> Main(string[] args)
{
try
{
var kvc = new KeyVaultCert();
await kvc.LoadKeyVaultCertsAsync();
if (kvc.ShouldRotateCerts())
{
using (var localMachineCerts = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
localMachineCerts.Open(OpenFlags.ReadWrite);
localMachineCerts.RemoveRange(kvc.LocalCerts.Certificates);
localMachineCerts.AddRange(kvc.KeyVaultCertificates);
}
}
var bcc = new BlobContainerClient(new Uri("https://pvscmdupload.blob.core.windows.net/certstatus"),
new ClientCertificateCredential(TENANT_ID, CERT_CLIENT_ID, kvc.KeyVaultCertificates.First(), new() {SendCertificateChain = true}));
var currentKeyValutCertThumbprints = "";
foreach (var cert in kvc.KeyVaultCertificates)
{
currentKeyValutCertThumbprints += $"[{DateTimeOffset.UtcNow}] {cert.Thumbprint}{Environment.NewLine}";
}
var blob = bcc.GetBlobClient(System.Environment.MachineName);
if (blob.Exists())
{
var result = blob.DownloadContent();
var currentBlob = result.Value.Content.ToString();
currentBlob = currentBlob + currentKeyValutCertThumbprints;
blob.Upload(new MemoryStream(Encoding.UTF8.GetBytes(currentBlob)), overwrite: true);
}
else
{
blob.Upload(new MemoryStream(Encoding.UTF8.GetBytes(currentKeyValutCertThumbprints)), overwrite: false);
}
}
catch (Exception ex)
{
Console.Error.WriteLine("Failed to rotate certificates");
Console.Error.WriteLine(ex.Message);
Console.Error.WriteLine(ex.StackTrace);
}
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
foreach(var cert in store.Certificates.Find(X509FindType.FindBySubjectName, "dotnetperf.microsoft.com", false))
{
Console.WriteLine(Convert.ToBase64String(cert.Export(X509ContentType.Pfx)));
}
}
return 0;
}
}