@@ -17,6 +17,11 @@ def token_hex(nbytes=None):
17
17
import botocore .client
18
18
import magic
19
19
import ssdeep
20
+ from botocore .credentials import (
21
+ ContainerProvider ,
22
+ InstanceMetadataFetcher ,
23
+ InstanceMetadataProvider ,
24
+ )
20
25
from flask_restful import abort
21
26
from flask_sqlalchemy import Pagination
22
27
@@ -176,8 +181,23 @@ def get_s3_client(
176
181
else :
177
182
endpoint_url = "http://" + endpoint
178
183
184
+ session_token = None
185
+
179
186
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
181
201
182
202
if access_key is None or secret_key is None :
183
203
raise RuntimeError (
@@ -189,5 +209,6 @@ def get_s3_client(
189
209
endpoint_url = endpoint_url ,
190
210
aws_access_key_id = access_key ,
191
211
aws_secret_access_key = secret_key ,
212
+ aws_session_token = session_token ,
192
213
region_name = region ,
193
214
)
0 commit comments