# HG changeset patch # User Jeff Hammel # Date 1475604320 25200 # Node ID 46674fb646864f651191c11078976a2e8f8f2b1c # Parent d4403d13fb31154247ea4095f354551c8154d9f0 secret k8s decoder ring diff -r d4403d13fb31 -r 46674fb64686 python/k8sdecode.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/k8sdecode.py Tue Oct 04 11:05:20 2016 -0700 @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +get a kubernetes secret and decode it +""" + +# imports +import argparse +import base64 +import json +import os +import subprocess +import sys + + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('secret', + help="k8s secret name") + options = parser.parse_args(args) + + # get JSON from `kubectl` + command = ['kubectl', 'get', 'secret', options.secret, '-o', 'json'] + try: + output = subprocess.check_output(command) + except subprocess.CalledProcessError as e: + print (e) + sys.exit(e.returncode) + data = json.loads(output)['data'] + + # decode them + output = {key: base64.b64decode(value) + for key, value in data.items()} + + # output them + print (json.dumps(output, indent=2, sort_keys=True)) + + +if __name__ == '__main__': + main()