summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/windows
diff options
context:
space:
mode:
authornwsparks <nsparks@outlook.com>2018-01-18 19:00:35 -0500
committerJordan Borean <jborean93@gmail.com>2018-01-19 10:00:35 +1000
commitbeb0fd9b8bd6b172d44916adb73e68949b585837 (patch)
tree2b85aec313407cf90fd6e4cdec6b196b807003b8 /lib/ansible/modules/windows
parent389f4ef1fb71629a3389306e6324ea5b118275ff (diff)
downloadansible-beb0fd9b8bd6b172d44916adb73e68949b585837.tar.gz
win_iis_webbinding: Fix bug with ipaddress * returning multiple bindings (#34721)
* win_iis_webbinding: Fix bug with ipaddress * returning multiple bindings instead of only the ones defined as *. Address possible future issues around hostheader * by just disallowing it. Resolves 25473. Added new test for this case. Removed all validation for https binding collisions due to difficulty in validating all cases in which they could or could not collide. As a result, also removed return values relating to certificate data. Updated testing and docs appropriately * win_iis_webbinding: added break to remove binding loops
Diffstat (limited to 'lib/ansible/modules/windows')
-rw-r--r--lib/ansible/modules/windows/win_iis_webbinding.ps1180
-rw-r--r--lib/ansible/modules/windows/win_iis_webbinding.py33
2 files changed, 57 insertions, 156 deletions
diff --git a/lib/ansible/modules/windows/win_iis_webbinding.ps1 b/lib/ansible/modules/windows/win_iis_webbinding.ps1
index 8f020c3196..94d827f94e 100644
--- a/lib/ansible/modules/windows/win_iis_webbinding.ps1
+++ b/lib/ansible/modules/windows/win_iis_webbinding.ps1
@@ -52,84 +52,39 @@ function Create-BindingInfo {
}
# Used instead of get-webbinding to ensure we always return a single binding
+# We can't filter properly with get-webbinding...ex get-webbinding ip * returns all bindings
# pass it $binding_parameters hashtable
function Get-SingleWebBinding {
- $bind_search_splat = @{
- 'name' = $args[0].name
- 'protocol' = $args[0].protocol
- 'port' = $args[0].port
- 'ip' = $args[0].ip
- 'hostheader' = $args[0].hostheader
- }
- # if no bindings exist, get-webbinding fails with an error that can't be ignored via error actions on older systems
- # let's ignore that specific error
- If (-not $bind_search_splat['hostheader'])
- {
- Try {
- Get-WebBinding @bind_search_splat | Where-Object {$_.BindingInformation.Split(':')[-1] -eq [string]::Empty}
- }
- Catch {
- If (-not $_.Exception.Message.CompareTo('Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value'))
- {
- Throw $_.Exception.Message
- }
- }
+ Try {
+ $site_bindings = get-webbinding -name $args[0].name
}
- Else
- {
- Try {
- Get-WebBinding @bind_search_splat
- }
- Catch {
- If (-not $_.Exception.Message.CompareTo('Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value'))
- {
- Throw $_.Exception.Message
- }
+ Catch {
+ # 2k8r2 throws this error when you run get-webbinding with no bindings in iis
+ If (-not $_.Exception.Message.CompareTo('Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value'))
+ {
+ Throw $_.Exception.Message
}
+ Else { return }
}
-}
-
-Function Get-CertificateSubjects {
-Param (
-[string]$CertPath
-)
- If (-Not (Test-Path $CertPath) )
- {
- Fail-Json -obj $result -message "Unable to locate certificate at $CertPath"
- }
-
- $cert = get-item $CertPath
- If ([version][System.Environment]::OSVersion.Version -ge [version]6.2)
+ Foreach ($binding in $site_bindings)
{
- $cert.DnsNameList.unicode
- }
- Else
- {
- $san = $cert.extensions | Where-Object {$_.Oid.FriendlyName -eq 'Subject Alternative Name'}
- If ($san)
- {
- $san.Format(1) -split '\r\n' | Where-Object {$_} | ForEach-Object {
- ($_ -split '=')[-1]
- }
- }
- Else
+ $splits = $binding.bindingInformation -split ':'
+
+ if (
+ $args[0].protocol -eq $binding.protocol -and
+ $args[0].ipaddress -eq $splits[0] -and
+ $args[0].port -eq $splits[1] -and
+ $args[0].hostheader -eq $splits[2]
+ )
{
- If ($cert.subject -like "*,*")
- {
- ($cert.Subject | Select-String "CN=(.*?),?").matches.groups[-1].value
- }
- Else
- {
- $cert.subject -replace "CN=",''
- }
+ Return $binding
}
}
}
-
#############################
### Pre-Action Validation ###
#############################
@@ -203,29 +158,6 @@ If ($certificateHash -and $state -eq 'present')
{
Fail-Json -obj $result -message "Unable to locate certificate at $cert_path"
}
-
- #check if cert is wildcard and update results with useful info.
- $cert_subjects = Get-CertificateSubjects $cert_path
- $result.certificate_subjects = $cert_subjects
- If ($cert_subjects | Where-Object {$_ -match '^\*'})
- {
- $cert_is_wildcard = $true
- $result.cert_is_wildcard = $cert_is_wildcard
- }
- Else
- {
- $cert_is_wildcard = $false
- $result.cert_is_wildcard = $cert_is_wildcard
- }
-
- If ($os_version -lt [version]6.2 -and $host_header -and -not $cert_is_wildcard)
- {
- Fail-Json -obj $result -message "You cannot specify host headers with SSL unless it is a wildcard certificate."
- }
- Elseif ($os_version -ge [version]6.2 -and $host_header -and (-not $cert_is_wildcard -and $sslFlags -eq 0))
- {
- Fail-Json -obj $result -message "You cannot specify host headers with SSL unless it is a wildcard certificate or SNI is enabled."
- }
}
# make sure binding info is valid for central cert store if sslflags -gt 1
@@ -236,10 +168,10 @@ If ($sslFlags -gt 1 -and ($certificateHash -ne [string]::Empty -or $certificateS
the certificate is automatically retrieved from the store rather than manually assigned to the binding."
}
-# make sure host_header: '*' only present when state: absent
-If ($host_header -match '^\*$' -and $state -ne 'absent')
+# disallow host_header: '*'
+If ($host_header -eq '*')
{
- Fail-Json -obj $result -message "host_header: '*' can only be used in combinaiton with state: absent"
+ Fail-Json -obj $result -message "To make or remove a catch-all binding, please omit the host_header parameter entirely rather than specify host_header *"
}
##########################
@@ -259,6 +191,10 @@ If ($host_header)
{
$binding_parameters.HostHeader = $host_header
}
+Else
+{
+ $binding_parameters.HostHeader = [string]::Empty
+}
# Get bindings matching parameters
Try {
@@ -274,10 +210,27 @@ Catch {
If ($current_bindings -and $state -eq 'absent')
{
Try {
- # will remove multiple objects in the case of * host header
- $current_bindings | Remove-WebBinding -WhatIf:$check_mode
+ #there is a bug in this method that will result in all bindings being removed if the IP in $current_bindings is a *
+ #$current_bindings | Remove-WebBinding -verbose -WhatIf:$check_mode
+
+ #another method that did not work. It kept failing to match on element and removed everything.
+ #$element = @{protocol="$protocol";bindingInformation="$ip`:$port`:$host_header"}
+ #Remove-WebconfigurationProperty -filter $current_bindings.ItemXPath -Name Bindings.collection -AtElement $element -WhatIf #:$check_mode
+
+ #this method works
+ [array]$bindings = Get-WebconfigurationProperty -filter $current_bindings.ItemXPath -Name Bindings.collection
+
+ $index = Foreach ($item in $bindings) {
+ If ( $protocol -eq $item.protocol -and $current_bindings.bindingInformation -eq $item.bindingInformation ) {
+ $bindings.indexof($item)
+ break
+ }
+ }
+
+ Remove-WebconfigurationProperty -filter $current_bindings.ItemXPath -Name Bindings.collection -AtIndex $index -WhatIf:$check_mode
$result.changed = $true
}
+
Catch {
Fail-Json -obj $result -message "Failed to remove the binding from IIS - $($_.Exception.Message)"
}
@@ -299,7 +252,7 @@ ElseIf (-Not $current_bindings -and $state -eq 'absent')
### Modify existing bindings ###
################################
<#
-since we have already.binding_info the parameters available to get-webbinding,
+since we have already have the parameters available to get-webbinding,
we just need to check here for the ones that are not available which are the
ssl settings (hash, store, sslflags). If they aren't set we update here, or
exit with changed: false
@@ -373,44 +326,6 @@ ElseIf ($current_bindings)
########################
ElseIf (-not $current_bindings -and $state -eq 'present')
{
- If ($certificateHash)
- {
- <#
- Make sure a valid binding is specified. It's possible for another site to have a binding on the same IP:PORT. If
- we bind to that same ip port without hostheader/sni it will cause a collision. Note, this check only matters for
- https. Http will generate an error when new-webbinding is called if there is a conflict, unlike https.
-
- I couldn't think of a good way to handle scenarios involving wildcards. There's just too many to think about and I
- wouldn't want to potentially hard fail valid scenarios here that I did not consider...so those can still collide. We just skip
- validation anytime an existing binding is a wildcard.
-
- If a collision does occur, the website will be stopped. To help with this we'll return the website state into results.
- #>
-
- #use this instead of get-webbinding. on 2k8r2 get-webbinding fails with an error if a site with no bindings exists
- $binding_matches = (Get-Website).bindings.collection | Where-Object {$_.BindingInformation -eq "$ip`:$port`:"}
-
- #get dns names for all certs in matching bindings
- $subjects = Foreach ($binding in $binding_matches)
- {
- $cert_path = "cert:\localmachine\$($binding.certificatestorename)\$($binding.certificatehash)"
- Get-CertificateSubjects $cert_path
- }
-
- #skip validating scenarios where existing certs are wildcard
- If (-not ($subjects | Where-Object {$_ -match "^\*"}))
- {
- If ($sslFlags -eq 0 -and $binding_matches -and $os_version -gt [version]6.2)
- {
- Fail-Json -obj $result -message "A conflicting binding has been found on the same ip $ip and port $port. To continue, you will either have to remove the offending binding or enable sni"
- }
- ElseIf ($binding_matches -and $os_version -lt [version]6.2)
- {
- Fail-Json -obj $result -message "A conflicting binding has been found on the same ip $ip and port $port. To continue you will need to remove the existing binding or assign a new IP or Port to this one"
- }
- }
- }
-
# add binding. this creates the binding, but does not apply a certificate to it.
Try
{
@@ -437,7 +352,8 @@ ElseIf (-not $current_bindings -and $state -eq 'present')
If ($certificateHash -and -not $check_mode)
{
Try {
- $new_binding = get-webbinding -Name $name -IPAddress $ip -port $port -Protocol $protocol -hostheader $host_header
+ #$new_binding = get-webbinding -Name $name -IPAddress $ip -port $port -Protocol $protocol -hostheader $host_header
+ $new_binding = Get-SingleWebBinding $binding_parameters
$new_binding.addsslcertificate($certificateHash,$certificateStoreName)
}
Catch {
diff --git a/lib/ansible/modules/windows/win_iis_webbinding.py b/lib/ansible/modules/windows/win_iis_webbinding.py
index 1dd173bc26..5e74be940d 100644
--- a/lib/ansible/modules/windows/win_iis_webbinding.py
+++ b/lib/ansible/modules/windows/win_iis_webbinding.py
@@ -42,8 +42,7 @@ options:
host_header:
description:
- The host header to bind to / use for the new site.
- - For state absent, you can use c('*') here to remove all bindings for a particular
- protocol/ip/port combination.
+ - If you are creating/removing a catch-all binding, omit this parameter rather than defining it as '*'.
protocol:
description:
- The protocol to be used for the Web binding (usually HTTP, HTTPS, or FTP).
@@ -80,11 +79,19 @@ EXAMPLES = r'''
port: 9090
state: absent
+- name: Remove the default http binding
+ win_iis_webbinding:
+ name: Default Web Site
+ port: 80
+ ip: '*'
+ state: absent
+
- name: Add a HTTPS binding
win_iis_webbinding:
name: Default Web Site
protocol: https
port: 443
+ ip: 127.0.0.1
certificate_hash: B0D0FA8408FC67B230338FCA584D03792DA73F4C
state: present
@@ -97,31 +104,9 @@ EXAMPLES = r'''
ssl_flags: 1
certificate_hash: D1A3AF8988FD32D1A3AF8988FD323792DA73F4C
state: present
-
-- name: Remove all https bindings on port 443
- win_iis_webbinding:
- name: Default Web Site
- protocol: https
- port: 443
- host_header: '*'
- state: absent
'''
RETURN = r'''
-cert_is_wildcard:
- description:
- - Tells you if the certificate you are using is a wildcard
- returned: when certificate_hash is defined
- type: boolean
- sample: false
- version_added: "2.5"
-certificate_subjects:
- description:
- - All of the subject names for the certificate you are using
- returned: when certificate_hash is defined
- type: list
- sample: ["*.test.com","test.com"]
- version_added: "2.5"
website_state:
description:
- The state of the website being targetted