From 330112a45cc0d4b3403d7dbbbf86f981c73d68f0 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Thu, 7 May 2020 12:17:06 +1000 Subject: [PATCH] Fix win_psmodule and win_psrepository tests --- changelogs/fragments/win_psmodule-repo-tls.yaml | 4 ++++ lib/ansible/modules/windows/win_psmodule.ps1 | 16 ++++++++++++++-- lib/ansible/modules/windows/win_psrepository.ps1 | 12 +++++++++++- .../targets/win_psrepository/tasks/main.yml | 4 ++-- .../targets/win_psrepository/tasks/tests.yml | 4 ++-- 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/win_psmodule-repo-tls.yaml diff --git a/changelogs/fragments/win_psmodule-repo-tls.yaml b/changelogs/fragments/win_psmodule-repo-tls.yaml new file mode 100644 index 00000000000..96c2c0f0fa8 --- /dev/null +++ b/changelogs/fragments/win_psmodule-repo-tls.yaml @@ -0,0 +1,4 @@ +bugfixes: +- win_psmodule - Fix TLS 1.2 compatibility with PSGallery. +- win_psrepository - Fix TLS 1.2 compatibility with PSGallery. +- win_psrepository - Fix ``Ignore`` error when trying to retrieve the list of registered repositories diff --git a/lib/ansible/modules/windows/win_psmodule.ps1 b/lib/ansible/modules/windows/win_psmodule.ps1 index b24178e49ff..529887bcaf8 100644 --- a/lib/ansible/modules/windows/win_psmodule.ps1 +++ b/lib/ansible/modules/windows/win_psmodule.ps1 @@ -27,6 +27,18 @@ $result = @{changed = $false nuget_changed = $false repository_changed = $false} + +# Enable TLS1.1/TLS1.2 if they're available but disabled (eg. .NET 4.5) +$security_protocols = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::SystemDefault +if ([System.Net.SecurityProtocolType].GetMember("Tls11").Count -gt 0) { + $security_protocols = $security_protocols -bor [System.Net.SecurityProtocolType]::Tls11 +} +if ([System.Net.SecurityProtocolType].GetMember("Tls12").Count -gt 0) { + $security_protocols = $security_protocols -bor [System.Net.SecurityProtocolType]::Tls12 +} +[System.Net.ServicePointManager]::SecurityProtocol = $security_protocols + + Function Install-NugetProvider { Param( [Bool]$CheckMode @@ -162,7 +174,7 @@ Function Add-DefinedParameter { ) ForEach ($ParameterName in $ParametersNames) { - $ParameterVariable = Get-Variable -Name $ParameterName -ErrorAction Ignore + $ParameterVariable = Get-Variable -Name $ParameterName -ErrorAction SilentlyContinue if ( $ParameterVariable.Value -and $Hashtable.Keys -notcontains $ParameterName ){ $Hashtable.Add($ParameterName,$ParameterVariable.Value) } @@ -381,7 +393,7 @@ if ( ($state -eq "latest") -and } if ( $repo -and (-not $url) ) { - $RepositoryExists = Get-PSRepository -Name $repo -ErrorAction Ignore + $RepositoryExists = Get-PSRepository -Name $repo -ErrorAction SilentlyContinue if ( $null -eq $RepositoryExists) { $ErrorMessage = "The repository $repo doesn't exist." Fail-Json $result $ErrorMessage diff --git a/lib/ansible/modules/windows/win_psrepository.ps1 b/lib/ansible/modules/windows/win_psrepository.ps1 index da637f02ebe..99607e9f471 100644 --- a/lib/ansible/modules/windows/win_psrepository.ps1 +++ b/lib/ansible/modules/windows/win_psrepository.ps1 @@ -18,6 +18,16 @@ $installationpolicy = Get-AnsibleParam -obj $params -name "installation_policy" $result = @{"changed" = $false} +# Enable TLS1.1/TLS1.2 if they're available but disabled (eg. .NET 4.5) +$security_protocols = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::SystemDefault +if ([System.Net.SecurityProtocolType].GetMember("Tls11").Count -gt 0) { + $security_protocols = $security_protocols -bor [System.Net.SecurityProtocolType]::Tls11 +} +if ([System.Net.SecurityProtocolType].GetMember("Tls12").Count -gt 0) { + $security_protocols = $security_protocols -bor [System.Net.SecurityProtocolType]::Tls12 +} +[System.Net.ServicePointManager]::SecurityProtocol = $security_protocols + Function Update-NuGetPackageProvider { $PackageProvider = Get-PackageProvider -ListAvailable | Where-Object { ($_.name -eq 'Nuget') -and ($_.version -ge "2.8.5.201") } if ($null -eq $PackageProvider) { @@ -25,7 +35,7 @@ Function Update-NuGetPackageProvider { } } -$Repo = Get-PSRepository -Name $name -ErrorAction Ignore +$Repo = Get-PSRepository -Name $name -ErrorAction SilentlyContinue if ($state -eq "present") { if ($null -eq $Repo) { if ($null -eq $installationpolicy) { diff --git a/test/integration/targets/win_psrepository/tasks/main.yml b/test/integration/targets/win_psrepository/tasks/main.yml index 65cc278853a..3b7af7117e5 100644 --- a/test/integration/targets/win_psrepository/tasks/main.yml +++ b/test/integration/targets/win_psrepository/tasks/main.yml @@ -5,7 +5,7 @@ --- - name: unregister the repository - win_shell: Unregister-PSRepository {{ repository_name | quote }} -ErrorAction Ignore + win_shell: Unregister-PSRepository {{ repository_name | quote }} -ErrorAction SilentlyContinue - block: - name: run all tests @@ -13,4 +13,4 @@ always: - name: ensure test repo is unregistered - win_shell: Unregister-PSRepository {{ repository_name | quote }} -ErrorAction Ignore + win_shell: Unregister-PSRepository {{ repository_name | quote }} -ErrorAction SilentlyContinue diff --git a/test/integration/targets/win_psrepository/tasks/tests.yml b/test/integration/targets/win_psrepository/tasks/tests.yml index 5dc80fa9af7..b9c97322c01 100644 --- a/test/integration/targets/win_psrepository/tasks/tests.yml +++ b/test/integration/targets/win_psrepository/tasks/tests.yml @@ -161,7 +161,7 @@ register: removing_repository_check - name: get result of remove repository - check mode - win_shell: '(Get-PSRepository -Name {{ repository_name | quote }} -ErrorAction Ignore | Measure-Object).Count' + win_shell: '(Get-PSRepository -Name {{ repository_name | quote }} -ErrorAction SilentlyContinue | Measure-Object).Count' changed_when: false register: result_removing_repository_check @@ -178,7 +178,7 @@ register: removing_repository - name: get result of remove repository - win_shell: '(Get-PSRepository -Name {{ repository_name | quote }} -ErrorAction Ignore | Measure-Object).Count' + win_shell: '(Get-PSRepository -Name {{ repository_name | quote }} -ErrorAction SilentlyContinue | Measure-Object).Count' changed_when: false register: result_removing_repository