How to use sanitize_arn_pattern method in localstack

Best Python code snippet using localstack_python

mocking.py

Source:mocking.py Github

copy

Full Screen

...114 pattern = pattern.replace("\\p{M}", "[`]")115 pattern = pattern.replace("\\p{IsLetter}", "[a-zA-Z]")116 pattern = pattern.replace("[:alnum:]", "[a-zA-Z0-9]")117 return pattern118def sanitize_arn_pattern(pattern: str) -> str:119 # clown emoji120 # some devs were just lazy ...121 if pattern in [122 ".*",123 "arn:.*",124 "arn:.+",125 "^arn:.+",126 "arn:aws.*:*",127 "^arn:aws.*",128 "^arn:.*",129 ".*\\S.*",130 "^[A-Za-z0-9:\\/_-]*$",131 "^arn[\\/\\:\\-\\_\\.a-zA-Z0-9]+$",132 ".{0,1600}",133 "^arn:[!-~]+$",134 "[\\S]+",135 "[\\s\\S]*",136 "^([\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*)$",137 "[a-zA-Z0-9_:\\-\\/]+",138 ]:139 pattern = "arn:aws:[a-z]{4}:us-east-1:[0-9]{12}:[a-z]{8}"140 # common pattern to describe a partition141 pattern = pattern.replace("arn:[^:]*:", "arn:aws:")142 pattern = pattern.replace("arn:[a-z\\d-]+", "arn:aws")143 pattern = pattern.replace("arn:[\\w+=\\/,.@-]+", "arn:aws")144 pattern = pattern.replace("arn:[a-z-]+?", "arn:aws")145 pattern = pattern.replace("arn:[a-z0-9][-.a-z0-9]{0,62}", "arn:aws")146 pattern = pattern.replace(":aws(-\\w+)*", ":aws")147 pattern = pattern.replace(":aws[a-z\\-]*", ":aws")148 pattern = pattern.replace(":aws(-[\\w]+)*", ":aws")149 pattern = pattern.replace(":aws[^:\\s]*", ":aws")150 pattern = pattern.replace(":aws[A-Za-z0-9-]{0,64}", ":aws")151 # often the account-id152 pattern = pattern.replace(":[0-9]+:", ":[0-9]{13}:")153 pattern = pattern.replace(":\\w{12}:", ":[0-9]{13}:")154 # substitutions155 pattern = pattern.replace("[a-z\\-\\d]", "[a-z0-9]")156 pattern = pattern.replace(157 "[\\u0020-\\uD7FF\\uE000-\\uFFFD\\uD800\\uDC00-\\uDBFF\\uDFFF\\r\\n\\t]", "[a-z0-9]"158 )159 pattern = pattern.replace("[\\w\\d-]", "[a-z0-9]")160 pattern = pattern.replace("[\\w+=/,.@-]", "[a-z]")161 pattern = pattern.replace("[^:]", "[a-z]")162 pattern = pattern.replace("[^/]", "[a-z]")163 pattern = pattern.replace("\\d+", "[0-9]+")164 pattern = pattern.replace("\\d*", "[0-9]*")165 pattern = pattern.replace("\\S+", "[a-z]{4}")166 pattern = pattern.replace("\\d]", "0-9]")167 pattern = pattern.replace("[a-z\\d", "[a-z0-9")168 pattern = pattern.replace("[a-zA-Z\\d", "[a-z0-9")169 pattern = pattern.replace("^$|", "")170 pattern = pattern.replace("(^$)|", "")171 pattern = pattern.replace("[:/]", "[a-z]")172 pattern = pattern.replace("/.{", "/[a-z]{")173 pattern = pattern.replace(".{", "[a-z]{")174 pattern = pattern.replace("-*", "-")175 pattern = pattern.replace("\\n", "")176 pattern = pattern.replace("\\r", "")177 # quantifiers178 pattern = pattern.replace("{11}{0,1011}", "{11}")179 pattern = pattern.replace("}+", "}")180 pattern = pattern.replace("]*", "]{6}")181 pattern = pattern.replace("]+", "]{6}")182 pattern = pattern.replace(".*", "[a-z]{6}")183 pattern = pattern.replace(".+", "[a-z]{6}")184 return pattern185custom_arns = {186 "DeviceFarmArn": "arn:aws:devicefarm:us-east-1:1234567890123:mydevicefarm",187 "KmsKeyArn": "arn:aws:kms:us-east-1:1234567890123:key/somekmskeythatisawesome",188}189@singledispatch190def generate_instance(shape: Shape, graph: ShapeGraph) -> Optional[Instance]:191 if shape is None:192 return None193 raise ValueError("could not generate shape for type %s" % shape.type_name)194@generate_instance.register195def _(shape: StructureShape, graph: ShapeGraph) -> Dict[str, Instance]:196 if shape.is_tagged_union:197 k, v = random.choice(list(shape.members.items()))198 members = {k: v}199 else:200 members = shape.members201 if shape.name in graph.cycle_shapes:202 return {}203 return {204 name: generate_instance(member_shape, graph)205 for name, member_shape in members.items()206 if member_shape.name != shape.name207 }208@generate_instance.register209def _(shape: ListShape, graph: ShapeGraph) -> List[Instance]:210 if shape.name in graph.cycle_shapes:211 return []212 return [generate_instance(shape.member, graph) for _ in range(shape.metadata.get("min", 1))]213@generate_instance.register214def _(shape: MapShape, graph: ShapeGraph) -> Dict[str, Instance]:215 if shape.name in graph.cycle_shapes:216 return {}217 return {generate_instance(shape.key, graph): generate_instance(shape.value, graph)}218def generate_arn(shape: StringShape):219 if not shape.metadata:220 return "arn:aws:ec2:us-east-1:1234567890123:instance/i-abcde0123456789f"221 def _generate_arn():222 # some custom hacks223 if shape.name in custom_arns:224 return custom_arns[shape.name]225 max_len = shape.metadata.get("max") or math.inf226 min_len = shape.metadata.get("min") or 0227 pattern = shape.metadata.get("pattern")228 if pattern:229 # FIXME: also conforming to length may be difficult230 pattern = sanitize_arn_pattern(pattern)231 arn = rstr.xeger(pattern)232 else:233 arn = "arn:aws:ec2:us-east-1:1234567890123:instance/i-abcde0123456789f"234 # if there's a value set for the region, replace with a randomly picked region235 # TODO: splitting the ARNs here by ":" sometimes fails for some reason (e.g. or dynamodb for some reason)236 arn_parts = arn.split(":")237 if len(arn_parts) >= 4:238 region = arn_parts[3]239 if region:240 # TODO: check service in ARN and try to get the actual region for the service241 regions = botocore.session.Session().get_available_regions("lambda")242 picked_region = random.choice(regions)243 arn_parts[3] = picked_region244 arn = ":".join(arn_parts)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful