Created
June 2, 2020 13:33
-
-
Save jiang-wei/ca07bac991d912ff042258768abb9daf to your computer and use it in GitHub Desktop.
Decode k8s secret yaml
This file contains 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 | |
############# | |
# save to ~/bin/decode-secret-yaml | |
# chmod +x ~/bin/decode-secret-yaml | |
# | |
# usage: | |
# ~/bin/decode-secret-yaml foo.yaml | |
# kubectl get secret foo -o yaml | ~/bin/decode-secret-yaml | |
################# | |
import base64 | |
import yaml | |
import sys | |
def parse_one(doc): | |
data = doc.get('data') | |
if data is not None: | |
for k,v in data.items(): | |
try: | |
decoded = str(base64.b64decode(v), 'utf-8') | |
except e: | |
decoded = v # intacted | |
data[k] = decoded | |
#doc['data'] = data | |
return doc | |
def parse(stream): | |
documents = yaml.safe_load_all(stream) | |
for d in documents: | |
parsed = parse_one(d) | |
print('---') | |
print(yaml.safe_dump(parsed)) | |
if __name__ == '__main__': | |
if len(sys.argv) > 1: | |
file = sys.argv[1] | |
with open(file) as f: | |
parse(f) | |
else: | |
parse(sys.stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment