Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3.  
  4. from ansible.parsing.vault import PromptVaultSecret, VaultLib
  5. from ruamel.yaml import YAML
  6.  
  7. old_secret = PromptVaultSecret(prompt_formats=["Old password: "])
  8. old_secret.load()
  9.  
  10. new_secret = PromptVaultSecret(prompt_formats=["New password: "])
  11. new_secret.load()
  12.  
  13. vl = VaultLib(secrets=[
  14. (None, old_secret)
  15. ])
  16.  
  17.  
  18. class VaultSecret:
  19. yaml_tag = u'!vault'
  20.  
  21. def __init__(self, secret):
  22. self.secret = secret
  23.  
  24. def __repr__(self):
  25. return '**SECRET**'
  26.  
  27. @classmethod
  28. def to_yaml(cls, representer, node):
  29. assert isinstance(node, VaultSecret)
  30. return representer.represent_scalar(cls.yaml_tag, vl.encrypt(node.secret, new_secret).decode('utf-8'), style='|')
  31.  
  32. @classmethod
  33. def from_yaml(cls, constructor, node):
  34. return VaultSecret(vl.decrypt(node.value))
  35.  
  36.  
  37. yaml = YAML()
  38. yaml.indent(mapping=2, sequence=4, offset=2)
  39. yaml.register_class(VaultSecret)
  40.  
  41. with open(sys.argv[1], 'r') as orig:
  42. y = yaml.load(orig)
  43.  
  44. with open(sys.argv[1], 'w') as dest:
  45. yaml.dump(y, dest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement