|
35 | 35 | 'org_admin_account_id': None, |
36 | 36 | 'save_to': None, |
37 | 37 | 'role_arn': None, |
| 38 | + 'aws_profile': None, |
38 | 39 | 'mfa_serial': None, |
39 | 40 | 'duration_seconds': 3600 |
40 | 41 | } |
@@ -272,42 +273,61 @@ def get_all_profiles(self) -> List[str]: |
272 | 273 | def get_aws_session(self) -> boto3.Session: |
273 | 274 | """Create AWS session with AssumeRole/MFA if configured""" |
274 | 275 | role_arn = self.get('role_arn') |
| 276 | + aws_profile = self.get('aws_profile') |
275 | 277 | mfa_serial = self.get('mfa_serial') |
276 | 278 | duration = self.get('duration_seconds', 3600) |
277 | 279 |
|
278 | | - if not role_arn: |
279 | | - return boto3.Session() |
280 | | - |
281 | | - mfa_token = None |
282 | | - if mfa_serial: |
283 | | - mfa_token = input(f"🔐 Enter MFA token for {mfa_serial}: ") |
284 | | - |
285 | | - sts = boto3.client('sts') |
286 | | - |
287 | | - assume_role_params = { |
288 | | - 'RoleArn': role_arn, |
289 | | - 'RoleSessionName': f'kosty-{self.profile}', |
290 | | - 'DurationSeconds': duration |
291 | | - } |
292 | | - |
293 | | - if mfa_serial and mfa_token: |
294 | | - assume_role_params['SerialNumber'] = mfa_serial |
295 | | - assume_role_params['TokenCode'] = mfa_token |
296 | | - |
297 | | - try: |
298 | | - response = sts.assume_role(**assume_role_params) |
| 280 | + # Priority: role_arn > aws_profile > default credentials |
| 281 | + if role_arn: |
| 282 | + # AssumeRole flow |
| 283 | + mfa_token = None |
| 284 | + if mfa_serial: |
| 285 | + mfa_token = input(f"🔐 Enter MFA token for {mfa_serial}: ") |
299 | 286 |
|
300 | | - return boto3.Session( |
301 | | - aws_access_key_id=response['Credentials']['AccessKeyId'], |
302 | | - aws_secret_access_key=response['Credentials']['SecretAccessKey'], |
303 | | - aws_session_token=response['Credentials']['SessionToken'] |
304 | | - ) |
305 | | - except Exception as e: |
306 | | - config_file = self._find_config_file() or 'No config file' |
307 | | - print(f"\nError: Failed to assume role") |
308 | | - print(f" Profile: {self.profile}") |
309 | | - print(f" Config: {config_file}") |
310 | | - print(f" Role ARN: {role_arn}") |
311 | | - print(f" Reason: {e}") |
312 | | - print("\nCannot proceed without valid role access. Aborting.\n") |
313 | | - raise SystemExit(1) |
| 287 | + sts = boto3.client('sts') |
| 288 | + |
| 289 | + assume_role_params = { |
| 290 | + 'RoleArn': role_arn, |
| 291 | + 'RoleSessionName': f'kosty-{self.profile}', |
| 292 | + 'DurationSeconds': duration |
| 293 | + } |
| 294 | + |
| 295 | + if mfa_serial and mfa_token: |
| 296 | + assume_role_params['SerialNumber'] = mfa_serial |
| 297 | + assume_role_params['TokenCode'] = mfa_token |
| 298 | + |
| 299 | + try: |
| 300 | + response = sts.assume_role(**assume_role_params) |
| 301 | + |
| 302 | + return boto3.Session( |
| 303 | + aws_access_key_id=response['Credentials']['AccessKeyId'], |
| 304 | + aws_secret_access_key=response['Credentials']['SecretAccessKey'], |
| 305 | + aws_session_token=response['Credentials']['SessionToken'] |
| 306 | + ) |
| 307 | + except Exception as e: |
| 308 | + config_file = self._find_config_file() or 'No config file' |
| 309 | + print(f"\nError: Failed to assume role") |
| 310 | + print(f" Profile: {self.profile}") |
| 311 | + print(f" Config: {config_file}") |
| 312 | + print(f" Role ARN: {role_arn}") |
| 313 | + print(f" Reason: {e}") |
| 314 | + print("\nCannot proceed without valid role access. Aborting.\n") |
| 315 | + raise SystemExit(1) |
| 316 | + |
| 317 | + elif aws_profile: |
| 318 | + # Use AWS CLI profile |
| 319 | + try: |
| 320 | + return boto3.Session(profile_name=aws_profile) |
| 321 | + except Exception as e: |
| 322 | + config_file = self._find_config_file() or 'No config file' |
| 323 | + print(f"\nError: Failed to use AWS profile") |
| 324 | + print(f" Profile: {self.profile}") |
| 325 | + print(f" Config: {config_file}") |
| 326 | + print(f" AWS Profile: {aws_profile}") |
| 327 | + print(f" Reason: {e}") |
| 328 | + print(f"\nMake sure '{aws_profile}' exists in ~/.aws/credentials or ~/.aws/config\n") |
| 329 | + raise SystemExit(1) |
| 330 | + |
| 331 | + else: |
| 332 | + # Use default credentials (env vars, instance role, default profile) |
| 333 | + return boto3.Session() |
0 commit comments