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

Vault agent role for ansible #10

Merged
merged 5 commits into from
Dec 1, 2024
Merged

Vault agent role for ansible #10

merged 5 commits into from
Dec 1, 2024

Conversation

Zakurama
Copy link
Contributor

This Ansible role may be used to deploy automatic retrieving of SSL certificate on Vault.

- name: Copy retrieving_cert.tmpl
ansible.builtin.template:
src: retrieving_cert.tmpl.j2
dest: /root/vault_agent_certificat/retrieving_cert.tmpl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a good idea to change the template variable delimiters for this task so we don't have to escape all the {{ and }} in the retrieving_cert.tmpl file. This can be achieved by adding the following line : variable_start_string: << (see variable_start_string)

@@ -0,0 +1,10 @@
{{ '{{' }}with secret "secret/certificat-web"{{ '}}' }}
{{ '{{' }} index .Data.data "privkey.pem" | writeToFile "{{vault_agent_certificate_directory}}/privkey.pem" "" "" "0400"{{ '}}' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. with the delimiters << and >>

Suggested change
{{ '{{' }} index .Data.data "privkey.pem" | writeToFile "{{vault_agent_certificate_directory}}/privkey.pem" "" "" "0400"{{ '}}' }}
{{ index .Data.data "privkey.pem" | writeToFile "<< vault_agent_certificate_directory>>/privkey.pem" "" "" "0400"}}

perms = "0600"

exec {
command = {{vault_agent_command | tojson}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nymous suggested renaming the variable vault_agent_command to something more descriptive, such as service_reload_command, to make its purpose clearer. Additionally, a comment could be added to explain the expected structure or functionality of the command.

Changign variables string in templates
Clearer variable name and comments for reload command
nymous
nymous previously approved these changes Nov 25, 2024
Copy link
Member

@nymous nymous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved so I don't block the merge, you can see the comments or not ^^


[Service]
Type=notify
WorkingDirectory=/root/vault_agent_certificat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WorkingDirectory=/root/vault_agent_certificat
WorkingDirectory=/root/vault_agent_certificates

Just to be consistent ^^
And when you have a path that is repeated in many places (here and in tasks.yml), you can create a default variable (in roles/vault_agent/defaults/main.yml) and reuse it everywhere, e.g.

vault_agent_working_directory: /root/vault_agent_certificates

perms = "0600"

exec {
command = {{service_reload_command | tojson}} {# to reload the service after retrieving the certificate #}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
command = {{service_reload_command | tojson}} {# to reload the service after retrieving the certificate #}
// command used to to reload the service after retrieving the certificate, in the form of ["binary", "arg1", "arg2"]
command = {{ vault_agent_service_reload_command | tojson }}

Also I found out that since we did this last year, the command is deprecated and replaced with exec 😅 https://developer.hashicorp.com/vault/docs/agent-and-proxy/agent/template#command

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken the parameter command is not used directly but as a parameter of exec. Thus, I think this way of doing is not deprecated https://developer.hashicorp.com/vault/docs/agent-and-proxy/agent/template#exec

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mondays are hard... You are right, sorry 😅

Comment on lines +14 to +18
- name: Intall Vault
ansible.builtin.apt:
update_cache: true
name:
- vault
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not for this PR as our needs are simple and Vault-Agent should be mostly compatible with any version of Vault server (https://developer.hashicorp.com/vault/docs/agent-and-proxy/agent/versions), but at some point we might want to pin a specific version of packages, especially Vault since it's a critical part of our infrastructure and we should check changelogs and ensure backups before upgrades.

The Ansible docs hint at vault=1.18.2 for example, however this doesn't prevent someone on the server from running apt update && apt upgrade and installing a different version, as this does not pin it for apt itself.
The surefire way to pin a version is to create a /etc/apt/preferences.d/vault.pref:

Explanation: Ansible - Vault pinning
Package: vault
Pin: version 1.18.2-*
Pin-Priority: 1000

Pinned Vault package
Created working directory default variable
Copy link
Member

@nymous nymous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! You can merge when you want, and maybe then rebase your mail branch on top of the new main?

@Zakurama Zakurama merged commit 5827ecd into main Dec 1, 2024
3 checks passed
@Zakurama Zakurama deleted the vault-agent branch December 1, 2024 08:48
Zakurama added a commit that referenced this pull request Dec 1, 2024
This Ansible role may be used to deploy automatic retrieving of SSL
certificate on Vault.

---------

Co-authored-by: Thomas Gaudin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants