【问题标题】:How to upload Private Key Certificates (.pfx), Public Key Certificates (.cer) to Azure WebApp如何将私钥证书 (.pfx)、公钥证书 (.cer) 上传到 Azure WebApp
【发布时间】:2020-01-18 14:50:27
【问题描述】:

如何使用 Azure Powershell 将私有、公共证书上传到 Azure AppService。我知道 New-AzureRmWebAppSSLBinding 但我没有进行任何 SSL 绑定。

我们有使用 SSL 绑定的 Azure 应用服务。为此,我使用 New-AzureRmWebAppSSLBinding 上传证书。我确实在我的网络应用程序上为每个主机上传了一个证书。这工作正常。 但我想将额外的私有和公共证书上传到此应用服务以进行 API 验证。我没有找到任何用于上传私有或公共证书的 azure powershell 命令。

Azure 门户允许上传私有证书及其密码或公共证书。但是我想使用 powershell 做同样的事情。门户 UI 还具有从密钥库导入证书的选项。我确定可以将证书上传到密钥保管库,但没有 powershell 命令将其导入 Azure 应用服务。

<a href="https://ibb.co/Kh7t5DL"><img src="https://i.ibb.co/fFt3X9n/Capture-Cert.jpg" alt="Capture-Cert" border="0"></a>

我已经阅读了这些文章,但它们都使用相同的命令。 https://github.com/Azure/azure-powershell/issues/2108 How to add a certificate to an Azure RM website with Powershell

New-AzureRmWebAppSSLBinding -ResourceGroupName $RGName -WebAppName $webAppName -CertificateFilePath $filePath -CertificatePassword $pass

如果我调用此方法,它会询问主机名。由于我已经为此主机名上传了带有 SSL 绑定的证书,因此我无法使用它。如果不提供主机名,此命令将失败。

【问题讨论】:

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


    【解决方案1】:

    好的,我终于能够弄清楚并上传私人和公共证书。 Azure resource explorer 对了解文件夹结构和证书位置很有帮助。

    上传公共证书:这些是每个应用服务附加的。

    $webApps = @{
                "Dev_AppServicesGroup" = "DevUserService"
            }
    $certName = "chain-cert.cer"
    $Path = "C:\Certs"    
    
    $fullpath = $path + '\' + $certname
    $pwd = ConvertTo-SecureString -String 'anyPwd' -AsPlainText -Force
    $cert  = New-AzureRmApplicationGatewaySslCertificate -Name 'someCert' -CertificateFile $fullpath -Password $pwd
    $apiVersion = '2018-02-01'
    
    if($cert)
    {
        $PropertiesObject = @{
            blob=$cert.Data; 
            publicCertificateLocation= "CurrentUserMy"
        }
    
        foreach($resourceGroup in $webApps.Keys)
        {
           $webAppName = $webApps.Item($resourceGroup)        
           $resource = Get-AzureRmWebApp -ResourceGroupName $resourceGroup -Name $webAppName
           $resourceName = $resource.Name + "/"+$certName
           New-AzureRmResource -Location $resource.Location -PropertyObject $PropertiesObject -ResourceGroupName $resource.ResourceGroup -ResourceType Microsoft.Web/sites/publicCertificates -ResourceName $resourceName -ApiVersion $apiVersion -Force        
    
           #Apply the cert to the deployment slots if any
           $slots = Get-AzureRmResource -ResourceGroupName $resource.ResourceGroup -ResourceType Microsoft.Web/sites/slots -ResourceName $webAppName -ApiVersion $apiVersion
           foreach($slot in $slots)
           {            
              $resourceName = $slot.Name + "/"+$certName                     
              New-AzureRmResource -Location $slot.Location -PropertyObject $PropertiesObject -ResourceGroupName $slot.ResourceGroupName -ResourceType Microsoft.Web/sites/slots/publicCertificates -ResourceName $resourceName -ApiVersion $apiVersion -Force            
           }
        }
    }
    

    上传私有证书:这些证书按资源组上传,可供该组下的所有应用服务使用。

    #Private certs needs to be uploaded to each resource group with app services
    $resourceGroups = @("Dev_AppServicesGroup1", "Dev_AppServicesGroup2")
    $certName = "event-store-user.p12"
    
    $certPwd = "Your certificate password" #This is the private cert password
    $Path = "C:\Certs"   
    
    $fullpath = $path + '\' + $certname    
    
    $pwd = ConvertTo-SecureString -String 'SomePwd' -AsPlainText -Force
    $cert  = New-AzureRmApplicationGatewaySslCertificate -Name someCert -CertificateFile $fullpath -Password $pwd
    $apiVersion = '2018-02-01'
    
    if($cert)
    {
        $PropertiesObject = @{
            pfxBlob=$cert.Data;  
            password =$certPwd; #This is the private cert password        
            ResourceType = "Microsoft.Web/Certificates"
        }
    
        foreach($resourceGroup in $resourceGroups)
        {
            $resource = Get-AzureRmResourceGroup -Name $resourceGroup       
            New-AzureRmResource -ResourceName $certName -Location $resource.Location -PropertyObject $PropertiesObject -ResourceGroupName $resource.ResourceGroupName -ResourceType Microsoft.Web/certificates -ApiVersion $apiVersion -Force        
        }
    }
    

    就是这样。要上传 SSL 证书并将其绑定到应用服务,您可以使用命令“New-AzWebAppSSLBinding”。

    【讨论】:

      【解决方案2】:

      根据我的测试,如果你想为你的Azure web app绑定ssl,可以参考如下脚本:

      $webappName=""
      $groupName=""
      # set custom doamin
      $fqdn="<your custom domain name>"
      Set-AzureRmWebApp -Name $webappName -ResourceGroupName $groupName -HostNames($fqdn, "$webappName.azurewebsites.net") 
      
      #bind ssl
      $pfxPath="<Replace with path to your .PFX file>"
      $pfxPassword="<Replace with your .PFX password>"
      #Upload and bind the SSL certificate to the web app
      New-AzureRmWebAppSSLBinding -WebAppName $webappName -ResourceGroupName $groupName -Name $fqdn -CertificateFilePath $pfxPath -CertificatePassword $pfxPassword -SslState SniEnabled   
      
      #bind an existing Azure certificate
      New-AzureRmWebAppSSLBinding -WebAppName $webappName -ResourceGroupName $groupName -Name $fqdn -Thumbprint "the thumbprint of the cert"
      

      【讨论】:

      • 正如我提到的,我已经使用该命令将 SSL 证书绑定到我的应用程序。我说的是上传额外的私有和公共证书。
      • 根据我的研究,我们可以使用 Azure CLI 命令az webapp config ssl upload 上传私钥证书。更多详情请参考docs.microsoft.com/en-us/azure/app-service/…
      • 该命令等效于 New-AzWebAppSSLBinding。在您分享的页面中查看 CLI 和 powershell 的示例代码。我的要求是上传私有和公共证书,而不是 SSL 绑定。这些是不同的东西。
      • 命令az webapp config ssl upload只是用来上传私钥证书。如果要绑定ssl,我们应该继续运行命令az webapp config ssl bind
      • 关于如何上传公共证书,我需要做一些测试。
      猜你喜欢
      • 2013-07-11
      • 2015-01-20
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2019-12-20
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多