Skip to content

Commit 5dccd72

Browse files
authored
Add Django users to groups (#31)
* Add Django users to groups * Refactor user/group logic Group membership is now obtained from the pulp server for all defined users and tested against what is defined in pulp_django_users. Group memberships defined in pulp_django_users and not on pulp server are added, and group memberships defined on pulp server and not in pulp_django_users are removed. Users may have no groups defined, and pulp_django_users may also be entirely empty. * Fix when user.groups is null not empty list
1 parent 7efc114 commit 5dccd72

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed

roles/pulp_django_user/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Role variables
1212
* `pulp_validate_certs`: Whether to validate Pulp server certificate. Default is `true`
1313
* `pulp_django_users`: List of Django users to create. Default is an empty list.
1414

15+
Note: User groups are evauluated against the user's current list of groups returned from the Pulp server API. Removing a group from the list of groups defined in `pulp_django_users[*].groups` will result in the user being removed from that group, and adding a group will result in the user being added to that group. Adding an empty `groups:` for a user will result in that user being removed from all groups.
16+
1517
Example playbook
1618
----------------
1719

@@ -28,4 +30,7 @@ Example playbook
2830
pulp_django_users:
2931
- username: test-user
3032
password: correct horse battery staple
33+
groups:
34+
- existing.container.namespace.consumers.one
35+
- existing.container.namespace.consumers.two
3136
```

roles/pulp_django_user/tasks/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@
4646
loop: "{{ pulp_django_users }}"
4747
loop_control:
4848
label: "{{ item.username }}"
49+
50+
- name: Add or remove user from group(s)
51+
include_tasks: user_groups/add_or_remove_users.yml
52+
# Noop if pulp_django_users is defined but empty
53+
loop: "{{ pulp_django_users | default([], true) }}"
54+
loop_control:
55+
loop_var: user
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
3+
- name: Get all groups that user belongs to
4+
uri:
5+
url: "{{ pulp_get_user_url }}?username={{ user.username }}"
6+
user: "{{ pulp_admin_username }}"
7+
password: "{{ pulp_admin_password }}"
8+
method: GET
9+
status_code: 200
10+
force_basic_auth: true
11+
register: user_group_result
12+
13+
- name: Set current_group_membership fact
14+
set_fact:
15+
current_group_membership: "{{ user_group_result | json_query('json.results[*].groups') | flatten | map(attribute='name') | list }}"
16+
17+
- name: Remove user from groups not defined in pulp_django_users
18+
include_tasks: remove_user_from_groups.yml
19+
loop: "{{ current_group_membership | difference(user.groups | default([], true)) }}"
20+
loop_control:
21+
loop_var: remove_group
22+
23+
- name: Add user to groups defined in pulp_django_users
24+
include_tasks: add_user_to_groups.yml
25+
loop: "{{ user.groups | default([], true) | difference(current_group_membership) }}"
26+
loop_control:
27+
loop_var: add_group
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# These user/group combinations definitely DO NOT exist on pulp server
3+
4+
- name: Get group href from Pulp API
5+
uri:
6+
url: "{{ pulp_add_group_user_url }}?name={{ add_group }}"
7+
user: "{{ pulp_admin_username }}"
8+
password: "{{ pulp_admin_password }}"
9+
method: GET
10+
status_code: 200
11+
force_basic_auth: true
12+
register: group_href_result
13+
14+
- name: Fail when group cannot be unambiguosly returned from Pulp
15+
fail:
16+
msg: >
17+
"Pulp API query: GET '{{ group_href_result.url }}' did not return
18+
exactly one group. Groupname '{{ add_group }}' was found
19+
'{{ group_href_result.json.count }}' times.
20+
Groupnames should be unique and exist before associating users
21+
with them"
22+
when: group_href_result.json.count != 1
23+
24+
# Will fail before we get here if the group doesn't exist or has
25+
# an ambiguous name
26+
- name: Add user to group
27+
uri:
28+
url: "{{ pulp_url }}{{ group_href_result.json.results[0].pulp_href }}users/"
29+
user: "{{ pulp_admin_username }}"
30+
password: "{{ pulp_admin_password }}"
31+
body:
32+
username: "{{ user.username }}"
33+
body_format: json
34+
method: POST
35+
force_basic_auth: true
36+
# Always returns 201 even if user/group exists
37+
status_code: 201
38+
# If we get here we're always changing something
39+
changed_when: true
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
# These user/group combinations definitely DO exist on pulp server
3+
4+
- name: Get group href from Pulp API
5+
uri:
6+
url: "{{ pulp_add_group_user_url }}?name={{ remove_group }}"
7+
user: "{{ pulp_admin_username }}"
8+
password: "{{ pulp_admin_password }}"
9+
method: GET
10+
status_code: 200
11+
force_basic_auth: true
12+
register: group_href_result
13+
14+
# We should only return a single group here because we've just queried
15+
# from pulp API
16+
- name: Fail when group cannot be unambiguosly returned from Pulp API
17+
fail:
18+
msg: >
19+
"Pulp API query: GET '{{ group_href_result.url }}' did not return
20+
exactly one group. Groupname '{{ remove_group }}' was found
21+
'{{ group_href_result.json.count }}' times.
22+
Groupnames should be unique and exist before associating users
23+
with them"
24+
when: group_href_result.json.count != 1
25+
26+
- name: Get user id from Pulp API
27+
uri:
28+
url: "{{ pulp_get_user_url }}?username={{ user.username }}"
29+
user: "{{ pulp_admin_username }}"
30+
password: "{{ pulp_admin_password }}"
31+
method: GET
32+
status_code: 200
33+
force_basic_auth: true
34+
register: user_list_result
35+
36+
- name: Fail when user id cannot be unambiguosly returned from Pulp API
37+
fail:
38+
msg: >
39+
"Pulp API query: GET '{{ user_list_result.url }}' did not return
40+
exactly one user id. User '{{ user.username }}' was found
41+
'{{ user_list_result.json.count }}' times."
42+
when: user_list_result.json.count != 1
43+
44+
# Will fail before we get here if the group/user doesn't exist
45+
- name: Remove user from group
46+
# DELETE {{ pulp_url }}/pulp/api/v3/groups/880/users/11/
47+
uri:
48+
url: "{{ pulp_url }}{{ group_href_result.json.results[0].pulp_href }}users/{{ user_list_result.json.results[0].id }}/"
49+
user: "{{ pulp_admin_username }}"
50+
password: "{{ pulp_admin_password }}"
51+
method: DELETE
52+
# Always returns 204 even if user/group doesn't exist
53+
status_code: 204
54+
force_basic_auth: true
55+
# If we get here, we're always changing something
56+
changed_when: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
---
22
pulp_login_url: "{{ pulp_url }}/admin/login/?next=/admin/"
33
pulp_add_user_url: "{{ pulp_url }}/admin/auth/user/add/"
4+
pulp_get_user_url: "{{ pulp_url }}/pulp/api/v3/users/"
5+
pulp_add_group_user_url: "{{ pulp_url }}/pulp/api/v3/groups/"

0 commit comments

Comments
 (0)