Skip to content

Instantly share code, notes, and snippets.

@jiang-wei
Created June 2, 2020 13:33
Show Gist options
  • Save jiang-wei/ca07bac991d912ff042258768abb9daf to your computer and use it in GitHub Desktop.
Save jiang-wei/ca07bac991d912ff042258768abb9daf to your computer and use it in GitHub Desktop.
Decode k8s secret yaml
#!/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