-
Notifications
You must be signed in to change notification settings - Fork 108
Centralized SSH key management #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0075f27
b4f375e
57944c6
09d3c7f
b59df72
d2074f4
a845b2e
ea52293
c6f1909
bbc6f6e
2e84023
fcf4910
fd21843
bb0bf88
78f89ec
4b83cf4
5e46b50
7b560f1
3f4a611
d81a23b
e57454d
7231e1d
5bc6c69
d0fda49
2daca36
92d4c70
1db9d3a
a07df90
46e63d5
7a3378f
a79e928
a5930e0
03e7462
763c8b4
5a02de0
2d1d251
a32073b
d591222
4a1de84
028691d
69e64b9
f27b481
adb2ffc
af64aac
7adac48
2a8b233
263307b
b823dd6
0a2fad3
508957b
d38b28f
3b27073
d4931a7
f7916b7
d3278fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||
| --- | ||||||
|
|
||||||
| ssh_users: | ||||||
| - user: galaxy | ||||||
| key: | ||||||
| - {{ galaxy_user_public_key }} | ||||||
| roles: [{{ galaxy_user.name }}] | ||||||
|
|
||||||
| - user: bgruening | ||||||
| key: | ||||||
| - https://github.com/bgruening.keys | ||||||
| roles: [root,stats,centos, {{ galaxy_user.name }}] | ||||||
|
|
||||||
| - user: mira-miracolu | ||||||
| key: | ||||||
| - https://github.com/mira-miracolu.keys | ||||||
| roles: [root,stats,centos, {{ galaxy_user.name }}] | ||||||
|
|
||||||
| - user: dominguj | ||||||
|
||||||
| - user: dominguj | |
| - user: kysrpex |
If this is referencing to GH user handles this would be @kysrpex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its more for identification purposes. Its not used in playbook (:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this name is not even used in the playbook.
Imo this field should contain something that can be easily traced to a person and a full name would do wonders (after all, our full names are all already here https://usegalaxy-eu.github.io/people).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- | ||
|
|
||
| - name: Initialize user_keys_map entry for {{ user_account }} if missing | ||
| ansible.builtin.set_fact: | ||
| user_keys_map: "{{ user_keys_map | default({}) | combine({ user_account: (user_keys_map[user_account] | default([])) }) }}" | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Loop over user's URL list and append fetched keys | ||
| ansible.builtin.set_fact: | ||
| user_keys_map: >- | ||
| {{ | ||
| user_keys_map | ||
| | default({}) | ||
| | combine({ | ||
| user_account: ( | ||
| (user_keys_map[user_account] | default([])) | ||
| + ( | ||
| lookup('url', url_item) | ||
| | default('') | ||
| | split(',') | ||
| ) | ||
| ) | ||
| }) | ||
| }} | ||
| loop: "{{ user_url_list }}" | ||
| loop_control: | ||
| loop_var: url_item | ||
| delegate_to: "{{ target_host | default(omit) }}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
|
|
||
| - name: Compute non-root role accounts | ||
| ansible.builtin.set_fact: | ||
| non_root_role_accounts: >- | ||
| {{ ssh_users | ||
| | map(attribute='roles') | ||
| | flatten | ||
| | reject('equalto', 'root') | ||
| | unique | ||
| | list }} | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Get passwd database on target | ||
| ansible.builtin.getent: | ||
| database: passwd | ||
| register: getent_passwd_result | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Compute existing non-root role accounts | ||
| ansible.builtin.set_fact: | ||
| existing_role_accounts: >- | ||
| {{ non_root_role_accounts | ||
| | intersect((getent_passwd_result.ansible_facts.getent_passwd | default({})).keys() | list) }} | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Ensure .ssh directory exists | ||
| become: true | ||
| ansible.builtin.file: | ||
| path: "/home/{{ item }}/.ssh" | ||
| state: directory | ||
| owner: "{{ item }}" | ||
| group: "{{ item }}" | ||
| mode: '0700' | ||
| loop: "{{ existing_role_accounts }}" | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Initialize user_keys_map with non-https keys for each existing account | ||
| ansible.builtin.set_fact: | ||
| user_keys_map: >- | ||
| {{ user_keys_map | default({}) | combine({ | ||
| item: ( | ||
| ssh_users | ||
| | selectattr('roles','contains', item) | ||
| | map(attribute='key') | ||
| | flatten | ||
| | reject('match','^https?://') | ||
| | list | ||
| ) | ||
| }) }} | ||
| loop: "{{ existing_role_accounts }}" | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Fetch and append keys from https:// URLs per user | ||
| include_tasks: fetch_user_keys_from_urls.yml | ||
| loop: "{{ existing_role_accounts }}" | ||
| loop_control: | ||
| loop_var: user_account | ||
| vars: | ||
| user_url_list: >- | ||
| {{ ssh_users | ||
| | selectattr('roles','contains', user_account) | ||
| | map(attribute='key') | ||
| | flatten | ||
| | select('match','^https?://') | ||
| | list }} | ||
| when: user_url_list | length > 0 | ||
|
|
||
| - name: Deduplicate keys and write authorized_keys for each user | ||
| become: true | ||
| vars: | ||
| keys_for_user: "{{ (user_keys_map[item] | default([])) | map('trim') | reject('equalto','') | unique | list }}" | ||
| ansible.builtin.copy: | ||
| dest: "/home/{{ item }}/.ssh/authorized_keys" | ||
| content: "{{ keys_for_user | join('\n') + (keys_for_user | length > 0 and '\n' or '') }}" | ||
| owner: "{{ item }}" | ||
| group: "{{ item }}" | ||
| mode: '0600' | ||
| loop: "{{ existing_role_accounts }}" | ||
| delegate_to: "{{ target_host | default(omit) }}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #SPDX-License-Identifier: MIT-0 | ||
kysrpex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| --- | ||
| - name: Include SSH Keys for root users | ||
| ansible.builtin.include_tasks: root.yml | ||
|
|
||
| - name: Include SSH Keys for other role users | ||
| ansible.builtin.include_tasks: generic.yml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # fixed tasks for root account management | ||
| - name: Ensure .ssh directory exists for root | ||
| become: true | ||
| ansible.builtin.file: | ||
| path: /root/.ssh | ||
| state: directory | ||
| owner: root | ||
| group: root | ||
| mode: '0700' | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Add Keys to Github | ||
| become: false | ||
| ansible.builtin.set_fact: | ||
| root_keys_list: >- | ||
| {{ ssh_users | ||
| | selectattr('roles','contains','root') | ||
| | map(attribute='key') | ||
| | flatten | ||
| | reject('match', '^https://') | ||
| | list }} | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Append keys fetched from Github | ||
| become: false | ||
| ansible.builtin.set_fact: | ||
| root_keys_list: >- | ||
| {{ | ||
| root_keys_list + | ||
| ( | ||
| lookup('url', item) | ||
| | default('') | ||
| | split(',') | ||
| ) | ||
| }} | ||
| loop: "{{ ssh_users | selectattr('roles','contains','root') | map(attribute='key') | flatten | select('match','^https://') | list }}" | ||
| when: (ssh_users | selectattr('roles','contains','root') | map(attribute='key') | flatten | select('match','^https://') | list) | length > 0 | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Deduplicate root_keys_list | ||
| become: false | ||
| ansible.builtin.set_fact: | ||
| root_keys_list: "{{ root_keys_list | default([]) | unique | list }}" | ||
| delegate_to: "{{ target_host | default(omit) }}" | ||
|
|
||
| - name: Write /root/.ssh/authorized_keys | ||
| become: true | ||
| ansible.builtin.copy: | ||
| dest: /root/.ssh/authorized_keys | ||
| content: "{{ (root_keys_list | default([])) | join('\n') + (root_keys_list | length > 0 and '\n' or '') }}" | ||
| owner: root | ||
| group: root | ||
| mode: '0600' | ||
| delegate_to: "{{ target_host | default(omit) }}" |
Uh oh!
There was an error while loading. Please reload this page.