Update to 2022-09-06 18:00

This commit is contained in:
Daniel Berteaud
2022-09-06 18:00:22 +02:00
parent ac46e06fb3
commit 382804b568
33 changed files with 311 additions and 113 deletions

View File

@@ -88,6 +88,12 @@ consul_base_conf:
enabled: False
# The default_policy is also used for intentions in the service mesh
default_policy: deny
enable_token_persistence: True
# You can set tokens used by the agent
# tokens:
# default: ab47bc38-d97f-19af-93a5-17b528d154c9
# agent: 5459979a-3f23-8b1f-ff8a-2478856e9216
tokens: {}
tls:
# No TLS will be stup unless this is set to True
@@ -110,18 +116,21 @@ consul_extra_conf: {}
consul_host_conf: {}
consul_conf: "{{ consul_base_conf | combine(consul_extra_conf, recursive=True) | combine(consul_host_conf, recursive=True) }}"
# To get certificates from vault
consul_base_vault_tls:
enabled: False
# address: https://active.vault.service.consul:8200
# token: XXXXXX
# TLS certs and token retrival from vault
consul_base_vault_secrets:
# vault_address: https://active.vault.service.consul:8200
# vault_token: XXXXXX
pki:
enabled: False
path: /pki/consul
role: consul-{{ consul_conf.server | ternary('server', 'client') }}
ttl: 24h
consul_extra_vault_tls: {}
consul_host_vault_tls: {}
consul_vault_tls: "{{ consul_base_vault_tls | combine(consul_extra_vault_tls, recursive=True) | combine(consul_host_vault_tls, recursive=True) }}"
tokens:
enabled: False
path: /consul
role: consul-agent
consul_extra_vault_secrets: {}
consul_host_vault_secrets: {}
consul_vault_secrets: "{{ consul_base_vault_secrets | combine(consul_extra_vault_secrets, recursive=True) | combine(consul_host_vault_secrets, recursive=True) }}"
# For example
# consul_extra_conf:

View File

@@ -94,42 +94,40 @@
loop: "{{ consul_backup_configs.stdout_lines }}"
tags: consul
- when: consul_vault_tls.enabled
block:
- name: Deploy consul-template config
template: src=consul-template.hcl.j2 dest={{ consul_root_dir }}/consul-template/consul-template.hcl
notify: restart consul-template-consul
- name: Deploy consul-template agent cert template
template: src=agent_cert.tpl.j2 dest={{ consul_root_dir }}/consul-template/{{ item.where }} owner=root group=root
loop:
- what: certificate
where: agent.crt.tpl
- what: private_key
where: agent.key.tpl
- what: issuing_ca
where: ca.crt.tpl
notify: restart consul-template-consul
- name: Check if certificate exists
stat: path={{ consul_conf.tls.defaults.cert_file }}
register: consul_tls_cert_file
- name: Deploy consul-template config
template: src=consul-template.hcl.j2 dest={{ consul_root_dir }}/consul-template/consul-template.hcl mode=600 owner=root group=root
notify: restart consul-template-consul
when: consul_vault_secrets.pki.enabled or consul_vault_secrets.tokens.enabled
tags: consul
- when: consul_vault_tls.enabled and consul_conf.server
block:
- name: Deploy consul-template agent cert template
template: src=agent_cert.tpl.j2 dest={{ consul_root_dir }}/consul-template/{{ item.where }} owner=root group=root
loop:
- what: certificate
where: agent.crt.tpl
- what: private_key
where: agent.key.tpl
- what: issuing_ca
where: ca.crt.tpl
notify: restart consul-template-consul
when: consul_vault_secrets.pki.enabled
tags: consul
- name: Deploy consul-template cli cert template
template: src=cli_cert.tpl.j2 dest={{ consul_root_dir }}/consul-template/{{ item.where }} owner=root group=root
loop:
- what: certificate
where: cli.crt.tpl
- what: private_key
where: cli.key.tpl
notify: restart consul-template-consul
- name: Deploy consul-template cli cert template
template: src=cli_cert.tpl.j2 dest={{ consul_root_dir }}/consul-template/{{ item.where }} owner=root group=root
loop:
- what: certificate
where: cli.crt.tpl
- what: private_key
where: cli.key.tpl
notify: restart consul-template-consul
when: consul_vault_secrets.pki.enabled and consul_conf.server
tags: consul
- name: Deploy the consul-template agent token template
template: src=agent.token.tpl.j2 dest={{ consul_root_dir }}/consul-template/agent.token.tpl owner=root group=root
notify: restart consul-template-consul
when: consul_vault_secrets.tokens.enabled
tags: consul
- name: Set ACL on the TLS dir

View File

@@ -6,6 +6,9 @@
tags: consul
- name: Handle consul-template-consul service
service: name=consul-template-consul state={{ consul_vault_tls.enabled | ternary('started', 'stopped') }} enabled={{ consul_vault_tls.enabled | ternary(True, False) }}
service:
name: consul-template-consul
state: "{{ (consul_vault_secrets.pki.enabled or consul_vault_secrets.tokens.enabled) | ternary('started', 'stopped') }}"
enabled: "{{ (consul_vault_secrets.pki.enabled or consul_vault_secrets.tokens.enabled) | ternary(True, False) }}"
tags: consul

View File

@@ -0,0 +1,3 @@
[[ with secret "{{ consul_vault_secrets.tokens.path }}/creds/{{ consul_vault_secrets.tokens.role }}" ]]
[[ .Data.token ]]
[[ end ]]

View File

