Skip to content

fix: fix the copy terraform script to maintain the files permissions while extracting the ZIP files - #4640

Merged
moelasmar merged 14 commits into
aws:developfrom
moelasmar:fix-unzip-terraform
Mar 30, 2023
Merged

fix: fix the copy terraform script to maintain the files permissions while extracting the ZIP files#4640
moelasmar merged 14 commits into
aws:developfrom
moelasmar:fix-unzip-terraform

Conversation

@moelasmar

@moelasmar moelasmar commented Jan 31, 2023

Copy link
Copy Markdown
Contributor

Which issue(s) does this change fix?

#4605

Why is this change necessary?

The current copy terraform built artifacts script loses all the file permissions while extracting ZIP files, and this cause some issues to the customers while invoking the lambda functions locally.

How does it address the issue?

The current script is using the standard ZIP library to unzip which has this bug and switch to use our ZIP module.
I had to split our ZIP module into two modules as it contains some functions to read a file from a URL, and require some libraries that does not exist in the standard library, and so it could not be used with the copy terraform built artifacts script.

I tested generating the PyInstaller package, and tested that I can execute the sam commands on a terraform project.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Add input/output type hints to new functions/methods
  • Write design document if needed (Do I need to write a design document?)
  • Write/update unit tests
  • Write/update integration tests
  • Write/update functional tests if needed
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@moelasmar
moelasmar requested a review from a team as a code owner January 31, 2023 19:28
Comment thread .pylintrc Outdated
Comment thread samcli/hook_packages/terraform/hooks/prepare/makefile_generator.py Outdated
Comment on lines +54 to +59
try:
# this case will work only while executing the copy terraform command from the makefile
from zip import unzip # type: ignore
except ImportError:
# this way to fix the hidden import unit testing
from samcli.local.lambdafn.zip import unzip

@torresxb1 torresxb1 Feb 1, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we use something different for unit tests than for the actual execution? Doesn't this remove the confidence we can have in our unit tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is the script will run in a different way than we do in our unit testing (the hidden import test case) .. let me check with Wing how can we remove it from this test case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which unit test does it break?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my instinct is we should fix the test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped the samcli.hook_packages.terraform.copy_terraform_built_artifacts from the test_import_should_succeed_for_a_defined_hidden_package test case, as this script will be only executed as a standalone script and does not depend on any non standard modules

Comment thread samcli/local/lambdafn/remote_files.py

@torresxb1 torresxb1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left comments above, but will change to approve so I don't block


# Forcefully set the permissions to 700 on files and directories. This is to ensure the owner
# of the files is the only one that can read, write, or execute the files.
unzip(layer_zip_path, unzip_output_dir, permission=0o700)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to be very careful with directly setting permissions anywhere, whatever we do should be additive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unzipped files will be used only for local testing, and will not be packaged or deployed using sam cli, so I think there is no risk

…raform

# Conflicts:
#	samcli/local/lambdafn/zip.py
#	samcli/local/layers/layer_downloader.py
@moelasmar
moelasmar requested a review from sriram-mv March 30, 2023 09:53
samcli_root_path = Path(os.path.dirname(__file__)).parent.parent.parent.parent

# copy zip.py script into output directory
ZIP_UTILS_MODULE_script_path = os.path.join(samcli_root_path, "local", "lambdafn", ZIP_UTILS_MODULE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use all lowercase for variable name - https://peps.python.org/pep-0008/#function-and-variable-names

@moelasmar
moelasmar enabled auto-merge March 30, 2023 17:24
@moelasmar
moelasmar dismissed sriram-mv’s stale review March 30, 2023 17:24

Sriram is on vacation.

@moelasmar
moelasmar added this pull request to the merge queue Mar 30, 2023
Merged via the queue into aws:develop with commit c0e9678 Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants