Best Python code snippet using localstack_python
create_vpn.py
Source:create_vpn.py
...12cert_server = {"name": "vpn-server"}13vpn_id = None14sg_id = None15ec2 = boto3.client('ec2')16res = ec2.describe_client_vpn_endpoints()17for e in res["ClientVpnEndpoints"]:18 for tag in e["Tags"]:19 if tag["Key"] == "Name" and tag["Value"] == vpn_name:20 vpn_id = e["ClientVpnEndpointId"]21 if "AssociatedTargetNetworks" in e:22 vpn_networks = e["AssociatedTargetNetworks"]23 else:24 vpn_networks = []25 break26if vpn_id:27 print("VPN endpoint already exists")28 sys.exit(0)29res = ec2.describe_subnets(SubnetIds=[subnet_id])30if len(res["Subnets"]) == 0:31 print(f"Subnet '{subnet_id} doesn't exist !")32 sys.exit(1)33vpc_id = res["Subnets"][0]["VpcId"]34with open("pki/ca.crt") as f:35 cert_server["ca"]= f.read()36with open(f"pki/issued/{cert_server['name']}.crt") as f:37 cert_server["public"] = f.read()38with open(f"pki/private/{cert_server['name']}.key") as f:39 cert_server["private"] = f.read()40acm = boto3.client('acm')41res = acm.list_certificates()42for cert in res["CertificateSummaryList"]:43 if cert["DomainName"] == cert_server["name"]:44 cert_server["CertificateArn"] = cert["CertificateArn"]45print("Importing server certificate")46if "CertificateArn" in cert_server:47 acm.import_certificate(CertificateArn=cert_server["CertificateArn"], Certificate=cert_server["public"], PrivateKey=cert_server["private"], CertificateChain=cert_server["ca"])48else:49 res = acm.import_certificate(Certificate=cert_server["public"], PrivateKey=cert_server["private"], CertificateChain=cert_server["ca"])50 cert_server["CertificateArn"] = res["CertificateArn"]51logs = boto3.client('logs')52print("Creating Clouwatch logs")53try:54 logs.create_log_group(logGroupName="vpn-client-logs")55 logs.create_log_stream(logGroupName="vpn-client-logs", logStreamName="connections")56 logs.put_retention_policy(logGroupName="vpn-client-logs", retentionInDays=7)57except:58 pass59res = ec2.describe_security_groups(Filters=[{"Name": "group-name", "Values":[vpn_name]}])60if len(res["SecurityGroups"]) > 0:61 sg_id = res["SecurityGroups"][0]["GroupId"]62else:63 print('Creating security group')64 res = ec2.create_security_group(GroupName=vpn_name, VpcId=vpc_id, Description="For VPN access")65 sg_id = res["GroupId"]66 ec2.authorize_security_group_ingress( 67 GroupId=sg_id, 68 IpPermissions=[{'IpProtocol': '-1', 'FromPort': -1, 'ToPort': -1, 'UserIdGroupPairs': [{ 'GroupId': sg_id}] }],69 )70print("Creating VPN endpoint")71res = ec2.create_client_vpn_endpoint(72 ClientCidrBlock=vpn_cidr, 73 ServerCertificateArn=cert_server["CertificateArn"],74 AuthenticationOptions=[{"Type":"certificate-authentication", "MutualAuthentication": {"ClientRootCertificateChainArn": cert_server["CertificateArn"]}}],75 SplitTunnel=True,76 VpcId=vpc_id,77 SecurityGroupIds=[sg_id],78 ConnectionLogOptions={"Enabled": True, "CloudwatchLogGroup": "vpn-client-logs" , "CloudwatchLogStream": "connections"},79 TagSpecifications=[{"ResourceType": "client-vpn-endpoint", "Tags":[{"Key": "Name", "Value": vpn_name}]}]80 )81vpn_id = res["ClientVpnEndpointId"]82print("Associating subnet")83ec2.associate_client_vpn_target_network(ClientVpnEndpointId=vpn_id, SubnetId=subnet_id)84print("Adding authorization")85ec2.authorize_client_vpn_ingress(ClientVpnEndpointId=vpn_id, TargetNetworkCidr="0.0.0.0/0", AuthorizeAllGroups=True)86print("Adding route")87ec2.create_client_vpn_route(ClientVpnEndpointId=vpn_id, DestinationCidrBlock="0.0.0.0/0", TargetVpcSubnetId=subnet_id)88print('Waiting for VPN endpoint availability ...')89while True:90 time.sleep(5)91 res = ec2.describe_client_vpn_endpoints(ClientVpnEndpointIds=[vpn_id])92 if res["ClientVpnEndpoints"][0]["Status"]["Code"] == "available":93 break...
get_configuration.py
Source:get_configuration.py
...12 public_key = f.read()13with open(f"pki/private/{cert_client}.key") as f:14 private_key = f.read()15ec2 = boto3.client('ec2')16res = ec2.describe_client_vpn_endpoints()17for e in res["ClientVpnEndpoints"]:18 for tag in e["Tags"]:19 if tag["Key"] == "Name" and tag["Value"] == vpn_name:20 vpn_id = e["ClientVpnEndpointId"]21if not vpn_id:22 print("VPN endpoint not found", file=sys.stderr)23 sys.exit(1)24res = ec2.export_client_vpn_client_configuration(ClientVpnEndpointId=vpn_id)25openvpn_config = res["ClientConfiguration"]26i = public_key.find("-----BEGIN CERTIFICATE")27public_key = public_key[i:]28openvpn_config += f"\n\n<cert>\n{public_key}</cert>\n\n<key>\n{private_key}</key>\n"...
infratest_explore.py
Source:infratest_explore.py
...11 PP.pprint(response)12def client_vpn():13 session = boto3.Session(profile_name='infratest')14 client = session.client('ec2')15 PP.pprint(client.describe_client_vpn_endpoints())16def main():17 session = boto3.Session(profile_name='infratest')18 client = session.client('ec2')19 vpcs = client.describe_vpcs()20 for x in vpcs.get('Vpcs'):21 print(x.get('CidrBlock'), x.get('VpcId'))22if __name__ == '__main__':...
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!!