| 
791
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 # -*- coding: utf-8 -*-
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 """
 | 
| 
 | 
     5 get a kubernetes secret and decode it
 | 
| 
 | 
     6 """
 | 
| 
 | 
     7 
 | 
| 
 | 
     8 # imports
 | 
| 
 | 
     9 import argparse
 | 
| 
 | 
    10 import base64
 | 
| 
 | 
    11 import json
 | 
| 
 | 
    12 import os
 | 
| 
 | 
    13 import subprocess
 | 
| 
 | 
    14 import sys
 | 
| 
 | 
    15 
 | 
| 
 | 
    16 
 | 
| 
 | 
    17 def main(args=sys.argv[1:]):
 | 
| 
 | 
    18     """CLI"""
 | 
| 
 | 
    19 
 | 
| 
 | 
    20     # parse command line
 | 
| 
 | 
    21     parser = argparse.ArgumentParser(description=__doc__)
 | 
| 
 | 
    22     parser.add_argument('secret',
 | 
| 
 | 
    23                         help="k8s secret name")
 | 
| 
 | 
    24     options = parser.parse_args(args)
 | 
| 
 | 
    25 
 | 
| 
 | 
    26     # get JSON from `kubectl`
 | 
| 
 | 
    27     command = ['kubectl', 'get', 'secret', options.secret, '-o', 'json']
 | 
| 
 | 
    28     try:
 | 
| 
 | 
    29         output = subprocess.check_output(command)
 | 
| 
 | 
    30     except subprocess.CalledProcessError as e:
 | 
| 
 | 
    31         print (e)
 | 
| 
 | 
    32         sys.exit(e.returncode)
 | 
| 
 | 
    33     data = json.loads(output)['data']
 | 
| 
 | 
    34 
 | 
| 
 | 
    35     # decode them
 | 
| 
 | 
    36     output = {key: base64.b64decode(value)
 | 
| 
 | 
    37               for key, value in data.items()}
 | 
| 
 | 
    38 
 | 
| 
 | 
    39     # output them
 | 
| 
 | 
    40     print (json.dumps(output, indent=2, sort_keys=True))
 | 
| 
 | 
    41 
 | 
| 
 | 
    42 
 | 
| 
 | 
    43 if __name__ == '__main__':
 | 
| 
 | 
    44     main()
 |