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 IAM token expiration #234

Merged
merged 7 commits into from
Jan 15, 2024
Merged
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
41 changes: 23 additions & 18 deletions karton/core/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
InstanceMetadataFetcher,
InstanceMetadataProvider,
)
from botocore.session import get_session
from redis import AuthenticationError, StrictRedis
from redis.client import Pipeline
from urllib3.response import HTTPResponse
Expand Down Expand Up @@ -120,7 +121,6 @@ def __init__(
config, identity=identity, service_info=service_info
)

session_token = None
endpoint = config.get("s3", "address")
access_key = config.get("s3", "access_key")
secret_key = config.get("s3", "secret_key")
Expand All @@ -136,22 +136,10 @@ def __init__(
)

if iam_auth:
iam_providers = [
ContainerProvider(),
InstanceMetadataProvider(
iam_role_fetcher=InstanceMetadataFetcher(
timeout=1000, num_attempts=2
)
),
]

for provider in iam_providers:
creds = provider.load()
if creds:
access_key = creds.access_key
secret_key = creds.secret_key
session_token = creds.token
break
s3_client = self.iam_auth_s3(endpoint)
if s3_client:
self.s3 = s3_client
return

if access_key is None or secret_key is None:
raise RuntimeError(
Expand All @@ -163,9 +151,26 @@ def __init__(
endpoint_url=endpoint,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
aws_session_token=session_token,
)

def iam_auth_s3(self, endpoint: str):
yankovs marked this conversation as resolved.
Show resolved Hide resolved
boto_session = get_session()
iam_providers = [
ContainerProvider(),
InstanceMetadataProvider(
iam_role_fetcher=InstanceMetadataFetcher(timeout=1000, num_attempts=2)
),
]

for provider in iam_providers:
creds = provider.load()
if creds:
boto_session._credentials = creds # type: ignore
return boto3.Session(botocore_session=boto_session).client(
"s3",
endpoint_url=endpoint,
)

@staticmethod
def _validate_identity(identity: str):
disallowed_chars = [" ", "?"]
Expand Down
Loading