Last active
April 10, 2025 11:39
-
-
Save venkata-qa/ede2342e676a2514783fdb28be767d29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import yaml | |
| from collections import defaultdict | |
| # Load Vault JSON | |
| with open("vaultfile.json") as f: | |
| vault_data = json.load(f) | |
| # Load YAML config | |
| with open("apis.yaml") as f: | |
| yaml_data = yaml.safe_load(f) | |
| # Output files | |
| TXT_FILE = "missing_tokens_report.txt" | |
| MD_FILE = "missing_tokens_report.md" | |
| # Build Vault token pairs | |
| vault_pairs = set() | |
| for token in vault_data.get("tokens", []): | |
| svc = token.get("service") | |
| cons = token.get("consumer") | |
| if svc and cons: | |
| vault_pairs.add((svc, cons)) | |
| # Group missing tokens by API | |
| missing_by_api = defaultdict(list) | |
| for entry in yaml_data: | |
| api_number = entry.get("api_number") | |
| service = entry.get("service") | |
| consumers = entry.get("consumers", []) | |
| for consumer in consumers: | |
| if (service, consumer) not in vault_pairs: | |
| missing_by_api[api_number].append((service, consumer)) | |
| # Write results to both files | |
| with open(TXT_FILE, "w") as txt, open(MD_FILE, "w") as md: | |
| def write(line=""): | |
| txt.write(line + "\n") | |
| md.write(line + "\n") | |
| write("π Vault Token Validation Report (Missing Only)") | |
| write("=================================================") | |
| write() | |
| if not missing_by_api: | |
| write("β All services and consumers have valid Vault tokens.") | |
| else: | |
| for api, missing_pairs in missing_by_api.items(): | |
| write(f"API: {api}") | |
| md.write(f"\n### API: `{api}`\n") | |
| grouped = defaultdict(list) | |
| for svc, con in missing_pairs: | |
| grouped[svc].append(con) | |
| for svc, cons_list in grouped.items(): | |
| write(f" Service: {svc}") | |
| md.write(f"- **Service**: `{svc}`\n") | |
| for con in cons_list: | |
| write(f" - Missing token for consumer: {con}") | |
| md.write(f" - β Missing token for consumer: `{con}`\n") | |
| write() | |
| write(f"β Total APIs with missing tokens: {len(missing_by_api)}") | |
| write("\nβ Validation complete.") | |
| print(f"β Reports saved as:\n- {TXT_FILE}\n- {MD_FILE}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment