Skip to content

Commit 47be8a5

Browse files
authored
S3: Fix authentication via IAM (iam_auth) (#704)
1 parent 0a5e93c commit 47be8a5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

mwdb/core/util.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def token_hex(nbytes=None):
1717
import botocore.client
1818
import magic
1919
import ssdeep
20+
from botocore.credentials import (
21+
ContainerProvider,
22+
InstanceMetadataFetcher,
23+
InstanceMetadataProvider,
24+
)
2025
from flask_restful import abort
2126
from flask_sqlalchemy import Pagination
2227

@@ -176,8 +181,23 @@ def get_s3_client(
176181
else:
177182
endpoint_url = "http://" + endpoint
178183

184+
session_token = None
185+
179186
if iam_auth:
180-
return boto3.client("iam", endpoint_url=endpoint_url, region_name=region)
187+
iam_providers = [
188+
ContainerProvider(),
189+
InstanceMetadataProvider(
190+
iam_role_fetcher=InstanceMetadataFetcher(timeout=1000, num_attempts=2)
191+
),
192+
]
193+
194+
for provider in iam_providers:
195+
creds = provider.load()
196+
if creds:
197+
access_key = creds.access_key
198+
secret_key = creds.secret_key
199+
session_token = creds.token
200+
break
181201

182202
if access_key is None or secret_key is None:
183203
raise RuntimeError(
@@ -189,5 +209,6 @@ def get_s3_client(
189209
endpoint_url=endpoint_url,
190210
aws_access_key_id=access_key,
191211
aws_secret_access_key=secret_key,
212+
aws_session_token=session_token,
192213
region_name=region,
193214
)

0 commit comments

Comments
 (0)