diff --git a/Modulefile b/Modulefile index 9ca55fc..de1dfa0 100644 --- a/Modulefile +++ b/Modulefile @@ -5,4 +5,4 @@ license 'MIT' summary 'Module that will manage IIS for windows server 2008 and above. It will help maintain application pools, sites and virtual applications' project_page 'https://github.com/opentable/puppet-iis' dependency 'puppetlabs/stdlib', '>=3.0.0' -dependency 'joshcooper/powershell', '>=0.0.6' +dependency 'puppetlabs/powershell', '>=1.0.2' diff --git a/manifests/manage_binding.pp b/manifests/manage_binding.pp index f7aed1e..674dd8b 100644 --- a/manifests/manage_binding.pp +++ b/manifests/manage_binding.pp @@ -1,5 +1,5 @@ # -define iis::manage_binding($site_name, $protocol, $port, $host_header = '', $ip_address = '*', $certificate_thumbprint = '', $ensure = 'present') { +define iis::manage_binding($site_name, $protocol, $port, $host_header = '', $ip_address = '*', $certificate_thumbprint = '', $ensure = 'present', $only_manage_binding = false) { include 'iis::param::powershell' if ! ($protocol in [ 'http', 'https', 'net.tcp', 'net.pipe', 'netmsmq', 'msmq.formatname' ]) { @@ -9,20 +9,28 @@ validate_string($site_name) validate_re($site_name,['^(.)+$'], 'site_name must not be empty') validate_re($ensure, '^(present|installed|absent|purged)$', 'ensure must be one of \'present\', \'installed\', \'absent\', \'purged\'') - + validate_bool($only_manage_binding) if ! ($ip_address == '*') { validate_re($ip_address, ['^([0-9]){1,3}\.([0-9]){1,3}\.([0-9]){1,3}\.([0-9]){1,3}$'], "\"${ip_address}\" is not a valid ip address") } if ($ensure in ['present','installed']) { - exec { "CreateBinding-${title}": - path => "${iis::param::powershell::path};${::path}", - command => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; New-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\"\"", - onlyif => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; if (Get-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\" | Where-Object {\$_.bindingInformation -eq \\\"${ip_address}:${port}:${host_header}\\\"}) { exit 1 } else { exit 0 }\"", - logoutput => true, - require => Iis::Manage_site[$site_name], + if($only_manage_binding){ + exec { "CreateBinding-${title}": + path => "${iis::param::powershell::path};${::path}", + command => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; New-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\"\"", + onlyif => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; if (Get-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\" | Where-Object {\$_.bindingInformation -eq \\\"${ip_address}:${port}:${host_header}\\\"}) { exit 1 } else { exit 0 }\"", + logoutput => true, + } + }else{ + exec { "CreateBinding-${title}": + path => "${iis::param::powershell::path};${::path}", + command => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; New-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\"\"", + onlyif => "${iis::param::powershell::command} -Command \"Import-Module WebAdministration; if (Get-WebBinding -Name \\\"${site_name}\\\" -Port ${port} -Protocol \\\"${protocol}\\\" -HostHeader \\\"${host_header}\\\" -IPAddress \\\"${ip_address}\\\" | Where-Object {\$_.bindingInformation -eq \\\"${ip_address}:${port}:${host_header}\\\"}) { exit 1 } else { exit 0 }\"", + logoutput => true, + require => Iis::Manage_site[$site_name], + } } - if ($protocol == 'https') { validate_re($certificate_thumbprint, ['^(.)+$'], 'certificate_thumbprint required for https bindings') if ($ip_address == '0.0.0.0') { diff --git a/templates/create-certificate-binding.ps1.erb b/templates/create-certificate-binding.ps1.erb index 551e177..7bf6765 100644 --- a/templates/create-certificate-binding.ps1.erb +++ b/templates/create-certificate-binding.ps1.erb @@ -1,19 +1,19 @@ Import-Module WebAdministration -$site = Get-Website | Where-Object { $_.Name -eq "<%= site_name %>" } +$site = Get-Website | Where-Object { $_.Name -eq "<%= @site_name %>" } $certsAttachedToSite = Get-ChildItem IIS:\SSLBindings | ? { $site | Select-Object { $_ -contains $_.Sites.Value }} | % { $_.Thumbprint } -$certificate = Get-ChildItem CERT:\LocalMachine\My | ? { $certsAttachedToSite -contains $_.Thumbprint} | Where-Object { $_.Thumbprint -eq "<%= certificate_thumbprint %>" } +$certificate = Get-ChildItem CERT:\LocalMachine\My | ? { $certsAttachedToSite -contains $_.Thumbprint} | Where-Object { $_.Thumbprint -eq "<%= @certificate_thumbprint %>" } -if((Test-Path IIS:\SslBindings\<%= ip_address %>!<%= port %>) -eq $false) { +if((Test-Path IIS:\SslBindings\<%= @ip_address %>!<%= @port %>) -eq $false) { push-location IIS:\SslBindings - Get-Item Cert:\LocalMachine\My\<%= certificate_thumbprint %> | New-Item <%= ip_address %>!<%= port %> + Get-Item Cert:\LocalMachine\My\<%= @certificate_thumbprint %> | New-Item <%= @ip_address %>!<%= @port %> } -elseif (((Test-Path IIS:\SslBindings\<%= ip_address %>!<%= port %>) -eq $true) -and (($certificate -ne $null) -or ($certificate.Thumbprint -ne "<%= certificate_thumbprint %>"))) { +elseif (((Test-Path IIS:\SslBindings\<%= @ip_address %>!<%= @port %>) -eq $true) -and (($certificate -ne $null) -or ($certificate.Thumbprint -ne "<%= @certificate_thumbprint %>"))) { push-location IIS:\SslBindings - Get-Item Cert:\LocalMachine\My\<%= certificate_thumbprint %> | Set-Item <%= ip_address %>!<%= port %> + Get-Item Cert:\LocalMachine\My\<%= @certificate_thumbprint %> | Set-Item <%= @ip_address %>!<%= @port %> foreach ($cert in $certificates) { - if(($cert -ne $null) -and ($cert.Thumbprint -ne "<%= certificate_thumbprint %>")) { - Get-Item Cert:\LocalMachine\My\$cert.Thumbprint | Remove-Item <%= ip_address %>!<%= port %> + if(($cert -ne $null) -and ($cert.Thumbprint -ne "<%= @certificate_thumbprint %>")) { + Get-Item Cert:\LocalMachine\My\$cert.Thumbprint | Remove-Item <%= @ip_address %>!<%= @port %> } } } diff --git a/templates/inspect-certificate-binding.ps1.erb b/templates/inspect-certificate-binding.ps1.erb index e7b1418..cba98e9 100644 --- a/templates/inspect-certificate-binding.ps1.erb +++ b/templates/inspect-certificate-binding.ps1.erb @@ -1,9 +1,9 @@ Import-Module WebAdministration -if((Test-Path IIS:\SslBindings\<%= ip_address %>!<%= port %>) -eq $false) { - $site = Get-Website | Where-Object { $_.Name -eq "<%= site_name %>" } +if((Test-Path IIS:\SslBindings\<%= @ip_address %>!<%= @port %>) -eq $false) { + $site = Get-Website | Where-Object { $_.Name -eq "<%= @site_name %>" } $certsAttachedToSite = Get-ChildItem IIS:\SSLBindings | ? { $site | Select-Object { $_ -contains $_.Sites.Value }} | % { $_.Thumbprint } - $certificate = Get-ChildItem CERT:\LocalMachine\My | ? { $certsAttachedToSite -contains $_.Thumbprint} | Where-Object { $_.Thumbprint -eq "<%= certificate_thumbprint %>" } + $certificate = Get-ChildItem CERT:\LocalMachine\My | ? { $certsAttachedToSite -contains $_.Thumbprint} | Where-Object { $_.Thumbprint -eq "<%= @certificate_thumbprint %>" } if ($certificate -ne $null ){ exit 1 }