Skip to content

Commit

Permalink
Merge pull request #18 from shibayan/feature/reliability
Browse files Browse the repository at this point in the history
Improve reliability
  • Loading branch information
shibayan authored Feb 22, 2019
2 parents 3478343 + cf63290 commit 5c2b561
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
25 changes: 25 additions & 0 deletions AzureKeyVault.LetsEncrypt/Internal/RetriableActivityException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Runtime.Serialization;

namespace AzureKeyVault.LetsEncrypt.Internal
{
[Serializable]
public class RetriableActivityException : Exception
{
public RetriableActivityException()
{
}

public RetriableActivityException(string message) : base(message)
{
}

public RetriableActivityException(string message, Exception inner) : base(message, inner)
{
}

protected RetriableActivityException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
43 changes: 22 additions & 21 deletions AzureKeyVault.LetsEncrypt/SharedFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static async Task IssueCertificate([OrchestrationTrigger] DurableOrchestr
var result = await context.CallActivityAsync<ChallengeResult>(nameof(Dns01Authorization), authorization);

// Azure DNS で正しくレコードが引けるか確認
await context.CallActivityWithRetryAsync(nameof(CheckIsDnsRecord), new RetryOptions(TimeSpan.FromSeconds(10), 6), result);
await context.CallActivityWithRetryAsync(nameof(CheckDnsChallenge), new RetryOptions(TimeSpan.FromSeconds(10), 6), result);

challenges.Add(result);
}
Expand Down Expand Up @@ -197,8 +197,8 @@ public static async Task<ChallengeResult> Dns01Authorization([ActivityTrigger] D
};
}

[FunctionName(nameof(CheckIsDnsRecord))]
public static async Task CheckIsDnsRecord([ActivityTrigger] DurableActivityContext context, ILogger log)
[FunctionName(nameof(CheckDnsChallenge))]
public static async Task CheckDnsChallenge([ActivityTrigger] DurableActivityContext context, ILogger log)
{
var challenge = context.GetInput<ChallengeResult>();

Expand All @@ -212,27 +212,13 @@ public static async Task CheckIsDnsRecord([ActivityTrigger] DurableActivityConte
// レコードが存在しなかった場合はエラー
if (txtRecords.Length == 0)
{
throw new InvalidOperationException($"{challenge.DnsRecordName} did not resolve.");
throw new RetriableActivityException($"{challenge.DnsRecordName} did not resolve.");
}

// レコードに今回のチャレンジが含まれていない場合もエラー
if (!txtRecords.Any(x => x.Text.Contains(challenge.DnsRecordValue)))
{
throw new InvalidOperationException($"{challenge.DnsRecordName} value is not correct.");
}
}

[FunctionName(nameof(AnswerChallenges))]
public static async Task AnswerChallenges([ActivityTrigger] DurableActivityContext context, ILogger log)
{
var challenges = context.GetInput<IList<ChallengeResult>>();

var acme = await CreateAcmeClientAsync();

// Answer の準備が出来たことを通知
foreach (var challenge in challenges)
{
await acme.AnswerChallengeAsync(challenge.Url);
throw new RetriableActivityException($"{challenge.DnsRecordName} value is not correct.");
}
}

Expand All @@ -247,8 +233,8 @@ public static async Task CheckIsReady([ActivityTrigger] DurableActivityContext c

if (orderDetails.Payload.Status == "pending")
{
// pending の場合は何もしない
throw new InvalidOperationException("ACME domain validation is pending.");
// pending の場合はリトライする
throw new RetriableActivityException("ACME domain validation is pending.");
}

if (orderDetails.Payload.Status == "invalid")
Expand All @@ -266,10 +252,25 @@ public static async Task CheckIsReady([ActivityTrigger] DurableActivityContext c
}
}

// invalid の場合は最初から実行が必要なので失敗させる
throw new InvalidOperationException("Invalid order status. Required retry at first.");
}
}

[FunctionName(nameof(AnswerChallenges))]
public static async Task AnswerChallenges([ActivityTrigger] DurableActivityContext context, ILogger log)
{
var challenges = context.GetInput<IList<ChallengeResult>>();

var acme = await CreateAcmeClientAsync();

// Answer の準備が出来たことを通知
foreach (var challenge in challenges)
{
await acme.AnswerChallengeAsync(challenge.Url);
}
}

[FunctionName(nameof(FinalizeOrder))]
public static async Task FinalizeOrder([ActivityTrigger] DurableActivityContext context, ILogger log)
{
Expand Down

0 comments on commit 5c2b561

Please sign in to comment.