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

Commit f2670fe

Browse files
committed
Add, modify, and clear up role mappings
1 parent 361c44e commit f2670fe

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,33 @@ es_roles:
390390
- create_index
391391
```
392392

393+
* ```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.
394+
395+
```yaml
396+
es_role_mappings:
397+
groupname-editor:
398+
enabled: true
399+
roles:
400+
- editor
401+
rules:
402+
field:
403+
groups: "EditorGroup"
404+
groupname-admin:
405+
enabled: true
406+
roles:
407+
- editor
408+
rules:
409+
field:
410+
groups: "AdminGroup"
411+
realmname-viewer:
412+
enabled: true
413+
roles:
414+
- viewer
415+
rules:
416+
field:
417+
realm.name: realm1
418+
```
419+
393420
* ```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:
394421

395422
```yaml

defaults/main.yml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
when: manage_native_realm | bool
103103
run_once: True
104104

105+
- name: include xpack/security/elasticsearch-security-role_mappings.yml
106+
include: ./xpack/security/elasticsearch-security-role_mappings.yml
107+
when: es_role_mappings is defined and es_role_mappings.keys() | list | length > 0
108+
run_once: True
109+
105110
#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.
106111
#We also do after the native realm to ensure any changes are applied here first and its denf up.
107112
- name: include elasticsearch-template.yml
Lines changed: 43 additions & 0 deletions
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)