我有一个Powershell脚本,可以将PFX证书导入Machine商店。此脚本旨在由厨师调用。当我手动运行它时,效果很好。然而,当厨师运行它时,它似乎只完成了一半的操作。
echo "Importing as PFX" >> $log
$rule = new-object security.accesscontrol.filesystemaccessrule ".\IIS_IUSRS", "Read", allow
$pfx = Get-PfxCertificate -FilePath $file
$certThumbprint = $pfx.Thumbprint
echo "Thumbprint is $certThumbprint" >> $log
"$password" | certutil -ImportPFX "$certStoreLocation" "$file" >> $log
$keyPath = "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\" + (((Get-ChildItem -Path cert:\LocalMachine\My | Where-Object { $_.thumbprint -eq $certThumbprint }).PrivateKey).CspKeyContainerInfo).UniqueKeyContainerName
echo "Updating private key permissions: $keyPath" >> $log
$acl = get-acl -path $keyPath
$acl.AddAccessRule($rule)
# Allow IIS to read this private key
set-acl $keyPath $acl
在我的日志中,我看到成功输出:
Examining certificate for import: c:\www\certificates\xyz.pfx
Importing as PFX
Thumbprint is 206E30F2694C8627C18EAE97729C643A3CC1317E
Enter PFX password:
Certificate "xyz" added to store.
CertUtil: -importPFX command completed successfully.
Updating private key permissions: C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\aee87c90ef6606b733130f970131e498_051677f6-7959-49c2-afb8-d2fad091409a
而且,在MMC中,我可以看到我的证书。但是,缺少日志中列出的路径,当我尝试检查私钥时,它表示它已丢失。再一次,当我像我一样运行它时,它导入很好。
我以为我疯了,我用Process Monitor看看发生了什么:
我不知道为什么,但似乎Powershell在关闭文件时标记了删除的私钥。什么??为什么???
笔记:
是否有理由不使用现有的Chef资源来管理Windows中的证书?
https://github.com/chef-cookbooks/windows#windows_certificate
你的rceipe就是一个例子:
windows_certificate 'c:\www\certificates\xyz.pfx' do
pfx_password "my_password"
private_key_acl [".\IIS_USRS"]
store_name "MY"
end