Windows 上的厨房 EC2 - 最终 WinRM::WinRMAuthorizationError

Kitchen EC2 on Windows - Eventual WinRM::WinRMAuthorizationError

我正在使用 Kitchen EC2 驱动程序在来自 Amazon 的 Windows Server 2012R2、2016 和 1803 AMI 上测试一些 Windows "base" 食谱。在通过 Kitchen 执行食谱时,我遇到了一个似乎在特定时间后发生的问题,而不是点击特定的代码行。

在配方收敛过程中的某个时刻,它以 WinRM::WinRMAuthorizationError 停止。如果我重新运行 converge,它会立即以同样的错误引导我。我试过更改和移动配方中的资源,似乎这不是配方的特定部分的问题,它只是随机发生然后继续发生。

这是因为我试图删除我们的自定义用户数据脚本,而是使用 Kitchen-EC2 驱动程序生成的脚本。它们 大致 相同,只是做事的方式略有不同。

我打算做更多的故障排除(this 似乎是一个很好的资源)但是考虑到我正在尝试使用原始设置,我希望我遗漏了一些明显的东西。

.kitchen.yml的相关部分:

transport:
  name: 'winrm'
  elevated: true
  username: 'Administrator'
  ssh_key: ~/.ssh/test-kitchen

默认用户数据:

# Logic for determining $logfile is removed...
$logfile='C:\Program Files\Amazon\Ec2ConfigService\Logs\kitchen-ec2.log'

# Allow script execution
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force

# PS Remoting and & winrm.cmd basic config
Enable-PSRemoting -Force -skipnetworkprofilecheck

& winrm.cmd set winrm/config '@{MaxTimeoutms="1800000"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser="50"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile

# Firewall Config
& netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any  >> $logfile
Set-ItemProperty -Name LocalAccountTokenFilterPolicy -Path HKLM:\software\Microsoft\Windows\CurrentVersion\Policies\system -Value 1

我最好的猜测是这与我使用的 AMI 有关,它是一个 CIS 图像。

更具体地说,我认为用户数据脚本确实适用于初始 WinRM 授权。但是一些与用户数据中的 winrm.cmd 命令冲突的 CIS AMI 组策略 可能 后来应用并且终止了连接。我的想法是 运行 gpupdate 后来作为正常配方的一部分重新应用它们。

我认为 运行 用户数据中的这些命令很有帮助。它需要安装包含 Set-PolicyFileEntry 命令的模块。

Set-PolicyFileEntry -Path $MachineDir -Key Software\Policies\Microsoft\Windows\WinRM\Client -ValueName AllowBasic -Data 1 -Type DWord  
Set-PolicyFileEntry -Path $MachineDir -Key Software\Policies\Microsoft\Windows\WinRM\Service -ValueName AllowBasic -Data 1 -Type DWord 
Set-PolicyFileEntry -Path $MachineDir -Key Software\Policies\Microsoft\WindowsFirewall\DomainProfile -ValueName EnableFirewall -Data 0 -Type DWord 
Set-PolicyFileEntry -Path $MachineDir -Key Software\Policies\Microsoft\WindowsFirewall\PublicProfile -ValueName EnableFirewall -Data 0 -Type DWord 
Set-PolicyFileEntry -Path $MachineDir -Key Software\Policies\Microsoft\WindowsFirewall\PrivateProfile -ValueName EnableFirewall -Data 0 -Type DWord 
Set-PolicyFileEntry -Path $MachineDir -Key Software\Microsoft\Windows\CurrentVersion\Policies\System -ValueName LocalAccountTokenFilterPolicy -Data 1 -Type DWord 

gpupdate