forked from TerryHowe/ansible-modules-hashivault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashivault_approle_role.py
207 lines (196 loc) · 7.48 KB
/
hashivault_approle_role.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
#!/usr/bin/python
from ansible.module_utils.hashivault import hashivault_argspec
from ansible.module_utils.hashivault import hashivault_auth_client
from ansible.module_utils.hashivault import hashivault_init
from ansible.module_utils.hashivault import hashiwrapper
import json
ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'}
DOCUMENTATION = '''
---
module: hashivault_approle_role
version_added: "4.0.0"
short_description: Hashicorp Vault approle management role module
description:
- Module to manage an approle role from Hashicorp Vault.
options:
state:
description:
- present or absent
default: present
name:
description:
- role name.
mount_point:
description:
- mount point for role
default: approle
bind_secret_id:
description:
- Require secret_id to be presented when logging in using this AppRole.
secret_id_bound_cidrs:
description:
- Comma-separated string or list of CIDR blocks.
secret_id_num_uses:
description:
- Number of times any particular SecretID can be used.
secret_id_ttl:
description:
- Duration after which any SecretID expires.
enable_local_secret_ids:
description:
- If set, the secret IDs generated using this role will be cluster local.
token_ttl:
description:
- Duration to set as the TTL for issued tokens and at renewal time.
token_max_ttl:
description:
- Duration after which the issued token can no longer be renewed.
policies:
description:
- Policies for the role.
token_policies:
description:
- Policies for the role.
bound_cidr_list:
description:
- Deprecated. Use token_bound_cidrs instead. Comma-separated string or list of CIDR blocks.
token_bound_cidrs:
description:
- Comma-separated string or list of CIDR blocks.
token_explicit_max_ttl:
description:
- Encode this value onto the token.
token_no_default_policy:
description:
- Default policy will not be set on generated tokens.
token_num_uses:
description:
- Number of times issued tokens can be used. A value of 0 means unlimited uses.
period:
description:
- Duration of the token generated.
token_period:
description:
- Duration of the token generated.
token_type:
description:
- Type of token that should be generated, normally `service`, `batch` or `default`.
extends_documentation_fragment: hashivault
'''
EXAMPLES = '''
---
- hosts: localhost
tasks:
- hashivault_approle_role:
name: ashley
- hashivault_approle_role:
name: ashley
state: absent
- hashivault_approle_role:
name: terry
role_file: path/to/file.json
'''
def main():
argspec = hashivault_argspec()
argspec['state'] = dict(required=False, choices=['present', 'absent'], default='present')
argspec['name'] = dict(required=True, type='str')
argspec['role_file'] = dict(required=False, type='str')
argspec['mount_point'] = dict(required=False, type='str', default='approle')
argspec['bind_secret_id'] = dict(required=False, type='bool', no_log=True)
argspec['secret_id_bound_cidrs'] = dict(required=False, type='list')
argspec['secret_id_num_uses'] = dict(required=False, type='str')
argspec['secret_id_ttl'] = dict(required=False, type='str')
argspec['enable_local_secret_ids'] = dict(required=False, type='bool')
argspec['token_ttl'] = dict(required=False, type='str')
argspec['token_max_ttl'] = dict(required=False, type='str')
argspec['policies'] = dict(required=False, type='list')
argspec['token_policies'] = dict(required=False, type='list', default=[])
argspec['token_bound_cidrs'] = dict(required=False, type='list')
argspec['bound_cidr_list'] = dict(required=False, type='list')
argspec['token_explicit_max_ttl'] = dict(required=False, type='str')
argspec['token_no_default_policy'] = dict(required=False, type='bool')
argspec['token_num_uses'] = dict(required=False, type='int')
argspec['period'] = dict(required=False, type='str')
argspec['token_period'] = dict(required=False, type='str')
argspec['token_type'] = dict(required=False, type='str')
module = hashivault_init(argspec, supports_check_mode=True)
result = hashivault_approle_role(module)
if result.get('failed'):
module.fail_json(**result)
else:
module.exit_json(**result)
@hashiwrapper
def hashivault_approle_role(module):
params = module.params
state = params.get('state')
role_file = params.get('role_file')
mount_point = params.get('mount_point')
name = params.get('name')
bound_cidr_depr = params.get('bound_cidr_list')
if bound_cidr_depr is not None and len(bound_cidr_depr) > 0:
module.warn("parameter bound_cidr_list is deprecated, use token_bound_cidrs instead")
params['token_bound_cidrs'] = bound_cidr_depr
client = hashivault_auth_client(params)
if state == 'present':
args = [
'bind_secret_id',
'secret_id_bound_cidrs',
'secret_id_num_uses',
'secret_id_ttl',
'enable_local_secret_ids',
'token_ttl',
'token_max_ttl',
'policies',
'token_policies',
'token_bound_cidrs',
'token_explicit_max_ttl',
'token_no_default_policy',
'token_num_uses',
'period',
'token_period',
'token_type',
]
desired_state = {}
if role_file:
try:
desired_state = json.loads(open(params.get('role_file'), 'r').read())
except Exception as e:
return {'changed': False, 'failed': True,
'msg': 'Error opening role file <%s>: %s' % (params.get('role_file'), str(e))}
else:
for arg in args:
value = params.get(arg)
if value is not None:
desired_state[arg] = value
try:
previous_state = client.auth.approle.read_role(name, mount_point=mount_point)
except Exception:
if not module.check_mode:
client.auth.approle.create_or_update_approle(name, mount_point=mount_point, **desired_state)
return {'changed': True}
changed = False
missing = []
current_data = previous_state.get('data', {})
for key in desired_state:
if key in current_data:
if current_data[key] != desired_state[key]:
changed = True
else:
missing.append(key)
changed = True
if not changed:
return {'changed': False, 'missing': missing, 'previous_state': previous_state}
if not module.check_mode:
client.auth.approle.create_or_update_approle(name, mount_point=mount_point, **desired_state)
return {'changed': True, 'missing': missing}
if module.check_mode:
try:
client.auth.approle.read_role(name, mount_point=mount_point)
except Exception:
return {'changed': False}
return {'changed': True}
else:
client.auth.approle.delete_role(name, mount_point=mount_point)
return {'changed': True}
if __name__ == '__main__':
main()