Update to 2023-07-08 16:00

This commit is contained in:
Daniel Berteaud
2023-07-08 16:00:11 +02:00
parent f16dd6e98d
commit d87c1bdc89
60 changed files with 708 additions and 439 deletions

View File

@@ -0,0 +1,13 @@
{% if consul_conf.server %}
[[ with pkiCert "{{ vault_agent_consul.consul_pki.path }}/issue/{{ vault_agent_consul.consul_pki.role }}" "common_name=server-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" "alt_names=consul.service.{{ consul_conf.domain | default('consul') }},server.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}{% if consul_conf.alt_domain is defined %},consul.service.{{ consul_conf.alt_domain }},server.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.alt_domain }}{% endif %}"{% if vault_agent_consul.consul_pki.ttl is defined %} "ttl={{ vault_agent_consul.consul_pki.ttl }}"{% endif %} ]]
[[ .CA ]]
[[ .Cert ]]
[[ .Key ]]
[[ .Cert | writeToFile "{{ consul_conf.tls.defaults.cert_file }}" "root" "{{ consul_user }}" "0644" ]]
[[ .Key | writeToFile "{{ consul_conf.tls.defaults.key_file }}" "root" "{{ consul_user }}" "0640" ]]
[[ end ]]
{% else %}
[[ with secret "{{ vault_agent_consul.consul_pki.path }}/cert/ca_chain" ]]
[[ .Data.certificate | writeToFile "{{ consul_conf.tls.defaults.ca_file }}" "root" "{{ consul_user }}" "0644" ]]
[[ end ]]
{% endif %}

View File

@@ -0,0 +1,12 @@
{% if vault_agent_consul.consul_pki.enabled %}
template {
source = "{{ vault_agent_root_dir }}/templates/consul/agent_bundle.pem.tpl"
destination = "{{ consul_root_dir }}/tls/agent_bundle.pem"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
exec {
command = ["systemctl", "reload", "consul"]
}
}
{% endif %}

View File

@@ -0,0 +1,10 @@
[[ with pkiCert "{{ vault_agent_nomad.nomad_pki.path }}/issue/{{ vault_agent_nomad.nomad_pki.role }}" "common_name={{ (nomad_conf.server.enabled) | ternary('server', 'client') }}-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ nomad_conf.region | default('global') }}.nomad" "alt_names=localhost,{{ (nomad_conf.server.enabled) | ternary('server', 'client') }}.{{ nomad_conf.region | default('global') }}.nomad{% if nomad_conf.server.enabled and nomad_conf.client.enabled %},client.{{ nomad_conf.region | default('global') }}.nomad{% endif %}{% if consul_conf is defined %},nomad{{ nomad_conf.server.enabled | ternary('', '-client') }}.service.{{ consul_conf.domain | default('consul') }}{% if consul_conf is defined and consul_conf.alt_domain is defined %},nomad{{ nomad_conf.server.enabled | ternary('', '-client') }}.service.{{ consul_conf.alt_domain }}{% endif %}{% endif %}"{% if nomad_vault_secrets.pki.ttl is defined %} "ttl={{ vault_agent_nomad.nomad_pki.ttl }}"{% endif %} ]]
[[ .CA ]]
[[ .Cert ]]
[[ .Key ]]
[[ .Cert | writeToFile "{{ nomad_conf.tls.cert_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0644" ]]
[[ .Key | writeToFile "{{ nomad_conf.tls.key_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0640" ]]
[[ end ]]
[[ with secret "{{ vault_agent_nomad.nomad_pki.path }}/cert/ca_chain" ]]
[[ .Data.certificate | writeToFile "{{ nomad_conf.tls.ca_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0644" ]]
[[ end ]]

View File

@@ -0,0 +1,6 @@
[[ with pkiCert "{{ vault_agent_nomad.nomad_pki.path }}/issue/{{ vault_agent_nomad.nomad_pki.role }}" "common_name=cli-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ nomad_conf.region | default('global') }}.nomad" ]]
[[ .Cert ]]
[[ .Key ]]
[[ .Cert | writeToFile "{{ nomad_root_dir }}/tls/cli.crt" "{{ nomad_user }}" "{{ nomad_user }}" "0644" ]]
[[ .Key | writeToFile "{{ nomad_root_dir }}/tls/cli.key" "{{ nomad_user }}" "{{ nomad_user }}" "0640" ]]
[[ end ]]

