Skip to content

feature: allow use of short lived creds for local container #3501

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,7 @@ A few important notes:
- If you are using S3 data as input, it is pulled from S3 to your local environment. Ensure you have sufficient space to store the data locally.
- If you run into problems it often due to different Docker containers conflicting. Killing these containers and re-running often solves your problems.
- Local Mode requires Docker Compose and `nvidia-docker2 <https://github.com/NVIDIA/nvidia-docker>`__ for ``local_gpu``.
- Set ``USE_SHORT_LIVED_CREDENTIALS=1`` if running on EC2 and you would like to use the session credentials instead of EC2 Metadata Service credentials.

.. warning::

Expand Down
7 changes: 6 additions & 1 deletion src/sagemaker/local/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ def _aws_credentials(session):
"AWS_ACCESS_KEY_ID=%s" % (str(access_key)),
"AWS_SECRET_ACCESS_KEY=%s" % (str(secret_key)),
]
if not _aws_credentials_available_in_metadata_service():
if _use_short_lived_credentials() or not _aws_credentials_available_in_metadata_service():
logger.warning(
"Using the short-lived AWS credentials found in session. They might expire while "
"running."
Expand Down Expand Up @@ -1052,6 +1052,11 @@ def _aws_credentials_available_in_metadata_service():
return not instance_metadata_provider.load() is None


def _use_short_lived_credentials():
"""Use short-lived AWS credentials found in session."""
return os.environ.get("USE_SHORT_LIVED_CREDENTIALS") == "1"


def _write_json_file(filename, content):
"""Write the contents dict as json to the file.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,5 +860,25 @@ def test__aws_credentials_with_short_lived_credentials_and_ec2_metadata_service_
]


@patch("sagemaker.local.image._aws_credentials_available_in_metadata_service")
def test__aws_credentials_with_short_lived_credentials_and_ec2_metadata_service_having_credentials_override(
mock,
):
os.environ["USE_SHORT_LIVED_CREDENTIALS"] = "1"
credentials = Credentials(
access_key=_random_string(), secret_key=_random_string(), token=_random_string()
)
session = Mock()
session.get_credentials.return_value = credentials
mock.return_value = True
aws_credentials = _aws_credentials(session)

assert aws_credentials == [
"AWS_ACCESS_KEY_ID=%s" % credentials.access_key,
"AWS_SECRET_ACCESS_KEY=%s" % credentials.secret_key,
"AWS_SESSION_TOKEN=%s" % credentials.token,
]


def _random_string(size=6, chars=string.ascii_uppercase):
return "".join(random.choice(chars) for x in range(size))