fix: Ensure zip excluded files are not checked (#40120)

* fix: Ensure zip excluded files are not checked

Fixes #26279

* test: Verify unarchive excludes behaves as expected

* fix: Typos and whitespaces
pull/39041/merge
Sijis Aviles 6 years ago committed by Adam Miller
parent 8414230266
commit 529ef6446e

@ -132,6 +132,7 @@ EXAMPLES = r'''
import binascii import binascii
import codecs import codecs
import datetime import datetime
import fnmatch
import grp import grp
import os import os
import platform import platform
@ -263,7 +264,11 @@ class ZipArchive(object):
else: else:
try: try:
for member in archive.namelist(): for member in archive.namelist():
if member not in self.excludes: if self.excludes:
for exclude in self.excludes:
if not fnmatch.fnmatch(member, exclude):
self._files_in_archive.append(to_native(member))
else:
self._files_in_archive.append(to_native(member)) self._files_in_archive.append(to_native(member))
except: except:
archive.close() archive.close()

@ -17,7 +17,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make sure we start fresh # Make sure we start fresh
# Need unzup for unarchive module, and zip for archive creation. # Need unzip for unarchive module, and zip for archive creation.
- name: Ensure zip and unzip is present to create test archive (yum) - name: Ensure zip and unzip is present to create test archive (yum)
yum: name=zip,unzip state=latest yum: name=zip,unzip state=latest
when: ansible_pkg_mgr == 'yum' when: ansible_pkg_mgr == 'yum'
@ -26,11 +26,11 @@
# dnf: name=zip state=latest # dnf: name=zip state=latest
# when: ansible_pkg_mgr == 'dnf' # when: ansible_pkg_mgr == 'dnf'
- name: Ensure zip & unzup is present to create test archive (apt) - name: Ensure zip & unzip is present to create test archive (apt)
apt: name=zip,unzip state=latest apt: name=zip,unzip state=latest
when: ansible_pkg_mgr == 'apt' when: ansible_pkg_mgr == 'apt'
- name: Ensure zip & unzup is present to create test archive (pkg) - name: Ensure zip & unzip is present to create test archive (pkg)
pkgng: name=zip,unzip state=present pkgng: name=zip,unzip state=present
when: ansible_pkg_mgr == 'pkgng' when: ansible_pkg_mgr == 'pkgng'
@ -51,18 +51,18 @@
# This gets around an unzip timestamp bug in some distributions # This gets around an unzip timestamp bug in some distributions
# Recent unzip on Ubuntu and BSD will randomly round some timestamps up. # Recent unzip on Ubuntu and BSD will randomly round some timestamps up.
# But that doesn't seem to happen when the timestamp has an even second. # But that doesn't seem to happen when the timestamp has an even second.
- name: Bug work around - name: Bug work around
command: touch -t "201705111530.00" {{output_dir}}/foo-unarchive.txt {{output_dir}}/foo-unarchive-777.txt {{output_dir}}/FOO-UNAR.TXT command: touch -t "201705111530.00" {{output_dir}}/foo-unarchive.txt {{output_dir}}/foo-unarchive-777.txt {{output_dir}}/FOO-UNAR.TXT
# See Ubuntu bug 1691636: https://bugs.launchpad.net/ubuntu/+source/unzip/+bug/1691636 # See Ubuntu bug 1691636: https://bugs.launchpad.net/ubuntu/+source/unzip/+bug/1691636
# When these are fixed, this code should be removed. # When these are fixed, this code should be removed.
- name: prep a zip file - name: prep a zip file
shell: zip test-unarchive.zip foo-unarchive.txt foo-unarchive-777.txt chdir={{output_dir}} shell: zip test-unarchive.zip foo-unarchive.txt foo-unarchive-777.txt chdir={{output_dir}}
- name: add a file with Windows permissions to zip file - name: add a file with Windows permissions to zip file
shell: zip -k test-unarchive.zip FOO-UNAR.TXT chdir={{output_dir}} shell: zip -k test-unarchive.zip FOO-UNAR.TXT chdir={{output_dir}}
- name: prep a subdirectory - name: prep a subdirectory
file: path={{output_dir}}/unarchive-dir state=directory file: path={{output_dir}}/unarchive-dir state=directory
@ -178,12 +178,32 @@
- name: repeat the last request to verify no changes - name: repeat the last request to verify no changes
unarchive: src={{output_dir}}/test-unarchive.zip dest={{output_dir | expanduser}}/test-unarchive-zip remote_src=yes list_files=True unarchive: src={{output_dir}}/test-unarchive.zip dest={{output_dir | expanduser}}/test-unarchive-zip remote_src=yes list_files=True
register: unarchive03b register: unarchive03b
- name: verify that the task was not marked as changed - name: verify that the task was not marked as changed
assert: assert:
that: that:
- "unarchive03b.changed == false" - "unarchive03b.changed == false"
- name: "Create {{ output_dir }}/exclude directory"
file:
state: directory
path: "{{ output_dir }}/exclude"
- name: Unpack zip file excluding one file.
unarchive:
src: "{{ output_dir }}/test-unarchive.zip"
dest: "{{ output_dir }}/exclude"
exclude: "foo-unarchive-*.txt"
- name: verify that the file was unarchived
shell: find {{ output_dir }}/exclude chdir={{ output_dir }}
register: unarchive_dir01
- name: verify that zip extraction excluded file
assert:
that:
- "'foo-unarchive-777.txt' not in unarchive_dir01.stdout"
- name: remove our zip unarchive destination - name: remove our zip unarchive destination
file: path={{output_dir}}/test-unarchive-zip state=absent file: path={{output_dir}}/test-unarchive-zip state=absent
@ -204,7 +224,7 @@
when: unarchive04.stat.exists when: unarchive04.stat.exists
- name: try unarchiving to /tmp - name: try unarchiving to /tmp
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest=/tmp remote_src=yes unarchive: src={{output_dir}}/test-unarchive.tar.gz dest=/tmp remote_src=yes
register: unarchive05 register: unarchive05
- name: verify that the file was marked as changed - name: verify that the file was marked as changed
@ -251,7 +271,6 @@
- name: create our unarchive destination - name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive over existing extraction and set mode to 0644 - name: unarchive over existing extraction and set mode to 0644
unarchive: unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz" src: "{{ output_dir }}/test-unarchive.tar.gz"
@ -293,7 +312,6 @@
- name: remove our tar.gz unarchive destination - name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
- name: create our unarchive destination - name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-zip state=directory file: path={{output_dir}}/test-unarchive-zip state=directory
@ -358,11 +376,9 @@
- name: remove our zip unarchive destination - name: remove our zip unarchive destination
file: path={{ output_dir }}/test-unarchive-zip state=absent file: path={{ output_dir }}/test-unarchive-zip state=absent
- name: create our unarchive destination - name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: create a directory with quotable chars - name: create a directory with quotable chars
file: path="{{ output_dir }}/test-quotes~root" state=directory file: path="{{ output_dir }}/test-quotes~root" state=directory
@ -503,7 +519,6 @@
- name: remove our tar.gz unarchive destination - name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
- name: Create a file - name: Create a file
file: file:
path: "{{ output_dir }}/test-unarchive-tar-gz" path: "{{ output_dir }}/test-unarchive-tar-gz"

Loading…
Cancel
Save