Best Python code snippet using localstack_python
test_main.py
Source:test_main.py
...7def format_two_decimal_points(value):8 return "{:.2f}".format(value)9def get_public_access_information(bucket_name):10 try:11 s3_client.get_public_access_block(Bucket=bucket_name)12 return True13 except ClientError:14 return False15def get_website_information(bucket_name):16 try:17 response = s3_client.get_bucket_website(Bucket=bucket_name)18 return True, response['RedirectAllRequestsTo']['HostName']19 except ClientError:20 return False, None21def get_encryption_information(bucket_name):22 try:23 s3_client.get_bucket_encryption(Bucket=bucket_name)24 return True25 except ClientError:...
test_s3control.py
Source:test_s3control.py
...6remote_endpoint = "https://%s:%s" % (LOCALHOST_HOSTNAME, EDGE_PORT)7s3control_client = create_external_boto_client("s3control", endpoint_url=remote_endpoint)8def test_lifecycle_public_access_block():9 with pytest.raises(ClientError) as ce:10 s3control_client.get_public_access_block(AccountId=TEST_AWS_ACCOUNT_ID)11 assert ce.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"12 access_block_config = {13 "BlockPublicAcls": True,14 "IgnorePublicAcls": True,15 "BlockPublicPolicy": True,16 "RestrictPublicBuckets": True,17 }18 put_response = s3control_client.put_public_access_block(19 AccountId=TEST_AWS_ACCOUNT_ID, PublicAccessBlockConfiguration=access_block_config20 )21 assert put_response["ResponseMetadata"]["HTTPStatusCode"] == 20122 get_response = s3control_client.get_public_access_block(AccountId=TEST_AWS_ACCOUNT_ID)23 assert access_block_config == get_response["PublicAccessBlockConfiguration"]24 s3control_client.delete_public_access_block(AccountId=TEST_AWS_ACCOUNT_ID)25def test_public_access_block_validations():26 with pytest.raises(ClientError) as error:27 s3control_client.get_public_access_block(AccountId="111111111111")28 assert error.value.response["Error"]["Code"] == "AccessDenied"29 with pytest.raises(ClientError) as error:30 s3control_client.put_public_access_block(31 AccountId="111111111111",32 PublicAccessBlockConfiguration={"BlockPublicAcls": True},33 )34 assert error.value.response["Error"]["Code"] == "AccessDenied"35 with pytest.raises(ClientError) as error:36 s3control_client.put_public_access_block(37 AccountId=TEST_AWS_ACCOUNT_ID, PublicAccessBlockConfiguration={}38 )...
c7n-s3-public-block.py
Source:c7n-s3-public-block.py
2import logging3import boto34import json5from botocore.exceptions import ClientError6def get_public_access_block(bucket_name):7 """Retrieve public access block of s3 bucket_name"""8 s3 = boto3.client('s3')9 try:10 response = s3.get_public_access_block(Bucket=bucket_name)11 if response["PublicAccessBlockConfiguration"]["IgnorePublicAcls"] == True or response["PublicAccessBlockConfiguration"]["BlockPublicPolicy"] == True or response["PublicAccessBlockConfiguration"]["BlockPublicAcls"] == True or response["PublicAccessBlockConfiguration"]["RestrictPublicBuckets"] == True:12 return 013 except ClientError as e:14 logging.error(e)15 return 116def put_public_access_block(bucket_name):17 """Put public access block on s3 bucket_name"""18 s3 = boto3.client('s3')19 try:20 response = s3.put_public_access_block(21 Bucket=bucket_name,22 PublicAccessBlockConfiguration={23 'BlockPublicAcls': False,24 'IgnorePublicAcls': True,25 'BlockPublicPolicy': False,26 'RestrictPublicBuckets': False27 }28 )29 except ClientError as e:30 logging.info(e)31 return None32def c7n_update_public_access_block(event_list):33 """Set up logging"""34 logging.basicConfig(level=logging.INFO,35 format='%(levelname)s: %(asctime)s: %(message)s')36 """Prasing event"""37 s3_bucket_name = event_list["resources"][0]['Name']38 """Get public access block"""39 if get_public_access_block(s3_bucket_name) != 0:40 logging.info("No public block found: %s", s3_bucket_name)41 """Put public access block"""42 try:43 block = put_public_access_block(s3_bucket_name)44 return 045 except ClientError as e:46 logging.error(e)47 return -148def lambda_handler(event, context):...
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!!