【问题标题】:Azure DevOps Powershell script Could not create SSL/TLS secure channelAzure DevOps Powershell 脚本无法创建 SSL/TLS 安全通道
【发布时间】:2021-01-04 11:10:17
【问题描述】:

问题:
如果我从 DevOps 中的 'Azure Powershell' 任务 调用命令 Get-AzureDeployment(也是 Get-AzureService),我得到:'请求被中止:可能不创建 SSL/TLS 安全通道。'

简介:

  • 我使用 Azure DevOps 将 Azure 云服务(经典)部署到 Azure 中
  • “Azure Powershell”任务一直在运行,但没有进行任何更改就停止了工作
  • 我还将证书(由 DevOps 使用)导入我的电脑(通过 Import-AzurePublishSettingsFile)并尝试运行相同的 powershell 脚本,并且每个脚本都可以正常工作,所以 我预计 DevOps 会出现问题

DevOps 连接:

  • 我在 Azure DevOps 中创建了“服务连接”以连接到 Azure
  • 服务 连接类型为“Azure 经典”(因为“Azure 资源管理器”不适用于“经典云服务”)
  • 的认证方法 “Azure 经典服务连接”是“基于证书的”。
  • 我用过 由“发布设置文件”为我的 azure 生成的证书 订阅。证书被 azure 放入 我的订阅->管理证书(到期日期在中间 2021)
  • 我使用此服务连接将应用程序(经典云服务)部署到 Azure 没有问题(通过 DevOps 任务“Azure 云服务部署”)只是“ Azure Powershell 的任务开始失败

这一切都工作了 3 个月,然后由于任何原因停止工作。 奇怪的是,当我在玩 DevOps 以找出问题所在时,任务曾经成功运行,但是当我再次尝试时,我又遇到了错误。

我有两个日志,来自成功的调用和失败的调用。 2506行日志相同,变化在此行之后。

我可以将两个完整的日志都发送给您,但我不想在这里放置这么长的日志。

尝试成功:

VERBOSE: 8:31:40 AM - Begin Operation: Get-AzureDeployment
VERBOSE: 8:31:42 AM - Completed Operation: Get-AzureDeployment
... some other info about the deployment in slot

通话失败记录:

VERBOSE: 9:53:39 AM - Begin Operation: Get-AzureDeployment
##[debug]Caught exception from task script.
##[debug]Error record:
##[debug]Get-AzureDeployment : An error occurred while sending the request.
##[debug]At D:\a\r1\a\_Tools\Powershell\cloud-service_swap-slot.ps1:14 char:15
##[debug]+ ... eployment = Get-AzureDeployment -Slot "Staging" -ServiceName $CloudSe ...
##[debug]+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##[debug]    + CategoryInfo          : CloseError: (:) [Get-AzureDeployment], HttpRequestException
##[debug]    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.GetAzureDeploymentComma    nd
##[debug] 
##[debug]Script stack trace:
##[debug]at <ScriptBlock>, D:\a\r1\a\_Tools\Powershell\cloud-service_swap-slot.ps1: line 14
##[debug]at <ScriptBlock>, <No file>: line 1
##[debug]at <ScriptBlock>, D:\a\_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\3.171.2\AzurePowerShell.ps1: line 145
##[debug]at <ScriptBlock>, D:\a\_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\3.171.2\AzurePowerShell.ps1: line 141
##[debug]at <ScriptBlock>, <No file>: line 1
##[debug]at <ScriptBlock>, <No file>: line 22
##[debug]at <ScriptBlock>, <No file>: line 18
##[debug]at <ScriptBlock>, <No file>: line 1
##[debug]Exception:
##[debug]System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
##[debug]   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
##[debug]   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
##[debug]   --- End of inner exception stack trace ---
##[debug]   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
##[debug]   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
##[debug]   at Microsoft.WindowsAzure.Management.Compute.DeploymentOperationsExtensions.GetBySlot(IDeploymentOperations operations, String serviceName, DeploymentSlot deploymentSlot)
##[debug]   at Microsoft.WindowsAzure.Commands.Utilities.Common.ServiceManagementBaseCmdlet.ExecuteClientActionNewSM[TResult](Object input, String operationDescription, Func`1 action, Func`3 contextFactory)
##[error]An error occurred while sending the request.
##[debug]Processed: ##vso[task.logissue type=error]An error occurred while sending the request.
##[debug]Processed: ##vso[task.complete result=Failed]

在这两个日志中,我还可以找到将 Azure 帐户添加到 Powershell 的内容:

