|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 17 | +using Microsoft.Azure.Commands.WebApps.Models; |
| 18 | +using Microsoft.Azure.Commands.WebApps.Models.WebApp; |
| 19 | +using Microsoft.Azure.Commands.WebApps.Utilities; |
| 20 | +using Microsoft.Azure.Management.WebSites.Models; |
| 21 | +using System; |
| 22 | +using System.Linq; |
| 23 | +using System.Management.Automation; |
| 24 | +using System.Net; |
| 25 | +using System.Net.Http; |
| 26 | +using System.Text; |
| 27 | +using System.Threading; |
| 28 | + |
| 29 | +namespace Microsoft.Azure.Commands.WebApps.Cmdlets.Certificates |
| 30 | +{ |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// This commandlet will let you create a new managed certificate |
| 34 | + /// </summary> |
| 35 | + [Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "WebAppCertificate", SupportsShouldProcess = true), OutputType(typeof(PSCertificate))] |
| 36 | + public class NewAzureWebAppCertificate : WebAppBaseClientCmdLet |
| 37 | + { |
| 38 | + // Poll status for a maximum of 6 minutes (360 seconds / 2 seconds per status check) |
| 39 | + private const int NumStatusChecks = 72; |
| 40 | + const string CertNamePostFixSeparator = "_"; |
| 41 | + const string ParameterSet1Name = "S1"; |
| 42 | + |
| 43 | + [Parameter(ParameterSetName = ParameterSet1Name, Position = 0, Mandatory = true, HelpMessage = "The name of the resource group.")] |
| 44 | + [ResourceGroupCompleter] |
| 45 | + [ValidateNotNullOrEmpty] |
| 46 | + public string ResourceGroupName { get; set; } |
| 47 | + |
| 48 | + [Parameter(ParameterSetName = ParameterSet1Name, Position = 1, Mandatory = true, HelpMessage = "The name of the web app.")] |
| 49 | + [ResourceNameCompleter("Microsoft.Web/sites", "ResourceGroupName")] |
| 50 | + [ValidateNotNullOrEmpty] |
| 51 | + public string WebAppName { get; set; } |
| 52 | + |
| 53 | + [Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "The name of the certificate")] |
| 54 | + [ValidateNotNullOrEmpty] |
| 55 | + public string Name { get; set; } |
| 56 | + |
| 57 | + [Parameter(ParameterSetName = ParameterSet1Name, Position = 2, Mandatory = false, HelpMessage = "The name of the web app slot.")] |
| 58 | + [ResourceNameCompleter("Microsoft.Web/sites/slots", "ResourceGroupName", "WebAppName")] |
| 59 | + [ValidateNotNullOrEmpty] |
| 60 | + public string Slot { get; set; } |
| 61 | + |
| 62 | + [Parameter(ParameterSetName = ParameterSet1Name, Position = 3, Mandatory = true, HelpMessage = "Custom hostnames associated with web app/slot.")] |
| 63 | + [ValidateNotNullOrEmpty] |
| 64 | + public string HostName { get; set; } |
| 65 | + |
| 66 | + [Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "To add the created certificate to WebApp/slot.")] |
| 67 | + [ValidateNotNullOrEmpty] |
| 68 | + public SwitchParameter AddBinding { get; set; } |
| 69 | + |
| 70 | + [Parameter(ParameterSetName = ParameterSet1Name, Mandatory = false, HelpMessage = "Ssl state option. Use either 'SniEnabled' or 'IpBasedEnabled'. Default option is 'SniEnabled'.")] |
| 71 | + [ValidateNotNullOrEmpty] |
| 72 | + public SslState? SslState { get; set; } |
| 73 | + |
| 74 | + public override void ExecuteCmdlet() |
| 75 | + { |
| 76 | + if (!string.IsNullOrWhiteSpace(ResourceGroupName) && !string.IsNullOrWhiteSpace(WebAppName)) |
| 77 | + { |
| 78 | + string certName = null; |
| 79 | + HttpStatusCode statusCode = HttpStatusCode.OK; |
| 80 | + var webApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, WebAppName, Slot)); |
| 81 | + var location = webApp.Location; |
| 82 | + |
| 83 | + Certificate createdCertdetails = new Certificate(); |
| 84 | + |
| 85 | + var certificate = new Certificate( |
| 86 | + webApp.Location, |
| 87 | + type: "Microsoft.Web/certificates", |
| 88 | + canonicalName: HostName, |
| 89 | + password: "", |
| 90 | + serverFarmId: webApp.ServerFarmId); |
| 91 | + if (this.ShouldProcess(this.WebAppName, string.Format($"Creating an App service managed certificate for Web App '{WebAppName}'"))) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + //Default certName is HostName |
| 96 | + certName = Name != null ? Name : HostName; |
| 97 | + createdCertdetails = (PSCertificate)WebsitesClient.CreateCertificate(ResourceGroupName, certName, certificate); |
| 98 | + } |
| 99 | + catch (DefaultErrorResponseException e) |
| 100 | + { |
| 101 | + statusCode = e.Response.StatusCode; |
| 102 | + // 'Conflict' exception is thrown when certificate already exists. Let's swallow it and continue. |
| 103 | + //'Accepted' exception is thrown by default for create cert method. |
| 104 | + if (e.Response.StatusCode != HttpStatusCode.Conflict && |
| 105 | + e.Response.StatusCode != HttpStatusCode.Accepted) |
| 106 | + { |
| 107 | + throw; |
| 108 | + } |
| 109 | + if (e.Response.StatusCode == HttpStatusCode.Accepted) |
| 110 | + { |
| 111 | + var poll_url = e.Response.Headers["Location"].FirstOrDefault(); |
| 112 | + var token=WebsitesClient.GetAccessToken(DefaultContext); |
| 113 | + HttpClient client = new HttpClient(); |
| 114 | + client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken); |
| 115 | + |
| 116 | + HttpResponseMessage r; |
| 117 | + int numChecks = 0; |
| 118 | + do |
| 119 | + { |
| 120 | + Thread.Sleep(TimeSpan.FromSeconds(5)); |
| 121 | + r = client.GetAsync(poll_url).Result; |
| 122 | + numChecks++; |
| 123 | + } while (r.StatusCode == HttpStatusCode.Accepted && numChecks < NumStatusChecks); |
| 124 | + |
| 125 | + if (r.StatusCode == HttpStatusCode.Accepted && numChecks >= NumStatusChecks) |
| 126 | + { |
| 127 | + var rec = new ErrorRecord(new Exception(string.Format($"The creation of the managed certificate '{this.HostName}' is taking longer than expected." + |
| 128 | + $" Please re-try the operation '{CreateInputCommand()}'")), |
| 129 | + string.Empty, ErrorCategory.OperationTimeout, null); |
| 130 | + WriteError(rec); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | + createdCertdetails = WebsitesClient.GetCertificate(ResourceGroupName, certName); |
| 137 | + |
| 138 | + //Add only when user is opted for Binding |
| 139 | + if (AddBinding) |
| 140 | + { |
| 141 | + |
| 142 | + WebsitesClient.UpdateHostNameSslState(ResourceGroupName, |
| 143 | + WebAppName, |
| 144 | + Slot, |
| 145 | + webApp.Location, |
| 146 | + HostName, SslState.HasValue ? SslState.Value : Management.WebSites.Models.SslState.SniEnabled, |
| 147 | + createdCertdetails.Thumbprint); |
| 148 | + } |
| 149 | + WriteObject(createdCertdetails); |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + private string CreateInputCommand() |
| 156 | + { |
| 157 | + StringBuilder command = new StringBuilder("New-AzWebAppCertificate "); |
| 158 | + command.Append($"-ResourceGroupName {this.ResourceGroupName} -WebAppName {this.WebAppName} -HostName {this.HostName} "); |
| 159 | + if (Slot != null) |
| 160 | + command.Append($"-Slot {this.Slot} "); |
| 161 | + if (AddBinding) |
| 162 | + command.Append($"-AddBinding "); |
| 163 | + if (SslState != null) |
| 164 | + command.Append($"-SslState {this.SslState} "); |
| 165 | + return command.ToString(); ; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments