|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class StaxAuth:
|
16 |
| - def __init__(self, config_branch): |
| 16 | + def __init__(self, config_branch, max_retries: int = 3): |
17 | 17 | config = StaxConfig.api_config
|
18 | 18 |
|
19 | 19 | self.identity_pool = config.get(config_branch).get("identityPoolId")
|
20 | 20 | self.user_pool = config.get(config_branch).get("userPoolId")
|
21 | 21 | self.client_id = config.get(config_branch).get("userPoolWebClientId")
|
22 | 22 | self.aws_region = config.get(config_branch).get("region")
|
| 23 | + self.max_retries = max_retries |
23 | 24 |
|
24 | 25 | def requests_auth(self, username, password, **kwargs):
|
25 | 26 | if username is None:
|
@@ -83,23 +84,35 @@ def sts_from_cognito_identity_pool(self, token, cognito_client=None, **kwargs):
|
83 | 84 | region_name=self.aws_region,
|
84 | 85 | config=BotoConfig(signature_version=UNSIGNED),
|
85 | 86 | )
|
86 |
| - try: |
87 |
| - id = cognito_client.get_id( |
88 |
| - IdentityPoolId=self.identity_pool, |
89 |
| - Logins={ |
90 |
| - f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token |
91 |
| - }, |
92 |
| - ) |
93 |
| - id_creds = cognito_client.get_credentials_for_identity( |
94 |
| - IdentityId=id["IdentityId"], |
95 |
| - Logins={ |
96 |
| - f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token |
97 |
| - }, |
98 |
| - ) |
99 |
| - except ClientError as e: |
| 87 | + |
| 88 | + for i in range(self.max_retries): |
| 89 | + try: |
| 90 | + id = cognito_client.get_id( |
| 91 | + IdentityPoolId=self.identity_pool, |
| 92 | + Logins={ |
| 93 | + f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token |
| 94 | + }, |
| 95 | + ) |
| 96 | + id_creds = cognito_client.get_credentials_for_identity( |
| 97 | + IdentityId=id["IdentityId"], |
| 98 | + Logins={ |
| 99 | + f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token |
| 100 | + }, |
| 101 | + ) |
| 102 | + break |
| 103 | + except ClientError as e: |
| 104 | + # AWS eventual consistency, attempt to retry up to 3 times |
| 105 | + if "Couldn't verify signed token" in str(e): |
| 106 | + continue |
| 107 | + else: |
| 108 | + raise InvalidCredentialsException( |
| 109 | + f"Unexpected Client Error. Error details: {e}" |
| 110 | + ) |
| 111 | + else: |
100 | 112 | raise InvalidCredentialsException(
|
101 |
| - f"Unexpected Client Error. Error details: {e}" |
| 113 | + "Retries Exceeded: Unexpected Client Error" |
102 | 114 | )
|
| 115 | + |
103 | 116 | return id_creds
|
104 | 117 |
|
105 | 118 | def sigv4_signed_auth_headers(self, id_creds):
|
|
0 commit comments