Coverage for src\clauth\aws_utils.py: 12%
52 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-18 10:09 -0400
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-18 10:09 -0400
1import boto3
2from botocore.config import Config
3from botocore.exceptions import NoCredentialsError, ClientError,BotoCoreError ,TokenRetrievalError
9def user_is_authenticated(profile: str) -> bool:
10 """Check if user is authenticated with AWS using the specified profile."""
11 try:
12 session = boto3.Session(profile_name=profile)
13 sts = session.client("sts")
14 ident = sts.get_caller_identity()
15 account_id = ident["Account"]
16 print(f'User account: {account_id}')
17 return True
18 except (NoCredentialsError, TokenRetrievalError):
19 print("No credentials — user probably never logged in.")
20 return False
21 except ClientError as e:
22 error_code = e.response["Error"]["Code"]
23 if error_code in ("UnauthorizedSSOToken", "ExpiredToken", "InvalidClientTokenId"):
24 print(f"Token expired — please run `aws sso login --profile {profile}`.")
25 return False
26 else:
27 print(f'Error getting token: {e}')
28 return False
29 except Exception as e:
30 print(f'Unexpected error during authentication: {e}')
31 return False
33def list_bedrock_profiles(profile: str, region: str, provider: str = 'anthropic', sort: bool = True) -> tuple[list[str], list[str]]:
34 """
35 List available Bedrock inference profiles for the specified provider.
37 Args:
38 profile: AWS profile name to use
39 region: AWS region to query
40 provider: Model provider to filter by (default: 'anthropic')
41 sort: Whether to sort results in reverse order (default: True)
43 Returns:
44 Tuple of (model_ids, model_arns) lists
45 """
46 try:
47 session = boto3.Session(profile_name=profile, region_name=region)
48 client = session.client("bedrock")
50 resp = client.list_inference_profiles()
51 inference_summaries = resp.get("inferenceProfileSummaries", [])
53 if not inference_summaries:
54 print(f"No inference profiles found in region {region}")
55 return [], []
57 model_arns = [p["inferenceProfileArn"] for p in inference_summaries]
59 if model_arns and sort:
60 model_arns.sort(reverse=True)
62 # Filter by provider
63 model_arn_by_provider = [arn for arn in model_arns if provider.lower() in arn.lower()]
65 if not model_arn_by_provider:
66 print(f"No models found for provider '{provider}' in region {region}")
67 return [], []
69 model_ids = [arn.split('/')[-1] for arn in model_arn_by_provider]
70 return model_ids, model_arn_by_provider
72 except (BotoCoreError, ClientError) as e:
73 print(f"Error listing inference profiles: {e}")
74 return [], []
75 except Exception as e:
76 print(f"Unexpected error listing models: {e}")
77 return [], []
79if __name__=='__main__':
80 p=list_bedrock_profiles(profile='clauth',region='ap-southeast-2')
81 print('===============')
82 print(p)