##[debug]Added certificate to the certificate store.
##[command]Set-AzureSubscription -SubscriptionName PXX -SubscriptionId XXXXXX01-09f5-4703-bcc9-6ff914XXXXXX -Certificate ******** -Environment AzureCloud 
##[command]Select-AzureSubscription -SubscriptionId XXXXXX01-09f5-4703-bcc9-6ff914XXXXXX
##[debug]Leaving Initialize-Azure.
## Initializing Azure Complete 

(我已经用 X 替换了一些字符串)

YAML 中有 Powershell 任务:

steps:
- task: AzurePowerShell@3
  displayName: 'Swap slots'
  inputs:
    azureConnectionType: ConnectedServiceName
    azureClassicSubscription: 'PXX subscription'
    ScriptPath: '$(System.DefaultWorkingDirectory)/_Tools/Powershell/cloud-service_swap-slot.ps1'
    ScriptArguments: '-CloudServiceName $(CloudServiceName)'
    FailOnStandardError: true
    azurePowerShellVersion: LatestVersion

还有用于交换插槽的 Powershell 脚本,该脚本可在本地 pc(具有相同的证书)上工作,但在 DevOps 中失败:

[CmdletBinding(PositionalBinding=$True)]
Param(
    [Parameter(Mandatory = $true)]
    [String]$CloudServiceName              # required
)

# Check if Windows Azure Powershell is avaiable 
if ((Get-Module -ListAvailable Azure) -eq $null) 
{ 
    throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools" 
} 
 
# VIP Swap
$Deployment = Get-AzureDeployment -Slot "Staging" -ServiceName $CloudServiceName #It's failing here
if ($Deployment -ne $null -AND $Deployment.DeploymentId  -ne $null) 
{ 
     Write-Output ("Current Status of staging in {0}" -f $CloudServiceName); 
     Write-Host ($Deployment | Select-Object -Property * -ExcludeProperty Configuration,RolesConfiguration | Format-List | Out-String);

     $MoveStatus = Move-AzureDeployment -ServiceName $CloudServiceName 
     Write-Output ("Vip swap of {0} status: {1}" -f $CloudServiceName, $MoveStatus.OperationStatus)     
}else 
{ 
     Write-Output ("There is no deployment in staging slot of {0} to swap." -f $CloudServiceName) 
} 

有没有人和我一样的经历?问题可能出在哪里?

#更新

我尝试在脚本开头添加此安全协议设置,但出现相同的错误。

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

