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

fix: handle index error if the image info returned from docker client contains any empty list for RepoDigests attribute #5786

Merged
merged 1 commit into from
Aug 17, 2023

Conversation

moelasmar
Copy link
Contributor

Some customers got the following exception, when they execute sam local invoke command

Error: list index out of range
Traceback:
  File "click/core.py", line 1055, in main
  File "click/core.py", line 1657, in invoke
  File "click/core.py", line 1657, in invoke
  File "click/core.py", line 1404, in invoke
  File "click/core.py", line 760, in invoke
  File "click/decorators.py", line 84, in new_func
  File "click/core.py", line 760, in invoke
  File "samcli/lib/telemetry/metric.py", line 184, in wrapped
  File "samcli/lib/telemetry/metric.py", line 149, in wrapped
  File "samcli/lib/utils/version_checker.py", line 42, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/local/invoke/cli.py", line 100, in cli
  File "samcli/commands/local/invoke/cli.py", line 206, in do_cli
  File "samcli/commands/local/lib/local_lambda.py", line 144, in invoke
  File "samcli/lib/telemetry/metric.py", line 324, in wrapped_func
  File "samcli/local/lambdafn/runtime.py", line 189, in invoke
  File "samcli/local/lambdafn/runtime.py", line 85, in create
  File "samcli/local/docker/lambda_container.py", line 94, in __init__
  File "samcli/local/docker/lambda_container.py", line 237, in _get_image
  File "samcli/local/docker/lambda_image.py", line 197, in build
  File "samcli/local/docker/lambda_image.py", line 471, in _check_base_image_is_current
  File "samcli/local/docker/lambda_image.py", line 495, in is_base_image_current
  File "samcli/local/docker/lambda_image.py", line 530, in get_local_image_digest

An unexpected error was encountered while executing "sam local invoke".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20local%20invoke%20-%20IndexError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20local%20invoke%20-%20IndexError

It seems for some reason, the docker client returns an empty list while SAM CLI tries to get the RepoDigests attribute of the emulation local image, although this should not be a valid value, it should return a list contains one item which is contains the hash value of the emulation image's parent.

def is_base_image_current(self, image_name: str) -> bool:
"""
Return True if the base image is up-to-date with the remote environment by comparing the image digests
Parameters
----------
image_name : str
Base image name to check
Returns
-------
bool
True if local image digest is the same as the remote image digest
"""
return self.get_local_image_digest(image_name) == self.get_remote_image_digest(image_name)
def get_remote_image_digest(self, image_name: str) -> Optional[str]:
"""
Get the digest of the remote version of an image
Parameters
----------
image_name : str
Name of the image to get the digest
Returns
-------
str
Image digest, including `sha256:` prefix
"""
remote_info = self.docker_client.images.get_registry_data(image_name)
digest: Optional[str] = remote_info.attrs.get("Descriptor", {}).get("digest")
return digest
def get_local_image_digest(self, image_name: str) -> Optional[str]:
"""
Get the digest of the local version of an image
Parameters
----------
image_name : str
Name of the image to get the digest
Returns
-------
str
Image digest, including `sha256:` prefix
"""
image_info = self.docker_client.images.get(image_name)
full_digest: str = image_info.attrs.get("RepoDigests", [None])[0]
try:
return full_digest.split("@")[1]
except (AttributeError, IndexError):
return None

This pr is to add a safe guard against this problem, and to prevent SAM CLI from crashing, and it will recreate the emulation image in case of this issue happen.

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.

… contains any empty list for `RepoDigests` attribute
@moelasmar moelasmar requested a review from a team as a code owner August 17, 2023 19:49
@moelasmar moelasmar added this pull request to the merge queue Aug 17, 2023
Merged via the queue into aws:develop with commit 54c3a7b Aug 17, 2023
78 checks passed
@moelasmar moelasmar deleted the develop_empty_repo_digests_list branch August 18, 2023 00:54
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.

3 participants