View File

@@ -0,0 +1,3 @@
[[- with secret "{{ vault_agent_nomad.consul_token.path }}/creds/{{ vault_agent_nomad.consul_token.role }}" -]]
CONSUL_HTTP_TOKEN=[[ .Data.token ]]
[[- end -]]

View File

@@ -0,0 +1,10 @@
[[ with pkiCert "{{ vault_agent_nomad.consul_pki.path }}/issue/{{ vault_agent_nomad.consul_pki.role }}" "common_name={{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ (consul_conf is defined and consul_conf.domain is defined) | ternary(consul_conf.domain, 'consul') }}"{% if vault_agent_nomad.consul_pki.ttl is defined %} "ttl={{ vault_agent_nomad.consul_pki.ttl }}"{% endif %} ]]
[[ .CA ]]
[[ .Cert ]]
[[ .Key ]]
[[ .Cert | writeToFile "{{ nomad_conf.consul.cert_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0644" ]]
[[ .Key | writeToFile "{{ nomad_conf.consul.key_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0640" ]]
[[ end ]]
[[ with secret "{{ vault_agent_nomad.consul_pki.path }}/cert/ca_chain" ]]
[[ .Data.certificate | writeToFile "{{ nomad_conf.consul.ca_file }}" "{{ nomad_user }}" "{{ nomad_user }}" "0644" ]]
[[ end ]]

View File

@@ -0,0 +1,69 @@
{% if vault_agent_nomad.vault_token.enabled %}
template {
source = "{{ vault_agent_root_dir }}/templates/nomad/vault.env.tpl"
destination = "/run/nomad/vault.env"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
exec {
# Wait a few sec before reloading Nomad as it fails if reloaded while not fully initialized yet
command = ["chown", ":{{ nomad_user }}", "/run/nomad/vault.env"]
}
}
{% endif %}
{% if vault_agent_nomad.nomad_pki.enabled %}
template {
source = "{{ vault_agent_root_dir }}/templates/nomad/agent_bundle.pem.tpl"
destination = "{{ nomad_root_dir }}/tls/agent_bundle.pem"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
exec {
# Wait a few sec before reloading Nomad as it fails if reloaded while not fully initialized yet
command = ["sh", "-c", "sleep 15 && systemctl reload nomad || true"]
}
}
{% if nomad_conf.server.enabled %}
template {
source = "{{ vault_agent_root_dir }}/templates/nomad/cli_bundle.pem.tpl"
destination = "{{ nomad_root_dir }}/tls/cli_bundle.pem"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
{% if vault_agent_nomad.nomad_pki.cli.enabled and vault_agent_nomad.nomad_pki.cli.secret_path is defined %}
exec {
command = "{{ vault_agent_root_dir }}/bin/update_nomad_cert.sh"
}
{% endif %}
}
{% endif %}
{% endif %}
{% if vault_agent_nomad.consul_pki.enabled and nomad_conf.consul.ssl %}
template {
source = "{{ vault_agent_root_dir }}/templates/nomad/consul_bundle.pem.tpl"
destination = "{{ nomad_root_dir }}/tls/consul_bundle.pem"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
exec {
command = ["sh", "-c", "chown :{{ nomad_user }} && sleep 15 && systemctl reload nomad || true"]
}
}
{% endif %}
{% if vault_agent_nomad.consul_token.enabled %}
template {
source = "{{ vault_agent_root_dir }}/templates/nomad/consul.env.tpl"
destination = "/run/nomad/consul.env"
left_delimiter = "[["
right_delimiter = "]]"
perms = 0640
exec {
command = ["chown", ":{{ nomad_user}}", "/run/nomad/consul.env"]
}
}
{% endif %}

View File

@@ -0,0 +1,20 @@
#!/bin/sh
set -eo pipefail
VAULT_ADDR={{ vault_agent_vault_address }}
VAULT_STATUS=$(vault status -format=json)
if [ "$(echo ${VAULT_STATUS} | jq .is_self)" != "true" ]; then
echo "We're not the active vault, exiting"
elif [ "$(echo ${VAULT_STATUS} | jq .sealed)" != "false" ]; then
echo "Vault is sealed, exiting"
elif [ "$(echo ${VAULT_STATUS} | jq .initialized)" != "true" ]; then
echo "Vault is not initialized yet, exiting"
else
echo Updating Vault certificate to access Nomad API
vault write {{ vault_agent_nomad.nomad_pki.cli.secret_path }}/config/access \
ca_cert="$(cat {{ nomad_root_dir }}/tls/ca.crt)" \
client_cert="$(cat {{ nomad_root_dir }}/tls/cli.crt)" \
client_key="$(cat {{ nomad_root_dir }}/tls/cli.key)"
fi

View File

@@ -0,0 +1,3 @@
[[- with secret "auth/token/create/{{ vault_agent_nomad.vault_token.role }}" "no_parent=true" -]]
VAULT_TOKEN=[[ .Auth.ClientToken ]]
[[- end -]]

View File

@@ -0,0 +1,58 @@
vault {
address = "{{ vault_agent_vault_address }}"
}
auto_auth {
{% if vault_agent_auth == 'approle' %}
method {
type = "approle"
config {
role_id_file_path = "{{ vault_agent_root_dir }}/auth/role_id"
secret_id_file_path = "{{ vault_agent_root_dir }}/auth/secret_id"
remove_secret_id_file_after_reading = false
}
}
{% elif vault_agent_auth == 'token' %}
method {
type = "token_file"
config {
token_file_path = "{{ vault_agent_root_dir }}/auth/token"
}
}
{% endif %}
# Not used, but prevent service failing if there's not template yet
sink {
type = "file"
wrap_ttl = "1s"
config = {
path = "/run/vault_agent/vault.token"
mode = 600
}
}
}
{% for template in vault_agent_templates %}
template {
{% if template.source is defined %}
source = "{{ template.source }}"
{% elif template.contents is defined %}
contents = "{{ template.contents }}"
{% endif %}
destination = "{{ template.destination }}"
{% for prop in ['left_delimiter', 'right_delimiter', 'perms'] %}
{% if template[prop] is defined %}
{{ prop }} = "{{ template[prop] }}"
{% endif %}
{% endfor %}
{% if template.exec is defined and template.exec.command is defined %}
exec {
{% if template.exec.timeout is defined %}
timeout = "{{ template.exec.timeout }}"
{% endif %}
command = "template.exec.command"
}
{% endif %}
}
{% endfor %}

View File

@@ -0,0 +1,49 @@
[Unit]
Description="HashiCorp Vault Agent"
Documentation=https://www.vaultproject.io/docs/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty={{ vault_agent_root_dir }}/etc/vault-agent.hcl
StartLimitIntervalSec=60
StartLimitBurst=6
Before=consul.service
Before=nomad.service
{% if vault_agent_nomad.nomad_pki.enabled or vault_agent_nomad.vault_token.enabled or vault_agent_nomad.consul_pki.enabled or vault_agent_nomad.consul_token.enabled %}
PartOf=nomad.service
{% endif %}
{% if vault_agent_consul.consul_pki.enabled %}
PartOf=consul.service
{% endif %}
[Service]
Type=notify
User=root
Group=root
SyslogIdentifier=vault-agent
ProtectSystem=full
ProtectHome=read-only
PrivateTmp=yes
PrivateDevices=yes
SecureBits=keep-caps
AmbientCapabilities=CAP_IPC_LOCK
CapabilityBoundingSet=CAP_SYSLOG CAP_IPC_LOCK CAP_CHOWN CAP_FOWNER CAP_DAC_OVERRIDE
NoNewPrivileges=yes
ExecStart=/usr/local/bin/vault agent -config={{ vault_agent_root_dir }}/etc/
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGINT
Restart=on-failure
RestartSec=5
TimeoutStopSec=30
LimitNOFILE=65536
LimitMEMLOCK=infinity
[Install]
WantedBy=multi-user.target
{% if vault_agent_nomad.nomad_pki.enabled or vault_agent_nomad.vault_token.enabled or vault_agent_nomad.consul_pki.enabled or vault_agent_nomad.consul_token.enabled %}
RequiredBy=nomad.service
{% endif %}
{% if vault_agent_consul.consul_pki.enabled %}
RequiredBy=consul.service
{% endif %}