Best Python code snippet using localstack_python
provider.py
Source:provider.py
...422 aliases_of_key = kms_backends.get(context.region).get_all_aliases().get(key_id) or []423 for alias_name in aliases_of_key:424 response_aliases.append(425 AliasListEntry(426 AliasArn=kms_alias_arn(alias_name, region_name=context.region),427 AliasName=alias_name,428 TargetKeyId=key_id,429 )430 )431 page, nxt = response_aliases.get_page(432 lambda a: a["AliasName"], next_token=marker, page_size=limit433 )434 return ListAliasesResponse(Aliases=page, NextMarker=nxt, Truncated=nxt is not None)435 def _verify_key_exists(self, key_id):436 try:437 kms_backends[aws_stack.get_region()].describe_key(key_id)438 except Exception:439 raise ValidationError(f"Invalid key ID '{key_id}'")440 def _validate_grant(self, data: Dict):...
arns.py
Source:arns.py
1"""2Functions that will ensure that the ARN is returned if a string is passed.3If it is an object in troposphere, it will return GetAtt(obj, 'Arn') or Ref() depending on4what the object supports for return.5If it is a string, it must either comply to the last part of a ARN or be a full ARN6and match the ARN pattern7"""8import re9from troposphere import (10 AWS_REGION,11 AWS_ACCOUNT_ID12)13from troposphere import (14 ImportValue,15 Parameter,16 GetAtt,17 Sub,18 Ref19)20from troposphere.iam import (21 Role22)23from troposphere.s3 import Bucket24from troposphere.awslambda import Function25from troposphere.kms import (26 Key, Alias27)28from ozone.filters.regexes import (29 S3_ARN_PREFIX, S3_NAME, S3_ARN,30 IAM_ROLE_NAME, IAM_ROLE_ARN,31 LAMBDA_NAME, LAMBDA_ARN,32 LAMBDA_LAYER_VERSION, LAMBDA_LAYER_ARN,33 KMS_KEY_ARN, KMS_KEY_ID,34 KMS_ALIAS, KMS_ALIAS_ARN35)36def s3_bucket(bucket, any_object=False):37 """38 Args:39 bucket: represents the bucket object, or a function40 Returns:41 untouched if one of the functions supported42 string of the full ARN if the bucket name is given43 full ARN if full ARN is given and match S3 bucket ARN pattern44 """45 arn_pat = re.compile(S3_ARN)46 name_pat = re.compile(S3_NAME)47 if isinstance(bucket, (ImportValue, GetAtt, Sub, Ref)):48 return bucket49 elif isinstance(bucket, Parameter):50 if any_object:51 return Sub('arn:aws:s3:::{bucket}/*')52 else:53 return Sub('arn:aws:s3:::{bucket}')54 elif isinstance(bucket, Bucket):55 return GetAtt(bucket, 'Arn')56 elif isinstance(bucket, str):57 if arn_pat.match(bucket):58 return bucket59 elif name_pat.match(bucket):60 if any_object:61 return f'{S3_ARN_PREFIX}{bucket}/*'62 else:63 return f'{S3_ARN_PREFIX}{bucket}'64 else:65 raise ValueError('The S3 ARN must follow', S3_ARN)66 else:67 raise ValueError(68 'The S3 ARN must be computed with a function or follow the pattern',69 S3_ARN70 )71def iam_role(role):72 """73 Args:74 role: represents the role object, or a function75 Returns:76 untouched if one of the functions supported77 string of the full ARN if the role name is given78 full ARN if full ARN is given and match IAM role ARN pattern79 """80 arn_pattern = re.compile(IAM_ROLE_ARN)81 name_pattern = re.compile(IAM_ROLE_NAME)82 if isinstance(role, str):83 if name_pattern.match(role):84 role_arn = Sub(f'arn:aws:iam::${{AWS::AccountId}}:role/{role}')85 elif role.startswith('arn:aws:iam::') and arn_pattern.match(role):86 role_arn = role87 else:88 raise ValueError(89 'Role ARN must follow either the name or full arn patterns',90 IAM_ROLE_NAME,91 IAM_ROLE_ARN92 )93 elif isinstance(role, (Parameter, Role)):94 role_arn = GetAtt(role, 'Arn')95 elif isinstance(role, (GetAtt, Sub, Ref, ImportValue)):96 role_arn = role97 else:98 raise TypeError('role expected to be of type', str, ImportValue, Role, Sub, GetAtt, Ref)99 return role_arn100def lambda_function(function):101 """102 Args:103 function: represents the function object, or a function104 Returns:105 untouched if one of the functions supported106 string of the full ARN if the function name is given107 full ARN if full ARN is given and match function ARN pattern108 """109 arn_pattern = re.compile(LAMBDA_ARN)110 name_pattern = re.compile(LAMBDA_NAME)111 if isinstance(function, str):112 if name_pattern.match(function):113 function_arn = Sub(f'arn:aws:lambda:${{AWS::Region}}:${{AWS::AccountId}}:function:{function}')114 elif function.startswith('arn:aws:lambda:') and arn_pattern.match(function):115 function_arn = function116 else:117 raise ValueError(118 'Function ARN must follow either the name or full arn patterns',119 LAMBDA_NAME,120 LAMBDA_ARN121 )122 elif isinstance(function, (Parameter, Function)):123 function_arn = GetAtt(function, 'Arn')124 elif isinstance(function, (ImportValue, GetAtt, Sub, Ref)):125 function_arn = function126 else:127 raise TypeError('Function expected to be of type', str, Role, Sub, GetAtt, Ref, ImportValue)128 return function_arn129def lambda_layer(layer):130 """131 Args:132 layer: represents the layer object, or a function133 Returns:134 untouched if one of the functions supported135 string of the full ARN if the layer name is given136 full ARN if full ARN is given and match Lambda layer ARN pattern137 """138 arn_pattern = re.compile(LAMBDA_LAYER_ARN)139 version_pattern = re.compile(LAMBDA_LAYER_VERSION)140 if isinstance(layer, (GetAtt, Ref, Sub, ImportValue)):141 return layer142 elif isinstance(layer, str):143 if arn_pattern.match(layer):144 return layer145 elif version_pattern.match(layer):146 return Sub(f'arn:aws:lambda:${{AWS::Region}}:${{AWS::AccountId}}:layer:{layer}')147 else:148 raise ValueError(149 "Layer ARN expected of format"150 f"{LAMBDA_LAYER_ARN} or {LAMBDA_LAYER_VERSION}"151 )152 else:153 raise ValueError(154 'Layer does not comply to any required patterns of Functions'155 )156def kms_key(key):157 """158 Args:159 key: represents the key object, or a function160 Returns:161 untouched if one of the functions supported162 string of the full ARN if the key name is given163 full ARN if full ARN is given and match KMS key ARN pattern164 """165 arn_pattern = re.compile(KMS_KEY_ARN)166 id_pattern = re.compile(KMS_KEY_ID)167 if isinstance(key, (Ref, Sub, ImportValue, GetAtt)):168 return key169 if isinstance(key, (Parameter, Key)):170 return GetAtt(key, 'Arn')171 if isinstance(key, str):172 if arn_pattern.match(key):173 return key174 if id_pattern.match(key):175 return Sub(f'arn:aws:kms:${{AWS::Region}}:${{AWS::AccountId}}:key/{key}')176 else:177 raise ValueError('Key does not match pattern', KMS_KEY_ARN, KMS_KEY_ID)178def kms_alias(alias):179 """180 Args:181 alias: represents the alias object, or a function182 Returns:183 untouched if one of the functions supported184 string of the full ARN if the alias name is given185 full ARN if full ARN is given and match KMS Key alias ARN pattern186 """187 arn_pattern = re.compile(KMS_ALIAS_ARN)188 alias_pattern = re.compile(KMS_ALIAS)189 if isinstance(alias, (Ref, Sub, ImportValue, GetAtt)):190 return alias191 if isinstance(alias, (Parameter, Alias)):192 return GetAtt(alias, 'Arn')193 if isinstance(alias, str):194 if arn_pattern.match(alias):195 return alias196 if alias_pattern.match(alias):197 return Sub(f'arn:aws:kms:${{AWS::Region}}:${{AWS::AccountId}}:{alias}')198 else:...
regexes.py
Source:regexes.py
1S3_PATH_URL = r's3:\/\/([a-z0-9A-Z-]+)\/([\x00-\x7F][^\n]+$)'2S3_ARN_PREFIX = 'arn:aws:s3:::'3S3_NAME = r'[a-z0-9-]+'4S3_ARN = r'^(arn:aws:s3:::[a-z-]+)$'5OU_PATH = r'^([^\/][A-Za-z0-9\/]+[^\/]$)$|^(\/root)$|^([a-zA-Z0-9]+)$'6IAM_ROLE_NAME = r'^[a-zA-Z0-9-_]+$'7IAM_ROLE_ARN = r'^(arn:aws:iam::[0-9]{12}:role\/[a-zA-Z0-9-_]+$)'8LAMBDA_NAME = IAM_ROLE_NAME9LAMBDA_ARN = r'^(arn:aws:lambda:[a-z]{2}-[a-z]{1,12}-[0-9]{1}:[0-9]{12}:function\/[a-zA-Z0-9]+)$'10LAMBDA_LAYER_VERSION = r'(^[a-z]+:[0-9]+$)'11LAMBDA_LAYER_ARN = r'(^arn:aws:lambda:[a-z]{2}-[a-z]{1,12}-[0-9]{1}:[0-9]{12}:layer:[a-zA-Z0-9]+:[0-9]{1,10})'12KMS_KEY_ID = r'^([a-z0-9]{8}(-[a-z0-9-]{4}){3}-[a-z0-9]+)$'13KMS_KEY_ARN = r'^(arn:aws:kms:[a-z]{2}-[a-z]{1,10}-[0-9]{1}:[0-9]{12}:key\/[a-z0-9]{8}(-[a-z0-9-]{4}){3}-[a-z0-9]+)$'14KMS_ALIAS = r'(^(alias/)([a-zA-Z0-9-_/]+)$)'...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!