Skip to content

Commit

Permalink
nomis: D1568: add support for user creation (#437)
Browse files Browse the repository at this point in the history
* Add regular user functionality to users-and-groups role

* Add regular users to nomis-dev servers

* whitespace

* Commit changes made by code formatters

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
drobinson-moj and github-actions[bot] authored Dec 6, 2023
1 parent d1331be commit f7b0849
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ansible/group_vars/environment_name_nomis_development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ dns_search_domains:
- azure.noms.root
PROD_SYSCON_WEB_RELEASE: DB_V11.2.1.1.219

users_and_groups_regular:
- group: studio-webops
- group: syscon-nomis

weblogic_additional_form_servers: []
db_configs:
qa11r:
Expand Down
31 changes: 23 additions & 8 deletions ansible/roles/users-and-groups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ Define mapping between user to uid, and group to gid, in `vars/`. For example:
- [default-system-gids.yml](/ansible/roles/users-and-groups/vars/default-system-gids.yml)
- [default-system-uids.yml](/ansible/roles/users-and-groups/vars/default-system-uids.yml)

A custom mapping can be created if necessary. For example, create
`vars/myapp-system-gids.yml` and `vars/myapp-system-uids.yml` and set

```
users_and_groups_system_vars_prefix: myapp
```
A custom mapping can be created if necessary for a given business unit or application. For example, create
`vars/hmpps-system-gids.yml` and `vars/hmpps-system-uids.yml` and set

### Adding users and groups

Expand Down Expand Up @@ -55,6 +51,25 @@ Option 2. Import from another role
- wheel
```

## Non-System Users
## Regular Users

Users should add their ssh public keys to the relevant business unit vars file, e.g.
Also assign a unique UID for consistency across servers. Suggest the username is set
to the user's GitHub id.

- [hmpps-regular-users.yml](/ansible/roles/users-and-groups/vars/hmpps-regular-users.yml)

Define group details such as group membership in relevant business unit vars file, e.g.

Not implemented yet.
- [hmpps-regular-groups.yml](/ansible/roles/users-and-groups/vars/hmpps-regular-groups.yml)

Suggest the group names follow GitHub group ids.

Finally, define which users and groups to add by defining a variable in the relevant
server-type or environment_name group vars, e.g.

```
users_and_groups_regular:
- group: studio-webops
- group: syscon-nomis
```
29 changes: 25 additions & 4 deletions ansible/roles/users-and-groups/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
---
# Define which users to create using this variable. Groups are automatically
# created if they are referenced by a user. For example:
# Define which system users to create using this variable. Groups are
# automatically created if they are referenced by a user. For example:
# users_and_groups_system:
# - { name: oracle, group: oinstall }
# - { group: mygroup } # if just a group required
users_and_groups_system: []

# override this to load a different uid/gid vars file
users_and_groups_system_vars_prefix: "default"
# Define which regular users to create using this variable. Although you can
# specify individual users in the same way as `users_and_groups_system`, it
# is recommended just to specify group and all group members will be added.
# users_and_groups_regular:
# - group: studio-webops
users_and_groups_regular: []

# Override these in vars/
system_uids: {} # username -> uid
system_gids: {} # group -> gid
regular_uids: {} # username -> uid
regular_gids: {} # group -> gid
regular_users_authorized_keys: {} # username -> key (multi-line string)
regular_groups_additional_groups: {} # username -> groups
regular_groups_members: {} # group -> list of usernames

# Define which files to read in from /vars
users_and_groups_system_vars_prefixes:
- "default"
- "{{ ec2.tags['business-unit'] | lower }}"
users_and_groups_regular_vars_prefixes:
- "default"
- "{{ ec2.tags['business-unit'] | lower }}"
3 changes: 3 additions & 0 deletions ansible/roles/users-and-groups/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- role: get-ec2-facts
87 changes: 87 additions & 0 deletions ansible/roles/users-and-groups/tasks/add-regular.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
# Note these tasks can also be used elsewhere via import_role

- name: Include uid and gid vars
ansible.builtin.include_vars:
dir: vars
files_matching: "{{ item }}-regular-.*.yml$"
loop: "{{ users_and_groups_regular_vars_prefixes }}"

- name: Calculate list of groups
ansible.builtin.set_fact:
regular_groups_1: "{{ users_and_groups_regular | map(attribute='group') }}"
regular_groups_2: "{{ users_and_groups_regular | selectattr('groups', 'defined') | map(attribute='groups') | flatten }}"

- name: Add regular groups
ansible.builtin.group:
name: "{{ item }}"
state: present
system: no
gid: "{{ regular_gids[item]|default(omit) }}"
loop: "{{ (regular_groups_1 + regular_groups_2) | unique }}"

- name: Calculate groups where we will add all members
ansible.builtin.set_fact:
regular_groups_for_adding_users: "{{ users_and_groups_regular | rejectattr('name', 'defined') | selectattr('group', 'defined') | map(attribute='group') }}"
users_regular: []

- name: Add group members to user list fact
ansible.builtin.set_fact:
users_regular: "{{ users_regular + [{
'name': item.name,
'group': item.group,
'groups': item.groups|default([]) + regular_groups_additional_groups[item.group]|default([]),
'uid': item.uid | default(regular_uids[item.name]|default(omit)),
'create_home': item.create_home | default(true),
'home': item.home | default('/home/' + item.name),
'state': item.state | default('present'),
'authorized_keys': item.authorized_keys | default(regular_users_authorized_keys[item.name]|default(omit))
}] }}"
vars:
item:
group: "{{ loop_item[0].key }}"
name: "{{ loop_item[1] }}"
when: item.group in regular_groups_for_adding_users
loop_control:
loop_var: loop_item
label: "{{ item.name }}:{{ item.group }}"
loop: "{{ regular_groups_members | dict2items | subelements('value') }}"

- name: Add regular users to user list fact
set_fact:
users_regular: "{{ users_regular + [{
'name': item.name,
'group': item.group,
'groups': item.groups|default([]) + regular_groups_additional_groups[item.group]|default([]),
'uid': item.uid | default(regular_uids[item.name]|default(omit)),
'create_home': item.create_home | default(true),
'home': item.home | default('/home/' + item.name),
'state': item.state | default('present'),
'authorized_keys': item.authorized_keys | default(regular_users_authorized_keys[item.name]|default(omit))
}] }}"
loop_control:
label: "{{ item.name }}"
loop: "{{ users_and_groups_regular | selectattr('name', 'defined') }}"

- name: Update regular users
ansible.builtin.user:
name: "{{ item.name }}"
group: "{{ item.group }}"
groups: "{{ item.groups }}"
uid: "{{ item.uid|default(omit) }}"
create_home: "{{ item.create_home }}"
home: "{{ item.home }}"
state: "{{ item.state }}"
system: no
loop_control:
label: "{{ item.name }}:{{ item.group }}"
loop: "{{ users_regular }}"

- name: Update authorized keys
ansible.posix.authorized_key:
user: "{{ item.name }}"
key: "{{ item.authorized_keys }}"
loop_control:
label: "{{ item.name }}"
loop: "{{ users_regular | rejectattr('state', 'equalto', 'absent') | selectattr('authorized_keys', 'defined') }}"
when: not ansible_check_mode
9 changes: 5 additions & 4 deletions ansible/roles/users-and-groups/tasks/add-system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
- name: Include uid and gid vars
ansible.builtin.include_vars:
dir: vars
files_matching: "{{ users_and_groups_system_vars_prefix }}-system-.*.yml$"
files_matching: "{{ item }}-system-.*.yml$"
loop: "{{ users_and_groups_system_vars_prefixes }}"

- name: Calculate list of groups
ansible.builtin.set_fact:
Expand All @@ -16,15 +17,15 @@
name: "{{ item }}"
state: present
system: yes
gid: "{{ system_gids[item] }}"
loop: "{{ (system_groups_1 + system_groups_2) | unique | intersect(system_gids.keys()) }}"
gid: "{{ system_gids[item]|default(omit) }}"
loop: "{{ (system_groups_1 + system_groups_2) | unique }}"

- name: Add system users
ansible.builtin.user:
name: "{{ item.name }}"
group: "{{ item.group }}"
groups: "{{ item.groups | default([]) }}"
uid: "{{ item.uid | default(system_uids[item.name]) }}"
uid: "{{ item.uid | default(system_uids[item.name]|default(omit)) }}"
create_home: "{{ item.create_home | default(true) }}"
home: "{{ item.home | default('/home/' + item.name) }}"
system: yes
Expand Down
8 changes: 8 additions & 0 deletions ansible/roles/users-and-groups/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
- amibuild
- ec2provision
- ec2patch
- users-and-groups-system

- import_tasks: add-regular.yml
tags:
- amibuild
- ec2provision
- ec2patch
- users-and-groups-regular
22 changes: 22 additions & 0 deletions ansible/roles/users-and-groups/vars/hmpps-regular-groups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# define all non-system groups in use in hmpps accounts here

# define gid if you want consistency across servers
regular_gids:
studio-webops: 2001
syscon-nomis: 2051

# define any additional groups that team members should be added to
regular_groups_additional_groups:
studio-webops:
- wheel
syscon-nomis:
- wheel

# define members of each group
regular_groups_members:
studio-webops:
- drobinson-moj
- Sandhya1874
- KarenMoss1510
syscon-nomis:
- vinnydigital
21 changes: 21 additions & 0 deletions ansible/roles/users-and-groups/vars/hmpps-regular-users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Define UID/ssh keys for hmpps non-system users
# NOTE: please do not commit email addresssed in authorized keys

# define a UID if you want consistency across servers
regular_uids:
drobinson-moj: 2001
Sandhya1874: 2002
KarenMoss1510: 2003
vinnydigital: 2051

# please do not commit email addresses
regular_users_authorized_keys:
drobinson-moj: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDMmybIRrtx8sU+/7+d9s7RppiZv+UTgszC9Ed7NqOMYBI5D6lD32AJQmDgDvHWZtmt1PCq5SLkkrz8wVqKkKewTtsf7AZ8Nb8B2NBcKNvKeASgljE/cLpPm/zoRndID9qEsSfmZVYsp+C7QrS9O0PIUUDgnhZyN73LsAzRH3G2GSvoT4wvrHcftiMP6Cg9/Nc+JZZQ1SW8CTIOlvxSN3C0Rxw++WUIbwD2Hur9utMqHQvViR8GfAf7Du2YPwwgUEWYP7exC4e5YMyQbxEjVeKM/gF7KNygEeev4kpl0KMQw1vMF6IsXr6ffZygypIB/E7duCUnb50wwp3BdpLfEXthiul1+0z7OFnbCNejiC5GNShLV2UzwLCyVabQUTFbQnoHlSGHbtC6CCTYZei/l20djd1PDDhBCIViveZBM3vyv+APkEZwyE7/idy0wS4190dwpTJwVr6AmxgN9SBbv5BpXhlWZu628bo8tI9JoeXHistanfSi6xLmzRCLuRJeknE=
Sandhya1874: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC5pf/PHML6OILGUS2BHuiE2sZwBFPMGC6A2Q3C3YKg+aIXK0ZqEdydQOmgnUFd/s639stoOo0iDfEyoqMVXjYvw17sEI0QTG6U0xOwoBlB4kOOmODVvhxqzSxL4YsUJFNLLkZa/zMym5keLj2+Aq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