【问题标题】:Azure Website - configure SSL Bindings with PowerShell or cross-platform CLIAzure 网站 - 使用 PowerShell 或跨平台 CLI 配置 SSL 绑定
【发布时间】:2014-12-23 09:58:32
【问题描述】:

我们的情况是,我们希望经常向多个 Azure 网站添加新的自定义子域,我们需要能够自动化该过程,以便最大限度地减少人工干预的需要和搞砸事情的风险.

Azure 跨平台 CLI 工具和 PowerShell cmdlet 为我提供了足够的功能来编写脚本,但 SSL 绑定明显例外...网站都是仅 HTTPS,我们添加的每个域都需要SNI SSL 绑定。

Azure 管理门户允许您手动配置网站域的 SSL 绑定。如何使用 PowerShell cmdlet 或跨平台 CLI 工具实现相同的目标?如果无法使用这些工具中的任何一个,是否可以通过其他方式编写脚本,以便我们将域添加/删除到站点?

【问题讨论】:

    标签: powershell azure ssl azure-web-app-service


    【解决方案1】:

    现在可以使用 Azure Powershell 库的 Azure 资源管理器模式执行此操作。这也假设所选证书已经在 Azure 中可用,我已经在其他网站上使用了该证书,因此在将其添加到新站点之前不需要上传它。

    首先,将库设置为使用 Azure 资源管理器模式。 This 文章很好地介绍了这种模式提供的功能。

    Switch-AzureMode -Name AzureResourceManager
    Add-AzureAccount
    Select-AzureSubscription -SubscriptionId $subscriptionId
    

    以下代码中使用了以下变量:

    $apiVersion = "2015-08-01"
    $subscriptionId = "" #The ID of your Azure subscription
    $siteName = "myWebApp"
    $resGroup = "myResourceGroup"
    $appServicePlan = "myAppServicePlan"
    $location = "East US" #Select the appropriate Azure data centre.
    $hostName = "mywebapp.mydomain.com"
    $sslThumbprint = "" # Thumbprint of SSL certificate uploaded for use in Azure.
    

    选择正确的模式后,以下代码块将检索当前站点信息。然后可以将新的 SSL 信息添加到一个新对象中,该对象可以添加到当前 HostNameSslStates 数组的末尾。

    最后,可以使用 Set-AzureResource cmdlet 将其推送回 Azure。

    # Add SSL binding to custom domain
    $r = Get-AzureResource -Name $siteName -ResourceGroupName $resGroup -ResourceType Microsoft.Web/sites -ApiVersion $apiVersion -OutputObjectFormat New
    # Create an object containing the desired new SSL configuration
    $newSSL = @(
        @{
            "Name" = $hostName;
            "SslState" = 1;
            "Thumbprint" = $sslThumbprint;
            "ToUpdate" = $true;
        }
    )
    # Create an object which concatenates the existing SSL config with the new config object.
    $ssl = @{
        "HostNameSslStates" = $r.Properties.HostNameSslStates + $newSSL
    }
    # Upload the new configuration into the web app.
    Set-AzureResource -ApiVersion $apiVersion -Name $siteName -ResourceGroupName $resGroup -ResourceType Microsoft.Web/sites -PropertyObject $ssl -OutputObjectFormat New
    

    【讨论】:

    • 我认为您正在将 SSL 证书从一个站点复制到新站点,这样您就不需要为该站点执行“上传证书”步骤。那是对的吗?我看不出这实际上如何将 SSL 证书导入一个全新的网站。或者这是在 Azure 的管理证书区域中持有的 SSL 证书?
    • 注意:我只能在 SSL 证书手动上传到我在 powershell 中创建的网站后才能正常工作
    • 我用于新站点的证书是通配符证书,它已经上传并在现有站点上使用。
    • 以上内容也适用于我。通配符证书已上传到同一服务器场中的另一个网站。请注意,您不能获取 Get-AzureRMResource 的输出并对其进行调整 - 这会静默失败。但是,如果您干净地创建属性对象,它就可以正常工作。
    【解决方案2】:

    我终于通过使用 Azure 网站管理 REST API 成功地做到了这一点。

    我在 2014 年基于下面的示例代码的原始文档不再可用,但是 Zain 提到并链接到 cmets 中的博客文章的 Azure 资源浏览器在我看来是一个更好的资源。直接链接:https://resources.azure.com/

    服务管理 REST API 参考似乎与我使用的原始文档最接近,但目前缺少关于 Azure Web 应用程序(以前称为 Azure 网站)的任何内容:https://msdn.microsoft.com/library/azure/ee460799.aspx

    例如:

    using System;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Formatting;
    using System.Net.Http.Headers;
    using System.Security.Cryptography.X509Certificates;
    using System.Threading.Tasks;
    
    private const string managementThumbprint = "0000000000000000000000000000000000000000";
    private const string subscriptionId = "00000000-0000-0000-0000-000000000000";
    
    private const string sslThumbprint = "0000000000000000000000000000000000000000";
    private const string webspace = "eastasiawebspace";
    private const string websiteName = "myWebsite";
    private const string websiteDomain = "myDomain";
    private const SslState sslState = SslState.SniEnabled;
    
    public async Task SetSslState()
    {
        //Retrieve management certificate
        var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        var certificate = store.Certificates.Cast<X509Certificate2>().First(xc => xc.Thumbprint.Equals(managementThumbprint, StringComparison.OrdinalIgnoreCase));
    
        //Setup http client
        var handler = new WebRequestHandler();
        handler.ClientCertificates.Add(certificate);
        var client = new HttpClient(handler) {
            BaseAddress = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/WebSpaces/")
        };
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("x-ms-version", "2014-06-01");
    
        var requestData = new {
            HostNameSslStates = new[] {
                new {
                    Name = websiteDomain,
                    SslState = (long)sslState,
                    Thumbprint = sslThumbprint,
                    ToUpdate = true
                }
            }
        };
        var response = await client.PutAsJsonAsync(webspace + "/sites/" + websiteName, requestData);
    }
    
    public enum SslState
    {
        Disabled = 0,
        SniEnabled = 1,
        IpBasedEnabled = 2
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    相关资源
    最近更新 更多