annotate python/k8sdecode.py @ 887:79eeb5390c8a

hgignore
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Apr 2021 14:27:57 -0700
parents 46674fb64686
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
791
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 get a kubernetes secret and decode it
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import base64
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import json
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import os
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import subprocess
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import sys
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def main(args=sys.argv[1:]):
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 """CLI"""
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 # parse command line
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 parser = argparse.ArgumentParser(description=__doc__)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 parser.add_argument('secret',
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 help="k8s secret name")
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 options = parser.parse_args(args)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 # get JSON from `kubectl`
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 command = ['kubectl', 'get', 'secret', options.secret, '-o', 'json']
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 try:
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 output = subprocess.check_output(command)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 except subprocess.CalledProcessError as e:
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 print (e)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 sys.exit(e.returncode)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 data = json.loads(output)['data']
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 # decode them
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 output = {key: base64.b64decode(value)
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 for key, value in data.items()}
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 # output them
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 print (json.dumps(output, indent=2, sort_keys=True))
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 if __name__ == '__main__':
46674fb64686 secret k8s decoder ring
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 main()