@@ -1,14 +1,14 @@
{% if consul_conf.server %}
[[ with secret "{{ consul_vault_tls.pki.path }}/issue/{{ consul_vault_tls.pki.role }}" "common_name=server-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" "ttl={{ consul_vault_tls.pki.ttl }}" "alt_names=localhost,consul.service.{{ consul_conf.domain | default('consul') }},server.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" ]]
[[ with secret "{{ consul_vault_secrets.pki.path }}/issue/{{ consul_vault_secrets.pki.role }}" "common_name=server-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" "alt_names=localhost,consul.service.{{ consul_conf.domain | default('consul') }},server.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" ]]
[[ .Data.{{ item.what }} ]]
[[ end ]]
{% if item.what == 'certificate' %}
[[ with secret "{{ consul_vault_tls.pki.path }}/cert/ca" ]]
[[ with secret "{{ consul_vault_secrets.pki.path }}/cert/ca" ]]
[[ .Data.certificate ]]
[[ end ]]
{% endif %}
{% else %}
[[ with secret "{{ consul_vault_tls.pki.path }}/cert/ca" ]]
[[ with secret "{{ consul_vault_secrets.pki.path }}/cert/ca" ]]
[[ .Data.certificate ]]
[[ end ]]
{% endif %}

View File

@@ -1,3 +1,3 @@
[[ with secret "{{ consul_vault_tls.pki.path }}/issue/{{ consul_vault_tls.pki.role }}" "ttl={{ consul_vault_tls.pki.ttl }}" "common_name=cli-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" ]]
[[ with secret "{{ consul_vault_secrets.pki.path }}/issue/{{ consul_vault_secrets.pki.role }}" "common_name=cli-{{ ansible_fqdn | regex_replace('\\.', '-') }}.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}" ]]
[[ .Data.{{ item.what }} ]]
[[ end ]]

View File

@@ -8,6 +8,7 @@ ConditionFileNotEmpty={{ consul_root_dir }}/consul-template/consul-template.hcl
[Service]
Type=simple
ExecStart=/usr/local/bin/consul-template -config={{ consul_root_dir }}/consul-template/consul-template.hcl
SuccessExitStatus=12
ExecReload=/bin/kill --signal HUP $MAINPID
KillSignal=SIGINT
Restart=on-failure

View File

@@ -1,6 +1,6 @@
vault {
address = "{{ consul_vault_tls.address }}"
token = "{{ consul_vault_tls.token }}"
address = "{{ consul_vault_secrets.vault_address }}"
token = "{{ consul_vault_secrets.vault_token }}"
unwrap_token = false
}
@@ -11,10 +11,11 @@ template {
destination = "{{ consul_conf.tls.defaults.ca_file }}"
perms = 0644
exec {
command = "systemctl reload consul"
command = "sh -c 'systemctl reload consul || true'"
}
}
{% if consul_vault_secrets.pki.enabled %}
{% if consul_conf.server %}
template {
source = "{{ consul_root_dir }}/consul-template/agent.crt.tpl"
@@ -23,7 +24,7 @@ template {
destination = "{{ consul_conf.tls.defaults.cert_file }}"
perms = 0644
exec {
command = "systemctl reload consul"
command = "sh -c 'systemctl reload consul || true'"
}
}
@@ -34,7 +35,7 @@ template {
destination = "{{ consul_conf.tls.defaults.key_file }}"
perms = 0640
exec {
command = ["sh", "-c", "chgrp {{ consul_user }} {{ consul_conf.tls.defaults.key_file }} && systemctl reload consul"]
command = "sh -c 'chgrp {{ consul_user }} {{ consul_conf.tls.defaults.key_file }} && systemctl reload consul || true'"
}
}
@@ -53,3 +54,17 @@ template {
perms = 0640
}
{% endif %}
{% endif %}
{% if consul_vault_secrets.tokens.enabled %}
template {
source = "{{ consul_root_dir }}/consul-template/agent.token.tpl"
left_delimiter = "[["
right_delimiter = "]]"
destination = "{{ consul_root_dir }}/tmp/agent.token"
perms = 0600
exec {
command = "sh -c 'consul acl set-agent-token default $(grep -P \'^[^\s]\' {{ consul_root_dir }}/tmp/agent.token)'"
}
}
{% endif %}

View File

@@ -73,8 +73,19 @@ connect {
{% endif %}
acl {
enabled = {{ consul_conf.acl.enabled | ternary('true', 'false') }}
{% for key in ['enabled', 'enable_token_persistence'] %}
{% if consul_conf.acl[key] is defined %}
{{ key }} = {{ consul_conf.acl[key] | ternary('true', 'false') }}
{% endif %}
{% endfor %}
default_policy = "{{ consul_conf.acl.default_policy }}"
tokens {
{% for key in ['initial_management', 'default', 'agent', 'agent_recovery', 'replication'] %}
{% if consul_conf.acl.tokens[key] is defined %}
{{ key }} = "{{ consul_conf.acl.tokens[key] }}"
{% endif %}
{% endfor %}
}
}
{% if consul_conf.tls.enabled %}
@@ -108,5 +119,4 @@ auto_encrypt {
tls = true
}
{% endif %}
{% endif %}

View File

@@ -2,7 +2,7 @@
export CONSUL_HTTP_ADDR=https://localhost:{{ consul_services.https.port }}
export CONSUL_HTTP_SSL=true
export CONSUL_CACERT={{ consul_conf.tls.defaults.ca_file }}
{% if consul_vault_tls.enabled %}
{% if consul_vault_secrets.pki.enabled %}
export CONSUL_CLIENT_CERT={{ consul_root_dir }}/tls/cli.crt
export CONSUL_CLIENT_KEY={{ consul_root_dir }}/tls/cli.key
export CONSUL_TLS_SERVER_NAME=server.{{ consul_conf.datacenter | default('dc1') }}.{{ consul_conf.domain | default('consul') }}