#!/usr/bin/env python3 """ # go the script dir $ ssh jumpbox $ cd pp/bludgeon # OPEN token.txt and write your JWT including the Bearer prefix. $ vim token.txt # Run the script in dry run mode $ python3 bludgeon.py prod token.txt --dry_run # Run the script to delete the cache entries. $ python3 bludgeon.py prod token.txt --rm """ import requests import argparse import os import sys import re PROD_URL = "https://prod-ows-pdp.theorchard.io/cache/bludgeon/" QA_URL = "https://qa-ows-pdp.theorchard.io/cache/bludgeon/" def parse_args(): parser = argparse.ArgumentParser(description="Bludgeon the caches for a specified environment.", epilog="Example: python3 bludgeon.py prod token.txt --rm") parser.add_argument("environment", choices=['qa', 'prod']) parser.add_argument('token_file', help="Path to file that holds the bearer token. Should include 'Bearer' prefix") parser.add_argument("--dry_run", action="store_true", help="Run bludgeon in dry-run mode with `delete=false`") parser.add_argument("--rm", dest="remove_file", help="Delete the token file after running the script", action="store_true") return parser.parse_args() def main(): args = parse_args() token_file = os.path.realpath(args.token_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')) # Define the URL and the headers url = PROD_URL if args.environment == 'prod' else QA_URL # Send the POST request response = requests.post( url, headers={ 'Authorization': bearer_token, 'Content-Type': 'application/json' }, json={ "cache_entry_type": "allowed_tenants", "delete": not args.dry_run } ) # Print the response status code and content print(f'[allowed_tenants] Status Code: {response.status_code}') print(f'[allowed_tenants] Response Content: {response.json()}') # Send the POST request response = requests.post( url, headers={ 'Authorization': bearer_token, 'Content-Type': 'application/json' }, json={ "cache_entry_type": "list_tenant_roles", "delete": not args.dry_run } ) # Print the response status code and content print(f'[list_tenant_roles] Status Code: {response.status_code}') print(f'[list_tenant_roles] Response Content: {response.json()}') # Send the POST request response = requests.post( url, headers={ 'Authorization': bearer_token, 'Content-Type': 'application/json' }, json={ "cache_entry_type": "principal_pdp", "delete": not args.dry_run } ) # Print the response status code and content print(f'[principal_pdp] Status Code: {response.status_code}') print(f'[principal_pdp] Response Content: {response.json()}') if args.remove_file: print(f"Deleting: '{args.token_file}'") os.remove(args.token_file) if __name__ == '__main__': main()