Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[win_get_url] feature: Add support checksum to module win_get_url #51986

Merged
merged 49 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
6437f2c
set valid_until equal to current time + spot_wait_timeout
vutkin Jan 30, 2019
8472e77
Add checksum check for downloaded file.
Feb 4, 2019
a53b587
refactoring
vutkin Feb 5, 2019
c98e3b8
fix typo
vutkin Feb 5, 2019
0625957
add fixes
vutkin Feb 5, 2019
6fc18c9
mart try,catch handling
vutkin Feb 5, 2019
83f43ac
revert lib/ansible/modules/cloud/amazon/ec2.py from upstream
vutkin Feb 5, 2019
6c4eff5
refactoring
vutkin Feb 5, 2019
5d17ec5
remove empty lines
vutkin Feb 5, 2019
159944e
add checksum verification for existing file
vutkin Feb 6, 2019
8e84b71
fix current file check
vutkin Feb 6, 2019
bf14b56
refactoring destination file check
vutkin Feb 6, 2019
fff695b
add handling exceptions
vutkin Feb 6, 2019
77d1ffe
refactoring
vutkin Feb 6, 2019
c553451
Added download file hash data from url
Feb 6, 2019
179739a
fix string aligning
vutkin Feb 7, 2019
d29e351
fix bug with uri
vutkin Feb 7, 2019
d004644
Added get hash from multy-string file
Feb 7, 2019
dd2129a
Added URI support for checksum file location
Feb 7, 2019
d6d0d38
refactoing
vutkin Feb 7, 2019
3ec015c
Remove any non-alphanumeric characters for hash from url
vutkin Feb 7, 2019
a549592
fix discussions; add support for PS3
vutkin Feb 9, 2019
6151903
refactoring
vutkin Feb 9, 2019
c9670ea
add size return value
vutkin Feb 9, 2019
7836fef
checkout from upstream for lib/ansible/modules/cloud/amazon/ec2.py
vutkin Feb 9, 2019
4fcecae
add Ansible.ModuleUtils.Legacy support; refactoring
vutkin Feb 11, 2019
19a130a
Copyright added
Feb 11, 2019
60457fb
Checking files size before and after downloading added.
Feb 11, 2019
78d20ff
remove unused code
vutkin Feb 11, 2019
b191705
Corrected regexp for dotted slashed file name prefix in hash-file
Feb 12, 2019
589963f
hotfix typo error; add int tests
vutkin Feb 13, 2019
6390970
remove legacy module support; split checksum to checksum, checksum_al…
vutkin Feb 13, 2019
2549b92
changed default hash algorithm
vutkin Feb 13, 2019
d84ccdc
Fixed case for ContentLength = -1
Feb 13, 2019
c2453b5
Old comment removed
Feb 13, 2019
9c3b11e
fix typo
vutkin Feb 13, 2019
3bbc1df
Remove file size check before downloading
Feb 13, 2019
8127d8b
add alias to ; fix tests
vutkin Feb 13, 2019
9266edc
adjust tests; fix lint warnings from PSScritpAnalyzer
vutkin Feb 14, 2019
eb47345
workaround for bug in win_chocolatey module on win2008
vutkin Feb 14, 2019
14c0526
remove win_get_url.ps1 from /test/sanity/pslint/ignore.txt
vutkin Feb 14, 2019
2972f8b
add checksum_algorithm as retuen value
vutkin Feb 14, 2019
837d88f
first normalise before return Result
vutkin Feb 15, 2019
16cc556
resolve discussions
vutkin Feb 20, 2019
aee2059
fix discussions
vutkin Feb 25, 2019
13ffd93
fix last discussions
vutkin Feb 28, 2019
ff619f7
Reduce code duplication and add idempotency check
jborean93 Mar 4, 2019
064903c
fix sanity issue and remove testing code
jborean93 Mar 4, 2019
0f1943d
move back to using tmp file for checksum comparison
jborean93 Mar 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/win_get_url-checksum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- win_get_url - Add the ``checksum`` option to verify the integrity of a downloaded file.
- win_get_url - Add idempotency check if the remote file has the same contents as the dest file.
383 changes: 275 additions & 108 deletions lib/ansible/modules/windows/win_get_url.ps1

Large diffs are not rendered by default.

