Skip to content

Commit

Permalink
Add provider parameter to output JSON
Browse files Browse the repository at this point in the history
Fixes #270
  • Loading branch information
ebekker committed Aug 8, 2017
1 parent 4a96e08 commit 3c85e90
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 48 deletions.
198 changes: 156 additions & 42 deletions ACMESharp/ACMESharp/ACME/Providers/ManualChallengeHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -36,6 +37,9 @@ public bool Append
public bool Overwrite
{ get; private set; }

public bool OutputJson
{ get; set; }

public bool IsDisposed
{ get; private set; }

Expand Down Expand Up @@ -103,31 +107,86 @@ public void Handle(Challenge c)

if (dnsChallenge != null)
{
_writer.WriteLine($"== Manual Challenge Handler - DNS ==");
_writer.WriteLine($" * Handle Time: [{DateTime.Now}]");
_writer.WriteLine($" * Challenge Token: [{dnsChallenge.Token}]");
_writer.WriteLine($"To complete this Challenge please create a new Resource");
_writer.WriteLine($"Record (RR) with the following characteristics:");
_writer.WriteLine($" * RR Type: [TXT]");
_writer.WriteLine($" * RR Name: [{dnsChallenge.RecordName}]");
_writer.WriteLine($" * RR Value: [{dnsChallenge.RecordValue}]");
_writer.WriteLine($"------------------------------------");
var json = new
{
Label = "Manual Challenge Handler - DNS",
HandleTime = DateTime.Now,
ChallengeToken = dnsChallenge.Token,
ChallengeType = "DNS",
Description = new[]
{
"To complete this Challenge please create a new Resource",
"Record (RR) with the following characteristics:",
},
DnsDetails = new
{
RRType = "TXT",
RRName = dnsChallenge.RecordName,
RRValue = dnsChallenge.RecordValue,
}
};

if (OutputJson)
{
_writer.WriteLine(JsonConvert.SerializeObject(json, Formatting.Indented));
}
else
{
_writer.WriteLine($@"== {json.Label} ==
* Handle Time: [{json.HandleTime}]
* Challenge Token: [{json.ChallengeToken}]
{string.Join(Environment.NewLine, json.Description)}
* RR Type: [{json.DnsDetails.RRType}]
* RR Name: [{json.DnsDetails.RRName}]
* RR Value: [{json.DnsDetails.RRValue}]
------------------------------------
");
}
_writer.Flush();
}
else if (httpChallenge != null)
{
_writer.WriteLine($"== Manual Challenge Handler - HTTP ==");
_writer.WriteLine($" * Handle Time: [{DateTime.Now}]");
_writer.WriteLine($" * Challenge Token: [{httpChallenge.Token}]");
_writer.WriteLine($"To complete this Challenge please create a new file");
_writer.WriteLine($"under the server that is responding to the hostname");
_writer.WriteLine($"and path given with the following characteristics:");
_writer.WriteLine($" * HTTP URL: [{httpChallenge.FileUrl}]");
_writer.WriteLine($" * File Path: [{httpChallenge.FilePath}]");
_writer.WriteLine($" * File Content: [{httpChallenge.FileContent}]");
_writer.WriteLine($" * MIME Type: [text/plain]");
_writer.WriteLine($"------------------------------------");
_writer.Flush();
var json = new
{
Label = "Manual Challenge Handler - HTTP",
HandleTime = DateTime.Now,
ChallengeToken = httpChallenge.Token,
ChallengeType = "HTTP",
Description = new[]
{
"To complete this Challenge please create a new file",
"under the server that is responding to the hostname",
"and path given with the following characteristics:",
},
HttpDetails = new
{
HttpUrl = httpChallenge.FileUrl,
FilePath = httpChallenge.FilePath,
FileContent = httpChallenge.FileContent,
MimeType = "text/plain",
}
};

if (OutputJson)
{
_writer.WriteLine(JsonConvert.SerializeObject(json, Formatting.Indented));
}
else
{
_writer.WriteLine($@"== {json.Label} ==
* Handle Time: [{json.HandleTime}]
* Challenge Token: [{json.ChallengeToken}]
{string.Join(Environment.NewLine, json.Description)}
* HTTP URL: [{json.HttpDetails.HttpUrl}]
* File Path: [{json.HttpDetails.FilePath}]
* File Content: [{json.HttpDetails.FileContent}]
* MIME Type: [{json.HttpDetails.MimeType}]
------------------------------------
");
}
_writer.Flush();
}
else
{
Expand All @@ -146,30 +205,85 @@ public void CleanUp(Challenge c)

if (dnsChallenge != null)
{
_writer.WriteLine($"== Manual Challenge Handler - DNS ==");
_writer.WriteLine($" * CleanUp Time: [{DateTime.Now}]");
_writer.WriteLine($" * Challenge Token: [{dnsChallenge.Token}]");
_writer.WriteLine($"The Challenge has been completed -- you can now remove the");
_writer.WriteLine($"Resource Record created previously with the following");
_writer.WriteLine($"characteristics:");
_writer.WriteLine($" * RR Type: [TXT]");
_writer.WriteLine($" * RR Name: [{dnsChallenge.RecordName}]");
_writer.WriteLine($" * RR Value: [{dnsChallenge.RecordValue}]");
_writer.WriteLine($"------------------------------------");
var json = new
{
Label = "Manual Challenge Handler - DNS",
CleanUpTime = DateTime.Now,
ChallengeToken = dnsChallenge.Token,
ChallengeType = "DNS",
Description = new[]
{
"The Challenge has been completed -- you can now remove the",
"Resource Record created previously with the following",
"characteristics:",
},
DnsDetails = new
{
RRType = "TXT",
RRName = dnsChallenge.RecordName,
RRValue = dnsChallenge.RecordValue,
}
};

if (OutputJson)
{
_writer.WriteLine(JsonConvert.SerializeObject(json, Formatting.Indented));
}
else
{
_writer.WriteLine($@"== {json.Label} ==
* CleanUp Time: [{json.CleanUpTime}]
* Challenge Token: [{json.ChallengeToken}]
{string.Join(Environment.NewLine, json.Description)}
* RR Type: [{json.DnsDetails.RRType}]
* RR Name: [{json.DnsDetails.RRName}]
* RR Value: [{json.DnsDetails.RRValue}]
------------------------------------
");
}
_writer.Flush();
}
else if (httpChallenge != null)
{
_writer.WriteLine($"== Manual Challenge Handler - HTTP ==");
_writer.WriteLine($" * CleanUp Time: [{DateTime.Now}]");
_writer.WriteLine($" * Challenge Token: [{httpChallenge.Token}]");
_writer.WriteLine($"The Challenge has been completed -- you can now remove the");
_writer.WriteLine($"file created previously with the following characteristics:");
_writer.WriteLine($" * HTTP URL: [{httpChallenge.FileUrl}]");
_writer.WriteLine($" * File Path: [{httpChallenge.FilePath}]");
_writer.WriteLine($" * File Content: [{httpChallenge.FileContent}]");
_writer.WriteLine($" * MIME Type: [text/plain]");
_writer.WriteLine($"------------------------------------");
var json = new
{
Label = "Manual Challenge Handler - HTTP",
CleanUpTime = DateTime.Now,
ChallengeToken = httpChallenge.Token,
ChallengeType = "HTTP",
Description = new[]
{
"The Challenge has been completed -- you can now remove the",
"file created previously with the following characteristics:",
},
HttpDetails = new
{
HttpUrl = httpChallenge.FileUrl,
FilePath = httpChallenge.FilePath,
FileContent = httpChallenge.FileContent,
MimeType = "text/plain",
}
};

if (OutputJson)
{
_writer.WriteLine(JsonConvert.SerializeObject(json, Formatting.Indented));
}
else
{
_writer.WriteLine($@"== {json.Label} ==
* CleanUp Time: [{json.CleanUpTime}]
* Challenge Token: [{json.ChallengeToken}]
{string.Join(Environment.NewLine, json.Description)}
* HTTP URL: [{httpChallenge.FileUrl}]
* File Path: [{httpChallenge.FilePath}]
* File Content: [{httpChallenge.FileContent}]
* MIME Type: [text/plain]
------------------------------------
");
}
_writer.Flush();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ACMESharp.ACME.Providers
/// </remarks>
[ChallengeHandlerProvider("manual",
ChallengeTypeKind.DNS | ChallengeTypeKind.HTTP,
isCleanUpSupported: false,
isCleanUpSupported: true,
Label = "Manual Provider",
Description = "A manual provider for handling Challenges." +
" This provider supports the DNS and HTTP" +
Expand All @@ -35,12 +35,17 @@ public class ManualChallengeHandlerProvider : IChallengeHandlerProvider
ParameterType.BOOLEAN, label: "Append",
desc: "When true, output to a file will be appended");

public static readonly ParameterDetail OVERWRITE = new ParameterDetail(
nameof(ManualChallengeHandler.Overwrite),
ParameterType.BOOLEAN, label: "Overwrite",
desc: "When true, output to a file will overwrite the file");
public static readonly ParameterDetail OVERWRITE = new ParameterDetail(
nameof(ManualChallengeHandler.Overwrite),
ParameterType.BOOLEAN, label: "Overwrite",
desc: "When true, output to a file will overwrite the file");

private static readonly ParameterDetail[] PARAMS =
public static readonly ParameterDetail OUTPUT_JSON = new ParameterDetail(
nameof(ManualChallengeHandler.OutputJson),
ParameterType.BOOLEAN, label: "Output JSON",
desc: "When true, output will be formatted as JSON suitable for machine automation");

private static readonly ParameterDetail[] PARAMS =
{
WRITE_OUT_PATH,
APPEND,
Expand Down Expand Up @@ -75,6 +80,8 @@ public IChallengeHandler GetHandler(Challenge c, IReadOnlyDictionary<string, obj
a = (bool) initParams[APPEND.Name];
if (initParams.ContainsKey(OVERWRITE.Name))
o = (bool) initParams[OVERWRITE.Name];
if (initParams.ContainsKey(OUTPUT_JSON.Name))
h.OutputJson = (bool)initParams[OUTPUT_JSON.Name];

// Apply any changes
h.SetOut(p, a, o);
Expand Down

0 comments on commit 3c85e90

Please sign in to comment.