Best Python code snippet using localstack_python
svckms.py
Source:svckms.py
...115 return Tags(tags)116 except botocore.exceptions.ClientError as e:117 raise RdqError(self._utils.fail(e, op, 'CmkArn', cmkArn))118 #PREVIEW119 def update_key_description(self, cmkArn, description):120 op = 'update_key_description'121 args = {122 'KeyId': cmkArn,123 'Description': description124 }125 if self._utils.preview(op, args): return126 try:127 self._client.update_key_description(128 KeyId=cmkArn,129 Description=description130 )131 except botocore.exceptions.ClientError as e:132 raise RdqError(self._utils.fail(e, op, 'CmkArn', cmkArn))133 def tag_resource(self, cmkArn, tags):134 op = 'tag_resource'135 try:136 self._client.tag_resource(137 KeyId=cmkArn,138 Tags=tags.toList("Tag")139 )140 except botocore.exceptions.ClientError as e:141 raise RdqError(self._utils.fail(e, op, 'CmkArn', cmkArn))142 #PREVIEW143 def enable_key_rotation(self, cmkArn):144 op = 'enable_key_rotation'145 args = {146 'KeyId': cmkArn147 }148 if self._utils.preview(op, args): return149 try:150 self._client.enable_key_rotation(151 KeyId=cmkArn152 )153 except botocore.exceptions.ClientError as e:154 raise RdqError(self._utils.fail(e, op, 'CmkArn', cmkArn))155 #PREVIEW156 def schedule_key_deletion(self, cmkArn, pendingWindowInDays):157 op = 'schedule_key_deletion'158 op = 'enable_key_rotation'159 args = {160 'KeyId': cmkArn,161 'PendingWindowInDays': pendingWindowInDays162 }163 if self._utils.preview(op, args): return164 try:165 self._client.schedule_key_deletion(166 KeyId=cmkArn,167 PendingWindowInDays=pendingWindowInDays168 )169 return True170 except botocore.exceptions.ClientError as e:171 if self._utils.is_resource_not_found(e): return False172 raise RdqError(self._utils.fail(e, op, 'CmkArn', cmkArn))173 #PREVIEW174 def delete_alias(self, canonAlias):175 op = 'delete_alias'176 args = {177 'AliasName': canonAlias178 }179 if self._utils.preview(op, args): return True180 try:181 self._client.delete_alias(182 AliasName=canonAlias183 )184 return True185 except botocore.exceptions.ClientError as e:186 if self._utils.is_resource_not_found(e): return False187 raise RdqError(self._utils.fail(e, op, 'AliasName', canonAlias))188 def createPreviewArn(self):189 return self.create_preview_arn()190 def getCMKByAlias(self, alias):191 canonAlias = _canon_alias(alias)192 return self.describe_key(canonAlias)193 #PREVIEW194 def enableKeyRotation(self, cmkArn):195 self.enable_key_rotation(cmkArn)196 #PREVIEW197 def declareCMKArn(self, description, alias, policyStatements, tags):198 statements = [self.policy_statement_default()]199 statements.extend(policyStatements)200 policyMap = self._utils.policy_map(statements)201 reqdPolicyJson = self._utils.to_json(policyMap)202 canonAlias = _canon_alias(alias)203 exMeta = self.describe_key(canonAlias)204 createReqd = False205 if exMeta:206 keyState = exMeta['KeyState']207 if keyState == 'PendingDeletion':208 createReqd = True209 elif keyState == 'Enabled':210 createReqd = False211 else:212 erm = 'KMS CMK {} in unexpected state {}'.format(alias, keyState)213 raise RdqError(erm)214 else:215 createReqd = True216 if createReqd:217 newArn = self.create_key_arn(description, reqdPolicyJson, tags)218 self.create_alias(canonAlias, newArn)219 self.enable_key_rotation(newArn)220 return newArn221 exArn = exMeta['Arn']222 exDescription = exMeta['Description']223 exPolicyJson = self._utils.to_json(self.get_key_policy(exArn))224 if exPolicyJson != reqdPolicyJson:225 self.put_key_policy(exArn, reqdPolicyJson)226 if exDescription != description:227 self.update_key_description(exArn, description)228 isRotationEnabled = self.get_key_rotation_status(exArn)229 if not isRotationEnabled:230 self.enable_key_rotation(exArn)231 exTags = self.list_resource_tags(exArn)232 deltaTags = tags.subtract(exTags)233 if not deltaTags.isEmpty():234 self.tag_resource(exArn, deltaTags)235 return exArn236 def removeCMK(self, alias, pendingWindowInDays=7):237 canonAlias = _canon_alias(alias)238 exMeta = self.describe_key(canonAlias)239 if exMeta:240 exArn = exMeta['Arn']241 self.delete_alias(canonAlias)...
util_kms.py
Source:util_kms.py
...106 return response['Policy']107 except botocore.exceptions.ClientError as e:108 erm = _fail(e, 'get_key_policy', cmkArn)109 raise Exception(erm)110def update_key_description(ctx, cmkArn, description):111 try:112 client = ctx.client('kms')113 client.update_key_description(114 KeyId=cmkArn,115 Description=description116 )117 except botocore.exceptions.ClientError as e:118 erm = _fail(e, 'update_key_description', cmkArn)119 raise Exception(erm)120def enable_key_rotation(ctx, cmkArn):121 try:122 client = ctx.client('kms')123 client.enable_key_rotation(124 KeyId=cmkArn125 )126 except botocore.exceptions.ClientError as e:127 erm = _fail(e, 'enable_key_rotation', cmkArn)128 raise Exception(erm)129def schedule_key_deletion(ctx, cmkArn, pendingWindowInDays):130 try:131 client = ctx.client('kms')132 client.schedule_key_deletion(133 KeyId=cmkArn,134 PendingWindowInDays=pendingWindowInDays135 )136 except botocore.exceptions.ClientError as e:137 if _is_resource_not_found(e): return None138 erm = _fail(e, 'schedule_key_deletion', cmkArn)139 raise Exception(erm)140def delete_alias(ctx, canonAlias):141 try:142 client = ctx.client('kms')143 client.delete_alias(144 AliasName=canonAlias145 )146 except botocore.exceptions.ClientError as e:147 if _is_resource_not_found(e): return None148 erm = _fail(e, 'delete_alias', canonAlias)149 raise Exception(erm)150def getCMKMeta(ctx, keyId):151 try:152 client = ctx.client('kms')153 response = client.describe_key(154 KeyId=keyId155 )156 return response['KeyMetadata']157 except botocore.exceptions.ClientError as e:158 if _is_resource_not_found(e): return None159 erm = _fail(e, 'describe_key', keyId)160 raise Exception(erm)161def declareCMK(ctx, description, alias, policyStatements):162 statements = [policy_statement_default(ctx)]163 statements.extend(policyStatements)164 policyMap = policy_map(statements)165 reqdPolicyJson = json.dumps(policyMap)166 canonAlias = canon_alias(alias)167 exMeta = getCMKMeta(ctx, canonAlias)168 createReqd = False169 if exMeta:170 keyState = exMeta['KeyState']171 if keyState == 'PendingDeletion':172 createReqd = True173 elif keyState == 'Enabled':174 createReqd = False175 else:176 erm = 'KMS CMK {} in unexpected state {}'.format(alias, keyState)177 raise Exception(erm)178 else:179 createReqd = True180 if createReqd:181 newArn = create_key_arn(ctx, description, reqdPolicyJson)182 create_alias(ctx, canonAlias, newArn)183 enable_key_rotation(ctx, newArn)184 return newArn185 exArn = exMeta['Arn']186 exDescription = exMeta['Description']187 exPolicyJson = get_key_policy(ctx, exArn)188 exPolicyJsonCanon = json.dumps(json.loads(exPolicyJson))189 if exPolicyJsonCanon != reqdPolicyJson:190 put_key_policy(ctx, exArn, reqdPolicyJson)191 if exDescription != description:192 update_key_description(ctx, exArn, description)193 isRotationEnabled = get_key_rotation_status(ctx, exArn)194 if not isRotationEnabled:195 enable_key_rotation(ctx, exArn)196 return exArn197def deleteCMK(ctx, alias, pendingWindowInDays=7):198 canonAlias = canon_alias(alias)199 exMeta = getCMKMeta(ctx, canonAlias)200 if exMeta:201 exArn = exMeta['Arn']202 delete_alias(ctx, canonAlias)...
layer1.pyi
Source:layer1.pyi
...32 def put_key_policy(self, key_id: str, policy_name: str, policy: str) -> Optional[Dict[str, Any]]: ...33 def re_encrypt(self, ciphertext_blob: bytes, destination_key_id: str, source_encryption_context: Optional[Mapping[str, Any]] = ..., destination_encryption_context: Optional[Mapping[str, Any]] = ..., grant_tokens: Optional[List[str]] = ...) -> Optional[Dict[str, Any]]: ...34 def retire_grant(self, grant_token: str) -> Optional[Dict[str, Any]]: ...35 def revoke_grant(self, key_id: str, grant_id: str) -> Optional[Dict[str, Any]]: ......
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!!