66 changes: 65 additions & 1 deletion lib/ansible/modules/windows/win_get_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
required: yes
force:
description:
- If C(yes), will always download the file. If C(no), will only
- If C(yes), will download the file every time and replace the file if the contents change. If C(no), will only
download the file if it does not exist or the remote file has been
modified more recently than the local file.
- This works by sending an http HEAD request to retrieve last modified
Expand Down Expand Up @@ -73,6 +73,36 @@
type: bool
default: yes
version_added: '2.4'
checksum:
vutkin marked this conversation as resolved.
Show resolved Hide resolved
description:
- If a I(checksum) is passed to this parameter, the digest of the
destination file will be calculated after it is downloaded to ensure
its integrity and verify that the transfer completed successfully.
- This option cannot be set with I(checksum_url).
type: str
version_added: "2.8"
checksum_algorithm:
description:
- Specifies the hashing algorithm used when calculating the checksum of
the remote and destination file.
type: str
choices:
- md5
- sha1
- sha256
- sha385
- sha512
default: sha1
version_added: "2.8"
checksum_url:
description:
- Specifies a URL that contains the checksum values for the resource at
I(url).
- Like C(checksum), this is used to verify the integrity of the remote
transfer.
- This option cannot be set with I(checksum).
type: str
version_added: "2.8"
proxy_url:
description:
- The full URL of the proxy server to download through.
Expand Down Expand Up @@ -105,6 +135,9 @@
- If your URL includes an escaped slash character (%2F) this module will convert it to a real slash.
This is a result of the behaviour of the System.Uri class as described in
L(the documentation,https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/schemesettings-element-uri-settings#remarks).
- Since Ansible 2.8, the module will skip reporting a change if the remote
checksum is the same as the local local even when C(force=yes). This is to
better align with M(get_url).
seealso:
- module: get_url
- module: uri
Expand Down Expand Up @@ -140,6 +173,22 @@
dest: '%TEMP%\ftp-file.txt'
url_username: ftp-user
url_password: ftp-password

- name: Download src with sha256 checksum url
win_get_url:
url: http://www.example.com/earthrise.jpg
dest: C:\temp\earthrise.jpg
checksum_url: http://www.example.com/sha256sum.txt
checksum_algorithm: sha256
force: True

- name: Download src with sha256 checksum url
win_get_url:
url: http://www.example.com/earthrise.jpg
dest: C:\temp\earthrise.jpg
checksum: a97e6837f60cec6da4491bab387296bbcd72bdba
checksum_algorithm: sha1
force: True
'''

RETURN = r'''
Expand All @@ -148,11 +197,26 @@
returned: always
type: str
sample: C:\Users\RandomUser\earthrise.jpg
checksum_dest:
description: <algorithm> checksum of the file after the download
returned: success and dest has been downloaded
type: str
sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
checksum_src:
description: <algorithm> checksum of the remote resource
returned: force=yes or dest did not exist
type: str
sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
elapsed:
description: The elapsed seconds between the start of poll and the end of the module.
returned: always
type: float
sample: 2.1406487
size:
description: size of the dest file
returned: success
type: int
sample: 1220
url:
description: requested url
returned: always
Expand Down
3 changes: 3 additions & 0 deletions test/integration/targets/win_get_url/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
test_win_get_url_path: '{{win_output_dir}}\win_get_url'
test_win_get_url_host: www.redhat.com
test_win_get_url_env_var: WIN_GET_URL

remote_tmp_path: '{{win_output_dir}}\win_get_url_dest'
remote_http_path: '{{test_win_get_url_path}}\http'
16 changes: 13 additions & 3 deletions test/integration/targets/win_get_url/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
- name: ensure testing folder is present
win_file:
path: '{{test_win_get_url_path}}'
path: '{{ item }}'
state: directory
loop:
- '{{ test_win_get_url_path }}'
- '{{ remote_http_path }}'
- '{{ remote_tmp_path }}'

- name: copy across testing files
win_copy:
Expand Down Expand Up @@ -41,6 +45,9 @@
- name: run FTP tests
include_tasks: tests_ftp.yml

- name: run checksum tests
include_tasks: tests_checksum.yml

always:
- name: remove SlimFTPd service
win_service:
Expand All @@ -53,7 +60,10 @@
level: machine
state: absent

- name: remove testing folder
- name: remove testing folders
win_file:
path: '{{test_win_get_url_path}}'
path: '{{ item }}'
state: absent
loop:
- '{{ test_win_get_url_path }}'
- '{{ remote_tmp_path }}'
202 changes: 202 additions & 0 deletions test/integration/targets/win_get_url/tasks/tests_checksum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
---
- name: create src file
win_copy:
dest: '{{ remote_http_path }}\27617.txt'
content: 'ptux'

- name: create sha1 checksum file of src
win_copy:
dest: '{{ remote_http_path }}\sha1sum.txt'
content: 'a97e6837f60cec6da4491bab387296bbcd72bdba 27617.txt'

- name: add sha1 checksum not going to be downloaded
win_lineinfile:
dest: '{{ remote_http_path }}\sha1sum.txt'
line: '{{ item }}'
loop:
- '3911340502960ca33aece01129234460bfeb2791 not_target1.txt'
- '1b4b6adf30992cedb0f6edefd6478ff0a593b2e4 not_target2.txt'

- name: create sha256 checksum file of src
win_copy:
dest: '{{ remote_http_path }}\sha256sum.txt'
content: 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. 27617.txt'

- name: add sha256 checksum not going to be downloaded
win_lineinfile:
dest: '{{ remote_http_path }}\sha256sum.txt'
line: '{{ item }}'
loop:
- '30949cc401e30ac494d695ab8764a9f76aae17c5d73c67f65e9b558f47eff892 not_target1.txt'
- 'd0dbfc1945bc83bf6606b770e442035f2c4e15c886ee0c22fb3901ba19900b5b not_target2.txt'

- name: create sha256 checksum file of src with a dot leading path
win_copy:
dest: '{{ remote_http_path }}\sha256sum_with_dot.txt'
content: 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006. ./27617.txt'

- name: add sha256 checksums with dot leading path not going to be downloaded
win_lineinfile:
dest: '{{ remote_http_path }}\sha256sum_with_dot.txt'
line: '{{ item }}'
loop:
- '30949cc401e30ac494d695ab8764a9f76aae17c5d73c67f65e9b558f47eff892 ./not_target1.txt'
- 'd0dbfc1945bc83bf6606b770e442035f2c4e15c886ee0c22fb3901ba19900b5b ./not_target2.txt'

- name: copy files to ftp
win_copy:
src: '{{ remote_http_path }}\'
dest: '{{ test_win_get_url_path }}\ftp\{{ item }}\'
remote_src: yes
loop: '{{ dest_folders }}'
vars:
dest_folders:
- anon
- user
- user-pass

- name: download src with sha1 checksum url
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}'
checksum_url: 'ftp://localhost/anon/sha1sum.txt'
force: True
register: result_sha1

