@@ -131,6 +131,7 @@ def authenticate(command):
131131 def some_command(tf: TFRunner, paths: PathsHandler, args):
132132 # command logic
133133 """
134+
134135 @wraps (command )
135136 def new_command (* args , ** kwargs ):
136137 ctx = click .get_current_context ()
@@ -251,6 +252,7 @@ def refresh_layer_credentials(paths: PathsHandler):
251252 )
252253 logger .info (f"Credentials for { account_name } account written successfully." )
253254
255+
254256def refresh_layer_credentials_mfa (paths : PathsHandler ):
255257 tf_profile , raw_profiles = get_profiles (paths )
256258 config_updater = ConfigUpdater ()
@@ -271,7 +273,11 @@ def refresh_layer_credentials_mfa(paths: PathsHandler):
271273 layer_profile = tf_profile if raw_profile in ("${var.profile}" , "each.value.profile" ) else None
272274
273275 # replace variables with their corresponding values
274- profile_name = raw_profile .replace ("${var.profile}" , tf_profile ).replace ("${var.project}" , paths .project ).replace ("each.value.profile" , tf_profile )
276+ profile_name = (
277+ raw_profile .replace ("${var.profile}" , tf_profile )
278+ .replace ("${var.project}" , paths .project )
279+ .replace ("each.value.profile" , tf_profile )
280+ )
275281
276282 # if layer_profile wasn't set, use profile_name
277283 if layer_profile is None :
@@ -286,14 +292,17 @@ def refresh_layer_credentials_mfa(paths: PathsHandler):
286292 raise ExitError (
287293 40 ,
288294 f"Credentials for profile { profile_name } have not been properly configured. Please check your configuration.\n "
289- f"Check the following link for possible solutions: https://leverage.binbash.co/user-guide/troubleshooting/credentials/" )
290-
295+ f"Check the following link for possible solutions: https://leverage.binbash.co/user-guide/troubleshooting/credentials/" ,
296+ )
297+
291298 cache_file = paths .aws_cache_dir / profile_name
292299 if cache_file .exists ():
293300 logger .debug (f"Found cached credentials in { cache_file } ." )
294301 cached_credentials = json .loads (cache_file .read_text ())
295-
296- expiration = datetime .strptime (cached_credentials .get ("Expiration" ), "%Y-%m-%dT%H:%M:%SZ" ).replace (tzinfo = tzutc ())
302+
303+ expiration = datetime .strptime (cached_credentials .get ("Expiration" ), "%Y-%m-%dT%H:%M:%SZ" ).replace (
304+ tzinfo = tzutc ()
305+ )
297306 renewal = datetime .now (tzutc ()) + timedelta (seconds = (30 * 60 ))
298307 if renewal < expiration :
299308 logger .info ("Using cached credentials." )
@@ -302,10 +311,7 @@ def refresh_layer_credentials_mfa(paths: PathsHandler):
302311 else :
303312 logger .debug ("No cached credentials found." )
304313
305- client_session = boto3 .Session (
306- botocore_session = session ,
307- profile_name = source_profile
308- )
314+ client_session = boto3 .Session (botocore_session = session , profile_name = source_profile )
309315 client = client_session .client ("sts" )
310316 credentials = None
311317 for _ in range (3 ):
@@ -315,7 +321,9 @@ def refresh_layer_credentials_mfa(paths: PathsHandler):
315321 raise ExitError (1 , "Aborted by user." )
316322
317323 try :
318- logger .debug (f"Assuming role { role_arn } for { profile_name } profile with serial { mfa_serial } and token code { mfa_token_code } " )
324+ logger .debug (
325+ f"Assuming role { role_arn } for { profile_name } profile with serial { mfa_serial } and token code { mfa_token_code } "
326+ )
319327 credentials = client .assume_role (
320328 RoleArn = role_arn ,
321329 SourceIdentity = source_profile ,
@@ -342,15 +350,15 @@ def refresh_layer_credentials_mfa(paths: PathsHandler):
342350 elif error .response ["Error" ]["Code" ] == "ExpiredToken" :
343351 logger .error ("Token has expired. Please try again." )
344352 continue
345- elif error .response ["Error" ]["Code" ] == "ValidationError" and "Invalid length for parameter TokenCode" in error .response ["Error" ]["Message" ]:
353+ elif (
354+ error .response ["Error" ]["Code" ] == "ValidationError"
355+ and "Invalid length for parameter TokenCode" in error .response ["Error" ]["Message" ]
356+ ):
346357 logger .error ("Invalid token length, it must be 6 digits long. Please try again." )
347358 continue
348359 elif "An error occurred" in error .response ["Error" ]["Message" ]:
349- raise ExitError (
350- 50 ,
351- f"Error assuming role: { error } "
352- )
353-
360+ raise ExitError (50 , f"Error assuming role: { error } " )
361+
354362 if credentials is None :
355363 raise ExitError (60 , "Failed to get credentials after 3 attempts. Please try again later." )
356364
0 commit comments