Search is not available for this dataset
text
stringlengths
75
104k
def get(self, key, delete_if_expired=True): """ Retrieve key from Cache. :param key: key to look up in cache. :type key: ``object`` :param delete_if_expired: remove value from cache if it is expired. Default is True. :type delete_if_expired: ``bool`` :returns: value from cache or None :rtype: varies or None """ self._update_cache_stats(key, None) if key in self._CACHE: (expiration, obj) = self._CACHE[key] if expiration > self._now(): self._update_cache_stats(key, 'hit') return obj else: if delete_if_expired: self.delete(key) self._update_cache_stats(key, 'expired') return None self._update_cache_stats(key, 'miss') return None
def insert(self, key, obj, future_expiration_minutes=15): """ Insert item into cache. :param key: key to look up in cache. :type key: ``object`` :param obj: item to store in cache. :type obj: varies :param future_expiration_minutes: number of minutes item is valid :type param: ``int`` :returns: True :rtype: ``bool`` """ expiration_time = self._calculate_expiration(future_expiration_minutes) self._CACHE[key] = (expiration_time, obj) return True
def _update_cache_stats(self, key, result): """ Update the cache stats. If no cache-result is specified, we iniitialize the key. Otherwise, we increment the correct cache-result. Note the behavior for expired. A client can be expired and the key still exists. """ if result is None: self._CACHE_STATS['access_stats'].setdefault(key, {'hit': 0, 'miss': 0, 'expired': 0}) else: self._CACHE_STATS['access_stats'][key][result] +=1
def get_access_details(self, key=None): """Get access details in cache.""" if key in self._CACHE_STATS: return self._CACHE_STATS['access_stats'][key] else: return self._CACHE_STATS['access_stats']
def get_stats(self): """Get general stats for the cache.""" expired = sum([x['expired'] for _, x in self._CACHE_STATS['access_stats'].items()]) miss = sum([x['miss'] for _, x in self._CACHE_STATS['access_stats'].items()]) hit = sum([x['hit'] for _, x in self._CACHE_STATS['access_stats'].items()]) return { 'totals': { 'keys': len(self._CACHE_STATS['access_stats']), 'expired': expired, 'miss': miss, 'hit': hit, } }
def get_security_group(sg_obj, flags=FLAGS.ALL, **conn): """ Orchestrates calls to build a Security Group in the following format: { "Description": ..., "GroupName": ..., "IpPermissions" ..., "OwnerId" ..., "GroupId" ..., "IpPermissionsEgress" ..., "VpcId" ... } Args: sg_obj: name, ARN, or dict of Security Group flags: Flags describing which sections should be included in the return value. Default ALL Returns: dictionary describing the requested Security Group """ if isinstance(sg_obj, string_types): group_arn = ARN(sg_obj) if group_arn.error: sg_obj = {'GroupId': sg_obj} else: sg_obj = {'GroupId': group_arn.parsed_name} return registry.build_out(flags, sg_obj, **conn)
def get_user(user, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully build out an IAM User in the following format: { "Arn": ..., "AccessKeys": ..., "CreateDate": ..., # str "InlinePolicies": ..., "ManagedPolicies": ..., "MFADevices": ..., "Path": ..., "UserId": ..., "UserName": ..., "SigningCerts": ... } :param user: dict MUST contain the UserName and also a combination of either the ARN or the account_number :param output: Determines whether keys should be returned camelized or underscored. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing fully built out user. """ user = modify(user, output='camelized') _conn_from_args(user, conn) return registry.build_out(flags, start_with=user, pass_datastructure=True, **conn)
def get_all_users(flags=FLAGS.ACCESS_KEYS | FLAGS.MFA_DEVICES | FLAGS.LOGIN_PROFILE | FLAGS.SIGNING_CERTIFICATES, **conn): """ Returns a list of Users represented as dictionary below: { "Arn": ..., "AccessKeys": ..., "CreateDate": ..., # str "InlinePolicies": ..., "ManagedPolicies": ..., "MFADevices": ..., "Path": ..., "UserId": ..., "UserName": ..., "SigningCerts": ... } :param flags: :param conn: dict containing enough information to make a connection to the desired account. :return: list of dicts containing fully built out user. """ users = [] account_users = get_account_authorization_details('User', **conn) for user in account_users: temp_user = { 'Arn': user['Arn'], 'CreateDate': get_iso_string(user['CreateDate']), 'GroupList': user['GroupList'], 'InlinePolicies': user['UserPolicyList'], 'ManagedPolicies': [ { "name": x['PolicyName'], "arn": x['PolicyArn'] } for x in user['AttachedManagedPolicies'] ], 'Path': user['Path'], 'UserId': user['UserId'], 'UserName': user['UserName'] } user = modify(temp_user, output='camelized') _conn_from_args(user, conn) users.append(registry.build_out(flags, start_with=user, pass_datastructure=True, **conn)) return users
def get_vpc_flow_logs(vpc, **conn): """Gets the VPC Flow Logs for a VPC""" fl_result = describe_flow_logs(Filters=[{"Name": "resource-id", "Values": [vpc["id"]]}], **conn) fl_ids = [] for fl in fl_result: fl_ids.append(fl["FlowLogId"]) return fl_ids
def get_classic_link(vpc, **conn): """Gets the Classic Link details about a VPC""" result = {} try: cl_result = describe_vpc_classic_link(VpcIds=[vpc["id"]], **conn)[0] result["Enabled"] = cl_result["ClassicLinkEnabled"] # Check for DNS as well: dns_result = describe_vpc_classic_link_dns_support(VpcIds=[vpc["id"]], **conn)[0] result["DnsEnabled"] = dns_result["ClassicLinkDnsSupported"] except ClientError as e: # This is not supported for all regions. if 'UnsupportedOperation' not in str(e): raise e return result
def get_internet_gateway(vpc, **conn): """Gets the Internet Gateway details about a VPC""" result = {} ig_result = describe_internet_gateways(Filters=[{"Name": "attachment.vpc-id", "Values": [vpc["id"]]}], **conn) if ig_result: # Only 1 IG can be attached to a VPC: result.update({ "State": ig_result[0]["Attachments"][0]["State"], "Id": ig_result[0]["InternetGatewayId"], "Tags": ig_result[0].get("Tags", []) }) return result
def get_vpc_peering_connections(vpc, **conn): """Gets the Internet Gateway details about a VPC""" accepter_result = describe_vpc_peering_connections(Filters=[{"Name": "accepter-vpc-info.vpc-id", "Values": [vpc["id"]]}], **conn) requester_result = describe_vpc_peering_connections(Filters=[{"Name": "requester-vpc-info.vpc-id", "Values": [vpc["id"]]}], **conn) # Assuming that there will be no duplicates: peer_ids = [] for peering in accepter_result + requester_result: peer_ids.append(peering["VpcPeeringConnectionId"]) return peer_ids
def get_subnets(vpc, **conn): """Gets the VPC Subnets""" subnets = describe_subnets(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn) s_ids = [] for s in subnets: s_ids.append(s["SubnetId"]) return s_ids
def get_route_tables(vpc, **conn): """Gets the VPC Route Tables""" route_tables = describe_route_tables(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn) rt_ids = [] for r in route_tables: rt_ids.append(r["RouteTableId"]) return rt_ids
def get_network_acls(vpc, **conn): """Gets the VPC Network ACLs""" route_tables = describe_network_acls(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn) nacl_ids = [] for r in route_tables: nacl_ids.append(r["NetworkAclId"]) return nacl_ids
def get_base(vpc, **conn): """ The base will return: - ARN - Region - Name - Id - Tags - IsDefault - InstanceTenancy - CidrBlock - CidrBlockAssociationSet - Ipv6CidrBlockAssociationSet - DhcpOptionsId - Attributes - _version :param bucket_name: :param conn: :return: """ # Get the base: base_result = describe_vpcs(VpcIds=[vpc["id"]], **conn)[0] # The name of the VPC is in the tags: vpc_name = None for t in base_result.get("Tags", []): if t["Key"] == "Name": vpc_name = t["Value"] dhcp_opts = None # Get the DHCP Options: if base_result.get("DhcpOptionsId"): # There should only be exactly 1 attached to a VPC: dhcp_opts = describe_dhcp_options(DhcpOptionsIds=[base_result["DhcpOptionsId"]], **conn)[0]["DhcpOptionsId"] # Get the Attributes: attributes = {} attr_vals = [ ("EnableDnsHostnames", "enableDnsHostnames"), ("EnableDnsSupport", "enableDnsSupport") ] for attr, query in attr_vals: attributes[attr] = describe_vpc_attribute(VpcId=vpc["id"], Attribute=query, **conn)[attr] vpc.update({ 'name': vpc_name, 'region': conn["region"], 'tags': base_result.get("Tags", []), 'is_default': base_result["IsDefault"], 'instance_tenancy': base_result["InstanceTenancy"], 'dhcp_options_id': dhcp_opts, 'cidr_block': base_result["CidrBlock"], 'cidr_block_association_set': base_result.get("CidrBlockAssociationSet", []), 'ipv6_cidr_block_association_set': base_result.get("Ipv6CidrBlockAssociationSet", []), 'attributes': attributes, '_version': 1 }) return vpc
def get_vpc(vpc_id, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully fetch details about a VPC: { "Arn": ..., "Region": ..., "Name": ..., "Id": ..., "Tags: ..., "VpcPeeringConnections": ..., "ClassicLink": ..., "DhcpOptionsId": ..., "InternetGateway": ..., "IsDefault": ..., "CidrBlock": ..., "CidrBlockAssociationSet": ..., "Ipv6CidrBlockAssociationSet": ..., "InstanceTenancy": ..., "RouteTables": ..., "NetworkAcls": ..., "FlowLogs": ..., "Subnets": ..., "Attributes": ..., "FlowLogs": ..., "_version": 1 } :param vpc_id: The ID of the VPC :param flags: :param conn: :return: """ # Is the account number that's passed in the same as in the connection dictionary? if not conn.get("account_number"): raise CloudAuxException({"message": "Must supply account number in the connection dict to construct " "the VPC ARN.", "vpc_id": vpc_id}) if not conn.get("region"): raise CloudAuxException({"message": "Must supply region in the connection dict to construct " "the VPC ARN.", "vpc_id": vpc_id}) start = { 'arn': "arn:aws:ec2:{region}:{account}:vpc/{vpc_id}".format(region=conn["region"], account=conn["account_number"], vpc_id=vpc_id), 'id': vpc_id } return registry.build_out(flags, start_with=start, pass_datastructure=True, **conn)
def get_client(service, service_type='client', **conn_args): """ User function to get the correct client. Based on the GOOGLE_CLIENT_MAP dictionary, the return will be a cloud or general client that can interact with the desired service. :param service: GCP service to connect to. E.g. 'gce', 'iam' :type service: ``str`` :param conn_args: Dictionary of connection arguments. 'project' is required. 'user_agent' can be specified and will be set in the client returned. :type conn_args: ``dict`` :return: client_details, client :rtype: ``tuple`` of ``dict``, ``object`` """ client_details = choose_client(service) user_agent = get_user_agent(**conn_args) if client_details: if client_details['client_type'] == 'cloud': client = get_gcp_client( mod_name=client_details['module_name'], pkg_name=conn_args.get('pkg_name', 'google.cloud'), key_file=conn_args.get('key_file', None), project=conn_args['project'], user_agent=user_agent) else: client = get_google_client( mod_name=client_details['module_name'], key_file=conn_args.get('key_file', None), user_agent=user_agent, api_version=conn_args.get('api_version', 'v1')) else: # There is no client known for this service. We can try the standard API. try: client = get_google_client( mod_name=service, key_file=conn_args.get('key_file', None), user_agent=user_agent, api_version=conn_args.get('api_version', 'v1')) except Exception as e: raise e return client_details, client
def get_gcp_client(**kwargs): """Public GCP client builder.""" return _gcp_client(project=kwargs['project'], mod_name=kwargs['mod_name'], pkg_name=kwargs.get('pkg_name', 'google.cloud'), key_file=kwargs.get('key_file', None), http_auth=kwargs.get('http', None), user_agent=kwargs.get('user_agent', None))
def _gcp_client(project, mod_name, pkg_name, key_file=None, http_auth=None, user_agent=None): """ Private GCP client builder. :param project: Google Cloud project string. :type project: ``str`` :param mod_name: Module name to load. Should be found in sys.path. :type mod_name: ``str`` :param pkg_name: package name that mod_name is part of. Default is 'google.cloud' . :type pkg_name: ``str`` :param key_file: Default is None. :type key_file: ``str`` or None :param http_auth: httplib2 authorized client. Default is None. :type http_auth: :class: `HTTPLib2` :param user_agent: User Agent string to use in requests. Default is None. :type http_auth: ``str`` or None :return: GCP client :rtype: ``object`` """ client = None if http_auth is None: http_auth = _googleauth(key_file=key_file, user_agent=user_agent) try: # Using a relative path, so we prefix with a dot (.) google_module = importlib.import_module('.' + mod_name, package=pkg_name) client = google_module.Client(use_GAX=USE_GAX, project=project, http=http_auth) except ImportError as ie: import_err = 'Unable to import %s.%s' % (pkg_name, mod_name) raise ImportError(import_err) except TypeError: # Not all clients use gRPC client = google_module.Client(project=project, http=http_auth) if user_agent and hasattr(client, 'user_agent'): client.user_agent = user_agent return client
def _googleauth(key_file=None, scopes=[], user_agent=None): """ Google http_auth helper. If key_file is not specified, default credentials will be used. If scopes is specified (and key_file), will be used instead of DEFAULT_SCOPES :param key_file: path to key file to use. Default is None :type key_file: ``str`` :param scopes: scopes to set. Default is DEFAUL_SCOPES :type scopes: ``list`` :param user_agent: User Agent string to use in requests. Default is None. :type http_auth: ``str`` or None :return: HTTPLib2 authorized client. :rtype: :class: `HTTPLib2` """ if key_file: if not scopes: scopes = DEFAULT_SCOPES creds = ServiceAccountCredentials.from_json_keyfile_name(key_file, scopes=scopes) else: creds = GoogleCredentials.get_application_default() http = Http() if user_agent: http = set_user_agent(http, user_agent) http_auth = creds.authorize(http) return http_auth
def _build_google_client(service, api_version, http_auth): """ Google build client helper. :param service: service to build client for :type service: ``str`` :param api_version: API version to use. :type api_version: ``str`` :param http_auth: Initialized HTTP client to use. :type http_auth: ``object`` :return: google-python-api client initialized to use 'service' :rtype: ``object`` """ client = build(service, api_version, http=http_auth) return client
def gcp_conn(service, service_type='client', future_expiration_minutes=15): """ service_type: not currently used. """ def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): # Import here to avoid circular import issue from cloudaux.gcp.auth import get_client (conn_args, kwargs) = get_creds_from_kwargs(kwargs) client_details, client = get_client( service, service_type=service_type, future_expiration_minutes=15, **conn_args) if client_details: kwargs = rewrite_kwargs(client_details['client_type'], kwargs, client_details['module_name']) kwargs['client'] = client return f(*args, **kwargs) return decorated_function return decorator
def gcp_stats(): """ Collect stats Specifically, time function calls :returns: function response :rtype: varies """ def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): start_time = time.time() result = f(*args, **kwargs) end_time = time.time() strkey = _build_key(f.__name__, args, kwargs) _GCP_STATS.setdefault(strkey, []).append(end_time - start_time) return result return decorated_function return decorator
def gcp_cache(future_expiration_minutes=15): """ Cache function output :param future_expiration_minutes: Number of minutes in the future until item expires. Default is 15. :returns: function response, optionally from the cache :rtype: varies """ def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): strkey = _build_key(f.__name__, args, kwargs) cached_result = _GCP_CACHE.get(strkey) if cached_result: return cached_result else: result = f(*args, **kwargs) _GCP_CACHE.insert(strkey, result, future_expiration_minutes) return result return decorated_function return decorator
def iter_project(projects, key_file=None): """ Call decorated function for each item in project list. Note: the function 'decorated' is expected to return a value plus a dictionary of exceptions. If item in list is a dictionary, we look for a 'project' and 'key_file' entry, respectively. If item in list is of type string_types, we assume it is the project string. Default credentials will be used by the underlying client library. :param projects: list of project strings or list of dictionaries Example: {'project':..., 'keyfile':...}. Required. :type projects: ``list`` of ``str`` or ``list`` of ``dict`` :param key_file: path on disk to keyfile, for use with all projects :type key_file: ``str`` :returns: tuple containing a list of function output and an exceptions map :rtype: ``tuple of ``list``, ``dict`` """ def decorator(func): @wraps(func) def decorated_function(*args, **kwargs): item_list = [] exception_map = {} for project in projects: if isinstance(project, string_types): kwargs['project'] = project if key_file: kwargs['key_file'] = key_file elif isinstance(project, dict): kwargs['project'] = project['project'] kwargs['key_file'] = project['key_file'] itm, exc = func(*args, **kwargs) item_list.extend(itm) exception_map.update(exc) return (item_list, exception_map) return decorated_function return decorator
def get_creds_from_kwargs(kwargs): """Helper to get creds out of kwargs.""" creds = { 'key_file': kwargs.pop('key_file', None), 'http_auth': kwargs.pop('http_auth', None), 'project': kwargs.get('project', None), 'user_agent': kwargs.pop('user_agent', None), 'api_version': kwargs.pop('api_version', 'v1') } return (creds, kwargs)
def rewrite_kwargs(conn_type, kwargs, module_name=None): """ Manipulate connection keywords. Modifieds keywords based on connection type. There is an assumption here that the client has already been created and that these keywords are being passed into methods for interacting with various services. Current modifications: - if conn_type is not cloud and module is 'compute', then rewrite project as name. - if conn_type is cloud and module is 'storage', then remove 'project' from dict. :param conn_type: E.g. 'cloud' or 'general' :type conn_type: ``str`` :param kwargs: Dictionary of keywords sent in by user. :type kwargs: ``dict`` :param module_name: Name of specific module that will be loaded. Default is None. :type conn_type: ``str`` or None :returns kwargs with client and module specific changes :rtype: ``dict`` """ if conn_type != 'cloud' and module_name != 'compute': if 'project' in kwargs: kwargs['name'] = 'projects/%s' % kwargs.pop('project') if conn_type == 'cloud' and module_name == 'storage': if 'project' in kwargs: del kwargs['project'] return kwargs
def gce_list_aggregated(service=None, key_name='name', **kwargs): """General aggregated list function for the GCE service.""" resp_list = [] req = service.aggregatedList(**kwargs) while req is not None: resp = req.execute() for location, item in resp['items'].items(): if key_name in item: resp_list.extend(item[key_name]) req = service.aggregatedList_next(previous_request=req, previous_response=resp) return resp_list
def gce_list(service=None, **kwargs): """General list function for the GCE service.""" resp_list = [] req = service.list(**kwargs) while req is not None: resp = req.execute() for item in resp.get('items', []): resp_list.append(item) req = service.list_next(previous_request=req, previous_response=resp) return resp_list
def service_list(service=None, key_name=None, **kwargs): """General list function for Google APIs.""" resp_list = [] req = service.list(**kwargs) while req is not None: resp = req.execute() if key_name and key_name in resp: resp_list.extend(resp[key_name]) else: resp_list.append(resp) # Not all list calls have a list_next if hasattr(service, 'list_next'): req = service.list_next(previous_request=req, previous_response=resp) else: req = None return resp_list
def get_cache_access_details(key=None): """Retrieve detailed cache information.""" from cloudaux.gcp.decorators import _GCP_CACHE return _GCP_CACHE.get_access_details(key=key)
def get_user_agent_default(pkg_name='cloudaux'): """ Get default User Agent String. Try to import pkg_name to get an accurate version number. return: string """ version = '0.0.1' try: import pkg_resources version = pkg_resources.get_distribution(pkg_name).version except pkg_resources.DistributionNotFound: pass except ImportError: pass return 'cloudaux/%s' % (version)
def get_elbv2(alb, flags=FLAGS.ALL, **conn): """ Fully describes an ALB (ELBv2). :param alb: Could be an ALB Name, ALB ARN, or a dictionary. Likely the return value from a previous call to describe_load_balancers. At a minimum, must contain a key titled 'LoadBalancerArn'. :param flags: Flags describing which sections should be included in the return value. Default is FLAGS.ALL. :return: Returns a dictionary describing the ALB with the fields described in the flags parameter. """ # Python 2 and 3 support: try: basestring except NameError as _: basestring = str if isinstance(alb, basestring): from cloudaux.orchestration.aws.arn import ARN alb_arn = ARN(alb) if alb_arn.error: alb = dict(LoadBalancerName=alb) else: alb = dict(LoadBalancerArn=alb) return registry.build_out(flags, start_with=alb, pass_datastructure=True, **conn)
def get_event(rule, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully build out a CloudWatch Event Rule in the following format: { "Arn": ..., "Name": ..., "Region": ..., "Description": ..., "State": ..., "Rule": ..., "Targets" ..., "_version": 1 } :param rule: str cloudwatch event name :param flags: By default, set to ALL fields :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing a fully built out event rule with targets. """ # Python 2 and 3 support: try: basestring except NameError as _: basestring = str # If string is passed in, determine if it's a name or ARN. Build a dict. if isinstance(rule, basestring): rule_arn = ARN(rule) if rule_arn.error: rule_name = rule else: rule_name = rule_arn.name rule = describe_rule(Name=rule_name, **conn) return registry.build_out(flags, rule, **conn)
def list_rules(client=None, **kwargs): """ NamePrefix='string' """ result = client.list_rules(**kwargs) if not result.get("Rules"): result.update({"Rules": []}) return result
def list_targets_by_rule(client=None, **kwargs): """ Rule='string' """ result = client.list_targets_by_rule(**kwargs) if not result.get("Targets"): result.update({"Targets": []}) return result
def get_image(image_id, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully build out an EC2 Image (AMI, AKI, ARI) { "Architecture": "x86_64", "Arn": "arn:aws:ec2:us-east-1::image/ami-11111111", "BlockDeviceMappings": [], "CreationDate": "2013-07-11T16:04:06.000Z", "Description": "...", "Hypervisor": "xen", "ImageId": "ami-11111111", "ImageLocation": "111111111111/...", "ImageType": "machine", "KernelId": "aki-88888888", "LaunchPermissions": [], "Name": "...", "OwnerId": "111111111111", "ProductCodes": [], "Public": false, "RamdiskId": {}, "RootDeviceName": "/dev/sda1", "RootDeviceType": "ebs", "SriovNetSupport": "simple", "State": "available", "Tags": [], "VirtualizationType": "hvm", "_version": 1 } :param image_id: str ami id :param flags: By default, set to ALL fields :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing a fully built out image. """ image = dict(ImageId=image_id) conn['region'] = conn.get('region', 'us-east-1') return registry.build_out(flags, image, **conn)
def call(self, function_expr, **kwargs): """ cloudaux = CloudAux( **{'account_number': '000000000000', 'assume_role': 'role_name', 'session_name': 'testing', 'region': 'us-east-1', 'tech': 'kms', 'service_type': 'client' }) cloudaux.call("list_aliases") cloudaux.call("kms.client.list_aliases") """ if '.' in function_expr: tech, service_type, function_name = function_expr.split('.') else: tech = self.conn_details.get('tech') service_type = self.conn_details.get('service_type', 'client') function_name = function_expr @sts_conn(tech, service_type=service_type) def wrapped_method(function_name, **nargs): service_type = nargs.pop(nargs.pop('service_type', 'client')) return getattr(service_type, function_name)(**nargs) kwargs.update(self.conn_details) if 'tech' in kwargs: del kwargs['tech'] return wrapped_method(function_name, **kwargs)
def go(function_expr, **kwargs): """ CloudAux.go( 'list_aliases', **{ 'account_number': '000000000000', 'assume_role': 'role_name', 'session_name': 'cloudaux', 'region': 'us-east-1', 'tech': 'kms', 'service_type': 'client' }) CloudAux.go( 'kms.client.list_aliases', **{ 'account_number': '000000000000', 'assume_role': 'role_name', 'session_name': 'cloudaux', 'region': 'us-east-1' }) """ if '.' in function_expr: tech, service_type, function_name = function_expr.split('.') else: tech = kwargs.pop('tech') service_type = kwargs.get('service_type') function_name = function_expr @sts_conn(tech, service_type=service_type) def wrapped_method(function_name, **nargs): service_type = nargs.pop(nargs.pop('service_type', 'client')) return getattr(service_type, function_name)(**nargs) return wrapped_method(function_name, **kwargs)
def list_buckets(client=None, **kwargs): """ List buckets for a project. :param client: client object to use. :type client: Google Cloud Storage client :returns: list of dictionary reprsentation of Bucket :rtype: ``list`` of ``dict`` """ buckets = client.list_buckets(**kwargs) return [b.__dict__ for b in buckets]
def list_objects_in_bucket(**kwargs): """ List objects in bucket. :param Bucket: name of bucket :type Bucket: ``str`` :returns list of objects in bucket :rtype: ``list`` """ bucket = get_bucket(**kwargs) if bucket: return [o for o in bucket.list_blobs()] else: return None
def _modify(item, func): """ Modifies each item.keys() string based on the func passed in. Often used with inflection's camelize or underscore methods. :param item: dictionary representing item to be modified :param func: function to run on each key string :return: dictionary where each key has been modified by func. """ result = dict() for key in item: result[func(key)] = item[key] return result
def modify(item, output='camelized'): """ Calls _modify and either passes the inflection.camelize method or the inflection.underscore method. :param item: dictionary representing item to be modified :param output: string 'camelized' or 'underscored' :return: """ if output == 'camelized': return _modify(item, camelize) elif output == 'underscored': return _modify(item, underscore)
def get_role_managed_policy_documents(role, client=None, **kwargs): """Retrieve the currently active policy version document for every managed policy that is attached to the role.""" policies = get_role_managed_policies(role, force_client=client) policy_names = (policy['name'] for policy in policies) delayed_gmpd_calls = (delayed(get_managed_policy_document)(policy['arn'], force_client=client) for policy in policies) policy_documents = Parallel(n_jobs=20, backend="threading")(delayed_gmpd_calls) return dict(zip(policy_names, policy_documents))
def get_managed_policy_document(policy_arn, policy_metadata=None, client=None, **kwargs): """Retrieve the currently active (i.e. 'default') policy version document for a policy. :param policy_arn: :param policy_metadata: This is a previously fetch managed policy response from boto/cloudaux. This is used to prevent unnecessary API calls to get the initial policy default version id. :param client: :param kwargs: :return: """ if not policy_metadata: policy_metadata = client.get_policy(PolicyArn=policy_arn) policy_document = client.get_policy_version(PolicyArn=policy_arn, VersionId=policy_metadata['Policy']['DefaultVersionId']) return policy_document['PolicyVersion']['Document']
def get_group(group_name, users=True, client=None, **kwargs): """Get's the IAM Group details. :param group_name: :param users: Optional -- will return the IAM users that the group is attached to if desired (paginated). :param client: :param kwargs: :return: """ # First, make the initial call to get the details for the group: result = client.get_group(GroupName=group_name, **kwargs) # If we care about the user details, then fetch them: if users: if result.get('IsTruncated'): kwargs_to_send = {'GroupName': group_name} kwargs_to_send.update(kwargs) user_list = result['Users'] kwargs_to_send['Marker'] = result['Marker'] result['Users'] = user_list + _get_users_for_group(client, **kwargs_to_send) else: result.pop('Users', None) result.pop('IsTruncated', None) result.pop('Marker', None) return result
def get_group_policy_document(group_name, policy_name, client=None, **kwargs): """Fetches the specific IAM group inline-policy document.""" return client.get_group_policy(GroupName=group_name, PolicyName=policy_name, **kwargs)['PolicyDocument']
def _get_base(server_certificate, **conn): """Fetch the base IAM Server Certificate.""" server_certificate['_version'] = 1 # Get the initial cert details: cert_details = get_server_certificate_api(server_certificate['ServerCertificateName'], **conn) if cert_details: server_certificate.update(cert_details['ServerCertificateMetadata']) server_certificate['CertificateBody'] = cert_details['CertificateBody'] server_certificate['CertificateChain'] = cert_details.get('CertificateChain', None) # Cast dates from a datetime to something JSON serializable. server_certificate['UploadDate'] = get_iso_string(server_certificate['UploadDate']) server_certificate['Expiration'] = get_iso_string(server_certificate['Expiration']) return server_certificate
def get_server_certificate(server_certificate, flags=FLAGS.BASE, **conn): """ Orchestrates all the calls required to fully build out an IAM User in the following format: { "Arn": ..., "ServerCertificateName": ..., "Path": ..., "ServerCertificateId": ..., "UploadDate": ..., # str "Expiration": ..., # str "CertificateBody": ..., "CertificateChain": ..., "_version": 1 } :param flags: By default, Users is disabled. This is somewhat expensive as it has to call the `get_server_certificate` call multiple times. :param server_certificate: dict MUST contain the ServerCertificateName and also a combination of either the ARN or the account_number. :param output: Determines whether keys should be returned camelized or underscored. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing fully built out Server Certificate. """ if not server_certificate.get('ServerCertificateName'): raise MissingFieldException('Must include ServerCertificateName.') server_certificate = modify(server_certificate, output='camelized') _conn_from_args(server_certificate, conn) return registry.build_out(flags, start_with=server_certificate, pass_datastructure=True, **conn)
def get_item(item, **kwargs): """ API versioning for each OpenStack service is independent. Generically capture the public members (non-routine and non-private) of the OpenStack SDK objects. Note the lack of the modify_output decorator. Preserving the field naming allows us to reconstruct objects and orchestrate from stored items. """ _item = {} for k,v in inspect.getmembers(item, lambda a:not(inspect.isroutine(a))): if not k.startswith('_') and not k in ignore_list: _item[k] = v return sub_dict(_item)
def sub_list(l): """ Recursively walk a data-structure sorting any lists along the way. Any unknown types get mapped to string representation :param l: list :return: sorted list, where any child lists are also sorted. """ r = [] for i in l: if type(i) in prims: r.append(i) elif type(i) is list: r.append(sub_list(i)) elif type(i) is dict: r.append(sub_dict(i)) else: r.append(str(i)) r = sorted(r) return r
def sub_dict(d): """ Recursively walk a data-structure sorting any lists along the way. Any unknown types get mapped to string representation :param d: dict :return: dict where any lists, even those buried deep in the structure, have been sorted. """ r = {} for k in d: if type(d[k]) in prims: r[k] = d[k] elif type(d[k]) is list: r[k] = sub_list(d[k]) elif type(d[k]) is dict: r[k] = sub_dict(d[k]) else: r[k] = str(d[k]) return r
def boto3_cached_conn(service, service_type='client', future_expiration_minutes=15, account_number=None, assume_role=None, session_name='cloudaux', region='us-east-1', return_credentials=False, external_id=None, arn_partition='aws'): """ Used to obtain a boto3 client or resource connection. For cross account, provide both account_number and assume_role. :usage: # Same Account: client = boto3_cached_conn('iam') resource = boto3_cached_conn('iam', service_type='resource') # Cross Account Client: client = boto3_cached_conn('iam', account_number='000000000000', assume_role='role_name') # Cross Account Resource: resource = boto3_cached_conn('iam', service_type='resource', account_number='000000000000', assume_role='role_name') :param service: AWS service (i.e. 'iam', 'ec2', 'kms') :param service_type: 'client' or 'resource' :param future_expiration_minutes: Connections will expire from the cache when their expiration is within this many minutes of the present time. [Default 15] :param account_number: Required if assume_role is provided. :param assume_role: Name of the role to assume into for account described by account_number. :param session_name: Session name to attach to requests. [Default 'cloudaux'] :param region: Region name for connection. [Default us-east-1] :param return_credentials: Indicates if the STS credentials should be returned with the client [Default False] :param external_id: Optional external id to pass to sts:AssumeRole. See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html :param arn_partition: Optional parameter to specify other aws partitions such as aws-us-gov for aws govcloud :return: boto3 client or resource connection """ key = ( account_number, assume_role, session_name, external_id, region, service_type, service, arn_partition ) if key in CACHE: retval = _get_cached_creds(key, service, service_type, region, future_expiration_minutes, return_credentials) if retval: return retval role = None if assume_role: sts = boto3.session.Session().client('sts') # prevent malformed ARN if not all([account_number, assume_role]): raise ValueError("Account number and role to assume are both required") arn = 'arn:{partition}:iam::{0}:role/{1}'.format( account_number, assume_role, partition=arn_partition ) assume_role_kwargs = { 'RoleArn': arn, 'RoleSessionName': session_name } if external_id: assume_role_kwargs['ExternalId'] = external_id role = sts.assume_role(**assume_role_kwargs) if service_type == 'client': conn = _client(service, region, role) elif service_type == 'resource': conn = _resource(service, region, role) if role: CACHE[key] = role if return_credentials: return conn, role['Credentials'] return conn
def sts_conn(service, service_type='client', future_expiration_minutes=15): """ This will wrap all calls with an STS AssumeRole if the required parameters are sent over. Namely, it requires the following in the kwargs: - Service Type (Required) - Account Number (Required for Assume Role) - IAM Role Name (Required for Assume Role) - Region (Optional, but recommended) - AWS Partition (Optional, defaults to 'aws' if none specified) - IAM Session Name (Optional, but recommended to appear in CloudTrail) If `force_client` is set to a boto3 client, then this will simply pass that in as the client. `force_client` is mostly useful for mocks and tests. :param service: :param service_type: :param future_expiration_minutes: :return: """ def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): if kwargs.get("force_client"): kwargs[service_type] = kwargs.pop("force_client") kwargs.pop("account_number", None) kwargs.pop("region", None) else: kwargs[service_type] = boto3_cached_conn( service, service_type=service_type, future_expiration_minutes=future_expiration_minutes, account_number=kwargs.pop('account_number', None), assume_role=kwargs.pop('assume_role', None), session_name=kwargs.pop('session_name', 'cloudaux'), external_id=kwargs.pop('external_id', None), region=kwargs.pop('region', 'us-east-1'), arn_partition=kwargs.pop('arn_partition', 'aws') ) return f(*args, **kwargs) return decorated_function return decorator
def list_bucket_analytics_configurations(client=None, **kwargs): """ Bucket='string' """ result = client.list_bucket_analytics_configurations(**kwargs) if not result.get("AnalyticsConfigurationList"): result.update({"AnalyticsConfigurationList": []}) return result
def list_bucket_metrics_configurations(client=None, **kwargs): """ Bucket='string' """ result = client.list_bucket_metrics_configurations(**kwargs) if not result.get("MetricsConfigurationList"): result.update({"MetricsConfigurationList": []}) return result
def list_bucket_inventory_configurations(client=None, **kwargs): """ Bucket='string' """ result = client.list_bucket_inventory_configurations(**kwargs) if not result.get("InventoryConfigurationList"): result.update({"InventoryConfigurationList": []}) return result
def get_queue(queue, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully fetch details about an SQS Queue: { "Arn": ..., "Region": ..., "Name": ..., "Url": ..., "Attributes": ..., "Tags": ..., "DeadLetterSourceQueues": ..., "_version": 1 } :param queue: Either the queue name OR the queue url :param flags: By default, set to ALL fields. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing a fully built out SQS queue. """ # Check if this is a Queue URL or a queue name: if queue.startswith("https://") or queue.startswith("http://"): queue_name = queue else: queue_name = get_queue_url(QueueName=queue, **conn) sqs_queue = {"QueueUrl": queue_name} return registry.build_out(flags, sqs_queue, **conn)
def get_bucket(bucket_name, include_created=None, flags=FLAGS.ALL ^ FLAGS.CREATED_DATE, **conn): """ Orchestrates all the calls required to fully build out an S3 bucket in the following format: { "Arn": ..., "Name": ..., "Region": ..., "Owner": ..., "Grants": ..., "GrantReferences": ..., "LifecycleRules": ..., "Logging": ..., "Policy": ..., "Tags": ..., "Versioning": ..., "Website": ..., "Cors": ..., "Notifications": ..., "Acceleration": ..., "Replication": ..., "CreationDate": ..., "AnalyticsConfigurations": ..., "MetricsConfigurations": ..., "InventoryConfigurations": ..., "_version": 9 } NOTE: "GrantReferences" is an ephemeral field that is not guaranteed to be consistent -- do not base logic off of it :param include_created: legacy param moved to FLAGS. :param bucket_name: str bucket name :param flags: By default, set to ALL fields except for FLAGS.CREATED_DATE as obtaining that information is a slow and expensive process. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing a fully built out bucket. """ if type(include_created) is bool: # coerce the legacy param "include_created" into the flags param. if include_created: flags = flags | FLAGS.CREATED_DATE else: flags = flags & ~FLAGS.CREATED_DATE region = get_bucket_region(Bucket=bucket_name, **conn) if not region: return dict(Error='Unauthorized') conn['region'] = region return registry.build_out(flags, bucket_name, **conn)
def _get_base(role, **conn): """ Determine whether the boto get_role call needs to be made or if we already have all that data in the role object. :param role: dict containing (at the very least) role_name and/or arn. :param conn: dict containing enough information to make a connection to the desired account. :return: Camelized dict describing role containing all all base_fields. """ base_fields = frozenset(['Arn', 'AssumeRolePolicyDocument', 'Path', 'RoleId', 'RoleName', 'CreateDate']) needs_base = False for field in base_fields: if field not in role: needs_base = True break if needs_base: role_name = _get_name_from_structure(role, 'RoleName') role = CloudAux.go('iam.client.get_role', RoleName=role_name, **conn) role = role['Role'] # cast CreateDate from a datetime to something JSON serializable. role.update(dict(CreateDate=get_iso_string(role['CreateDate']))) role['_version'] = 3 return role
def get_role(role, flags=FLAGS.ALL, **conn): """ Orchestrates all the calls required to fully build out an IAM Role in the following format: { "Arn": ..., "AssumeRolePolicyDocument": ..., "CreateDate": ..., # str "InlinePolicies": ..., "InstanceProfiles": ..., "ManagedPolicies": ..., "Path": ..., "RoleId": ..., "RoleName": ..., "Tags": {}, "_version": 3 } :param role: dict containing (at the very least) role_name and/or arn. :param output: Determines whether keys should be returned camelized or underscored. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing a fully built out role. """ role = modify(role, output='camelized') _conn_from_args(role, conn) return registry.build_out(flags, start_with=role, pass_datastructure=True, **conn)
def get_all_roles(**conn): """ Returns a List of Roles represented as the dictionary below: { "Arn": ..., "AssumeRolePolicyDocument": ..., "CreateDate": ..., # str "InlinePolicies": ..., "InstanceProfiles": ..., "ManagedPolicies": ..., "Path": ..., "RoleId": ..., "RoleName": ..., } :param conn: dict containing enough information to make a connection to the desired account. :return: list containing dicts or fully built out roles """ roles = [] account_roles = get_account_authorization_details('Role', **conn) for role in account_roles: roles.append( { 'Arn': role['Arn'], 'AssumeRolePolicyDocument': role['AssumeRolePolicyDocument'], 'CreateDate': get_iso_string(role['CreateDate']), 'InlinePolicies': role['RolePolicyList'], 'InstanceProfiles': [{ 'path': ip['Path'], 'instance_profile_name': ip['InstanceProfileName'], 'create_date': get_iso_string(ip['CreateDate']), 'instance_profile_id': ip['InstanceProfileId'], 'arn': ip['Arn'] } for ip in role['InstanceProfileList']], 'ManagedPolicies': [ { "name": x['PolicyName'], "arn": x['PolicyArn'] } for x in role['AttachedManagedPolicies'] ], 'Path': role['Path'], 'RoleId': role['RoleId'], 'RoleName': role['RoleName'] } ) return roles
def _get_policy(lambda_function, **conn): """Get LambdaFunction Policies. (there can be many of these!) Lambda Function Policies are overly complicated. They can be attached to a label, a version, and there is also a default policy. This method attempts to gather all three types. AWS returns an exception if the policy requested does not exist. We catch and ignore these exceptions. """ policies = dict(Versions=dict(), Aliases=dict(), DEFAULT=dict()) for version in [v['Version'] for v in lambda_function['versions']]: try: policies['Versions'][version] = get_policy(FunctionName=lambda_function['FunctionName'], Qualifier=version, **conn) policies['Versions'][version] = json.loads(policies['Versions'][version]) except Exception as e: pass for alias in [v['Name'] for v in lambda_function['aliases']]: try: policies['Aliases'][alias] = get_policy(FunctionName=lambda_function['FunctionName'], Qualifier=alias, **conn) policies['Aliases'][alias] = json.loads(policies['Aliases'][alias]) except Exception as e: pass try: policies['DEFAULT'] = get_policy(FunctionName=lambda_function['FunctionName'], **conn) policies['DEFAULT'] = json.loads(policies['DEFAULT']) except Exception as e: pass return policies
def get_lambda_function(lambda_function, flags=FLAGS.ALL, **conn): """Fully describes a lambda function. Args: lambda_function: Name, ARN, or dictionary of lambda function. If dictionary, should likely be the return value from list_functions. At a minimum, must contain a key titled 'FunctionName'. flags: Flags describing which sections should be included in the return value. Default ALL Returns: dictionary describing the requested lambda function. """ # Python 2 and 3 support: try: basestring except NameError as _: basestring = str # If STR is passed in, determine if it's a name or ARN and built a dict. if isinstance(lambda_function, basestring): lambda_function_arn = ARN(lambda_function) if lambda_function_arn.error: lambda_function = dict(FunctionName=lambda_function) else: lambda_function = dict(FunctionName=lambda_function_arn.name, FunctionArn=lambda_function) # If an ARN is available, override the account_number/region from the conn dict. if 'FunctionArn' in lambda_function: lambda_function_arn = ARN(lambda_function['FunctionArn']) if not lambda_function_arn.error: if lambda_function_arn.account_number: conn['account_number'] = lambda_function_arn.account_number if lambda_function_arn.region: conn['region'] = lambda_function_arn.region return registry.build_out(flags, start_with=lambda_function, pass_datastructure=True, **conn)
def list_items(conn=None, **kwargs): """ :rtype: ``list`` """ return [x for x in getattr( getattr( conn, kwargs.pop('service') ), kwargs.pop('generator'))(**kwargs)]
def get_serviceaccount(client=None, **kwargs): """ service_account='string' """ service_account=kwargs.pop('service_account') resp = client.projects().serviceAccounts().get( name=service_account).execute() return resp
def get_serviceaccount_keys(client=None, **kwargs): """ service_account='string' """ service_account=kwargs.pop('service_account') kwargs['name'] = service_account return service_list(client.projects().serviceAccounts().keys(), key_name='keys', **kwargs)
def get_iam_policy(client=None, **kwargs): """ service_account='string' """ service_account=kwargs.pop('service_account') resp = client.projects().serviceAccounts().getIamPolicy( resource=service_account).execute() # TODO(supertom): err handling, check if 'bindings' is correct if 'bindings' in resp: return resp['bindings'] else: return None
def get_vault(vault_obj, flags=FLAGS.ALL, **conn): """ Orchestrates calls to build a Glacier Vault in the following format: { "VaultARN": ..., "VaultName": ..., "CreationDate" ..., "LastInventoryDate" ..., "NumberOfArchives" ..., "SizeInBytes" ..., "Policy" ..., "Tags" ... } Args: vault_obj: name, ARN, or dict of Glacier Vault flags: Flags describing which sections should be included in the return value. Default ALL Returns: dictionary describing the requested Vault """ if isinstance(vault_obj, string_types): vault_arn = ARN(vault_obj) if vault_arn.error: vault_obj = {'VaultName': vault_obj} else: vault_obj = {'VaultName': vault_arn.parsed_name} return registry.build_out(flags, vault_obj, **conn)
def get_rules(security_group, **kwargs): """ format the rule fields to match AWS to support auditor reuse, will need to remap back if we want to orchestrate from our stored items """ rules = security_group.pop('security_group_rules',[]) for rule in rules: rule['ip_protocol'] = rule.pop('protocol') rule['from_port'] = rule.pop('port_range_max') rule['to_port'] = rule.pop('port_range_min') rule['cidr_ip'] = rule.pop('remote_ip_prefix') rule['rule_type'] = rule.pop('direction') security_group['rules'] = sorted(rules) return security_group
def get_security_group(security_group, flags=FLAGS.ALL, **kwargs): result = registry.build_out(flags, start_with=security_group, pass_datastructure=True, **kwargs) """ just store the AWS formatted rules """ result.pop('security_group_rules', []) return result
def _conn_from_arn(arn): """ Extracts the account number from an ARN. :param arn: Amazon ARN containing account number. :return: dictionary with a single account_number key that can be merged with an existing connection dictionary containing fields such as assume_role, session_name, region. """ arn = ARN(arn) if arn.error: raise CloudAuxException('Bad ARN: {arn}'.format(arn=arn)) return dict( account_number=arn.account_number, )
def _get_name_from_structure(item, default): """ Given a possibly sparsely populated item dictionary, try to retrieve the item name. First try the default field. If that doesn't exist, try to parse the from the ARN. :param item: dict containing (at the very least) item_name and/or arn :return: item name """ if item.get(default): return item.get(default) if item.get('Arn'): arn = item.get('Arn') item_arn = ARN(arn) if item_arn.error: raise CloudAuxException('Bad ARN: {arn}'.format(arn=arn)) return item_arn.parsed_name raise MissingFieldException('Cannot extract item name from input: {input}.'.format(input=item))
def describe_load_balancers(arns=None, names=None, client=None): """ Permission: elasticloadbalancing:DescribeLoadBalancers """ kwargs = dict() if arns: kwargs.update(dict(LoadBalancerArns=arns)) if names: kwargs.update(dict(Names=names)) return client.describe_load_balancers(**kwargs)
def describe_listeners(load_balancer_arn=None, listener_arns=None, client=None): """ Permission: elasticloadbalancing:DescribeListeners """ kwargs = dict() if load_balancer_arn: kwargs.update(dict(LoadBalancerArn=load_balancer_arn)) if listener_arns: kwargs.update(dict(ListenerArns=listener_arns)) return client.describe_listeners(**kwargs)
def describe_rules(listener_arn=None, rule_arns=None, client=None): """ Permission: elasticloadbalancing:DescribeRules """ kwargs = dict() if listener_arn: kwargs.update(dict(ListenerArn=listener_arn)) if rule_arns: kwargs.update(dict(RuleArns=rule_arns)) return client.describe_rules(**kwargs)['Rules']
def describe_target_groups(load_balancer_arn=None, target_group_arns=None, names=None, client=None): """ Permission: elasticloadbalancing:DescribeTargetGroups """ kwargs = dict() if load_balancer_arn: kwargs.update(LoadBalancerArn=load_balancer_arn) if target_group_arns: kwargs.update(TargetGroupArns=target_group_arns) if names: kwargs.update(Names=names) return client.describe_target_groups(**kwargs)
def describe_target_health(target_group_arn, targets=None, client=None): """ Permission: elasticloadbalancing:DescribeTargetHealth """ kwargs = dict(TargetGroupArn=target_group_arn) if targets: kwargs.update(Targets=targets) return client.describe_target_health(**kwargs)['TargetHealthDescriptions']
def get_inline_policies(group, **conn): """Get the inline policies for the group.""" policy_list = list_group_policies(group['GroupName']) policy_documents = {} for policy in policy_list: policy_documents[policy] = get_group_policy_document(group['GroupName'], policy, **conn) return policy_documents
def get_managed_policies(group, **conn): """Get a list of the managed policy names that are attached to the group.""" managed_policies = list_attached_group_managed_policies(group['GroupName'], **conn) managed_policy_names = [] for policy in managed_policies: managed_policy_names.append(policy['PolicyName']) return managed_policy_names
def get_users(group, **conn): """Gets a list of the usernames that are a part of this group.""" group_details = get_group_api(group['GroupName'], **conn) user_list = [] for user in group_details.get('Users', []): user_list.append(user['UserName']) return user_list
def _get_base(group, **conn): """Fetch the base IAM Group.""" group['_version'] = 1 # Get the initial group details (only needed if we didn't grab the users): group.update(get_group_api(group['GroupName'], users=False, **conn)['Group']) # Cast CreateDate from a datetime to something JSON serializable. group['CreateDate'] = get_iso_string(group['CreateDate']) return group
def get_group(group, flags=FLAGS.BASE | FLAGS.INLINE_POLICIES | FLAGS.MANAGED_POLICIES, **conn): """ Orchestrates all the calls required to fully build out an IAM Group in the following format: { "Arn": ..., "GroupName": ..., "Path": ..., "GroupId": ..., "CreateDate": ..., # str "InlinePolicies": ..., "ManagedPolicies": ..., # These are just the names of the Managed Policies. "Users": ..., # False by default -- these are just the names of the users. "_version": 1 } :param flags: By default, Users is disabled. This is somewhat expensive as it has to call the `get_group` call multiple times. :param group: dict MUST contain the GroupName and also a combination of either the ARN or the account_number. :param output: Determines whether keys should be returned camelized or underscored. :param conn: dict containing enough information to make a connection to the desired account. Must at least have 'assume_role' key. :return: dict containing fully built out Group. """ if not group.get('GroupName'): raise MissingFieldException('Must include GroupName.') group = modify(group, output='camelized') _conn_from_args(group, conn) return registry.build_out(flags, start_with=group, pass_datastructure=True, **conn)
def get_base(managed_policy, **conn): """Fetch the base Managed Policy. This includes the base policy and the latest version document. :param managed_policy: :param conn: :return: """ managed_policy['_version'] = 1 arn = _get_name_from_structure(managed_policy, 'Arn') policy = get_policy(arn, **conn) document = get_managed_policy_document(arn, policy_metadata=policy, **conn) managed_policy.update(policy['Policy']) managed_policy['Document'] = document # Fix the dates: managed_policy['CreateDate'] = get_iso_string(managed_policy['CreateDate']) managed_policy['UpdateDate'] = get_iso_string(managed_policy['UpdateDate']) return managed_policy
def get_managed_policy(managed_policy, flags=FLAGS.ALL, **conn): """ Orchestrates all of the calls required to fully build out an IAM Managed Policy in the following format: { "Arn": "...", "PolicyName": "...", "PolicyId": "...", "Path": "...", "DefaultVersionId": "...", "AttachmentCount": 123, "PermissionsBoundaryUsageCount": 123, "IsAttachable": ..., "Description": "...", "CreateDate": "...", "UpdateDate": "...", "Document": "...", "_version": 1 } :param managed_policy: dict MUST contain the ARN. :param flags: :param conn: :return: """ _conn_from_args(managed_policy, conn) return registry.build_out(flags, start_with=managed_policy, pass_datastructure=True, **conn)
def _name(named): """Get the name out of an object. This varies based on the type of the input: * the "name" of a string is itself * the "name" of None is itself * the "name" of an object with a property named name is that property - as long as it's a string * otherwise, we raise a ValueError """ if isinstance(named, basestring) or named is None: return named elif hasattr(named, 'name') and isinstance(named.name, basestring): return named.name else: raise ValueError("Can't interpret %s as a name or a configuration object" % named)
def get_version(self): '''obtain the version or just 2.2.x if < 2.3.x Raises: FailedRequestError: If the request fails. ''' if self._version: return self._version url = "{}/about/version.xml".format(self.service_url) resp = self.http_request(url) version = None if resp.status_code == 200: dom = XML(resp.content) resources = dom.findall("resource") for resource in resources: if resource.attrib["name"] == "GeoServer": try: version = resource.find("Version").text break except AttributeError: pass # This will raise an exception if the catalog is not available # If the catalog is available but could not return version information, # it is an old version that does not support that if version is None: # just to inform that version < 2.3.x version = "2.2.x" self._version = version return version
def get_short_version(self): '''obtain the shory geoserver version ''' gs_version = self.get_version() match = re.compile(r'[^\d.]+') return match.sub('', gs_version).strip('.')
def delete(self, config_object, purge=None, recurse=False): """ send a delete request XXX [more here] """ rest_url = config_object.href params = [] # purge deletes the SLD from disk when a style is deleted if purge: params.append("purge=" + str(purge)) # recurse deletes the resource when a layer is deleted. if recurse: params.append("recurse=true") if params: rest_url = rest_url + "?" + "&".join(params) headers = { "Content-type": "application/xml", "Accept": "application/xml" } resp = self.http_request(rest_url, method='delete', headers=headers) if resp.status_code != 200: raise FailedRequestError('Failed to make DELETE request: {}, {}'.format(resp.status_code, resp.text)) self._cache.clear() # do we really need to return anything other than None? return (resp)
def save(self, obj, content_type="application/xml"): """ saves an object to the REST service gets the object's REST location and the data from the object, then POSTS the request. """ rest_url = obj.href data = obj.message() headers = { "Content-type": content_type, "Accept": content_type } logger.debug("{} {}".format(obj.save_method, obj.href)) resp = self.http_request(rest_url, method=obj.save_method.lower(), data=data, headers=headers) if resp.status_code not in (200, 201): raise FailedRequestError('Failed to save to Geoserver catalog: {}, {}'.format(resp.status_code, resp.text)) self._cache.clear() return resp
def get_stores(self, names=None, workspaces=None): ''' Returns a list of stores in the catalog. If workspaces is specified will only return stores in those workspaces. If names is specified, will only return stores that match. names can either be a comma delimited string or an array. Will return an empty list if no stores are found. ''' if isinstance(workspaces, Workspace): workspaces = [workspaces] elif isinstance(workspaces, list) and [w for w in workspaces if isinstance(w, Workspace)]: # nothing pass else: workspaces = self.get_workspaces(names=workspaces) stores = [] for ws in workspaces: ds_list = self.get_xml(ws.datastore_url) cs_list = self.get_xml(ws.coveragestore_url) wms_list = self.get_xml(ws.wmsstore_url) stores.extend([datastore_from_index(self, ws, n) for n in ds_list.findall("dataStore")]) stores.extend([coveragestore_from_index(self, ws, n) for n in cs_list.findall("coverageStore")]) stores.extend([wmsstore_from_index(self, ws, n) for n in wms_list.findall("wmsStore")]) if names is None: names = [] elif isinstance(names, basestring): names = [s.strip() for s in names.split(',') if s.strip()] if stores and names: return ([store for store in stores if store.name in names]) return stores
def get_store(self, name, workspace=None): ''' Returns a single store object. Will return None if no store is found. Will raise an error if more than one store with the same name is found. ''' stores = self.get_stores(workspaces=workspace, names=name) return self._return_first_item(stores)
def create_coveragestore(self, name, workspace=None, path=None, type='GeoTIFF', create_layer=True, layer_name=None, source_name=None, upload_data=False, contet_type="image/tiff"): """ Create a coveragestore for locally hosted rasters. If create_layer is set to true, will create a coverage/layer. layer_name and source_name are only used if create_layer ia enabled. If not specified, the raster name will be used for both. """ if path is None: raise Exception('You must provide a full path to the raster') if layer_name is not None and ":" in layer_name: ws_name, layer_name = layer_name.split(':') allowed_types = [ 'ImageMosaic', 'GeoTIFF', 'Gtopo30', 'WorldImage', 'AIG', 'ArcGrid', 'DTED', 'EHdr', 'ERDASImg', 'ENVIHdr', 'GeoPackage (mosaic)', 'NITF', 'RPFTOC', 'RST', 'VRT' ] if type is None: raise Exception('Type must be declared') elif type not in allowed_types: raise Exception('Type must be one of {}'.format(", ".join(allowed_types))) if workspace is None: workspace = self.get_default_workspace() workspace = _name(workspace) if upload_data is False: cs = UnsavedCoverageStore(self, name, workspace) cs.type = type cs.url = path if path.startswith("file:") else "file:{}".format(path) self.save(cs) if create_layer: if layer_name is None: layer_name = os.path.splitext(os.path.basename(path))[0] if source_name is None: source_name = os.path.splitext(os.path.basename(path))[0] data = "<coverage><name>{}</name><nativeName>{}</nativeName></coverage>".format(layer_name, source_name) url = "{}/workspaces/{}/coveragestores/{}/coverages.xml".format(self.service_url, workspace, name) headers = {"Content-type": "application/xml"} resp = self.http_request(url, method='post', data=data, headers=headers) if resp.status_code != 201: FailedRequestError('Failed to create coverage/layer {} for : {}, {}'.format(layer_name, name, resp.status_code, resp.text)) self._cache.clear() return self.get_resources(names=layer_name, workspaces=workspace)[0] else: data = open(path, 'rb') params = {"configure": "first", "coverageName": name} url = build_url( self.service_url, [ "workspaces", workspace, "coveragestores", name, "file.{}".format(type.lower()) ], params ) headers = {"Content-type": contet_type} resp = self.http_request(url, method='put', data=data, headers=headers) if hasattr(data, "close"): data.close() if resp.status_code != 201: FailedRequestError('Failed to create coverage/layer {} for : {}, {}'.format(layer_name, name, resp.status_code, resp.text)) return self.get_stores(names=name, workspaces=workspace)[0]
def add_granule(self, data, store, workspace=None): '''Harvest/add a granule into an existing imagemosaic''' ext = os.path.splitext(data)[-1] if ext == ".zip": type = "file.imagemosaic" upload_data = open(data, 'rb') headers = { "Content-type": "application/zip", "Accept": "application/xml" } else: type = "external.imagemosaic" upload_data = data if data.startswith("file:") else "file:{data}".format(data=data) headers = { "Content-type": "text/plain", "Accept": "application/xml" } params = dict() workspace_name = workspace if isinstance(store, basestring): store_name = store else: store_name = store.name workspace_name = store.workspace.name if workspace_name is None: raise ValueError("Must specify workspace") url = build_url( self.service_url, [ "workspaces", workspace_name, "coveragestores", store_name, type ], params ) try: resp = self.http_request(url, method='post', data=upload_data, headers=headers) if resp.status_code != 202: FailedRequestError('Failed to add granule to mosaic {} : {}, {}'.format(store, resp.status_code, resp.text)) self._cache.clear() finally: if hasattr(upload_data, "close"): upload_data.close() # maybe return a list of all granules? return None
def delete_granule(self, coverage, store, granule_id, workspace=None): '''Deletes a granule of an existing imagemosaic''' params = dict() workspace_name = workspace if isinstance(store, basestring): store_name = store else: store_name = store.name workspace_name = store.workspace.name if workspace_name is None: raise ValueError("Must specify workspace") url = build_url( self.service_url, [ "workspaces", workspace_name, "coveragestores", store_name, "coverages", coverage, "index/granules", granule_id, ".json" ], params ) # DELETE /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules/<granule_id>.json headers = { "Content-type": "application/json", "Accept": "application/json" } resp = self.http_request(url, method='delete', headers=headers) if resp.status_code != 200: FailedRequestError('Failed to delete granule from mosaic {} : {}, {}'.format(store, resp.status_code, resp.text)) self._cache.clear() # maybe return a list of all granules? return None
def list_granules(self, coverage, store, workspace=None, filter=None, limit=None, offset=None): '''List granules of an imagemosaic''' params = dict() if filter is not None: params['filter'] = filter if limit is not None: params['limit'] = limit if offset is not None: params['offset'] = offset workspace_name = workspace if isinstance(store, basestring): store_name = store else: store_name = store.name workspace_name = store.workspace.name if workspace_name is None: raise ValueError("Must specify workspace") url = build_url( self.service_url, [ "workspaces", workspace_name, "coveragestores", store_name, "coverages", coverage, "index/granules.json" ], params ) # GET /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules.json headers = { "Content-type": "application/json", "Accept": "application/json" } resp = self.http_request(url, headers=headers) if resp.status_code != 200: FailedRequestError('Failed to list granules in mosaic {} : {}, {}'.format(store, resp.status_code, resp.text)) self._cache.clear() return resp.json()
def mosaic_coverages(self, store): '''Returns all coverages in a coverage store''' params = dict() url = build_url( self.service_url, [ "workspaces", store.workspace.name, "coveragestores", store.name, "coverages.json" ], params ) # GET /workspaces/<ws>/coveragestores/<name>/coverages.json headers = { "Content-type": "application/json", "Accept": "application/json" } resp = self.http_request(url, headers=headers) if resp.status_code != 200: FailedRequestError('Failed to get mosaic coverages {} : {}, {}'.format(store, resp.status_code, resp.text)) self._cache.clear() return resp.json()
def publish_featuretype(self, name, store, native_crs, srs=None, jdbc_virtual_table=None, native_name=None): '''Publish a featuretype from data in an existing store''' # @todo native_srs doesn't seem to get detected, even when in the DB # metadata (at least for postgis in geometry_columns) and then there # will be a misconfigured layer if native_crs is None: raise ValueError("must specify native_crs") srs = srs or native_crs feature_type = FeatureType(self, store.workspace, store, name) # because name is the in FeatureType base class, work around that # and hack in these others that don't have xml properties feature_type.dirty['name'] = name feature_type.dirty['srs'] = srs feature_type.dirty['nativeCRS'] = native_crs feature_type.enabled = True feature_type.advertised = True feature_type.title = name if native_name is not None: feature_type.native_name = native_name headers = { "Content-type": "application/xml", "Accept": "application/xml" } resource_url = store.resource_url if jdbc_virtual_table is not None: feature_type.metadata = ({'JDBC_VIRTUAL_TABLE': jdbc_virtual_table}) params = dict() resource_url = build_url( self.service_url, [ "workspaces", store.workspace.name, "datastores", store.name, "featuretypes.xml" ], params ) resp = self.http_request(resource_url, method='post', data=feature_type.message(), headers=headers) if resp.status_code not in (200, 201, 202): FailedRequestError('Failed to publish feature type {} : {}, {}'.format(name, resp.status_code, resp.text)) self._cache.clear() feature_type.fetch() return feature_type
def get_resources(self, names=None, stores=None, workspaces=None): ''' Resources include feature stores, coverage stores and WMS stores, however does not include layer groups. names, stores and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering. Will always return an array. ''' stores = self.get_stores( names = stores, workspaces = workspaces ) resources = [] for s in stores: try: resources.extend(s.get_resources()) except FailedRequestError: continue if names is None: names = [] elif isinstance(names, basestring): names = [s.strip() for s in names.split(',') if s.strip()] if resources and names: return ([resource for resource in resources if resource.name in names]) return resources