【问题讨论】:

  • 在调用 azure 端点之前尝试将其添加到脚本内容中:[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  • 我试过了,但没有任何改变
  • @Petofi 无法获得您的最新信息,Neil 的解决方法对您有帮助吗?
  • @HughLin-MSFT 不,它没有帮助
  • 我们遇到了同样的问题。我们的 CI 构建已经被破坏了一天,我们不能向我们的客户发布修复:(

标签: azure powershell ssl azure-devops


【解决方案1】:

这似乎是 Microsoft 构建代理的问题,但将以下代码添加到每个 Azure Powershell 任务的开头似乎已经为我们解决了这个问题,直到他们弄清楚为止。

$pcert = (Get-Variable Endpoint -ValueOnly).Auth.Parameters.certificate
$bytes = [convert]::FromBase64String($pcert) 
[IO.File]::WriteAllBytes("C:\cert.pfx",$bytes) 
$null = Import-PfxCertificate -FilePath C:\cert.pfx -CertStoreLocation cert:\CurrentUser\My

【讨论】:

  • 最后一部分是什么?当前用户\我的??好像是我把它放到c里面,然后添加到Cert Store?
【解决方案2】:

有几个可行的解决方法。

  1. 解决方法 1

    1. 使用 Azure Powershell 任务版本 5
    2. 选择“Azure 资源管理器连接”
    3. ASM Module commands 更改为Az commands
  2. 解决方法 2

    1. 使用 Azure Powershell 任务版本 3
    2. 经典连接
    3. 将 Azure Powershell 版本降级到 4.2.1
  3. 解决方法 3(针对私人代理)

    1. 在您的 CurrentUser\My 商店中创建一个自签名证书。您可以在此处找到有关创建自签名证书的详细信息:https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-certs-create
    2. 将证书的公钥导出为 DER 编码的 CER 文件(您可以为此使用 mmc 或任何其他证书工具)
    3. 通过门户将 .Cer 文件作为管理证书上传:https://docs.microsoft.com/en-us/previous-versions/azure/azure-api-management-certs
    4. 使用 Set-AzureSubscription 进行身份验证:
    PS C:\> 清除 AzureProfile PS C:\> $cert = 获取项目证书:\CurrentUser\My\ PS C:\ > Set-AzureSubscription -SubscriptionName "" -SubscriptionId -Certificate $cert PS C:\> 选择-AzureSubscription -SubscriptionId
  4. 解决方法 4

    1. 将以下脚本添加到每个 Azure Powershell 任务

      $p = (Get-Variable Endpoint -ValueOnly).Auth.Parameters.certificate $bytes = [转换]::FromBase64String($p) [IO.File]::WriteAllBytes("C:\cert.pfx",$bytes) Import-PfxCertificate -FilePath C:\cert.pfx -CertStoreLocation cert:\CurrentUser\My . . 实际脚本 . . #从存储中删除证书 $thumb = (Get-PfxData -FilePath "C:\cert.pfx").EndEntityCertificates.Thumbprint Remove-Item -Path cert:\CurrentUser\My\$thumb -recurse -Force

【讨论】:

    【解决方案3】:

    我们遇到了同样的问题。它只出现在windows-2019 代理上,而不是vs2017-win2016。 因此,您可以通过更改代理类型来修复它,但更好的修复方法是使用特定的 Powershell 版本的 5.1.1 而不是 latest。似乎 latest 最近增加到 5.3.0 导致此错误。

    【讨论】:

    • 我在一行中有 3 个“Azure Powershell”任务(用于交换槽、挂起暂存和删除暂存槽)。我为所有这些设置了 vs2017-win2016 和版本 5.1.1 并尝试了。它仅适用于第一个“Azure Powershell”任务(交换插槽)。但它在同一命令“Get-AzureDeployment”上的第二个失败,并出现相同的错误“请求被中止:无法创建 SSL/TLS 安全通道。”嗯,为什么这只适用于第一个?
    • 使用 5.1.1 并没有最初看起来那么成功。如果我继续重新运行相同的管道,它将在大约 50-75% 的时间内成功。
    【解决方案4】:

    我有同样的问题并尝试了所有这些,对我来说,只有在 Initialize-Azure 部分之后添加它才有效:“Set-AzureSubscription -SubscriptionId [my subscription id] -CurrentStorageAccountName [storage name]”

    【讨论】:

      【解决方案5】:

      此问题是由 .NET 9 月更新引入的行为变化引起的。以下代码将通过环境变量恢复密钥的隐式存储(以前的 .Net 4.x 行为):

      Set-Item env:\COMPLUS_CngImplicitPersistKeySet 1
      

      注意:然后您需要创建一个新的发布设置文件,因为旧证书和密钥不会被后续导入覆盖。

      【讨论】:

      【解决方案6】:

      使用 AzurePowerShell@3 任务将 Az Powershell 版本降级到 4.2.1。

      【讨论】:

      • 这是一个在某些情况下有效的修复程序,所以不知道为什么不赞成,但我赞成回馈。人们在没有开始讨论的情况下随意投反对票,这太糟糕了。
      【解决方案7】:

      在我的情况下,我在本地 Team Foundation Server 2017 上的“运行内联 Azure Powershell”任务中的 Get-AzureServiceRemoteDesktopExtension 命令中遇到了这个问题。

      我尝试在此处添加@damien-caro 建议的行...

      Set-Item env:\COMPLUS_CngImplicitPersistKeySet 1
      

      ... 到我的脚本,但这仅解决了单个版本的问题。所有后续发布/部署再次失败,Could not create SSL/TLS secure channel

      只需将其作为变量添加到发布定义中即可解决问题。 (为了测试它,我已经成功地将相同的版本部署到两个环境中,连续 3 次。)

      我在这里找到了这个解决方案:https://github.com/Azure/azure-powershell/issues/13117#issuecomment-706665722

      【讨论】:

        【解决方案8】:

        就我而言,我不小心提到了 https 而不是 http。我的报表服务器是 http 的,没有证书,但即使我在组织内部键入 https,它也会在浏览器中重定向。但是 powershell 需要确切的 url。我使用的版本是 powershell@2,我调用了一个 powershell 脚本文件。

        【讨论】:

          猜你喜欢
          • 2019-08-12
          • 2015-08-11
          • 2016-11-18
          • 2021-12-13
          • 2014-12-26
          • 2017-02-10
          • 2017-08-22
          • 2016-08-03
          • 2018-06-17
          相关资源
          最近更新 更多