Skip to content

Commit 58bd6c7

Browse files
committed
fix aws impersonation
1 parent 2188803 commit 58bd6c7

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/snowflake/connector/wif_util.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ def get_aws_sts_hostname(region: str, partition: str) -> str:
145145
)
146146

147147

148+
def get_aws_session(impersonation_path: list[str] | None = None):
149+
"""Creates a boto3 session with the appropriate credentials. If impersonation_path is provided, uses role at end of the path."""
150+
session = boto3.session.Session()
151+
152+
impersonation_path = impersonation_path or []
153+
for arn in impersonation_path:
154+
response = session.client("sts").assume_role(
155+
RoleArn=arn, RoleSessionName="identity-federation-session"
156+
)
157+
creds = response["Credentials"]
158+
session = boto3.Session(
159+
aws_access_key_id=creds["AccessKeyId"],
160+
aws_secret_access_key=creds["SecretAccessKey"],
161+
aws_session_token=creds["SessionToken"],
162+
)
163+
return session
164+
165+
148166
def create_aws_attestation(
149167
impersonation_path: list[str] | None = None,
150168
) -> WorkloadIdentityAttestation:
@@ -153,26 +171,7 @@ def create_aws_attestation(
153171
If the application isn't running on AWS or no credentials were found, raises an error.
154172
"""
155173
# TODO: SNOW-2223669 Investigate if our adapters - containing settings of http traffic - should be passed here as boto urllib3session. Those requests go to local servers, so they do not need Proxy setup or Headers customization in theory. But we may want to have all the traffic going through one class (e.g. Adapter or mixin).
156-
session = boto3.session.Session()
157-
if impersonation_path:
158-
sts_client = boto3.client("sts")
159-
for arn in impersonation_path:
160-
# Assume target role
161-
response = sts_client.assume_role(
162-
RoleArn=arn, RoleSessionName="identity-federation-session"
163-
)
164-
165-
# Use the credentials from the last assumed role
166-
creds = response["Credentials"]
167-
access_key = creds["AccessKeyId"]
168-
secret_key = creds["SecretAccessKey"]
169-
session_token = creds["SessionToken"]
170-
171-
session = boto3.Session(
172-
aws_access_key_id=access_key,
173-
aws_secret_access_key=secret_key,
174-
aws_session_token=session_token,
175-
)
174+
session = get_aws_session(impersonation_path)
176175

177176
aws_creds = session.get_credentials()
178177
if not aws_creds:

0 commit comments

Comments
 (0)