Skip to content

Commit

Permalink
Add integration tests for API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkp committed Nov 19, 2017
1 parent 994bf8b commit 30a6f72
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ public async Task<IHttpActionResult> GenerateAndInstall(HttpKuduInstallModel mod
/// <param name="apiversion"></param>
/// <returns></returns>
[HttpPost]
[Route("api/certificates/challengeprovider/dns/azure")]
[ResponseType(typeof(CertificateInstallModel))]
public async Task<IHttpActionResult> Generate(DnsAzureModel model, [FromUri(Name = "api-version")]string apiversion = null)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var res = await CertificateManager.RequestDnsChallengeCertificate(model.AzureDnsEnvironment, model.AcmeConfig);

return Ok(res);
}

/// <summary>
/// Requests a Let's Encrypt certificate using the DNS challenge, using Azure DNS. The
/// certificate is installed to the web app.
/// </summary>
/// <param name="model"></param>
/// <param name="apiversion"></param>
/// <returns></returns>
[HttpPost]
[Route("api/certificates/challengeprovider/dns/azure/certificateinstall/azurewebapp")]
[ResponseType(typeof(CertificateInstallModel))]
public async Task<IHttpActionResult> GenerateAndInstall(DnsAzureInstallModel model, [FromUri(Name = "api-version")]string apiversion = null)
Expand All @@ -77,7 +99,7 @@ public async Task<IHttpActionResult> GenerateAndInstall(DnsAzureInstallModel mod
return BadRequest(ModelState);
}

var mgr = CertificateManager.CreateAzureDnsWebAppCertificateManager(model.AzureEnvironment, model.AcmeConfig, model.CertificateSettings, model.AzureDnsEnvironment);
var mgr = CertificateManager.CreateAzureDnsWebAppCertificateManager(model.AzureWebAppEnvironment, model.AcmeConfig, model.CertificateSettings, model);

return Ok(await mgr.AddCertificate());
}
Expand Down
1 change: 1 addition & 0 deletions LetsEncrypt-SiteExtension/LetsEncrypt.SiteExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
<Compile Include="Controllers\Api\CertificateController.cs" />
<Compile Include="Controllers\Api\ValidateApiVersionAttribute.cs" />
<Compile Include="Models\DnsAzureInstallModel.cs" />
<Compile Include="Models\DnsAzureModel.cs" />
<Compile Include="Models\HttpKuduInstallModel.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\HyakUtils.cs" />
Expand Down
161 changes: 157 additions & 4 deletions LetsEncrypt-SiteExtension/Models/DnsAzureInstallModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,167 @@
using LetsEncrypt.Azure.Core.Models;
using System;
using LetsEncrypt.Azure.Core.Models;
using System.ComponentModel.DataAnnotations;

namespace LetsEncrypt.SiteExtension.Models
{
public class DnsAzureInstallModel
public class DnsAzureInstallModel : IAzureDnsEnvironment
{
public AzureWebAppEnvironment AzureEnvironment { get; set; }
[Required]
public AzureWebAppEnvironment AzureWebAppEnvironment { get; set; }

[Required]
public AcmeConfig AcmeConfig { get; set; }

[Required]
public CertificateServiceSettings CertificateSettings { get; set; }

public AzureDnsEnvironment AzureDnsEnvironment { get; set; }
/// <summary>
/// The relative record set name.
/// </summary>
[Required]
public string RelativeRecordSetName
{
get;set;
}

/// <summary>
/// The zone name.
/// </summary>
[Required]
public string ZoneName
{
get;set;
}
string resourceGroupName;

/// <summary>
/// The resource group name defaults to AzureWebAppEnvironment.ResourceGroupName.
/// </summary>
public string ResourceGroupName
{
get
{
return resourceGroupName ?? AzureWebAppEnvironment?.ResourceGroupName;
}
set
{
resourceGroupName = value;
}
}

string tenant;
/// <summary>
/// Tenant defaults to AzureWebAppEnvironment.Tenant.
/// </summary>
public string Tenant
{
get
{
return tenant ?? AzureWebAppEnvironment?.Tenant;
}
set
{
tenant = value;
}
}

Guid clientId;
/// <summary>
/// The client id defaults to AzureWebAppEnvironment.ClientId.
/// </summary>
public Guid ClientId
{
get
{
return clientId == Guid.Empty ? AzureWebAppEnvironment.ClientId : clientId;
}
set
{
clientId = value;
}
}

string clientSecret;
/// <summary>
/// The client secret defaults to AzureWebAppEnvironment.ClientSecret.
/// </summary>
public string ClientSecret
{
get
{
return clientSecret ?? AzureWebAppEnvironment?.ClientSecret;
}
set
{
clientSecret = value;
}
}

Guid subscriptionId;

/// <summary>
/// The subscription id defaults to AzureWebAppEnvironment.SubscriptionId.
/// </summary>
public Guid SubscriptionId
{
get
{
return subscriptionId == Guid.Empty ? AzureWebAppEnvironment.SubscriptionId : subscriptionId;
}
set
{
subscriptionId = value;
}
}

Uri managementEndpoint;

/// <summary>
/// The management endpoint defaults to AzureWebAppEnvironment.ManagementEndpoint.
/// </summary>
public Uri ManagementEndpoint
{
get
{
return managementEndpoint ?? AzureWebAppEnvironment.ManagementEndpoint;
}
set
{
managementEndpoint = value;
}
}

Uri tokenAudience;

/// <summary>
/// The token audience defaults to AzureWebAppEnvironment.TokenAudience.
/// </summary>
public Uri TokenAudience
{
get
{
return tokenAudience ?? AzureWebAppEnvironment.TokenAudience;
}
set
{
tokenAudience = value;
}
}

Uri authenticationEndpoint;
/// <summary>
/// The authentication endpoint to sign in to. Defaults to AzureWebAppEnvironment.AuthenticationEndpoint.
/// </summary>
public Uri AuthenticationEndpoint
{
get
{
return authenticationEndpoint ?? AzureWebAppEnvironment.AuthenticationEndpoint;
}
set
{
authenticationEndpoint = value;
}
}
}
}
18 changes: 18 additions & 0 deletions LetsEncrypt-SiteExtension/Models/DnsAzureModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using LetsEncrypt.Azure.Core.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace LetsEncrypt.SiteExtension.Models
{
public class DnsAzureModel
{
[Required]
public AzureDnsEnvironment AzureDnsEnvironment { get; set; }

[Required]
public AcmeConfig AcmeConfig { get; set; }
}
}
6 changes: 6 additions & 0 deletions LetsEncrypt-SiteExtension/Models/HttpKuduInstallModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using LetsEncrypt.Azure.Core.Models;
using System.ComponentModel.DataAnnotations;

