-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchbase_rbac.py
executable file
·536 lines (465 loc) · 18.9 KB
/
couchbase_rbac.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env python
# Change log:
# 2019-07-02: Initial commit
# 2020-09-29: Add support for 6.5.x: LDAP integration
#
# Copyright: (c) 2019, Michael Hirschberg <[email protected]>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# couchbase_rbac is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# couchbase_rbac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details:
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['stableinterface'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: couchbase_rbac
short_description: Manage Couchbase Users
description:
- The M(couchbase_rbac) module can create/change/remove Couchbase internal/external users
author:
- "Michael Hirschberg"
options:
cb_admin:
description:
- "Couchbase admin user"
required: true
admin_password:
description:
- "Couchbase admin password. Make sure you set no_log=True in your tasks!"
required: true
admin_port:
description:
- "Couchbase admin port"
required: false
default: 8091
nodes:
description:
- List of all nodes in the cluster
required: true
ldap_enabled:
description:
- Enable LDAP authentication. Can't be used along with 'state'
required: false
default: false
ldap_hosts:
description:
- (couchbase 6.5.1+) The ldap hosts (IPs or DNS) to authenticate against separated by a comma
required: false
ldap_port:
description:
- (couchbase 6.5.1+) The ldap port to use
required: false
ldap_encryption:
description:
- (couchbase 6.5.1+) Security used to communicate with LDAP servers.
required: false
choices: ["tls", "startTLS", "none"]
default: "none"
ldap_user_dn_template:
description:
- (couchbase 6.5.1+) LDAP query to get user's DN.
ldap_cacert_path:
description:
- (couchbase 6.5.1+) CA certificate to be used for server's certificate validation
required: false
ldap_bind_dn:
description:
- (couchbase 6.5.1+) The DN of a user to authenticate as to allow user search and groups synchronization.
required: false
ldap_bind_password:
description:
- (couchbase 6.5.1+) The bind user password
required: false
state:
description:
- "Create/change user with 'state: present', remove user with 'state: absent'. Use run_once: True in your tasks". Can't be used along with 'ldap_enabled'
reqired: false
default: present
choices: ["present", "absent"]
user_type:
description:
- If the user should local or external
required: false
default: local
choices: ["local", "external"]
rbac_user_name:
description:
- User name to be created/deleted. Mandatory if create_user or delete_user is set to True
rbac_user_password:
description:
- The RBAC user password, if user_type=local
required: false
rbac_roles:
description:
- A mandatory list of rbac user roles if creating a new user. For more information please refer to the official Couchbase documentation
required: false
rbac_group_name:
description:
- Group name to create/delete. Mandatory if creating or deleting a_group
rbac_group_roles:
description:
- A list of roles for the group creation
required: false
rbac_group_ldap_ref:
description:
- ldap mapping for the group
required: false
'''
EXAMPLES = '''
# nodes list is needed to reliably detect the cluster's orchestrator
- name: "Create user"
couchbase_cluster:
cb_admin: Administrator
admin_password: MySuperSecretPassword
nodes:
- node01
- node02
rbac_user_name: alice
rbac_user_password: SuperSecretThePassword123
rbac_roles:
- ro_admin
- replication_admin
run_once: True
no_log: True
- name: "Delete user"
couchbase_cluster:
cb_admin: Administrator
admin_password: MySuperSecretPassword
nodes:
- node01
- node02
state: absent
rbac_user_name: bobby
user_type: external
run_once: True
no_log: True
'''
RETURN = '''
action:
description: if the taken action has failed or not
returned: success
type: string
sample: cluster has been configured
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.couchbase_common import *
from time import sleep
import socket
import requests
import json
import os
import re
class Couchbase(object):
def __init__(self, module):
self.module = module
self.cb_admin = module.params['cb_admin']
self.admin_password = module.params['admin_password']
self.admin_port = module.params['admin_port']
self.nodes = module.params['nodes']
self.ldap_enabled = module.params['ldap_enabled']
self.ldap_hosts = module.params['ldap_hosts']
self.ldap_port = module.params['ldap_port']
self.ldap_encryption = module.params['ldap_encryption']
self.ldap_user_dn_template = module.params['ldap_user_dn_template']
self.ldap_cacert_path = module.params['ldap_cacert_path']
self.ldap_bind_dn = module.params['ldap_bind_dn']
self.ldap_bind_password = module.params['ldap_bind_password']
self.rbac_group_name = module.params['rbac_group_name']
self.rbac_group_roles = module.params['rbac_group_roles']
self.rbac_group_ldap_ref = module.params['rbac_group_ldap_ref']
self.state = module.params['state']
self.user_type = module.params['user_type']
self.rbac_user_name = module.params['rbac_user_name']
self.rbac_user_password = module.params['rbac_user_password']
self.rbac_roles = module.params['rbac_roles']
def enable_ldap(self):
changed = False
rc = 0
msg = ""
failed,masters,msg = map(check_new(self).get,('failed','orchestrators','msg'))
if self.ldap_enabled:
cmd = [
cbcli, 'setting-ldap',
'-c', masters['cluster'],
'--username=' + self.cb_admin, '--password=' + self.admin_password,
]
node_state = get_health(self)
if cb_version_split <= node_state['version']:
cmd.extend(['--authentication-enabled=1', '--authorization-enabled=1'])
if self.ldap_hosts and self.ldap_port:
cmd.extend(['--hosts', self.ldap_hosts,
'--port', self.ldap_port])
if self.ldap_encryption:
cmd.extend(['--encryption', self.ldap_encryption])
if self.ldap_cacert_path:
cmd.extend(['--ldap-cacert', self.ldap_cacert_path])
else: # if we didn't provide a CACERT path we disable certificate validation
cmd.extend(['--server-cert-validation=0'])
if self.ldap_bind_dn:
cmd.extend(['--bind-dn', self.ldap_bind_dn])
if self.ldap_bind_password:
cmd.extend(['--bind-password', self.ldap_bind_password])
if self.ldap_user_dn_template:
cmd.extend(['--user-dn-template', self.ldap_user_dn_template])
else:
cmd.extend(['--ldap-enabled=1'])
rc, stdout, stderr = self.module.run_command(cmd)
if rc == 0:
changed = True
else:
failed = True
msg = stdout + stderr
return dict(failed=failed, changed=changed, msg=msg)
def createUser(self):
changed = False
failed,masters,msg = map(check_new(self).get,('failed','orchestrators','msg'))
cmd = [
cbcli, 'user-manage',
'-c', masters['cluster'],
'--username=' + self.cb_admin, '--password=' + self.admin_password,
'--set',
'--rbac-username=' + self.rbac_user_name,
]
if self.user_type == "local":
cmd.append("--rbac-password=" + self.rbac_user_password)
cmd.extend([
'--roles=' + ','.join(self.rbac_roles),
'--auth-domain=' + self.user_type
])
rc, stdout, stderr = self.module.run_command(cmd)
if rc == 0:
changed = True
msg = msg + " User has been created."
else:
failed = True
msg = msg + stderr
return dict(failed=failed, changed=changed, msg=msg)
def deleteUser(self):
changed = False
failed,masters,msg = map(check_new(self).get,('failed','orchestrators','msg'))
cmd = [
cbcli, 'user-manage',
'-c', masters['cluster'],
'--username=' + self.cb_admin, '--password=' + self.admin_password,
'--delete',
'--rbac-username=' + self.rbac_user_name,
'--auth-domain=' + self.user_type
]
rc, stdout, stderr = self.module.run_command(cmd)
if rc == 0:
changed = True
msg = msg + " User has been removed."
else:
failed = True
msg = msg + stderr
return dict(failed=failed, changed=changed, msg=msg)
def createGroup(self):
changed = False
failed, masters, msg = map(check_new(self).get, ('failed', 'orchestrators', 'msg'))
cmd = [
cbcli, 'user-manage',
'-c', masters['cluster'],
'--username=' + self.cb_admin, '--password=' + self.admin_password,
'--set-group',
'--group-name=' + self.rbac_group_name,
]
if self.rbac_group_roles != "":
cmd.extend(['--roles=' + ','.join(self.rbac_group_roles)])
if self.rbac_group_ldap_ref != "":
cmd.extend(['--ldap-ref', self.rbac_group_ldap_ref])
rc, stdout, stderr = self.module.run_command(cmd)
if rc == 0:
changed = True
else:
failed = True
msg = msg + stderr
return dict(failed=failed, changed=changed, msg=msg)
def deleteGroup(self):
changed = False
failed, masters, msg = map(check_new(self).get, ('failed', 'orchestrators', 'msg'))
cmd = [
cbcli, 'user-manage',
'-c', masters['cluster'],
'--username=' + self.cb_admin, '--password=' + self.admin_password,
'--delete-group',
'--group-name=' + self.rbac_group_name
]
rc, stdout, stderr = self.module.run_command(cmd)
if rc == 0:
changed = True
msg = msg + " Group has been removed."
else:
failed = True
msg = msg + stderr
return dict(failed=failed, changed=changed, msg=msg)
### Check functions --> ###
def check_ldap(self):
# Not possible to verify if LDAP is enabled -- we just always return changed=True
if self.ldap_enabled:
changed = True
failed = False
msg = "LDAP will be enabled no matter what. "
return dict(failed=failed, changed=changed, msg=msg)
def check_group(self):
changed = False
failed = False
msg = "Everything is configured as requeted"
all_users = get_users(self)
# Verify creation
if self.state == "present":
if self.rbac_group_name != "":
# No groups exist, a group should be created
if all_users['groups'] == []:
changed = True
msg = " A new group will be created "
else:
new_grp = True
for group in all_users['groups']:
# Groups exist, verify their names. If match, check the ldap ref and mapping if defined
if group['name'] == self.rbac_group_name:
new_grp = False
if self.rbac_group_ldap_ref != group['ldap_group_ref']:
changed = True
msg = msg + " LDAP mapping will be changed"
if sorted(self.rbac_group_roles) != sorted(group['roles']):
changed = True
msg = msg + " RBAC group roles will be changed"
# Group doesn't exist, has to be created
changed = True
msg = " A new group will be created"
# Validate removal
if self.state == "absent":
if self.rbac_group_name == "":
failed = True
msg = "Please provide a group name to be removed"
else:
for group in all_users['groups']:
if group['name'] == self.rbac_group_name:
changed = True
msg = "A group will be removed"
return dict(failed=failed, changed=changed, msg=msg)
def check_user(self):
# This function is NOT verifying user's password -- because there's no way how to.
changed = False
failed = False
msg = ""
failed,masters,msg = map(check_new(self).get,('failed','orchestrators','msg'))
cmd = [
cbcli, 'user-manage',
'-c', masters['cluster'],
'--list',
'--username=' + self.cb_admin, '--password=' + self.admin_password,
]
rc, stdout, stderr = self.module.run_command(cmd)
if rc != 0:
failed = True
elif self.state == "present":
name_ok = False
type_ok = False
role_ok = False
my_roles = []
for item in json.loads(stdout):
if item['id'] == self.rbac_user_name:
msg = msg + "User OK. "
name_ok = True
for role in item['roles']:
if len(role) > 1:
my_roles.append(role.values()[1] + "[" + role.values()[0] + "]")
else:
my_roles.append(role['role'])
if item['domain'] == self.user_type:
msg = msg + "User type OK. "
type_ok = True
if not name_ok:
msg = msg + "User not found. "
if not type_ok:
msg = msg + "Wrong user type. "
if sorted(my_roles) == sorted(self.rbac_roles):
msg = msg + "All roles found. "
role_ok = True
else:
msg = msg + "Not all roles found. "
if not name_ok or not type_ok or not role_ok:
changed = True
elif self.state == "absent":
for item in json.loads(stdout):
if item['id'] == self.rbac_user_name:
msg = msg + "User will be removed. "
changed = True
else:
msg = msg + "User not found anyway"
return dict(failed=failed, changed=changed, msg=msg)
### <-- Check functions ###
def execute(self):
if self.ldap_enabled:
failed,changed,msg = map(self.enable_ldap().get, ('failed','changed','msg'))
return dict(failed=failed,changed=changed,msg=msg)
if self.state == "absent":
if self.rbac_user_name != "":
failed,changed,msg = map(self.check_user().get, ('failed','changed','msg'))
if not failed and changed:
failed,changed,msg = map(self.deleteUser().get, ('failed','changed','msg'))
if self.rbac_group_name != "":
failed,changed,msg = map(self.check_group().get, ('failed','changed','msg'))
if not failed and changed:
failed,changed,msg = map(self.deleteGroup().get, ('failed','changed','msg'))
return dict(failed=failed,changed=changed,msg=msg)
# Because there is no way how to verify user's password, let's say it's always runs and always "changed".
if self.state == "present":
if self.rbac_user_name != "":
failed,changed,msg = map(self.createUser().get, ('failed','changed','msg'))
if self.rbac_group_name != "":
failed,changed,msg = map(self.check_group().get, ('failed','changed','msg'))
if changed and not failed:
failed,changed,msg = map(self.createGroup().get, ('failed','changed','msg'))
return dict(failed=failed,changed=changed,msg=msg)
def main():
fields = dict(
# Common things
cb_admin=dict(required=True),
admin_password=dict(required=True),
nodes=dict(required=True, type="list"),
admin_port=dict(default="8091"),
# RBAC config
ldap_enabled=dict(required=False, default=False, type="bool"),
ldap_hosts=dict(required=False),
ldap_port=dict(required=False),
ldap_encryption=dict(required=False, default="none", choices=["tls", "startTLS", "none"]),
ldap_user_dn_template=dict(required=False),
ldap_cacert_path=dict(required=False),
ldap_bind_dn=dict(required=False),
ldap_bind_password=dict(required=False),
rbac_group_name=dict(required=False, default=""),
rbac_group_roles=dict(required=False, default=[], type="list"),
rbac_group_ldap_ref=dict(required=False, default=""),
state=dict(required=False, default="present", choices=["present", "absent"]),
user_type=dict(required=False, default="local", choices=["local", "external"]),
rbac_user_name=dict(required=False, default=""),
rbac_user_password=dict(required=False),
rbac_roles=dict(required=False, default=[], type="list")
)
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
if module.check_mode:
if module.params['ldap_enabled'] == True:
result = Couchbase(module).check_ldap()
module.exit_json(**result)
if module.params['state'] == "present" or module.params['state'] == "absent":
result = Couchbase(module).check_user()
module.exit_json(**result)
result = Couchbase(module).execute()
module.exit_json(**result)
if __name__ == '__main__':
main()