Best Python code snippet using localstack_python
windows_rotation.py
Source:windows_rotation.py
...20 set_secret(service_client, arn, token)21 elif step == "testSecret":22 test_secret(service_client, arn, token)23 elif step == "finishSecret":24 finish_secret(service_client, arn, token)25 else:26 raise ValueError("Invalid step parameter")27def create_secret(service_client, arn, token):28 print('------ Inside create_secret ------')29 mysecretresponse = service_client.describe_secret(SecretId=arn)30 print("mysecretresponse",mysecretresponse)31 # we need this unicode conversion because the tags are in unicode and we have back errors32 for tag in mysecretresponse['Tags']:33 if tag['Key'] == 'instanceid':34 instance = tag['Value']35 print('working on instnaceid ' + str(instance))36 client = boto3.client('ssm')37 try:38 # Generate a random password39 print('Generating new password')40 # create a parameter store to pass the new generated password in a secure way41 ssm = "alias/aws/ssm"42 print('Send command to change the password to the instance')43 response = client.send_command(DocumentName="AWSSupport-RunEC2RescueForWindowsTool",44 Targets=[{"Key": "InstanceIds", "Values": [instance]}],45 Parameters={"Command": ["ResetAccess"], "Parameters": [ssm]}, TimeoutSeconds=600,46 MaxConcurrency="50", MaxErrors="0")47 print("send_command : ", response)48 print('sleepping')49 time.sleep(60)50 response = client.get_parameter(51 Name="/EC2Rescue/Passwords/"+instance,52 WithDecryption=True53 )54 print(response)55 print('Put the new password in a secret')56 secure_string = '{ \"Administrator\": \"' + response['Parameter']['Value'] + '\"\n }'57 print('password :' + response['Parameter']["Value"])58 service_client.put_secret_value(SecretId=arn, ClientRequestToken=token, SecretString=secure_string,59 VersionStages=['AWSCURRENT'])60 logger.info("createSecret: Successfully put secret for ARN %s and version %s." % (arn, token))61 # Before delete the parameter we need to wait some seconds that the powershell will be executed62 # the above send_command is asyncronous63 print('Delete parameter')64 response = client.delete_parameter(65 Name="/EC2Rescue/Passwords/"+instance,66 )67 time.sleep(20)68 return True69 except Exception as e:70 print("Error")71 print(e)72 return False73def set_secret(service_client, arn, token):74 print('------ Inside set_secret ------')75 print('nothing to be done here left because secret manager needs to call this')76 return True77def test_secret(service_client, arn, token):78 print('------ Inside test_secret ------')79 print('nothing to be done here left because secret manager needs to call this')80 return True81def finish_secret(service_client, arn, token):82 print('------ Inside finish_secret ------')83 print('nothing to be done here left because secret manager needs to call this')...
17_rotate_secrets.py
Source:17_rotate_secrets.py
...16 set_secret(service_client, arn, token)17 elif step == "testSecret":18 test_secret(service_client, arn, token)19 elif step == "finishSecret":20 finish_secret(service_client, arn, token)21 else:22 raise ValueError("Invalid step parameter")23def create_secret(service_client, arn, token):24 service_client.get_secret_value(SecretId=arn, VersionStage="AWSCURRENT")25 # Now try to get the secret version, if that fails, put a new secret26 try:27 service_client.get_secret_value(SecretId=arn, VersionId=token, VersionStage="AWSPENDING")28 except service_client.exceptions.ResourceNotFoundException:29 # Generate a random password30 passwd = service_client.get_random_password(ExcludeCharacters='/@"\'\\')31 # Put the secret32 service_client.put_secret_value(SecretId=arn, ClientRequestToken=token, SecretString=passwd['RandomPassword'], VersionStages=['AWSPENDING'])33def set_secret(service_client, arn, token):34 print("No database user credentials to update...")35def test_secret(service_client, arn, token):36 print("No need to testing against any service...")37def finish_secret(service_client, arn, token):38 39 # First describe the secret to get the current version40 metadata = service_client.describe_secret(SecretId=arn)41 for version in metadata["VersionIdsToStages"]:42 if "AWSCURRENT" in metadata["VersionIdsToStages"][version]:43 if version == token:44 # The correct version is already marked as current, return45 return46 # Finalize by staging the secret version current47 service_client.update_secret_version_stage(SecretId=arn, VersionStage="AWSCURRENT", MoveToVersionId=token, RemoveFromVersionId=version)48 break...
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!!