Skip to content

Commit

Permalink
Merge pull request #5 from shibayan/feature/refactoring
Browse files Browse the repository at this point in the history
Refactoring and improve reliability
  • Loading branch information
shibayan authored Oct 14, 2018
2 parents e631eeb + fd88b2f commit ec269db
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
5 changes: 3 additions & 2 deletions AzureKeyVault.LetsEncrypt/AddCertificate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using Microsoft.Azure.WebJobs;
Expand All @@ -19,7 +20,7 @@ public static async Task<HttpResponseMessage> HttpStart(

if (request.Domains == null || request.Domains.Length == 0)
{
return req.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, $"{nameof(request.Domains)} is empty.");
return req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{nameof(request.Domains)} is empty.");
}

// Function input comes from the request content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using ACMESharp.Crypto.JOSE;

namespace AzureKeyVault.LetsEncrypt
namespace AzureKeyVault.LetsEncrypt.Internal
{
internal class AccountKey
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Configuration;

namespace AzureKeyVault.LetsEncrypt
namespace AzureKeyVault.LetsEncrypt.Internal
{
internal class Settings
{
Expand Down
36 changes: 17 additions & 19 deletions AzureKeyVault.LetsEncrypt/SharedFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using ACMESharp.Protocol;
using ACMESharp.Protocol.Resources;

using AzureKeyVault.LetsEncrypt.Internal;

using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.Azure.Management.Dns;
Expand Down Expand Up @@ -48,8 +50,11 @@ public static async Task IssueCertificate([OrchestrationTrigger] DurableOrchestr
challenges.Add(await context.CallActivityAsync<Challenge>(nameof(Dns01Authorization), authorization));
}

// Order status が ready になるまで待つ
await context.CallActivityAsync(nameof(AnswerChallenges), (orderDetails, challenges));
// ACME Answer を実行
await context.CallActivityAsync(nameof(AnswerChallenges), challenges);

// Order のステータスが ready になるまで 60 秒待機
await context.CallActivityWithRetryAsync(nameof(CheckIsReady), new RetryOptions(TimeSpan.FromSeconds(5), 12), orderDetails);

await context.CallActivityAsync(nameof(FinalizeOrder), (dnsNames, orderDetails));
}
Expand Down Expand Up @@ -183,7 +188,7 @@ public static async Task<Challenge> Dns01Authorization([ActivityTrigger] Durable
[FunctionName(nameof(AnswerChallenges))]
public static async Task AnswerChallenges([ActivityTrigger] DurableActivityContext context, ILogger log)
{
var (orderDetails, challenges) = context.GetInput<(OrderDetails, IList<Challenge>)>();
var challenges = context.GetInput<IList<Challenge>>();

var acme = await CreateAcmeClientAsync();

Expand All @@ -192,28 +197,21 @@ public static async Task AnswerChallenges([ActivityTrigger] DurableActivityConte
{
await acme.AnswerChallengeAsync(challenge.Url);
}
}

// Order のステータスが ready になるまで 60 秒待機
for (int i = 0; i < 12; i++)
{
orderDetails = await acme.GetOrderDetailsAsync(orderDetails.OrderUrl, orderDetails);

if (orderDetails.Payload.Status == "ready")
{
return;
}
[FunctionName(nameof(CheckIsReady))]
public static async Task CheckIsReady([ActivityTrigger] DurableActivityContext context, ILogger log)
{
var orderDetails = context.GetInput<OrderDetails>();

await Task.Delay(TimeSpan.FromSeconds(5));
}
var acme = await CreateAcmeClientAsync();

log.LogError($"Timeout ACME challenge status : {orderDetails.Payload.Status}");
orderDetails = await acme.GetOrderDetailsAsync(orderDetails.OrderUrl, orderDetails);

if (orderDetails.Payload.Error != null)
if (orderDetails.Payload.Status != "ready")
{
log.LogError($"{orderDetails.Payload.Error.Type},{orderDetails.Payload.Error.Status},{orderDetails.Payload.Error.Detail}");
throw new InvalidOperationException($"Invalid order status is {orderDetails.Payload.Status}");
}

throw new InvalidOperationException();
}

[FunctionName(nameof(FinalizeOrder))]
Expand Down

0 comments on commit ec269db

Please sign in to comment.