Windows Chef添加ssl证书并绑定到IIS

时间:2018-08-23 00:47:24

标签: ssl ssl-certificate chef chef-recipe chef-windows

我正在使用Windows Chef食谱https://supermarket.chef.io/cookbooks/windows/versions/5.0.0#readme

创建并绑定ssl。

首先我尝试:

# Create/update certificate
windows_certificate "create cert" do
    source "c://hn/ssl/cert.pfx"
    pfx_password  {cert_pass}
    store_name "WEBHOSTING"
    action :create
end

# Bind certificate
windows_certificate_binding "bind to IIS" do
    action :create
    cert_name "{my_ssl_hash_number}"
    name_kind :hash
    port 443
    store_name "WEBHOSTING"
end

我遇到了以下错误:

  

STDOUT:SSL证书添加失败,错误:指定的1312 A   登录会话不存在。它可能已经终止了。

我做了一些研究,看来我导入的证书不可导出,需要授予私钥访问权限,请参考: SSL Certificate add failed when binding to port

以下是我的第二次尝试:

# Create/update certificate
windows_certificate "create cert" do
    source "c://hn/ssl/cert.pfx"
    pfx_password  {cert_pass}
    store_name "WEBHOSTING"
    private_key_acl ["IIS_IUSRS"]
    action [:create, :acl_add]
end 

# Bind certificate
windows_certificate_binding "bind to IIS" do
    action :create
    cert_name "{my_ssl_hash_number}"
    name_kind :hash
    port 443
    store_name "WEBHOSTING"
end

但是,我仍然遇到错误:

  

STDOUT:          STDERR:C:\ Users \ Administrator \ AppData \ Local \ Temp \ chef-script20180823-492-10cuvyo.ps1   :不存在私钥。

有人可以帮我吗?如何正确导入SSL并绑定到IIS?预先感谢。

2 个答案:

答案 0 :(得分:0)

对我来说,替代解决方案是使用Powershell脚本添加SSL证书,而不是使用Windows食谱

答案 1 :(得分:0)

下面的代码厨师食谱我曾经绑定过ssl证书和https端口。我也很注意是否要添加新证书,然后再添加它。

hostname = node['hostname']
hostnamelike = 'CN=' + node['hostname'].to_s + '*'
powershell_script 'find ssl certificate  on local machine root and assign certificate' do
  code <<-EOH
  $iisSite='your site name'
  $hostname="#{hostname}"
  $hostnamelike="#{hostnamelike}"
  $protocol='https'
  $port=443
  Get-WebBinding -Port $port -Name $iissite | Remove-WebBinding
  $guid_value = [GUID]::NewGUID().ToString('B')
  $thumbprint = (Get-ChildItem cert:\\LocalMachine\\my | where-object { $_.Subject -like $hostnamelike  } | Select-Object -First 1).Thumbprint
  New-WebBinding -Name $iissite -IP "*" -Port $port -Protocol https
  netsh http show sslcert ipport=0.0.0.0:$port
  if ($LASTEXITCODE -eq 1) {
  netsh http add sslcert ipport=0.0.0.0:$port certhash=$thumbprint appid=$guid_value certstorename=MY
  }
  else {
  netsh http delete sslcert ipport=0.0.0.0:$port
  netsh http add sslcert ipport=0.0.0.0:$port certhash=$thumbprint appid=$guid_value certstorename=MY
  }
  EOH
end