SHARE
TWEET

Untitled

a guest May 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. import subprocess
  5. import tempfile
  6. import yaml
  7.  
  8.  
  9. VAULT_BIN = "/usr/local/bin/vault"
  10. VAULT_PATH = "secret/salt/pillar_data"
  11. DEFAULT_EDITOR = "/usr/bin/emacs"
  12.  
  13.  
  14. def main():
  15.     if 'VAULT_ADDR' not in os.environ:
  16.         print("you do not have $VAULT_ADDR set")
  17.         print("remember to set that and authenticate yourself before running this")
  18.         return
  19.  
  20.     # read existing data from vault
  21.     v = subprocess.check_output("%s read -format=yaml %s" % (VAULT_BIN, VAULT_PATH), shell=True)
  22.     d = yaml.load(v)['data']
  23.     print("--- read data from vault ---")
  24.  
  25.     # save a YAML version to temp file
  26.     with tempfile.NamedTemporaryFile(suffix='yaml') as t:
  27.         t.write(yaml.dump(d, indent=2, default_flow_style=False))
  28.         t.flush()
  29.         print("--- wrote yaml temp file ---")
  30.  
  31.         # pop the user into an editor to modify
  32.         editor = os.environ.get('EDITOR', DEFAULT_EDITOR)
  33.         subprocess.call([editor, t.name])
  34.  
  35.         # read it back in
  36.         t.seek(0, 0)
  37.         text = t.read()
  38.     try:
  39.         updated = yaml.load(text)
  40.         print("--- read the yaml back in and parsed it ---")
  41.         # write it out as json (vault can only read json)
  42.         with tempfile.NamedTemporaryFile() as json_out:
  43.             json_out.write(json.dumps(updated))
  44.             json_out.flush()
  45.             print("--- wrote out a JSON version ---")
  46.  
  47.             # then put it back into vault
  48.             subprocess.call("%s write %s @%s" % (VAULT_BIN, VAULT_PATH, json_out.name), shell=True)
  49.     except yaml.YAMLError, exc:
  50.         print("YAML Error:", exc)
  51.         print("update aborted. please try again")
  52.  
  53. if __name__ == "__main__":
  54.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top