Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit c50b178

Browse files
committed
Add, modify, and clear up role mappings
1 parent f653b3e commit c50b178

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,33 @@ es_roles:
391391
- create_index
392392
```
393393

394+
* ```es_role_mappings``` - Elasticsearch role mappings can be declared here as yml. Each key is a name of a role mapping, with yaml formatted JSON defining the role mapping as described [here](https://www.elastic.co/guide/en/x-pack/current/mapping-roles.html) e.g.
395+
396+
```yaml
397+
es_role_mappings:
398+
groupname-editor:
399+
enabled: true
400+
roles:
401+
- editor
402+
rules:
403+
field:
404+
groups: "EditorGroup"
405+
groupname-admin:
406+
enabled: true
407+
roles:
408+
- editor
409+
rules:
410+
field:
411+
groups: "AdminGroup"
412+
realmname-viewer:
413+
enabled: true
414+
roles:
415+
- viewer
416+
rules:
417+
field:
418+
realm.name: realm1
419+
```
420+
394421
* ```es_xpack_license``` - X-Pack license. The license is a json blob. Set the variable directly (possibly protected by Ansible vault) or from a file in the Ansible project on the control machine via a lookup:
395422

396423
```yaml

defaults/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ es_ssl_verification_mode: "certificate"
6969
es_validate_certs: "yes"
7070
es_delete_unmanaged_file: true
7171
es_delete_unmanaged_native: true
72+
es_delete_unmanaged_role_mappings: true

tasks/main.yml

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
when: manage_native_realm | bool
104104
run_once: True
105105

106+
- name: include xpack/security/elasticsearch-security-role_mappings.yml
107+
include: ./xpack/security/elasticsearch-security-role_mappings.yml
108+
when: es_role_mappings is defined and es_role_mappings.keys() | list | length > 0
109+
run_once: True
110+
106111
#Templates done after restart - handled by flushing the handlers. e.g. suppose user removes security on a running node and doesn't specify es_api_basic_auth_username and es_api_basic_auth_password. The templates will subsequently not be removed if we don't wait for the node to restart.
107112
#We also do after the native realm to ensure any changes are applied here first and its denf up.
108113
- name: include elasticsearch-template.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
#List current role mappings
3+
- name: List Role Mappings
4+
uri:
5+
url: "{{ es_api_uri }}/{{ es_security_api }}/role_mapping"
6+
method: GET
7+
user: "{{es_api_basic_auth_username}}"
8+
password: "{{es_api_basic_auth_password}}"
9+
force_basic_auth: yes
10+
status_code: 200
11+
validate_certs: "{{ es_validate_certs }}"
12+
register: role_mapping_list_response
13+
check_mode: no
14+
15+
- name: set fact role_mappings_to_remove
16+
set_fact: role_mappings_to_remove={{ role_mapping_list_response.json.keys() | difference ( es_role_mappings.keys() | list) }}
17+
18+
#Delete all non required role mappings
19+
- name: Delete Role mappings
20+
uri:
21+
url: "{{ es_api_uri }}/{{ es_security_api }}/role_mapping/{{ item | urlencode }}"
22+
method: DELETE
23+
status_code: 200
24+
user: "{{es_api_basic_auth_username}}"
25+
password: "{{es_api_basic_auth_password}}"
26+
force_basic_auth: yes
27+
validate_certs: "{{ es_validate_certs }}"
28+
when: es_delete_unmanaged_role_mappings
29+
with_items: "{{ role_mappings_to_remove | default([]) }}"
30+
31+
#Update other roles mappings
32+
- name: Update Role Mappings
33+
uri:
34+
url: "{{ es_api_uri }}/{{ es_security_api }}/role_mapping/{{ item | urlencode }}"
35+
method: POST
36+
body_format: json
37+
body: "{{ es_role_mappings[item] | to_json}}"
38+
status_code: 200
39+
user: "{{es_api_basic_auth_username}}"
40+
password: "{{es_api_basic_auth_password}}"
41+
force_basic_auth: yes
42+
validate_certs: "{{ es_validate_certs }}"
43+
with_items: "{{ es_role_mappings.keys() | list | default([]) }}"

0 commit comments

Comments
 (0)