def __init__(self, session: boto.Session, partition: str): self._boto = session self._cache: Dict[str, bool] = {} self.partition = partition data_dir = os.path.join(os.path.dirname(boto.__file__), 'data') self._loader = create_loader(data_dir) endpoint_data = self._loader.load_data('endpoints') self._endpoints = EndpointResolver(endpoint_data)
def _get_region_host(self, session_key, payload): config = pc.get_proxy_info(session_key) tacommon.set_proxy_env(config) credentials_service = tacommon.create_credentials_service( get_splunkd_uri(), session_key) credentials = credentials_service.load( payload[tac.aws_account], payload[tac.aws_iam_role], ) config[tac.key_id] = credentials.aws_access_key_id config[tac.secret_key] = credentials.aws_secret_access_key config['aws_session_token'] = credentials.aws_session_token config[asc.bucket_name] = payload[asc.bucket_name] config[asc.host_name] = tac.CATEGORY_HOST_NAME_MAP[ credentials.category] if config[asc.host_name] == asc.default_host: region = get_region_for_bucketname(config) with open(ENDPOINTS_PATH, 'r') as endpoints_file: endpoints = json.load(endpoints_file) host_name = EndpointResolver(endpoints).construct_endpoint( 's3', region).get('hostname', asc.default_host) else: pattern = r's3[.-]([\w-]+)\.amazonaws.com' m = re.search(pattern, config[asc.host_name]) region = m.group(1) if m else 'us-east-1' host_name = config[asc.host_name] return (region, host_name)
def create_endpoint_resolver() -> EndpointResolver: """ Creates an instance of the botocore EndpointResolver. Used to inject the instance during application initialization to avoid loading endpoint data on a per-request basis. :return: the EndpointResolver instance """ return EndpointResolver(create_loader().load_data("endpoints"))
class RegionCache: def __init__(self, session: boto.Session, partition: str): self._boto = session self._cache: Dict[str, bool] = {} self.partition = partition data_dir = os.path.join(os.path.dirname(boto.__file__), 'data') self._loader = create_loader(data_dir) endpoint_data = self._loader.load_data('endpoints') self._endpoints = EndpointResolver(endpoint_data) def _region_is_available(self, region: str) -> bool: # https://www.cloudar.be/awsblog/checking-if-a-region-is-enabled-using-the-aws-api/ # https://stackoverflow.com/a/56184952 regional_sts = self._boto.create_client('sts', region_name=region) try: regional_sts.get_caller_identity() return True except ClientError: return False def _get_and_cache_region(self, region: str) -> bool: cached = self._cache.get(region) if cached is not None: return cached available = self._region_is_available(region) self._cache[region] = available return available def regions_for_service(self, service: str) -> Iterator[str]: sm = self._loader.load_service_model(service, type_name='service-2') prefix = sm['metadata'].get('endpointPrefix', service) for region in self._endpoints.get_available_endpoints( prefix, partition_name=self.partition): if self._get_and_cache_region(region): yield region
def main(): parser = argparse.ArgumentParser() parser.add_argument('--overwrite', action='store_true') parser.add_argument('--endpoints-file', help=('Path to endpoints.json. If this argument ' 'is not given, then the endpoints.json file ' 'bundled with botocore will be used.')) args = parser.parse_args() known_services_in_existing_endpoints = _load_endpoint_services( EXISTING_ENDPOINTS_FILE) session = botocore.session.get_session() if args.endpoints_file: with open(args.endpoints_file) as f: endpoint_data = json.load(f) else: endpoint_data = session.get_data('endpoints') resolver = EndpointResolver(endpoint_data) strict_resolver = StrictEndpointResolver(resolver, endpoint_data) builder = StaticEndpointBuilder(strict_resolver) static_endpoints = builder.build_static_endpoints( known_services_in_existing_endpoints) json_data = json.dumps(static_endpoints, indent=4, sort_keys=True) if args.overwrite: with open(EXISTING_ENDPOINTS_FILE, 'w') as f: f.write(json_data) else: print(json_data)
def create_default_resolver(): loader = self.get_component('data_loader') endpoints = loader.load_data('endpoints') return EndpointResolver(endpoints)