Skip to content
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

Removing keys from authorized_keys when re-provisioning #1087

Open
3 tasks done
jan-muller opened this issue Jun 18, 2019 · 2 comments
Open
3 tasks done

Removing keys from authorized_keys when re-provisioning #1087

jan-muller opened this issue Jun 18, 2019 · 2 comments

Comments

@jan-muller
Copy link

jan-muller commented Jun 18, 2019

Feature request

When re-provisioning a server with a users tag trellis provision --tags users env it does not remove the keys from authorized_keys. It would be nice if it could remove keys.

Working with a team of developers, we've put all the keys of all the team members in the /group_vars/all/users.yml. Removing a key in users.yml and re-provision the server does not remove any keys.

This is something to be aware of, might want to modify the docs with a warning.

Related discourse topic: https://discourse.roots.io/t/re-provisioning-with-tag-users-does-not-removing-ssh-keys-from-authorized-keys/15857

@jan-muller jan-muller changed the title Removing keys from "authorized_keys" when re-provisioning Removing keys from authorized_keys when re-provisioning Jun 18, 2019
@swalkinshaw
Copy link
Member

I looked into this and unfortunately it would be very complex to implement. Ansible's authorized_key module helpfully provides a exclusive option which will remove old keys automatically, but the way Trellis adds keys makes this very hard (if not impossible) to use.

To use the exclusive option, all keys need to be provided as a single string blob. But we have two different tasks:

- name: Add user SSH keys
authorized_key:
user: "{{ item.0.name }}"
key: "{{ item.1 }}"
with_subelements:
- "{{ users | default([]) }}"
- keys
- name: Add deploy SSH keys
authorized_key:
user: "{{ web_user }}"
key: "{{ lookup('file', item) }}"
with_fileglob: 'public_keys/*.pub'

Trying to get them all together would also mean not being able to support the https://github.com/username.keys URL feature.

If someone wants to try and implement this please feel free. It would be a great addition but I don't see a way to do it.

@dalepgrant
Copy link
Contributor

dalepgrant commented Nov 20, 2023

Spitballing an idea, we could do something like this:

trellis/roles/users/tasks/main.yml

+ - name: Overwrite user SSH keys with primary user
+  authorized_key:
+    user: "{{ item.name }}"
+    key: "{{ item['keys'] | first }}"  # get first SSH key listed in group_vars/all/users.yml
+    exclusive: true                    # remove all other keys
+  loop: "{{ users | default([]) }}"
+ loop_control:
+    label: "{{ item.name }}"

+ - name: Add other user SSH keys
- - name: Add user SSH keys 
   authorized_key: 
     user: "{{ item.0.name }}" 
     key: "{{ item.1 }}" 
   with_subelements: 
     - "{{ users | default([]) }}" 
     - keys 

There would be a moment where only the first key from each group in group_vars/all/users.yml is present on the server before the other keys are added.

Tested the above on a throwaway server, does the job. Could be improved before it makes it out into being used on production servers by first checking the key exists/isn't blank (if it's a Github key) and moving onto the next key if not. Could also be made to run only if a certain flag is given, it probably doesn't need to be run on every single provision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants