-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_vault_cli.py
executable file
·157 lines (123 loc) · 5.38 KB
/
kv_vault_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
import json
import datetime
import argparse
from libs.VaultClient import VaultClient
from libs.VaultManipulator import VaultManipulator
def delete_data_from_vault(vault_addr, vault_token, root_path=''):
vault = VaultClient(vault_addr, vault_token)
action = VaultManipulator(vault)
if root_path:
secret_root = {
root_path: {
'data': [],
'type': 'root'
}
}
else:
secret_root = action.get_kv_mounts()
to_delete = action.dump_secrets(secret_root, '')
action.full_delete(to_delete)
def dump_data_from_vault(vault_addr, vault_token, root_path='', file="vault_dump", time_stamp=True):
vault = VaultClient(vault_addr, vault_token)
action = VaultManipulator(vault)
if root_path:
secret_root = {
root_path: {
'data': [],
'type': 'root'
}
}
else:
secret_root = action.get_kv_mounts()
data = action.dump_secrets(secret_root)
if time_stamp:
file = f'{file}_{datetime.datetime.now().isoformat()}.json'
else:
file = f'{file}.json'
with open(file, 'w') as df:
df.write(json.dumps(data, indent=2,))
def restore_data_to_vault(vault_addr, vault_token, file="vault_dump"):
with open(file, 'r') as df:
data = json.load(df)
vault = VaultClient(vault_addr, vault_token)
action = VaultManipulator(vault)
action.restore_secrets(data)
def transfer_data(vault_addr_src, vault_token_src, vault_addr_dst, vault_token_dst, root_path='', not_merge=False):
vault_src = VaultClient(vault_addr_src, vault_token_src)
vault_dst = VaultClient(vault_addr_dst, vault_token_dst)
action_src = VaultManipulator(vault_src)
action_dst = VaultManipulator(vault_dst)
if root_path:
secret_root_src = {
root_path: {
'data': [],
'type': 'root'
}
}
else:
secret_root_src = action_src.get_kv_mounts()
data = action_src.dump_secrets(secret_root_src)
if not_merge:
secret_root_dst = action_dst.get_kv_mounts()
to_delete = action_dst.dump_secrets(secret_root_dst)
action_dst.full_delete(to_delete)
action_dst.restore_secrets(data)
else:
action_dst.restore_secrets(data)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('action', metavar='action', choices=['dump', 'restore', 'delete', 'transfer'],
type=str, help='Action: dump, restore, delete, transfer')
parser.add_argument('--vault-addr', help='Vault address', default='http://127.0.0.1:8200')
parser.add_argument('--vault-token', help='Vault token')
parser.add_argument('--vault-addr-src', help='Vault Source address')
parser.add_argument('--vault-addr-dst', help='Vault Destination addres')
parser.add_argument('--vault-token-src', help='Vault Source token')
parser.add_argument('--vault-token-dst', help='Vault Destination token')
parser.add_argument('--vault-root-path', help='Vault Root path', default='')
parser.add_argument('--file', help='File with secrets to restore')
parser.add_argument('--not-merge', action='store_true',
help='Do not merge')
args = parser.parse_args()
if args.action == 'dump':
if args.vault_addr and args.vault_token:
dump_data_from_vault(args.vault_addr, args.vault_token, args.vault_root_path)
else:
print('Usage: \n\n'
'data_vault_cli.py dump \\\n'
'\t\t--vault-addr=<vault address> \\\n'
'\t\t--vault-token=<vault token>\n\n'
'Dump Vault data into the file.')
elif args.action == 'restore':
if args.vault_addr and args.vault_token and args.file:
restore_data_to_vault(args.vault_addr, args.vault_token, args.file)
else:
print('Usage: \n\n'
'data_vault_cli.py restore \\\n'
'--vault-addr=<vault address> \\\n'
'\t\t--vault-token=<vault token> \\\n'
'\t\t--file=<file>\n\n'
'Restore Vault data from the file.')
elif args.action == 'delete':
if args.vault_addr and args.vault_token:
delete_data_from_vault(args.vault_addr, args.vault_token, args.vault_root_path)
else:
print('Usage: \n\n'
'data_vault_cli.py delete \\\n'
'\t\t--vault-addr=<vault address> \\\n'
'\t\t--vault-token=<vault token>\n\n'
'Full delete Vaults secrets.')
elif args.action == 'transfer':
if args.vault_addr_src and args.vault_token_src and args.vault_addr_dst and args.vault_token_dst:
transfer_data(args.vault_addr_src, args.vault_token_src,
args.vault_addr_dst, args.vault_token_dst,
args.vault_root_path, args.not_merge)
else:
print('Usage: \n\n'
'data_vault_cli.py transfer \\\n'
'\t\t--vault-addr-src=<vault address source> \\\n'
'\t\t--vault-token-src=<vault token source> \\\n'
'\t\t--vault-addr-dst=<vault address destination> \\\n'
'\t\t--vault-token-dst=<vault token destination>\n\n'
'Transfer data between Vaults.')