- name: download src with sha1 checksum value
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}'
checksum: 'a97e6837f60cec6da4491bab387296bbcd72bdba'
force: True
register: result_sha1_alt

- win_stat:
path: '{{ remote_tmp_path }}\27617.txt'
register: stat_result_sha1

- name: download src with sha256 checksum url
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}\27617sha256.txt'
checksum_url: 'ftp://localhost/anon/sha256sum.txt'
checksum_algorithm: sha256
force: True
register: result_sha256

- win_stat:
path: '{{ remote_tmp_path }}\27617.txt'
register: stat_result_sha256

- name: download src with sha256 checksum url with dot leading paths
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}\27617sha256_with_dot.txt'
checksum_url: 'ftp://localhost/anon/sha256sum_with_dot.txt'
checksum_algorithm: sha256
force: True
register: result_sha256_with_dot

- win_stat:
path: '{{ remote_tmp_path }}\27617sha256_with_dot.txt'
register: stat_result_sha256_with_dot

- name: Assert that the file was downloaded
assert:
that:
- result_sha1 is changed
- result_sha1.checksum_dest == 'a97e6837f60cec6da4491bab387296bbcd72bdba'
- result_sha1_alt is succeeded
- not result_sha1_alt is changed
- result_sha1_alt.checksum_dest == 'a97e6837f60cec6da4491bab387296bbcd72bdba'
- result_sha256 is changed
- result_sha256_with_dot is changed
- stat_result_sha1.stat.exists | bool
- stat_result_sha256.stat.exists | bool
- stat_result_sha256_with_dot.stat.exists | bool

# Check download with force: False

- name: download src with sha1 checksum url
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}'
checksum_url: 'ftp://localhost/anon/sha1sum.txt'
checksum_algorithm: sha1
force: False
register: result_sha1_no_force

- name: download src with sha256 checksum url
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}/27617sha256.txt'
checksum_url: 'ftp://localhost/anon/sha256sum.txt'
checksum_algorithm: sha256
force: False
register: result_sha256_no_force

- name: assert download single file with force no
assert:
that:
- result_sha1_no_force is not changed
- result_sha256_no_force is not changed

# Check download with check_mode: True

- name: download src with sha1 checksum url | checkmode
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}'
checksum_url: 'ftp://localhost/anon/sha1sum.txt'
checksum_algorithm: sha1
check_mode: True
register: result_sha1_check_mode

- name: download src with sha256 checksum url | checkmode
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}\27617sha256.txt'
checksum_url: 'ftp://localhost/anon/sha256sum.txt'
checksum_algorithm: sha256
check_mode: True
register: result_sha256_check_mode

- name: assert download single file with force no
assert:
that:
- result_sha1_check_mode.checksum_dest == 'a97e6837f60cec6da4491bab387296bbcd72bdba'
- result_sha256_check_mode.checksum_dest == 'b1b6ce5073c8fac263a8fc5edfffdbd5dec1980c784e09c5bc69f8fb6056f006'
- result_sha1_check_mode.checksum_src | length
- result_sha256_check_mode.checksum_src | length

# Check download with wrong checksum

- name: download src with sha1 checksum value | fail
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}'
checksum: 'redacted'
checksum_algorithm: sha1
failed_when:
- '"did not match" not in result_sha1_failed.msg'
- '"The checksum" not in result_sha1_failed.msg'
- '"Unknown error downloading" not in result_sha1_failed.msg'
register: result_sha1_failed

- name: download src with sha256 checksum value | fail
win_get_url:
url: 'ftp://localhost/anon/27617.txt'
dest: '{{ remote_tmp_path }}\27617sha256.txt'
checksum: 'redacted'
checksum_algorithm: sha256
failed_when:
- '"did not match" not in result_sha256_failed.msg'
- '"The checksum" not in result_sha256_failed.msg'
- '"Unknown error downloading" not in result_sha256_failed.msg'
register: result_sha256_failed

- name: assert failed downloads
assert:
that:
- result_sha1_failed is succeeded
- result_sha256_failed is succeeded
17 changes: 14 additions & 3 deletions test/integration/targets/win_get_url/tasks/tests_url.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
- http_download.dest
- http_download_result.stat.exists

# TODO: add check for idempotent run once it is added with force: yes
- name: download single file (idempotent)
win_get_url:
url: https://{{test_win_get_url_host}}
dest: '{{test_win_get_url_path}}\web.html'
register: http_download_again

- name: assert download single file (idempotent)
assert:
that:
- not http_download_again is changed

- name: download single file with force no
win_get_url:
Expand All @@ -53,8 +62,10 @@
that:
- http_download_no_force is not changed

- name: manually change last modified time on FTP source to older datetime
win_shell: (Get-Item -Path '{{test_win_get_url_path}}\web.html').LastWriteTime = (Get-Date -Date "01/01/1970")
- name: manually change the content and last modified time on FTP source to older datetime
win_shell: |
Set-Content -Path '{{test_win_get_url_path}}\web.html' -Value 'abc'
(Get-Item -Path '{{test_win_get_url_path}}\web.html').LastWriteTime = (Get-Date -Date "01/01/1970")

- name: download newer file with force no
win_get_url:
Expand Down
2 changes: 1 addition & 1 deletion test/sanity/pslint/ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ lib/ansible/modules/windows/win_find.ps1 PSAvoidUsingEmptyCatchBlock
lib/ansible/modules/windows/win_find.ps1 PSAvoidUsingWMICmdlet
lib/ansible/modules/windows/win_firewall_rule.ps1 PSAvoidUsingCmdletAliases
lib/ansible/modules/windows/win_firewall_rule.ps1 PSUseApprovedVerbs
lib/ansible/modules/windows/win_get_url.ps1 PSUseApprovedVerbs
lib/ansible/modules/windows/win_get_url.ps1 PSUsePSCredentialType # Credential param can take a base64 encoded string as well as a PSCredential
lib/ansible/modules/windows/win_hotfix.ps1 PSUseApprovedVerbs
lib/ansible/modules/windows/win_iis_webbinding.ps1 PSUseApprovedVerbs
lib/ansible/modules/windows/win_iis_website.ps1 PSAvoidUsingCmdletAliases
Expand Down