Skip to content

Commit

Permalink
Fixed buypass certificate issuing error (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Mar 5, 2024
1 parent 71e6916 commit 5239f57
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
62 changes: 42 additions & 20 deletions KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Threading.Tasks;

Expand Down Expand Up @@ -29,9 +30,15 @@ public async Task<AcmeProtocolClient> CreateClientAsync()
{
var account = LoadState<AccountDetails>("account.json");
var accountKey = LoadState<AccountKey>("account_key.json");
var directory = LoadState<ServiceDirectory>("directory.json");
var directory = LoadTempState<ServiceDirectory>("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)
{
Expand All @@ -46,7 +53,7 @@ public async Task<AcmeProtocolClient> CreateClientAsync()
directory = await acmeProtocolClient.GetDirectoryAsync();
}

SaveState(directory, "directory.json");
SaveTempState(directory, "directory.json");

acmeProtocolClient.Directory = directory;
}
Expand Down Expand Up @@ -128,33 +135,46 @@ private TState LoadState<TState>(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<TState>(json);
return JsonConvert.DeserializeObject<TState>(json);
}

SaveState(state, path);
private void SaveState<TState>(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<TState>(string path)
{
var fullPath = ResolveTempStateFullPath(path);

return JsonConvert.DeserializeObject<TState>(json);
if (!File.Exists(fullPath))
{
return default;
}

var json = File.ReadAllText(fullPath);

return JsonConvert.DeserializeObject<TState>(json);
}

private void SaveState<TState>(TState value, string path)
private void SaveTempState<TState>(TState value, string path)
{
var fullPath = ResolveStateFullPath(path);
var fullPath = ResolveTempStateFullPath(path);
var directoryPath = Path.GetDirectoryName(fullPath);

if (!Directory.Exists(directoryPath))
Expand All @@ -168,4 +188,6 @@ private void SaveState<TState>(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}");
}
12 changes: 3 additions & 9 deletions KeyVault.Acmebot/Internal/ApplicationVersionInitializer.cs
Original file line number Diff line number Diff line change
@@ -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<TStartup> : ITelemetryInitializer
internal class ApplicationVersionInitializer : ITelemetryInitializer
{
public string ApplicationVersion { get; } = typeof(TStartup).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;

public void Initialize(ITelemetry telemetry) => telemetry.Context.Component.Version = ApplicationVersion;
public void Initialize(ITelemetry telemetry) => telemetry.Context.Component.Version = Constants.ApplicationVersion;
}
10 changes: 10 additions & 0 deletions KeyVault.Acmebot/Internal/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;

namespace KeyVault.Acmebot.Internal;

internal static class Constants
{
public static string ApplicationVersion { get; } = typeof(Startup).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
}
4 changes: 2 additions & 2 deletions KeyVault.Acmebot/KeyVault.Acmebot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Route53" Version="3.7.302.19" />
<PackageReference Include="AWSSDK.Route53" Version="3.7.302.21" />
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Azure.ResourceManager.Dns" Version="1.1.0" />
<PackageReference Include="Azure.ResourceManager.PrivateDns" Version="1.1.0" />
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.6.0" />
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.6.0" />
<PackageReference Include="DnsClient" Version="1.7.0" />
<PackageReference Include="DurableTask.TypedProxy" Version="2.2.2" />
<PackageReference Include="Google.Apis.Dns.v1" Version="1.66.0.3304" />
<PackageReference Include="Google.Apis.Dns.v1" Version="1.67.0.3339" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.13.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="[6.0.*,7.0.0)" />
Expand Down
2 changes: 1 addition & 1 deletion KeyVault.Acmebot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Configure(IFunctionsHostBuilder builder)

builder.Services.AddHttpClient();

builder.Services.AddSingleton<ITelemetryInitializer, ApplicationVersionInitializer<Startup>>();
builder.Services.AddSingleton<ITelemetryInitializer, ApplicationVersionInitializer>();

builder.Services.AddSingleton(provider =>
{
Expand Down

0 comments on commit 5239f57

Please sign in to comment.