""" https://theorchard.atlassian.net/browse/PP-1055 Usage (Can only be run by a PP team member) # Copy the script to the jumpbox scp attach_roles_in_prod.py jumpbox:pp/telvis/ ssh jumpbox cd jumpbox # Put a valid JWT in token.txt vim token.txt # run the script python3 attach_roles_in_prod.py Verify in Snowflake SELECT affected_identity.email AS AFFECTED_IDENTITY_EMAIL, IDENTITY_UUID, TENANT_UUID, tenant_type, ROLES FROM PERMISSIONS_PLATFORM.PROD.PP_IDENTITY pp JOIN FACTS.PROD.IDENTITY affected_identity ON affected_identity.id=pp.identity_uuid WHERE ROLES LIKE '%contract_viewer%' AND IDENTITY_UUID IN ( '43b3c342-f14b-4509-97a7-ca0a14cd2594', '8dd58ec8-3b75-43e9-b8fb-aaf7b6f02ba4', '8d7979a2-14cf-4709-8ae5-7f631d79a7bc', '8eb95c1e-3c52-4596-9380-3927408cf646', '9b0b8880-9946-4cc5-be54-d95294757a36', 'c00346b4-b8d3-40f2-9b1e-861a0903db2c', '13cc7f35-41e9-45a7-bf0a-c69ca467c751', 'ad6ee420-6f95-4d49-a026-e9ed04eee76f', 'd3bb3e3d-3bb5-4dea-a2ea-2b737a9959d9', 'ba1b3c25-b552-442b-9dd6-5f723aefd7c6', '3cfa5301-3caf-4b65-b6af-54139e343759', 'dd3e7c0c-1f31-43b8-afce-e2444c896faa' ) ORDER BY AFFECTED_IDENTITY_EMAIL, IDENTITY_UUID, TENANT_UUID DESC LIMIT 200; """ import time import requests import os import sys import re import json HOST="prod-ows-pdp.theorchard.io" def load_token(input_file: str): token_file = os.path.realpath(input_file) if not os.path.isfile(token_file): print(f"Did not find bearer token in '{token_file}'") sys.exit(1) with open(token_file, 'rb') as fp: bearer_token = fp.read() bearer_token = re.sub(r"\n", "", bearer_token.decode('utf-8')) return bearer_token def get_identity_roles(identity: str): bearer_token = load_token("./token.txt") print("") print("GET identity roles") response = requests.get( f"https://{HOST}/identity/{identity}/roles/", headers={ 'Authorization': bearer_token, 'Content-Type': 'application/json' } ) print(f"status: {response.status_code}") print(f"get_identity_roles[{identity}]: {json.dumps(json.loads(response.text), indent=2)}") def attach_roles(identity: str, role: str, tenant:str, tenant_type: str) -> None: bearer_token = load_token("./token.txt") print("") print(f"PUT attach: {identity}/{role}/{tenant}") response = requests.put( f"https://{HOST}/identity/{identity}/tenant/{tenant}/attach-and-detach/roles/", headers={ 'Authorization': bearer_token, 'Content-Type': 'application/json' }, json={ "tenant_type": tenant_type, "tenant_uuid": tenant, "roles_to_attach": [{ "role": role }], "roles_to_detach": [] } ) print(f"status: {response.status_code}") print(f"response: {response.json()}") time.sleep(0.1) print("\nSleep 1/2...") def main(): """ attach some roles """ # TODO: This could consume a backfill CSV formatted file. tenants = [ {'tenant_type': 'company_brand', 'tenant_uuid': '2ed1077d-bcd3-4db6-9a47-c37da82aab18'}, {'tenant_type': 'company_brand', 'tenant_uuid': '31f4f0f0-cbb4-4a2c-9eb0-d7288c5a2588'}, {'tenant_type': 'company_brand', 'tenant_uuid': '567e4261-ad8a-4e89-8875-d44c26b96f2c'}, {'tenant_type': 'company_brand', 'tenant_uuid': '581557e3-94a4-40a1-8f85-1b4328170144'}, {'tenant_type': 'company_brand', 'tenant_uuid': '77c6c150-19f3-451f-a692-1a5d7ef30e85'}, {'tenant_type': 'company_brand', 'tenant_uuid': '7c8b382c-fc37-4179-9115-2165b1a93bed'}, {'tenant_type': 'company_brand', 'tenant_uuid': '9b36a627-4be9-4762-b790-5628676f5f90'}, {'tenant_type': 'company_brand', 'tenant_uuid': '9c270cea-cde6-4240-81cb-945ec2bac811'}, {'tenant_type': 'company_brand', 'tenant_uuid': 'ae886607-ae74-42f4-b7a7-d33f6a6288f4'}, {'tenant_type': 'company_brand', 'tenant_uuid': 'd25a4cd1-e820-45f2-be5c-56edcfeb8298'}, ] identities = [ '43b3c342-f14b-4509-97a7-ca0a14cd2594', '8dd58ec8-3b75-43e9-b8fb-aaf7b6f02ba4', '8d7979a2-14cf-4709-8ae5-7f631d79a7bc', '8eb95c1e-3c52-4596-9380-3927408cf646', '9b0b8880-9946-4cc5-be54-d95294757a36', 'c00346b4-b8d3-40f2-9b1e-861a0903db2c', '13cc7f35-41e9-45a7-bf0a-c69ca467c751', 'ad6ee420-6f95-4d49-a026-e9ed04eee76f', 'd3bb3e3d-3bb5-4dea-a2ea-2b737a9959d9', 'ba1b3c25-b552-442b-9dd6-5f723aefd7c6', '3cfa5301-3caf-4b65-b6af-54139e343759', "dd3e7c0c-1f31-43b8-afce-e2444c896faa" ] # identities = [ # "ca034907-1746-4c7b-9c42-502dc4002d6c" # ] for identity in identities: for tenant in tenants: attach_roles( identity=identity, role="contract_viewer", tenant=tenant['tenant_uuid'], tenant_type=tenant['tenant_type'], ) get_identity_roles(identity=identity) if __name__ == '__main__': main()