forked from TerryHowe/ansible-modules-hashivault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashivault.py
401 lines (342 loc) · 14.2 KB
/
hashivault.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os
import hvac
import requests
from ansible.module_utils.basic import AnsibleModule, env_fallback
from hvac.exceptions import InvalidPath
normalize = {'list': list, 'str': str, 'dict': dict, 'bool': bool, 'int': int, 'duration': str}
def hashivault_argspec():
argument_spec = dict(
url=dict(required=False, default=os.environ.get('VAULT_ADDR', ''), type='str'),
ca_cert=dict(required=False, default=os.environ.get('VAULT_CACERT', ''), type='str'),
ca_path=dict(required=False, default=os.environ.get('VAULT_CAPATH', ''), type='str'),
client_cert=dict(required=False, default=os.environ.get('VAULT_CLIENT_CERT', ''), type='str'),
client_key=dict(required=False, default=os.environ.get('VAULT_CLIENT_KEY', ''), type='str'),
verify=dict(required=False, default=(not os.environ.get('VAULT_SKIP_VERIFY', '')), type='bool'),
authtype=dict(required=False, default=os.environ.get('VAULT_AUTHTYPE', 'token'), type='str'),
login_mount_point=dict(required=False, default=os.environ.get('VAULT_LOGIN_MOUNT_POINT', None), type='str'),
token=dict(required=False, fallback=(hashivault_default_token, ''), type='str', no_log=True),
username=dict(required=False, default=os.environ.get('VAULT_USER', ''), type='str'),
password=dict(required=False, fallback=(env_fallback, ['VAULT_PASSWORD']), type='str', no_log=True),
role_id=dict(required=False, fallback=(env_fallback, ['VAULT_ROLE_ID']), type='str', no_log=True),
secret_id=dict(required=False, fallback=(env_fallback, ['VAULT_SECRET_ID']), type='str', no_log=True),
aws_header=dict(required=False, fallback=(env_fallback, ['VAULT_AWS_HEADER']), type='str', no_log=True),
namespace=dict(required=False, default=os.environ.get('VAULT_NAMESPACE', None), type='str'),
timeout=dict(required=False, default=30, type=int)
)
return argument_spec
def hashivault_init(argument_spec, supports_check_mode=False, required_if=None, required_together=None,
required_one_of=None, mutually_exclusive=None):
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=supports_check_mode,
required_if=required_if, required_together=required_together,
required_one_of=required_one_of, mutually_exclusive=mutually_exclusive)
module.no_log_values.discard('0')
module.no_log_values.discard(0)
module.no_log_values.discard('1')
module.no_log_values.discard(1)
module.no_log_values.discard(True)
module.no_log_values.discard(False)
module.no_log_values.discard('ttl')
return module
def hashivault_normalize_from_doc(module, options, documentation):
desired_state = {}
for key, value in options.items():
config_type = documentation.get(key, {}).get('type')
if config_type is None:
module.warn('Unknown option "{}". Make sure this is not a typo, if it is not, please open an '
'issue at https://github.com/TerryHowe/ansible-modules-hashivault/issues.'.format(key))
elif value is not None:
try:
value = normalize[config_type](value)
except Exception as e:
raise Exception({
'changed': False,
'failed': True,
'msg':
'config item \'{}\' with value \'{}\' could not be converted to \'{}\': {}'
.format(key, value, config_type, "\n".join(e.args))})
desired_state[key] = value
return desired_state
def get_ec2_iam_role():
request = requests.get(url='http://169.254.169.254/latest/meta-data/iam/security-credentials/')
request.raise_for_status()
return request.content
def get_ec2_iam_credentials(header_value, role_id):
role_name = get_ec2_iam_role()
metadata_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/{role}'.format(
role=role_name
)
response = requests.get(url=metadata_url)
response.raise_for_status()
credentials = response.json()
return dict(
access_key=credentials['AccessKeyId'],
secret_key=credentials['SecretAccessKey'],
session_token=credentials['Token'],
header_value=header_value,
role=role_id
)
def hashivault_client(params):
url = params.get('url')
ca_cert = params.get('ca_cert')
ca_path = params.get('ca_path')
client_cert = params.get('client_cert')
client_key = params.get('client_key')
cert = (client_cert, client_key)
check_verify = params.get('verify')
namespace = params.get('namespace', None)
timeout = params.get('timeout')
if check_verify == '' or check_verify:
if ca_cert:
verify = ca_cert
elif ca_path:
verify = ca_path
else:
verify = check_verify
else:
verify = check_verify
client = hvac.Client(url=url, cert=cert, verify=verify, namespace=namespace, timeout=timeout)
return client
def hashivault_auth(client, params):
token = params.get('token')
authtype = params.get('authtype')
login_mount_point = params.get('login_mount_point', authtype)
if not login_mount_point:
login_mount_point = authtype
username = params.get('username')
password = params.get('password')
secret_id = params.get('secret_id')
role_id = params.get('role_id')
if authtype == 'github':
client.auth.github.login(token, mount_point=login_mount_point)
elif authtype == 'userpass':
client.auth.userpass.login(username, password, mount_point=login_mount_point)
elif authtype == 'ldap':
client.auth.ldap.login(username, password, mount_point=login_mount_point)
elif authtype == 'approle':
client = AppRoleClient(client, role_id, secret_id, mount_point=login_mount_point)
elif authtype == 'tls':
client.auth_tls()
elif authtype == 'aws':
credentials = get_ec2_iam_credentials(params.get['aws_header'], role_id)
client.auth_aws_iam(**credentials)
else:
client.token = token
return client
def hashivault_auth_client(params):
client = hashivault_client(params)
return hashivault_auth(client, params)
def hashiwrapper(function):
def wrapper(*args, **kwargs):
result = {"changed": False, "rc": 0}
result.update(function(*args, **kwargs))
return result
return wrapper
def hashivault_default_token():
"""Get a default Vault token from an environment variable or a file."""
if 'VAULT_TOKEN' in os.environ:
return os.environ['VAULT_TOKEN']
token_file = os.path.expanduser('~/.vault-token')
if os.path.exists(token_file):
with open(token_file, 'r') as f:
return f.read().strip()
return ''
@hashiwrapper
def hashivault_read(params):
result = {"changed": False, "rc": 0}
client = hashivault_auth_client(params)
version = params.get('version')
mount_point = params.get('mount_point')
secret = params.get('secret')
secret_version = params.get('secret_version')
key = params.get('key')
default = params.get('default')
if secret.startswith('/'):
secret = secret.lstrip('/')
mount_point = ''
if mount_point:
secret_path = '%s/%s' % (mount_point, secret)
else:
secret_path = secret
try:
if version == 2:
response = client.secrets.kv.v2.read_secret_version(secret, mount_point=mount_point, version=secret_version)
else:
response = client.secrets.kv.v1.read_secret(secret, mount_point=mount_point)
except InvalidPath:
response = None
except Exception as e:
result['rc'] = 1
result['failed'] = True
error_string = "%s(%s)" % (e.__class__.__name__, e)
result['msg'] = u"Error %s reading %s" % (error_string, secret_path)
return result
if not response:
if default is not None:
result['value'] = default
return result
result['rc'] = 1
result['failed'] = True
result['msg'] = u"Secret %s is not in vault" % secret_path
return result
if version == 2:
try:
data = response.get('data', {})
metadata = data.get('metadata', {})
data = data.get('data', {})
except Exception:
data = str(response)
else:
data = response['data']
metadata = {}
lease_duration = response.get('lease_duration', None)
if lease_duration is not None:
result['lease_duration'] = lease_duration
lease_id = response.get('lease_id', None)
if lease_id is not None:
result['lease_id'] = lease_id
renewable = response.get('renewable', None)
if renewable is not None:
result['renewable'] = renewable
wrap_info = response.get('wrap_info', None)
if wrap_info is not None:
result['wrap_info'] = wrap_info
if key and key not in data:
if default is not None:
result['value'] = default
return result
result['rc'] = 1
result['failed'] = True
result['msg'] = u"Key %s is not in secret %s" % (key, secret_path)
return result
if key:
value = data[key]
else:
value = data
result['value'] = value
result['metadata'] = metadata
return result
class AppRoleClient(object):
"""
hvac.Client decorator which generates and sets a new approle token on every
function call. This allows multiple calls to Vault without having to manually
generate and set a token on every Vault call.
"""
def __init__(self, client, role_id, secret_id, mount_point):
object.__setattr__(self, 'client', client)
object.__setattr__(self, 'role_id', role_id)
object.__setattr__(self, 'secret_id', secret_id)
object.__setattr__(self, 'login_mount_point', mount_point)
def __setattr__(self, name, val):
"""
sets attribute in decorated class (Client)
"""
client = object.__getattribute__(self, 'client')
client.__setattr__(name, val)
def __getattribute__(self, name):
"""
generates and sets new approle token in decorated class (Client)
returns decorated class (Client) attribute
"""
client = object.__getattribute__(self, 'client')
attr = client.__getattribute__(name)
role_id = object.__getattribute__(self, 'role_id')
secret_id = object.__getattribute__(self, 'secret_id')
login_mount_point = object.__getattribute__(self, 'login_mount_point')
resp = client.auth.approle.login(role_id, secret_id=secret_id, mount_point=login_mount_point)
client.token = str(resp['auth']['client_token'])
return attr
def _compare_state(desired_state, current_state, ignore=None):
"""Compares desired state to current state. Returns true if objects are equal
Recursively walks dict object to compare all keys
:param desired_state: The state user desires.
:param current_state: The state that currently exists.
:param ignore: Ignore these keys.
:type ignore: list
:return: True if the states are the same.
:rtype: bool
"""
if ignore is None:
ignore = []
if (type(desired_state) is list):
if ((type(current_state) != list) or (len(desired_state) != len(current_state))):
return False
for i in range(len(desired_state)):
if (not _compare_state(desired_state[i], current_state[i])):
return False
return True
if (type(desired_state) is dict):
if (type(current_state) != dict):
return False
# iterate over dictionary keys
for key in desired_state.keys():
if key in ignore:
continue
v = desired_state[key]
if ((key not in current_state) or (not _compare_state(v, current_state.get(key)))):
return False
return True
# lots of things get handled as strings in ansible that aren't necessarily strings, can extend this list later.
if isinstance(desired_state, str) and isinstance(current_state, int):
current_state = str(current_state)
return ((desired_state == current_state))
def _convert_to_seconds(original_value):
try:
value = str(original_value)
seconds = 0
if 'h' in value:
ray = value.split('h')
seconds = int(ray.pop(0)) * 3600
value = ''.join(ray)
if 'm' in value:
ray = value.split('m')
seconds += int(ray.pop(0)) * 60
value = ''.join(ray)
if value:
ray = value.split('s')
seconds += int(ray.pop(0))
return seconds
except Exception:
pass
return original_value
def get_keys_updated(desired_state, current_state, ignore=None):
"""Return list of keys that have different values
Recursively walks dict object to compare all keys
:param desired_state: The state user desires.
:type desired_state: dict
:param current_state: The state that currently exists.
:type current_state: dict
:param ignore: Ignore these keys.
:type ignore: list
:return: Different items
:rtype: list
"""
if ignore is None:
ignore = []
differences = []
for key in desired_state.keys():
if key in ignore:
continue
if (key not in current_state):
differences.append(key)
continue
new_value = desired_state[key]
old_value = current_state[key]
if 'ttl' in key:
if _convert_to_seconds(old_value) != _convert_to_seconds(new_value):
differences.append(key)
elif not _compare_state(new_value, old_value):
differences.append(key)
return differences
def is_state_changed(desired_state, current_state, ignore=None):
"""Return list of keys that have different values
Recursively walks dict object to compare all keys
:param desired_state: The state user desires.
:type desired_state: dict
:param current_state: The state that currently exists.
:type current_state: dict
:param ignore: Ignore these keys.
:type ignore: list
:return: Different
:rtype: bool
"""
return(len(get_keys_updated(desired_state, current_state)) > 0)