From 5239f57f9ef5c226fe362e411873adec9953a80a Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Wed, 6 Mar 2024 00:12:08 +0900 Subject: [PATCH] Fixed buypass certificate issuing error (#679) --- .../Internal/AcmeProtocolClientFactory.cs | 62 +++++++++++++------ .../Internal/ApplicationVersionInitializer.cs | 12 +--- KeyVault.Acmebot/Internal/Constants.cs | 10 +++ KeyVault.Acmebot/KeyVault.Acmebot.csproj | 4 +- KeyVault.Acmebot/Startup.cs | 2 +- 5 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 KeyVault.Acmebot/Internal/Constants.cs diff --git a/KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs b/KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs index 76dd40a..01ac67e 100644 --- a/KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs +++ b/KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Net.Http.Headers; using System.Security.Cryptography; using System.Threading.Tasks; @@ -29,9 +30,15 @@ public async Task CreateClientAsync() { var account = LoadState("account.json"); var accountKey = LoadState("account_key.json"); - var directory = LoadState("directory.json"); + var directory = LoadTempState("directory.json"); - var acmeProtocolClient = new AcmeProtocolClient(_options.Endpoint, directory, account, accountKey?.GenerateSigner(), usePostAsGet: true); + var acmeProtocolClient = new AcmeProtocolClient(_options.Endpoint, directory, account, accountKey?.GenerateSigner(), usePostAsGet: true) + { + BeforeHttpSend = (_, req) => + { + req.Headers.UserAgent.Add(new ProductInfoHeaderValue("KeyVault-Acmebot", Constants.ApplicationVersion)); + } + }; if (directory is null) { @@ -46,7 +53,7 @@ public async Task CreateClientAsync() directory = await acmeProtocolClient.GetDirectoryAsync(); } - SaveState(directory, "directory.json"); + SaveTempState(directory, "directory.json"); acmeProtocolClient.Directory = directory; } @@ -128,33 +135,46 @@ private TState LoadState(string path) if (!File.Exists(fullPath)) { - // Fallback legacy state - var legacyFullPath = Environment.ExpandEnvironmentVariables(@"%HOME%/.acme/" + path); - - if (!File.Exists(legacyFullPath)) - { - return default; - } + return default; + } - var json = File.ReadAllText(legacyFullPath); + var json = File.ReadAllText(fullPath); - var state = JsonConvert.DeserializeObject(json); + return JsonConvert.DeserializeObject(json); + } - SaveState(state, path); + private void SaveState(TState value, string path) + { + var fullPath = ResolveStateFullPath(path); + var directoryPath = Path.GetDirectoryName(fullPath); - return state; - } - else + if (!Directory.Exists(directoryPath)) { - var json = File.ReadAllText(fullPath); + Directory.CreateDirectory(directoryPath); + } + + var json = JsonConvert.SerializeObject(value, Formatting.Indented); + + File.WriteAllText(fullPath, json); + } + + private TState LoadTempState(string path) + { + var fullPath = ResolveTempStateFullPath(path); - return JsonConvert.DeserializeObject(json); + if (!File.Exists(fullPath)) + { + return default; } + + var json = File.ReadAllText(fullPath); + + return JsonConvert.DeserializeObject(json); } - private void SaveState(TState value, string path) + private void SaveTempState(TState value, string path) { - var fullPath = ResolveStateFullPath(path); + var fullPath = ResolveTempStateFullPath(path); var directoryPath = Path.GetDirectoryName(fullPath); if (!Directory.Exists(directoryPath)) @@ -168,4 +188,6 @@ private void SaveState(TState value, string path) } private string ResolveStateFullPath(string path) => Environment.ExpandEnvironmentVariables($"%HOME%/data/.acmebot/{_options.Endpoint.Host}/{path}"); + + private string ResolveTempStateFullPath(string path) => Environment.ExpandEnvironmentVariables($"%TEMP%/.acmebot/{_options.Endpoint.Host}/{path}"); } diff --git a/KeyVault.Acmebot/Internal/ApplicationVersionInitializer.cs b/KeyVault.Acmebot/Internal/ApplicationVersionInitializer.cs index c876e37..c323641 100644 --- a/KeyVault.Acmebot/Internal/ApplicationVersionInitializer.cs +++ b/KeyVault.Acmebot/Internal/ApplicationVersionInitializer.cs @@ -1,15 +1,9 @@ -using System.Reflection; - -using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Extensibility; namespace KeyVault.Acmebot.Internal; -internal class ApplicationVersionInitializer : ITelemetryInitializer +internal class ApplicationVersionInitializer : ITelemetryInitializer { - public string ApplicationVersion { get; } = typeof(TStartup).Assembly - .GetCustomAttribute() - ?.InformationalVersion; - - public void Initialize(ITelemetry telemetry) => telemetry.Context.Component.Version = ApplicationVersion; + public void Initialize(ITelemetry telemetry) => telemetry.Context.Component.Version = Constants.ApplicationVersion; } diff --git a/KeyVault.Acmebot/Internal/Constants.cs b/KeyVault.Acmebot/Internal/Constants.cs new file mode 100644 index 0000000..dcb06cb --- /dev/null +++ b/KeyVault.Acmebot/Internal/Constants.cs @@ -0,0 +1,10 @@ +using System.Reflection; + +namespace KeyVault.Acmebot.Internal; + +internal static class Constants +{ + public static string ApplicationVersion { get; } = typeof(Startup).Assembly + .GetCustomAttribute() + ?.InformationalVersion; +} diff --git a/KeyVault.Acmebot/KeyVault.Acmebot.csproj b/KeyVault.Acmebot/KeyVault.Acmebot.csproj index 3d5584b..76abfbb 100644 --- a/KeyVault.Acmebot/KeyVault.Acmebot.csproj +++ b/KeyVault.Acmebot/KeyVault.Acmebot.csproj @@ -4,7 +4,7 @@ v4 - + @@ -12,7 +12,7 @@ - + diff --git a/KeyVault.Acmebot/Startup.cs b/KeyVault.Acmebot/Startup.cs index 73b7842..874804d 100644 --- a/KeyVault.Acmebot/Startup.cs +++ b/KeyVault.Acmebot/Startup.cs @@ -38,7 +38,7 @@ public override void Configure(IFunctionsHostBuilder builder) builder.Services.AddHttpClient(); - builder.Services.AddSingleton>(); + builder.Services.AddSingleton(); builder.Services.AddSingleton(provider => {