""" QA / UAT Script for ImpersonationOwsClient ------------------ Standalone setup: mkdir uat_impersonation_client && cd uat_impersonation_client python3 -mvenv env ./env/bin/pip3 install boto3 ./env/bin/pip3 install owsclient==0.10.0 -i https://pypi.theorchard.io/ awsume permissions-platform-qa To run: # Impersonate pdptest and telvis for a `/self/allowed-tenants/` request. ./env/bin/python3 uat_PP-1199_impersonation_owsclient.py \ 4d5f24f5-83f9-4989-9f82-0924a5feaf88 \ d7fac0ab-184a-4f76-ab2f-2acdcdf14ba0 \ --action view --resource audience Help: ./env/bin/python3 uat_PP-1199_impersonation_owsclient.py -h --------------- OR ------------------------- Dev Setup: poetry add boto3 --group dev awsume permissions-platform-qa To run: poetry run python3 uat/uat_PP-1199_impersonation_owsclient.py \ 4d5f24f5-83f9-4989-9f82-0924a5feaf88 \ d7fac0ab-184a-4f76-ab2f-2acdcdf14ba0 \ --action view --resource audience Cleanup: poetry remove boto3 --group dev git checkout pyproject.toml poetry.lock """ import argparse import json import boto3 from owsclient import ( ImpersonationM2MTokenManager, ImpersonationOwsClient ) class UatSecretsManager: def __init__(self) -> None: """Init AWS Secrets Manager client.""" self.boto3_session = boto3.session.Session() self._client = self.boto3_session.client("secretsmanager") def get_secret(self, secret_name: str) -> str: """Get a secret from AWS Secrets Manager.""" get_secret_value_response = self._client.get_secret_value(SecretId=secret_name) secret_string = get_secret_value_response["SecretString"] print(f"Read secret from '{secret_name}'.") return secret_string def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="UAT script for ImpersonationOwsClient") parser.add_argument("impersonated_identity_uuid", help="UUID of identity to impersonate", nargs='+') parser.add_argument( "--resource", default="identity", help="Resource used in the allowed-tenants request. Default: 'identity'" ) parser.add_argument( "--action", default="view", help="Action used in the allowed-tenants request. Default: 'view'" ) parser.add_argument( "--service_name", default="lambda-test-m2m-can-impersonate-client", help="Service name for M2M_AUTH0_CLIENT_CREDENTIALS secret. Default: 'lambda-test-m2m-can-impersonate-client'" ) return parser.parse_args() def main() -> None: args = parse_args() impersonated_identity_uuid_list = args.impersonated_identity_uuid resource = args.resource action = args.action service_name = args.service_name sm = UatSecretsManager() # Create ImpersonationM2MTokenManager token_manager = ImpersonationM2MTokenManager( secrets_manager=sm, environment="qa", service_name=service_name, ) # Create ImpersonationOwsClient with the token manager client = ImpersonationOwsClient( environment="qa", service_name="uat-script", m2m_token_manager=token_manager, ) for impersonated_identity_uuid in impersonated_identity_uuid_list: print("\n--------------------------------") print(f"Calling allowed-tenants for identity: {impersonated_identity_uuid}") print(f"Resource: '{resource}', Action: '{action}'") # Make the request with impersonation response = client.post( "ows-pdp", path="/identity/self/allowed-tenants/", json={ "action": action, "resource_type": resource, }, impersonated_identity_uuid=impersonated_identity_uuid, ) if response.status_code != 200: raise RuntimeError(f"Something went wrong: {response.text}") print(f"\nResponse Status: {response.status_code}") print(f"Response Body:\n{json.dumps(response.json(), indent=2)}") print("\n--------------------------------") print("What's in the cache????") i = 1 for key, entry in client.m2m_token_manager._cache.items(): print(f"------------Entry {i}--------------------") print(f"\nkey='{key}'") print(f"entry={json.dumps(json.loads(entry), indent=2)}") i+=1 if __name__ == "__main__": main()