Pillar openssh.known_hosts_salt_ssh (#128)

* Pillar openssh.known_hosts_salt_ssh

* Dropped ill-named file

* Fixed aliasing of host names

* Improved pillar.example

* Opt-in to include localhost

* pillar/known_hosts_salt_ssh: clear cache in run()

* Dropped forgotten debugging output
This commit is contained in:
alxwr
2018-06-01 14:11:52 +02:00
committed by Niels Abspoel
parent 11366b3c17
commit aa3da8f2c2
5 changed files with 273 additions and 10 deletions
+34 -5
View File
@@ -3,7 +3,7 @@
#}
{#- Generates one known_hosts entry per given key #}
{%- macro known_host_entry(host, host_names, keys) %}
{%- macro known_host_entry(host, host_names, keys, include_localhost) %}
{#- Get IPv4 and IPv6 addresses from the DNS #}
{%- set ip4 = salt['dig.A'](host) -%}
@@ -11,7 +11,13 @@
{#- The host names to use are to be found within the dict 'host_names'. #}
{#- If there are none, the host is used directly. #}
{%- set names = [host_names.get(host, host)] -%}
{%- set names = host_names.get(host, host) -%}
{%- set names = [names] if names is string else names %}
{%- if include_localhost and host == grains['id'] %}
{%- do names.append('localhost') %}
{%- do names.append('127.0.0.1') %}
{%- do names.append('::1') %}
{%- endif -%}
{#- Extract the hostname from the FQDN and add it to the names. #}
{%- if use_hostnames is iterable -%}
@@ -44,7 +50,7 @@
{%- endmacro -%}
{#- Pre-fetch pillar data #}
{%- set target = salt['pillar.get']('openssh:known_hosts:target', '*') -%}
{%- set target = salt['pillar.get']('openssh:known_hosts:target', "*.{}".format(grains['domain'])) -%}
{%- set tgt_type = salt['pillar.get']('openssh:known_hosts:tgt_type', 'glob') -%}
{%- set keys_function = salt['pillar.get']('openssh:known_hosts:mine_keys_function', 'public_ssh_host_keys') -%}
{%- set hostname_function = salt['pillar.get']('openssh:known_hosts:mine_hostname_function', 'public_ssh_hostname') -%}
@@ -52,6 +58,7 @@
{%- set hostnames_target_default = '*' if grains['domain'] == '' else "*.{}".format(grains['domain']) -%}
{%- set hostnames_target = salt['pillar.get']('openssh:known_hosts:hostnames:target', hostnames_target_default) -%}
{%- set hostnames_tgt_type = salt['pillar.get']('openssh:known_hosts:hostnames:tgt_type', 'glob') -%}
{%- set include_localhost = salt['pillar.get']('openssh:known_hosts:include_localhost', False) -%}
{#- Lookup IP of all aliases so that when we have a matching IP, we inject the alias name
in the SSH known_hosts entry -#}
@@ -63,11 +70,33 @@
{%- endfor -%}
{%- endfor -%}
{#- Loop over targetted minions -#}
{#- Salt Mine #}
{%- set host_keys = salt['mine.get'](target, keys_function, tgt_type=tgt_type) -%}
{%- set host_names = salt['mine.get'](target, hostname_function, tgt_type=tgt_type) -%}
{#- Salt SSH (if any) #}
{%- for minion_id, minion_host_keys in salt['pillar.get'](
'openssh:known_hosts:salt_ssh:public_ssh_host_keys',
{}
).items() -%}
{%- if salt["match.{}".format(tgt_type)](target, minion_id=minion_id) -%}
{% do host_keys.update({minion_id: minion_host_keys}) %}
{%- endif -%}
{%- endfor -%}
{%- for minion_id, minion_host_names in salt['pillar.get'](
'openssh:known_hosts:salt_ssh:public_ssh_host_names',
{}
).items() -%}
{%- if salt["match.{}".format(tgt_type)](target, minion_id=minion_id) -%}
{% do host_names.update({minion_id: minion_host_names}) %}
{%- endif -%}
{%- endfor %}
{#- Static Pillar data #}
{%- do host_keys.update(salt['pillar.get']('openssh:known_hosts:static',
{}).items()) -%}
{#- Loop over targetted minions -#}
{%- for host, keys in host_keys| dictsort -%}
{{ known_host_entry(host, host_names, keys) }}
{{ known_host_entry(host, host_names, keys, include_localhost) }}
{%- endfor -%}
+36
View File
@@ -0,0 +1,36 @@
{%- set minions = salt.slsutil.renderer(opts['config_dir'] + '/roster') %}
{%- set cache_dir = opts['cachedir'] + '/../master/known_hosts_salt_ssh' %}
{%- set cmd = "cat /etc/ssh/ssh_host_*_key.pub 2>/dev/null" %}
{{ cache_dir }}:
file.directory:
- makedirs: True
{%- for minion_id in minions %}
{%- set salt_ssh_cmd = "salt-ssh --out=json --static '{}' cmd.run_all '{}'".format(minion_id, cmd) %}
{%- set result = salt['cmd.run_all'](salt_ssh_cmd,
python_shell=True,
runas=salt['pillar.get']('openssh:known_hosts:salt_ssh:user', 'salt-master')
)
%}
{%- set pubkeys = False %}
{%- if result['retcode'] == 0 %}
{%- load_json as inner_result %}
{{ result['stdout'] }}
{%- endload %}
{%- set pubkeys = inner_result[minion_id]['stdout'].splitlines() | sort | join("\n") %}
{%- else %}
{%- do salt.log.error("{} failed: {}".format(salt_ssh_cmd, result)) %}
{%- endif %}
{%- if pubkeys %}
{{ cache_dir }}/{{ minion_id }}.pub:
file.managed:
- contents: |
{{ pubkeys | indent(8) }}
- require:
- file: {{ cache_dir }}
{%- endif %}
{%- endfor %}