Best Python code snippet using localstack_python
reportgen.py
Source:reportgen.py
...41 except:42 c = content43 finally:44 return c.decode('utf-8')45def generate_credential_report():46 iam.generate_credential_report()47def get_credential_report():48 response = iam.get_credential_report()49 return ensure_plaintext(response['Content'])50def save_credential_report(data, account_id):51 s3 = boto3.client('s3')52 now = datetime.now().strftime("%Y/%m/%d")53 key = f'{now}/report-{account_id}.csv'54 s3.put_object(Bucket=BUCKET_NAME, Key=key, Body=data)55def get_user_access_keys(username: str) -> Tuple[str, str]:56 try:57 r = iam.list_access_keys(UserName=username)58 aks = r['AccessKeyMetadata']59 except ClientError as e:60 return 'Error', 'Error'61 if len(aks) == 2:62 ak1, ak2 = (ak['AccessKeyId'] for ak in aks)63 elif len(aks) == 1:64 ak1, ak2 = aks[0]['AccessKeyId'], NA65 else:66 ak1, ak2 = NA, NA67 return ak1, ak268def enrich_report(report: str, account_id: str) -> str:69 # Load Secrets from Secrets Manager70 searcher = SecretsManagerSearcher()71 f_in = StringIO(report)72 reader = csv.DictReader(f_in, delimiter=',')73 # Include extra fields for contextual information74 extra_fields = ['access_key_id_1', 'access_key_id_2',75 'access_key_id_1_location', 'access_key_id_2_location',76 'account_id']77 78 fieldnames = deepcopy(reader.fieldnames)79 fieldnames.extend(extra_fields)80 f_out = StringIO()81 writer = csv.DictWriter(f_out, fieldnames=fieldnames, delimiter=',')82 writer.writeheader()83 for user in reader:84 # Include Account ID85 user['account_id'] = account_id86 # Obtain Access Key ID information87 ak1, ak2 = None, None88 if user['access_key_1_active'].lower() == 'true' \89 or user['access_key_2_active'].lower() == 'true':90 ak1, ak2 = get_user_access_keys(user['user'])91 user['access_key_id_1'], user['access_key_id_2'] = ak1, ak292 else:93 user['access_key_id_1'], user['access_key_id_2'] = NA, NA94 # Search Secrets Manager for stored credentials95 user['access_key_id_1_location'] = searcher.search(ak1)96 user['access_key_id_2_location'] = searcher.search(ak2)97 # Write row to output98 writer.writerow(user)99 f_out.flush()100 f_out.seek(0)101 return f_out.read()102def handler(event, context):103 if event['task'] == 'generate_credential_report':104 generate_credential_report()105 elif event['task'] == 'get_credential_report':106 report = get_credential_report()107 account_id = context.invoked_function_arn.split(':')[4]108 report = enrich_report(report, account_id)109 save_credential_report(report, account_id)110if __name__ == '__main__':111 # f = open('report.csv')112 # report = f.read()113 report = get_credential_report()114 r = enrich_report(report, '12345678912')...
generate_credential_report.py
Source:generate_credential_report.py
...17# Create IAM client18client = boto3.client('iam')1920# Generate credentials report of all users in account21response = client.generate_credential_report()2223print(response)2425# snippet-end:[iam.python.generate_credential_report.complete]26# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]27# snippet-sourcedescription:[generate_credential_report.py demonstrates how to generate a credential report for the AWS account.]28# snippet-keyword:[Python]29# snippet-sourcesyntax:[python]30# snippet-sourcesyntax:[python]31# snippet-keyword:[AWS SDK for Python (Boto3)]32# snippet-keyword:[Code Sample]33# snippet-keyword:[AWS Identity and Access Management (IAM)]34# snippet-service:[iam]35# snippet-sourcetype:[full-example]
...
credential-report.py
Source:credential-report.py
...6if os.path.isfile(credentials):7 os.environ['AWS_SHARED_CREDENTIALS_FILE'] = credentials8if os.path.isfile(config):9 os.environ['AWS_CONFIG_FILE'] = config10def generate_credential_report():11 client = boto3.client('iam')12 response = client.generate_credential_report()13 return response14def get_credential_report():15 client = boto3.client('iam')16 response = client.get_credential_report()17 return response18if __name__ == '__main__':19 generate_credential_report()...
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!!