namespace LetsEncrypt.SiteExtension.Models
{
public class HttpKuduInstallModel
{
[Required]
public AzureWebAppEnvironment AzureEnvironment { get; set; }

[Required]
public AcmeConfig AcmeConfig { get; set; }

[Required]
public CertificateServiceSettings CertificateSettings { get; set; }

[Required]
public AuthorizationChallengeProviderConfig AuthorizationChallengeProviderConfig { get; set; }
}
}
52 changes: 31 additions & 21 deletions LetsEncrypt.SiteExtension.Core/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public static CertificateManager CreateAzureDnsWebAppCertificateManager(IAzureWe
return new CertificateManager(settings, acmeConfig, new WebAppCertificateService(settings, certSettings), new AzureDnsAuthorizationChallengeProvider(dnsEnvironment));
}

/// <summary>
/// Request a certificate from lets encrypt using the DNS challenge, placing the challenge record in Azure DNS.
/// The certifiacte is not assigned, but just returned.
/// </summary>
/// <param name="azureDnsEnvironment"></param>
/// <param name="acmeConfig"></param>
/// <returns></returns>
public static async Task<CertificateInstallModel> RequestDnsChallengeCertificate(IAzureDnsEnvironment azureDnsEnvironment, IAcmeConfig acmeConfig)
{
return await new CertificateManager(null, acmeConfig, null, new AzureDnsAuthorizationChallengeProvider(azureDnsEnvironment)).RequestInternalAsync(acmeConfig);
}

/// <summary>
/// Used for automatic installation of letsencrypt certificate
/// </summary>
Expand Down Expand Up @@ -154,30 +166,28 @@ internal CertificateInstallModel RequestAndInstallInternal(IAcmeConfig config)
return RequestAndInstallInternalAsync(config).GetAwaiter().GetResult();
}

internal async Task<CertificateInstallModel> RequestAndInstallInternalAsync(IAcmeConfig config)
internal async Task<CertificateInstallModel> RequestInternalAsync(IAcmeConfig config)
{
try
{
Trace.TraceInformation("RequestAndInstallInternal");
var service = new AcmeService(config, this.challengeProvider);
var service = new AcmeService(config, this.challengeProvider);

var cert = await service.RequestCertificate();
var model = new CertificateInstallModel()
{
CertificateInfo = cert,
AllDnsIdentifiers = config.Hostnames.ToList(),
Host = config.Host,
};
this.certificateService.Install(model);
return model;
}
catch (Exception ex)
var cert = await service.RequestCertificate();
var model = new CertificateInstallModel()
{
Trace.TraceError("Unabled to create Azure Web Site Management client " + ex.ToString());
throw;
}
}

CertificateInfo = cert,
AllDnsIdentifiers = config.Hostnames.ToList(),
Host = config.Host,
};
return model;
}

internal async Task<CertificateInstallModel> RequestAndInstallInternalAsync(IAcmeConfig config)
{
Trace.TraceInformation("RequestAndInstallInternal");
var model = await RequestInternalAsync(config);
this.certificateService.Install(model);
return model;
}

public List<string> Cleanup()
{
return this.certificateService.RemoveExpired();
Expand Down
12 changes: 11 additions & 1 deletion LetsEncrypt.SiteExtension.Core/IAzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,23 @@ public string AzureWebSitesDefaultDomainName
get; set;
} = "azurewebsites.net";


string _servicePlanResourceGroupName;

/// <summary>
/// The app service plan resource group name,
/// only required if the web app and app service plan is in different resource groups.
/// </summary>
public string ServicePlanResourceGroupName
{
get; set;
get
{
return _servicePlanResourceGroupName ?? this.ResourceGroupName;
}
set
{
_servicePlanResourceGroupName = value;
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 30a6f72

Please sign in to comment.