@@ -47,26 +47,26 @@ def _aws_temp_credentials():
4747    if  access_key  and  secret_key :
4848        return  AwsCredential (
4949            access_key , secret_key , os .environ .get ('AWS_SESSION_TOKEN' ))
50-     # Check if environment variables exposed by IRSA are present 
50+     # Check if environment variables exposed by IAM Roles for Service Accounts (IRSA) are present. 
51+     # See https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html for details. 
5152    irsa_web_id_file  =  os .getenv ('AWS_WEB_IDENTITY_TOKEN_FILE' )
5253    irsa_role_arn  =  os .getenv ('AWS_ROLE_ARN' )
5354    if  irsa_web_id_file  and  irsa_role_arn :
54-         print ("in irsa flow with values: " , irsa_web_id_file , irsa_role_arn )
5555        try :
5656            with  open (irsa_web_id_file ) as  f :
5757                irsa_web_id_token  =  f .read ()
58-             access_key , secret_key , session_token  =  _irsa_assume_role (
59-                 irsa_role_arn ,
60-                 irsa_web_id_token ,
61-                 'pymongo-auth-aws' 
62-             )
58+         # Check for errors raised by `open` from older Python versions. 
59+         except  (OSError , IOError , InterruptedError ) as  exc :
60+             raise  PyMongoAuthAwsError (
61+                 'temporary MONGODB-AWS credentials could not be obtained, ' 
62+                 'error: %s'  %  (exc ,))
63+         try :
64+             return  _irsa_assume_role (irsa_role_arn , irsa_web_id_token , 'pymongo-auth-aws' )
6365        except  ClientError  as  error :
6466            error_message  =  error .response ['Error' ]['Message' ]
6567            raise  PyMongoAuthAwsError (
6668                'temporary MONGODB-AWS credentials could not be obtained, ' 
6769                'error: %s'  %  (error_message ,))
68-         print ("no error in irsa flow!" )
69-         return  AwsCredential (access_key , secret_key , session_token )
7070    # If the environment variable 
7171    # AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set then drivers MUST 
7272    # assume that it was set by an AWS ECS agent and use the URI 
@@ -124,7 +124,7 @@ def _aws_temp_credentials():
124124
125125
126126def  _irsa_assume_role (role_arn , token , role_session_name ):
127-     """Call sts:AssumeRoleWithWebIdentity and return temporary credentials""" 
127+     """Call sts:AssumeRoleWithWebIdentity and return temporary credentials. """ 
128128    sts_client  =  boto3 .client ('sts' )
129129    resp  =  sts_client .assume_role_with_web_identity (
130130        RoleArn = role_arn ,
@@ -136,7 +136,7 @@ def _irsa_assume_role(role_arn, token, role_session_name):
136136    secret_key  =  creds ['SecretAccessKey' ]
137137    session_token  =  creds ['SessionToken' ]
138138
139-     return  access_key , secret_key , session_token 
139+     return  AwsCredential ( access_key , secret_key , session_token ) 
140140
141141
142142_AWS4_HMAC_SHA256  =  'AWS4-HMAC-SHA256' 
0 commit comments