-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
major_changes: | ||
- sudo_by_ssh_agent - initial commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
release_summary: | | ||
| Release Date: 2023-03-09 | ||
| Initial release of the password_prompt role | ||
| Initial release of the sudo_by_ssh_agent role |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Ansible role: huyz.general.sudo_by_ssh_agent | ||
|
||
Configures sudo to use the `libpam-ssh-agent-auth` package and authorize the | ||
provided ssh key. | ||
|
||
## Installation | ||
|
||
This repo uses the FQCN convention. | ||
|
||
Include the collection in the Ansible Galaxy `requirements.yml`: | ||
|
||
```shell | ||
--- | ||
collections: | ||
- name: huyz.general | ||
``` | ||
|
||
You can then include the role `huyz.general.sudo_by_ssh_agent`. | ||
|
||
## Example | ||
|
||
```yaml | ||
- name: Authorize sudo by ssh agent | ||
ansible.builtin.include_role: | ||
name: huyz.general.sudo_by_ssh_agent | ||
vars: | ||
pub_files_for_sudo: ['~/.ssh/id_ed25519-vip.pub'] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Requires vars: | ||
# - pub_files_for_sudo: list of full path of the public keys to use for sudo | ||
--- | ||
- name: Ensure libpam-ssh-agent-auth package | ||
ansible.builtin.package: | ||
name: libpam-ssh-agent-auth | ||
become: true | ||
|
||
- name: Add to sudo authorized_keys the content of {{ pub_files_for_sudo }} | ||
ansible.posix.authorized_key: | ||
user: root # dummy | ||
key: "{{ lookup('file', item) }}" | ||
path: /etc/security/authorized_keys | ||
manage_dir: false | ||
become: true | ||
loop: "{{ pub_files_for_sudo }}" | ||
|
||
# Per https://www.lorier.net/docs/ssh-agent-sudo.html | ||
- name: Enable pam_ssh_agent_auth for sudo | ||
ansible.builtin.blockinfile: | ||
dest: /etc/pam.d/sudo | ||
marker: "# {mark} ANSIBLE MANAGED BLOCK: {{ block_id }}" | ||
insertbefore: "@include common-auth" | ||
block: | | ||
# Allow sudo by ssh agent | ||
auth sufficient pam_ssh_agent_auth.so file=/etc/security/authorized_keys | ||
vars: | ||
block_id: sudo_by_ssh_agent | ||
become: true | ||
|
||
# Per https://www.lorier.net/docs/ssh-agent-sudo.html | ||
- name: Preserve SSH_AUTH_SOCK for sudo | ||
ansible.builtin.blockinfile: | ||
dest: /etc/sudoers | ||
marker: "# {mark} ANSIBLE MANAGED BLOCK: {{ block_id }}" | ||
insertafter: '#Defaults:%sudo env_keep \+= "SSH_AGENT_PID SSH_AUTH_SOCK"' | ||
block: | | ||
Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK" | ||
vars: | ||
block_id: sudo_by_ssh_agent | ||
become: true |