Skip to content

Commit 681cfe2

Browse files
authored
Merge pull request #52 from stackhpc/api-update
adding roles for users, groups and rbac-cg
2 parents 16a9d6c + 89bbeef commit 681cfe2

File tree

26 files changed

+1067
-9
lines changed

26 files changed

+1067
-9
lines changed

roles/pulp_content_guard/README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,32 @@ Role variables
1010
* `pulp_username`: Username used to access Pulp server. Default is `admin`
1111
* `pulp_password`: Password used to access Pulp server. Default is unset
1212
* `pulp_validate_certs`: Whether to validate Pulp server certificate. Default is `true`
13-
* `pulp_content_guard_x509_cert_guards`: List of x509 cert guards. Each item is
14-
a dict with the following keys: `name`, `description`, `ca_certificate`,
15-
`state`.
13+
* `pulp_content_guard_x509_cert_guards`: List of x509 cert guards to create/update/delete. Each item is
14+
a dict containing:
15+
* `name` (Required)
16+
* `description`
17+
* `ca_certificate`
18+
* `state` (Default is `present`. Setting this value to `absent` will delete the content guard if it exists)
19+
* `pulp_content_guard_rbac`: List of RBAC content guards to create/update/delete. Default is an empty list. Each item is a dict containing:
20+
* `name` (Required)
21+
* `roles` List of dict containing:
22+
* `role` (role name)
23+
* `groups` List of groups to be assigned the role
24+
* `state` (default is `present`. Setting this value to `absent` will delete the content guard if it exists)
1625

26+
Note: groups assigned roles are evaluated against the content guard's current list of roles returned from the Pulp server API. Removing a group from the list of groups defined under any role in `pulp_content_guard_rbac[*].roles` will result in the group being removed, and adding a group will result in it being added. Adding an empty `groups:` for a role will result in all groups being removed from that role.
1727

1828
Example playbook
1929
----------------
2030

21-
```
31+
```yaml
2232
---
2333
- name: Create Pulp content guards
2434
any_errors_fatal: True
2535
gather_facts: True
2636
hosts: all
2737
roles:
28-
- role: stackhpc.pulp.pulp_contentguard
38+
- role: stackhpc.pulp.pulp_content_guard
2939
pulp_username: admin
3040
pulp_password: "{{ secrets_pulp_admin_password }}"
3141
pulp_content_guard_x509_cert_guards:
@@ -36,4 +46,26 @@ Example playbook
3646
...
3747
-----END CERTIFICATE-----
3848
state: present
49+
50+
- role: stackhpc.pulp.pulp_content_guard
51+
pulp_url: http://localhost:8080
52+
pulp_username: admin
53+
pulp_password: "{{ secrets_pulp_admin_password }}"
54+
pulp_content_guard_rbac:
55+
- name: test_rbac_cg_1
56+
description: test content guard number 1
57+
roles:
58+
- role: core.rbaccontentguard_downloader
59+
groups:
60+
- role: core.rbaccontentguard_viewer
61+
state: present
62+
- name: test_rbac_cg_2
63+
state: absent
64+
- name: test_rbac_cg_3
65+
description: test content guard number 3
66+
roles:
67+
- role: core.rbaccontentguard_viewer
68+
groups:
69+
- test_group_1
70+
- test_group_2
3971
```

roles/pulp_content_guard/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pulp_password:
55
pulp_validate_certs: true
66

77
pulp_content_guard_x509_cert_guards: []
8+
pulp_content_guard_rbac: []

roles/pulp_content_guard/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
with_items: "{{ pulp_content_guard_x509_cert_guards }}"
1313
loop_control:
1414
label: "{{ item.name }}"
15+
16+
- name: Ensure RBAC cert guards exist
17+
import_tasks: rbac/rbac.yml
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
3+
- name: Initialise facts
4+
set_fact:
5+
new_roles: []
6+
current_roles: []
7+
8+
- name: Get RBAC content guard list
9+
uri:
10+
url: "{{ pulp_rbac_cg_url }}?name={{ content_guard.name }}"
11+
user: "{{ pulp_username }}"
12+
password: "{{ pulp_password }}"
13+
method: GET
14+
status_code: 200
15+
force_basic_auth: true
16+
register: rbac_cg_result
17+
18+
- name: Get role list
19+
uri:
20+
url: "{{ pulp_url }}{{ (rbac_cg_result.json.results | first).pulp_href }}list_roles/"
21+
user: "{{ pulp_username }}"
22+
password: "{{ pulp_password }}"
23+
method: GET
24+
status_code: 200
25+
force_basic_auth: true
26+
register: role_list_result
27+
28+
- name: Remove unused roles
29+
vars:
30+
rolenames: "{{ content_guard.roles | default([]) | map(attribute='role') | list }}"
31+
url_query: "[?name=='{{ content_guard.name }}'].pulp_href"
32+
uri:
33+
url: "{{ pulp_url }}{{ (rbac_cg_result.json.results | first).pulp_href }}remove_role/"
34+
user: "{{ pulp_username }}"
35+
password: "{{ pulp_password }}"
36+
force_basic_auth: true
37+
method: POST
38+
status_code: 201
39+
body:
40+
role: "{{ item.role }}"
41+
groups: "{{ item.groups }}"
42+
body_format: form-urlencoded
43+
loop: "{{ role_list_result.json.roles }}"
44+
register: result
45+
when:
46+
- item.role not in rolenames
47+
- item.users == []
48+
changed_when: result.status == 201
49+
50+
- name: Loop on new roles
51+
include_tasks: add_or_remove_groups_from_role.yml
52+
loop: "{{ content_guard.roles | default([]) }}"
53+
loop_control:
54+
loop_var: rbac_cg_new_role
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
3+
- name: Set fact current groups
4+
vars:
5+
role_query: "[?role=='{{ rbac_cg_new_role.role }}'].groups"
6+
set_fact:
7+
current_groups: "{{ role_list_result.json.roles | json_query(role_query) | first | default([]) }}"
8+
9+
- name: Add new group to role
10+
uri:
11+
url: "{{ pulp_url }}{{ (rbac_cg_result.json.results | first).pulp_href }}add_role/"
12+
user: "{{ pulp_username }}"
13+
password: "{{ pulp_password }}"
14+
force_basic_auth: true
15+
method: POST
16+
status_code: 201
17+
body:
18+
role: "{{ rbac_cg_new_role.role }}"
19+
groups: "{{ item }}"
20+
body_format: form-urlencoded
21+
register: result
22+
loop: "{{ rbac_cg_new_role.groups | default([], true) }}"
23+
when: item not in current_groups
24+
changed_when: result.status == 201
25+
26+
- name: Remove old group from role
27+
uri:
28+
url: "{{ pulp_url }}{{ (rbac_cg_result.json.results | first).pulp_href }}remove_role/"
29+
user: "{{ pulp_username }}"
30+
password: "{{ pulp_password }}"
31+
force_basic_auth: true
32+
method: POST
33+
status_code: 201
34+
body:
35+
role: "{{ rbac_cg_new_role.role }}"
36+
groups: "{{ item }}"
37+
body_format: form-urlencoded
38+
register: result
39+
loop: "{{ current_groups }}"
40+
when: item not in (rbac_cg_new_role.groups | default([]))
41+
changed_when: result.status == 201
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
3+
- name: Get RBAC content guard list
4+
uri:
5+
url: "{{ pulp_rbac_cg_url }}"
6+
user: "{{ pulp_username }}"
7+
password: "{{ pulp_password }}"
8+
method: GET
9+
status_code: 200
10+
force_basic_auth: true
11+
register: rbac_cg_list_result
12+
13+
- name: Initialise remove_rbac_cg
14+
set_fact:
15+
remove_rbac_cg: []
16+
17+
- name: Set fact remove_rbac_cg
18+
set_fact:
19+
remove_rbac_cg: "{{ (remove_rbac_cg | default([])) + [item.name] }}"
20+
when: item.state is defined and item.state == 'absent'
21+
with_items: "{{ pulp_content_guard_rbac }}"
22+
23+
- name: Create RBAC content guards
24+
vars:
25+
rbaccgnames: "{{ rbac_cg_list_result.json.results | map(attribute='name') | list }}"
26+
uri:
27+
url: "{{ pulp_rbac_cg_url }}"
28+
user: "{{ pulp_username }}"
29+
password: "{{ pulp_password }}"
30+
force_basic_auth: true
31+
method: POST
32+
status_code: 201
33+
body:
34+
name: "{{ item.name }}"
35+
description: "{{ item.description | default(None) }}"
36+
body_format: form-urlencoded
37+
loop: "{{ pulp_content_guard_rbac }}"
38+
register: result
39+
when:
40+
- item.name not in rbaccgnames
41+
- item.state is not defined or item.state != 'absent'
42+
changed_when: result.status == 201
43+
44+
- name: Update existing rbac content guards
45+
vars:
46+
rbaccgnames: "{{ rbac_cg_list_result.json.results | map(attribute='name') | list }}"
47+
url_query: "[?name=='{{ item.name }}'].pulp_href"
48+
uri:
49+
url: "{{ pulp_url }}{{ rbac_cg_list_result.json.results | json_query(url_query) | first }}"
50+
user: "{{ pulp_username }}"
51+
password: "{{ pulp_password }}"
52+
force_basic_auth: true
53+
method: PATCH
54+
body:
55+
name: "{{ item.name }}"
56+
description: "{{ item.description | default(None) }}"
57+
body_format: form-urlencoded
58+
loop: "{{ pulp_content_guard_rbac }}"
59+
register: result
60+
when:
61+
- item.name in rbaccgnames
62+
- item.state is not defined or item.state != 'absent'
63+
changed_when:
64+
# The pulp API currently does not report when a change is made, so we must
65+
# manually check
66+
- result.json not in rbac_cg_list_result.json.results
67+
- result.status == 200
68+
69+
- name: Add or remove group roles from content guard
70+
include_tasks: add_or_remove_group_roles.yml
71+
loop: "{{ pulp_content_guard_rbac | default([], true) }}"
72+
loop_control:
73+
loop_var: content_guard
74+
when: not (content_guard.state is defined and content_guard.state == 'absent')
75+
76+
- name: Initialise hrefs
77+
set_fact:
78+
hrefs: []
79+
80+
- name: Set fact hrefs
81+
set_fact:
82+
hrefs: "{{ (hrefs | default([])) + [item.pulp_href] }}"
83+
when: item.name in (remove_rbac_cg | default([]))
84+
with_items: "{{ rbac_cg_list_result.json.results }}"
85+
86+
- name: Delete RBAC content guards
87+
uri:
88+
url: "{{ pulp_url }}{{ item }}"
89+
user: "{{ pulp_username }}"
90+
password: "{{ pulp_password }}"
91+
force_basic_auth: true
92+
method: DELETE
93+
status_code: 204
94+
body_format: form-urlencoded
95+
loop: "{{ hrefs }}"
96+
register: result
97+
changed_when: result.status == 204
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
pulp_rbac_cg_url: "{{ pulp_url }}/pulp/api/v3/contentguards/core/rbac/"

roles/pulp_distribution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Role variables
2525
Example playbook
2626
----------------
2727

28-
```
28+
```yaml
2929
---
3030
- name: Manage Pulp distributions
3131
any_errors_fatal: True

roles/pulp_django_user/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Note: User groups are evauluated against the user's current list of groups retur
2222
Example playbook
2323
----------------
2424

25-
```
25+
```yaml
2626
---
2727
- name: Create Pulp Django users
2828
gather_facts: True

roles/pulp_group/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pulp_group
2+
================
3+
4+
This role creates and deletes Pulp groups using the Pulp API.
5+
6+
To add users to groups or add groups to content guards, use the pulp_user and pulp_content_guard roles respectively.
7+
8+
Role variables
9+
--------------
10+
11+
* `pulp_url`: URL of Pulp server. Default is `https://localhost:8080`
12+
* `pulp_username`: Username used to access Pulp server. Default is `admin`
13+
* `pulp_password`: Password used to access Pulp server. Default is unset
14+
* `pulp_groups`: List of groups to be created/updated/deleted. Default is an empty list. Each item is a dict containing:
15+
* `name` (Required)
16+
* `state` (default is `present`. Setting this value to `absent` will delete the use if it exists)
17+
18+
19+
20+
Example playbook
21+
----------------
22+
23+
```yaml
24+
---
25+
- name: Create and delete groups
26+
gather_facts: True
27+
hosts: localhost
28+
roles:
29+
- role: stackhpc.pulp.pulp_group
30+
pulp_url: https://pulp.example.com
31+
pulp_username: admin
32+
pulp_password: "{{ secrets_pulp_admin_password }}"
33+
pulp_groups:
34+
- name: example-group-1
35+
state: present
36+
- name: example-group-2
37+
state: present
38+
- name: example-group-3
39+
state: absent
40+
```

0 commit comments

Comments
 (0)