ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
199 stars 153 forks source link

win_zip module to compress file to be compatible with Linux #442

Closed demolot closed 1 year ago

demolot commented 1 year ago
SUMMARY

The overall issue is that when we use win_zip on a Windows and then try to unzip on a Linux asset it returns warning:

        "err": "warning:  /home/ansible/.ansible/tmp/ansible-tmp-1663662914.2948062-10882-169245596920085/source appears to use backslashes as path separators\n",
        "out": "Archive:  /home/ansible/.ansible/tmp/ansible-tmp-1663662914.2948062-10882-169245596920085/source\n  inflating: /home/ansible/test/report/asset_info.json  \n  inflating: /home/ansible/test/report/sc_symantec_av_installed.json  \n  inflating: /home/ansible/test/report/sc_wsus_enabled.json  \n",
        "rc": 1

From my testings the file gets unzipped, however, I can't say that with 100% of confidence. The real root cause is in the creation of that zip file in the first place - that one should not use backslashes as a separator.

Looking at the community win_zip implementation at https://github.com/ansible-collections/community.windows/blob/107df28cbfad19ea06d794c3b877a4d829fccca7/plugins/modules/win_zip.ps1#L56 it is using [System.IO.Compression.ZipFile]::CreateFromDirectory, see relevant discussion about that method and baskslash separators at https://stackoverflow.com/questions/27289115/system-io-compression-zipfile-net-4-5-output-zip-in-not-suitable-for-linux-mac

Same issue could be found under https://github.com/ansible/ansible/issues/30976 however suggestion is to "hide" the issue instead of solving it

ISSUE TYPE
COMPONENT NAME
community.windows.win_zip
ANSIBLE VERSION
ansible [core 2.12.3]
  config file = /home/ansible/automation/ansible.cfg
  configured module search path = ['/home/ansible/automation/library']
  ansible python module location = /usr/local/share/venv_projects/lsf_automation/lib64/python3.8/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/share/venv_projects/lsf_automation/bin/ansible
  python version = 3.8.11 (default, Sep  1 2021, 12:33:46) [GCC 9.3.1 20200408 (Red Hat 9.3.1-2)]
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
community.windows             1.9.0 
OS / ENVIRONMENT

Windows / Linux

STEPS TO REPRODUCE
  1. Use Win_Zip module to compress a directory
  2. Transfer this file over to a linux distro
  3. Try to unzip file with unarchive module or other unzip module
  4. Gets warning as described on a summary
EXPECTED RESULTS

No warnings on unzip function on Linux

ACTUAL RESULTS
    "extract_results": {
        "cmd": [
            "/usr/bin/unzip",
            "-o",
            "/home/ansible/.ansible/tmp/ansible-tmp-1663662914.2948062-10882-169245596920085/source",
            "-d",
            "/home/ansible/test"
        ],
        "err": "warning:  /home/ansible/.ansible/tmp/ansible-tmp-1663662914.2948062-10882-169245596920085/source appears to use backslashes as path separators\n",
        "out": "Archive:  /home/ansible/.ansible/tmp/ansible-tmp-1663662914.2948062-10882-169245596920085/source\n  inflating: /home/ansible/test/report/asset_info.json  \n  inflating: /home/ansible/test/report/sc_symantec_av_installed.json  \n  inflating: /home/ansible/test/report/sc_wsus_enabled.json  \n",
        "rc": 1
    }
hiyokotaisa commented 1 year ago

Thank you for pointing out the issue for win_zip module!

I believe this behavior is coming from .NET Framework. However, this behavior has been changed from .NET Framework 4.6.1. I will test with that version to see there is any difference.

https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/mitigation-ziparchiveentry-fullname-path-separator?redirectedfrom=MSDN

To workaround this, might be I can add some function to replace backslash to forward slash forcibly.