Created
October 21, 2020 13:59
-
-
Save riptl/b19756fcb1de778eae4125f634b1a3d8 to your computer and use it in GitHub Desktop.
Python script to convert a Kubernetes secret file containing base64-encoded "data" entries to plain text "stringData" entries.
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
| #!/usr/bin/env python3 | |
| from base64 import b64decode | |
| import os | |
| import sys | |
| import yaml | |
| if os.isatty(sys.stdin.fileno()): | |
| print("Usage: ./decode_secret.py < secret.yaml", file=sys.stderr) | |
| sys.exit(1) | |
| # Parse manifest YAML | |
| manifest = yaml.safe_load(sys.stdin) | |
| if manifest.get("stringData") is None: | |
| manifest["stringData"] = {} | |
| # Move values from data to stringData. | |
| new_data = dict(manifest["data"]) | |
| for (key, value_b64) in manifest["data"].items(): | |
| value_bytes = b64decode(value_b64) | |
| try: | |
| value = value_bytes.decode("utf-8") | |
| except ValueError: | |
| continue | |
| del new_data[key] | |
| manifest["stringData"][key] = value | |
| manifest["data"] = new_data | |
| # Dump new YAML | |
| yaml.safe_dump(manifest, stream=sys.stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment