-
Notifications
You must be signed in to change notification settings - Fork 7
/
kv_recursive.py
201 lines (163 loc) · 7.51 KB
/
kv_recursive.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python3
import hvac
import requests
import urllib3
import argparse
# Wrapper methods
def list_recursive(client, path, kv_version, source_mount):
seed_list = list_path(client, path, kv_version, source_mount)
for i, li in enumerate(seed_list):
seed_list[i] = (path + li)
final_list = recursive_path_builder(client, seed_list, kv_version, source_mount)
return final_list
def delete_recursive(client, path, kv_version, source_mount):
kv_list = list_recursive(client, path, kv_version, source_mount)
delete_secrets_from_list(client, kv_list, kv_version, source_mount)
def read_recursive(client, path, kv_version, source_mount):
kv_list = list_recursive(client, path, kv_version, source_mount)
secrets_list = read_secrets_from_list(client, kv_list, kv_version, source_mount)
return secrets_list
def migrate_secrets(src_client, dest_client, src_path, source_mount, dest_mount, dest_path='', kv_version=1):
kv_list = read_recursive(src_client, src_path, kv_version, source_mount)
write_secrets_from_list(dest_client, kv_list, dest_path, src_path, kv_version, dest_mount)
print("Secrets copied: ", len(kv_list))
# Construction Methods
def recursive_path_builder(client, kv_list, kv_version, source_mount):
change = 0
# if any list items end in '/' return 1
for li in kv_list[:]:
if li[-1] == '/':
append_list = list_path(client, li, kv_version, source_mount)
for new_item in append_list:
kv_list.append(li + new_item)
# remove list item ending in '/'
kv_list.remove(li)
change = 1
# new list items added, rerun search
if change == 1:
recursive_path_builder(client, kv_list, kv_version, source_mount)
return kv_list
def list_path(client, path, kv_version, source_mount):
if kv_version == 2:
return client.secrets.kv.v2.list_secrets(path, mount_point=source_mount)['data']['keys']
elif kv_version == 1:
return client.secrets.kv.v1.list_secrets(path, mount_point=source_mount)['data']['keys']
def read_secrets_from_list(client, kv_list, kv_version, source_mount):
for i, li in enumerate(kv_list[:]):
k = kv_list[i]
if kv_version == 2:
v = client.secrets.kv.v2.read_secret_version(k, mount_point=source_mount,
raise_on_deleted_version=True)['data']['data']
elif kv_version == 1:
v = client.secrets.kv.v1.read_secret(k, mount_point=source_mount)['data']
kv_list[i] = {k: v}
return kv_list
def write_secrets_from_list(client, kv_list, dest_path, src_path, kv_version, dest_mount):
for li in kv_list:
sname = list(li)[0]
short_name = sname.replace(src_path, '')
if kv_version == 2:
client.secrets.kv.v2.create_or_update_secret(
path=(dest_path + short_name),
secret=li[sname],
mount_point=dest_mount
)
elif kv_version == 1:
client.secrets.kv.v1.create_or_update_secret(
path=(dest_path + short_name),
secret=li[sname],
mount_point=dest_mount
)
# this expects the secret to be in the json blob - need to fix
def delete_secrets_from_list(client, kv_list, kv_version, source_mount):
for li in kv_list:
if kv_version == 2:
client.secrets.kv.v2.delete_metadata_and_all_versions(path=li, mount_point=source_mount)
if kv_version == 1:
client.secrets.kv.v1.delete_secret(path=li, mount_point=source_mount)
def ensure_trailing_slash(s):
if s != '':
if s[-1] != '/':
s += '/'
return s
def main():
pass
# example run vars
# path = 'drew/' #must end in /
# path2 = 'nested/'
# client = hvac.Client(url='https://127.0.0.1:8200', token='<redacted>', verify=False, namespace="ns1")
# client2 = hvac.Client(url='https://127.0.0.1:8200', token='<redacted>', verify=False, namespace="kv1")
# #client = hvac.Client(url='https://vault.example.com', token='abc123', verify=False,
# namespace='namespace/child_namespace/sub_child_namespace')
# migrate_secrets(client, client2, path, path2, kv_version=1)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Recursively interact with Hashicorp Vault KV mount')
parser.add_argument('action', choices=['copy', 'move', 'delete', 'list', 'read',
'count'], default='list', metavar='ACTION')
parser.add_argument('--tls-skip-verify', action='store_false')
parser.add_argument('--source-path', '-s', default='')
parser.add_argument('--source-url', '-su', required=True)
parser.add_argument('--source-token', '-st', required=True)
parser.add_argument('--source-namespace', '-sns', default='')
parser.add_argument('--source-mount', '-sm', default='kv-v2')
parser.add_argument('--destination-path', '-d')
parser.add_argument('--destination-url', '-du')
parser.add_argument('--destination-token', '-dt')
parser.add_argument('--destination-namespace', '-dns', default='')
parser.add_argument('--kv-version', '-kvv', type=int, default=2, choices=[1, 2])
parser.add_argument('--destination-mount', '-dm', default='kv-v2')
args = parser.parse_args()
if not args.destination_path:
args.destination_path = args.source_path
if not args.destination_url:
args.destination_url = args.source_url
if not args.destination_token:
args.destination_token = args.source_token
if not args.tls_skip_verify:
requests = requests.Session()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
args.destination_path = ensure_trailing_slash(args.destination_path)
args.source_path = ensure_trailing_slash(args.source_path)
source_client = hvac.Client(
url=args.source_url,
token=args.source_token,
verify=args.tls_skip_verify,
namespace=args.source_namespace,
strict_http=True
)
destination_client = hvac.Client(
url=args.destination_url,
token=args.destination_token,
verify=args.tls_skip_verify,
namespace=args.destination_namespace,
strict_http=True
)
if args.action == 'copy':
migrate_secrets(
source_client,
destination_client,
args.source_path,
args.source_mount,
args.destination_mount,
args.destination_path,
kv_version=args.kv_version
)
elif args.action == 'list':
print(list_recursive(source_client, args.source_path, args.kv_version, args.source_mount))
elif args.action == 'count':
print(len(list_recursive(source_client, args.source_path, args.kv_version, args.source_mount)))
elif args.action == 'read':
print(read_recursive(source_client, args.source_path, args.kv_version, args.source_mount))
elif args.action == 'delete':
delete_recursive(source_client, args.source_path, args.kv_version, args.source_mount)
elif args.action == 'move':
migrate_secrets(
source_client,
destination_client,
args.source_path,
args.source_mount,
args.destination_mount,
args.destination_path,
kv_version=args.kv_version
)
delete_recursive(source_client, args.source_path, args.kv_version, args.source_mount)