# aws-discovery: every read assumes the customer's read-only role, inside the accounts and regions of the connection. import asyncio import boto3 from botocore.config import Config as BotoConfig AWS_SESSIONS = {} # kind -> (service, operation, result key, token parameter, token in the reply, fixed arguments) AWS_SERVERS = { "instances": ("ec2", "describe_instances", "Reservations", "NextToken", "NextToken", {}), "auto_scaling_groups": ("autoscaling", "describe_auto_scaling_groups", "AutoScalingGroups", "NextToken", "NextToken", {}), "volumes": ("ec2", "describe_volumes", "Volumes", "NextToken", "NextToken", {}), } AWS_WORKLOADS = { "lambda_functions": ("lambda", "list_functions", "Functions", "Marker", "NextMarker", {}), "ecs_clusters": ("ecs", "list_clusters", "clusterArns", "nextToken", "nextToken", {}), "app_runner_services": ("apprunner", "list_services", "ServiceSummaryList", "NextToken", "NextToken", {}), "elastic_beanstalk_environments": ("elasticbeanstalk", "describe_environments", "Environments", "NextToken", "NextToken", {}), } AWS_NETWORK = { "vpcs": ("ec2", "describe_vpcs", "Vpcs", "NextToken", "NextToken", {}), "subnets": ("ec2", "describe_subnets", "Subnets", "NextToken", "NextToken", {}), "route_tables": ("ec2", "describe_route_tables", "RouteTables", "NextToken", "NextToken", {}), "security_groups": ("ec2", "describe_security_groups", "SecurityGroups", "NextToken", "NextToken", {}), "nat_gateways": ("ec2", "describe_nat_gateways", "NatGateways", "NextToken", "NextToken", {}), "internet_gateways": ("ec2", "describe_internet_gateways", "InternetGateways", "NextToken", "NextToken", {}), "transit_gateways": ("ec2", "describe_transit_gateways", "TransitGateways", "NextToken", "NextToken", {}), "vpc_peering": ("ec2", "describe_vpc_peering_connections", "VpcPeeringConnections", "NextToken", "NextToken", {}), "vpc_endpoints": ("ec2", "describe_vpc_endpoints", "VpcEndpoints", "NextToken", "NextToken", {}), "load_balancers": ("elbv2", "describe_load_balancers", "LoadBalancers", "Marker", "NextMarker", {}), } AWS_DATA = { "rds_instances": ("rds", "describe_db_instances", "DBInstances", "Marker", "Marker", {}), "dynamodb_tables": ("dynamodb", "list_tables", "TableNames", "ExclusiveStartTableName", "LastEvaluatedTableName", {}), "s3_buckets": ("s3", "list_buckets", "Buckets", "ContinuationToken", "ContinuationToken", {}), } AWS_ORG_POLICY_TYPES = {"service_control_policies": "SERVICE_CONTROL_POLICY", "resource_control_policies": "RESOURCE_CONTROL_POLICY", "tag_policies": "TAG_POLICY", "backup_policies": "BACKUP_POLICY"} AWS_PILLARS = {"cost_optimizing": "cost", "security": "security", "fault_tolerance": "reliability", "performance": "performance", "operational_excellence": "operations", "service_limits": "reliability"} def _aws_csv(connection: Dict[str, Any], field: str) -> List[str]: return [v.strip() for v in str(connection.get(field) or "").split(",") if v.strip()] def _aws_session(connection: Dict[str, Any], account_id: Optional[str]): """A session on the customer's read-only role, or on the member account's when one is named.""" key = hashlib.sha256(json.dumps([connection, account_id], sort_keys=True).encode()).hexdigest() cached = AWS_SESSIONS.get(key) now = time.time() if cached and cached[1] > now + 60: return cached[0] def assume(session, role_arn, external_id=None): region = _aws_csv(connection, "regions")[0] request = {"RoleArn": role_arn, "RoleSessionName": "kab-discovery", "DurationSeconds": 3600} if external_id: request["ExternalId"] = external_id found = session.client("sts", region_name=region).assume_role(**request)["Credentials"] return boto3.Session(aws_access_key_id=found["AccessKeyId"], aws_secret_access_key=found["SecretAccessKey"], aws_session_token=found["SessionToken"]) base = boto3.Session(aws_access_key_id=connection["access_key_id"], aws_secret_access_key=connection["secret_access_key"]) session = assume(base, connection["role_arn"], connection.get("external_id")) if account_id: partition = str(connection["role_arn"]).split(":")[1] session = assume(session, f"arn:{partition}:iam::{account_id}:role/{connection['member_role_name']}") AWS_SESSIONS[key] = (session, now + 3000) return session def _aws_target(arguments: Dict[str, Any]): """(account, region) or a refusal. Nothing outside the connection's accounts and regions is ever reached.""" connection = get_connection() regions = _aws_csv(connection, "regions") region = arguments.get("region") or regions[0] if region not in regions: return None, None, _refuse("outside_scope", "region isn't in this connection's regions") account = arguments.get("account_id") if account and (account not in _aws_csv(connection, "account_ids") or not connection.get("member_role_name")): return None, None, _refuse("outside_scope", "account_id isn't in this connection's accounts") return account, region, None async def _aws_call(arguments: Dict[str, Any], service: str, operation: str, region: Optional[str] = None, **request): connection = get_connection() account = arguments.get("account_id") where = region or arguments.get("region") or _aws_csv(connection, "regions")[0] def run(): # Adaptive mode slows the client down when AWS throttles it, on top of retrying. retries = BotoConfig(retries={"max_attempts": 6, "mode": "adaptive"}, read_timeout=60, connect_timeout=10) client = _aws_session(connection, account).client(service, region_name=where, config=retries) return getattr(client, operation)(**request) return await asyncio.to_thread(run) async def _aws_kind(tool: str, arguments: Dict[str, Any], kinds: Dict[str, tuple], default: str) -> Dict[str, Any]: kind = arguments.get("kind") or default if kind not in kinds: return _refuse("unknown_kind", "kind must be one of: " + ", ".join(kinds)) account, region, refused = _aws_target(arguments) if refused: return refused service, operation, result_key, token_in, token_out, fixed = kinds[kind] try: upstream, skip = _read_page(arguments) except (ValueError, KeyError, TypeError): return _refuse("invalid_page_token", "page_token isn't one this server issued") request = dict(fixed) if upstream: request[token_in] = upstream try: body = await _aws_call(arguments, service, operation, region, **request) except Exception as error: return _upstream_failed("aws", error) extra = {"account_id": account, "region": region, "kind": kind} return _page_result("aws", tool, arguments, body.get(result_key, []), upstream, body.get(token_out), skip, extra) async def _aws_each(tool: str, arguments: Dict[str, Any], list_kind: tuple, read) -> Dict[str, Any]: """List one page of things, then read a detail for each. A detail that's absent or refused is reported.""" account, region, refused = _aws_target(arguments) if refused: return refused service, operation, result_key, token_in, token_out, fixed = list_kind try: upstream, skip = _read_page(arguments) except (ValueError, KeyError, TypeError): return _refuse("invalid_page_token", "page_token isn't one this server issued") request = dict(fixed) if upstream: request[token_in] = upstream try: body = await _aws_call(arguments, service, operation, region, **request) except Exception as error: return _upstream_failed("aws", error) items = [] for entry in body.get(result_key, [])[skip:skip + _page_size(arguments)]: try: items.append(await read(entry, region)) except Exception as error: items.append({"resource": entry, "unread": type(error).__name__}) consumed = skip + len(items) more = consumed < len(body.get(result_key, [])) next_upstream = upstream if more else body.get(token_out) result = _page_result("aws", tool, arguments, items, None, None, 0, {"account_id": account, "region": region}) if more or next_upstream: result["data"]["next_page_token"] = _write_page(next_upstream, consumed if more else 0) return result def _aws_document(text: Any) -> Any: """A policy document, parsed when it arrives as a JSON string, so its principals can be scrubbed.""" try: return json.loads(text) if isinstance(text, str) else text except ValueError: return text