feat(TOFS): ssh sshd configs known_host and banner

This commit is contained in:
nb committed 2019-07-01 14:46:46 +11:00
1 parent 14966e9a09
commit a47596f15a
12 files changed
+615 -13

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
Welcome to {{ grains['id'] }}!
Managed By
_____ ____ _____ __ __
/ ___/____ _/ / /_/ ___// /_____ ______/ /__
\__ \/ __ `/ / __/\__ \/ __/ __ `/ ___/ //_/
___/ / /_/ / / /_ ___/ / /_/ /_/ / /__/ ,<
/____/\__,_/_/\__//____/\__/\__,_/\___/_/|_|
Make sure changes get pushed into the state repo!
+13
View File
@@ -0,0 +1,13 @@
Welcome to {{ grains['id'] }}!
Managed By
( (
)\ ) ( ) )\ ) ) )
(()/( ) )\ ( /((()/( ( /( ) ( /(
/(_))( /( ((_))\())/(_)))\())( /( ( )\())
(_)) )(_)) _ (_))/(_)) (_))/ )(_)) )\ ((_)\
/ __|((_)_ | || |_ / __|| |_ ((_)_ ((_)| |(_)
\__ \/ _` || || _|\__ \| _|/ _` |/ _| | / /
|___/\__,_||_| \__||___/ \__|\__,_|\__| |_\_\
Make sure changes get pushed into the state repo!
+136
View File
@@ -0,0 +1,136 @@
{% from "openssh/map.jinja" import ssh_config with context %}
{#- present in ssh_config and known in actual file options -#}
{%- set processed_options = [] -%}
{%- set string_or_list_options = ['KexAlgorithms', 'Ciphers', 'MACs'] -%}
{%- macro render_raw_option(keyword, value) -%}
{%- if value is sameas true -%}
{{ keyword }} yes
{%- elif value is sameas false -%}
{{ keyword }} no
{%- elif value is string or value is number -%}
{{ keyword }} {{ value }}
{%- else -%}
{%- for single_value in value -%}
{{ keyword }} {{ single_value }}
{% endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- macro render_host_option(keyword, value) -%}
{%- if value is sameas true %}
{{ keyword }} yes
{%- elif value is sameas false %}
{{ keyword }} no
{%- elif value is string or value is number %}
{{ keyword }} {{ value }}
{%- else -%}
{%- if keyword in string_or_list_options %}
{{ keyword }} {{ value|join(',') }}
{%- else %}
{%- for single_value in value -%}
{{ keyword }} {{ single_value }}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{#- generic renderer used for ssh matches, known options, -#}
{#- and unknown options -#}
{%- macro render_option(keyword, default, config_dict=ssh_config) -%}
{%- set value = config_dict.get(keyword, default) -%}
{{ render_raw_option(keyword, value) }}
{%- endmacro -%}
{#- macros for render option according to present -#}
{%- macro option_impl(keyword, default, present) -%}
{%- if present -%}
{%- do processed_options.append(keyword) -%}
{%- set prefix='' -%}
{%- else -%}
{%- set prefix='#' -%}
{%- endif -%}
{#- add prefix to keyword -#}
{%- set keyword = prefix ~ keyword -%}
{{ render_option(keyword, default) }}
{%- endmacro -%}
{#- macros for render option commented by default -#}
{%- macro option(keyword, default, present) -%}
{{ option_impl(keyword, default, keyword in ssh_config) }}
{%- endmacro -%}
{#- macros for render option uncommented by default -#}
{%- macro option_default_uncommented(keyword, default, present) -%}
{{ option_impl(keyword, default, True) }}
{%- endmacro -%}
{#- macro for collapsing a list into a string -#}
{%- macro option_collapselist(keyword, sep) -%}
{%- do processed_options.append(keyword) -%}
{{keyword}} {{ssh_config.get(keyword)|join(sep)}}
{%- endmacro -%}
{#- macro for handling an option that can be specified as a list or a string -#}
{%- macro option_string_or_list(keyword, default, default_commented, sep=',') -%}
{%- if ssh_config.get(keyword, '') is string -%}
{%- if default_commented -%}
{{ option(keyword, default) }}
{%- else -%}
{{ option_default_uncommented(keyword, default) }}
{%- endif -%}
{%- else -%}
{{ option_collapselist(keyword, sep) }}
{%- endif -%}
{%- endmacro -%}
{%- if ssh_config.get('ConfigBanner', False) -%}
{{ ssh_config['ConfigBanner'] }}
{%- else -%}
# Do not edit this file manually!
# It will be overwritten by salt!
{%- endif %}
{%- set global_src_url = salt ['pillar.get']('__formulas:print_template_url', None) %}
{%- set local_src_url = salt ['pillar.get']('openssh-formula:print_template_url', None) %}
{%- if (global_src_url and local_src_url is none) or local_src_url %}
#
# Template used to generate this file:
# {{ source }}
{%- endif %}
{%- if 'Hosts' in ssh_config %}
{%- do processed_options.append('Hosts') %}
{% for host, conf in ssh_config['Hosts'].items() %}
Host {{ host }}
{%- for key, val in conf.items() -%}
{{ render_host_option(key, val) }}
{%- endfor %}
{%- endfor %}
{%- endif %}
{# Handling unknown in salt template options #}
{% for keyword in ssh_config.keys() -%}
{#- Matches have to be at the bottom and should be handled differently -#}
{%- if not keyword in processed_options and keyword != 'matches' -%}
{%- if not keyword in string_or_list_options -%}
{#- send a blank default as it doesn't matter #}
{{ render_option(keyword, '') }}
{%- else -%}
{#- same as above #}
{{ option_string_or_list(keyword, '', True) }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{# Handle matches last as they need to go at the bottom #}
{% if 'matches' in ssh_config -%}
{%- for match in ssh_config['matches'].values() %}
Match {{ match['type'].keys()[0] }} {{ match['type'].values()[0] }}
{%- for keyword in match['options'].keys() %}
{{ render_option(keyword, '', config_dict=match['options']) }}
{%- endfor %}
{%- endfor %}
{%- endif -%}
{#- vim: set ft=jinja : #}
+102
View File
@@ -0,0 +1,102 @@
{#
# vi:syntax=jinja
#}
{#- Generates one known_hosts entry per given key #}
{%- macro known_host_entry(host, host_names, keys, include_localhost) %}
{#- Get IPv4 and IPv6 addresses from the DNS #}
{%- set ip4 = salt['dig.A'](host) -%}
{%- set ip6 = salt['dig.AAAA'](host) -%}
{#- 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 = [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 -%}
{%- for name in names | sort -%}
{%- if salt["match.{}".format(hostnames_tgt_type)](hostnames_target, minion_id=name) -%}
{%- set hostname = name.split('.')|first -%}
{%- if hostname not in names -%}
{%- do names.append(hostname) -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{#- Append IP addresses and aliases (if they are not already present) #}
{%- for ip in (ip4 + ip6)|sort -%}
{%- do names.append(ip) -%}
{%- for alias in aliases_ips.get(ip, []) -%}
{%- if alias not in names -%}
{%- do names.append(alias) -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{#- Write one line per key; join the names together #}
{%- for line in keys.split('\n') -%}
{%- if line -%}
{{ ','.join(names) }} {{ line }}
{% endif -%}
{%- endfor -%}
{%- endmacro -%}
{#- Pre-fetch pillar data #}
{%- 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') -%}
{%- set use_hostnames = salt['pillar.get']('openssh:known_hosts:hostnames', False) -%}
{%- 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 -#}
{%- set aliases = salt['pillar.get']('openssh:known_hosts:aliases', []) -%}
{%- set aliases_ips = {} -%}
{%- for alias in aliases -%}
{%- for ip in salt['dig.A'](alias) + salt['dig.AAAA'](alias) -%}
{%- do aliases_ips.setdefault(ip, []).append(alias) -%}
{%- endfor -%}
{%- endfor -%}
{#- 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, include_localhost) }}
{%- endfor -%}
+232
View File
@@ -0,0 +1,232 @@
{% from "openssh/map.jinja" import sshd_config with context %}
{#- present in sshd_config and known in actual file options -#}
{%- set processed_options = [] -%}
{#- generic renderer used for sshd matches, known options, -#}
{#- and unknown options -#}
{%- macro render_option(keyword, config_dict=sshd_config) -%}
{%- set value = config_dict.get(keyword) -%}
{%- if value is sameas true -%}
{{ keyword }} yes
{% elif value is sameas false -%}
{{ keyword }} no
{% elif value is string or value is number -%}
{{ keyword }} {{ value }}
{% else -%}
{%- for single_value in value -%}
{{ keyword }} {{ single_value }}
{% endfor -%}
{%- endif -%}
{%- endmacro -%}
{#- macros for render option if present -#}
{%- macro option(keyword, present) -%}
{%- if keyword in sshd_config -%}
{%- do processed_options.append(keyword) -%}
{{ render_option(keyword) }}
{%- endif -%}
{%- endmacro -%}
{#- macro for collapsing a list into a string -#}
{%- macro option_collapselist(keyword, sep, config_dict=None) -%}
{%- if config_dict is sameas None -%}
{%- do processed_options.append(keyword) -%}
{%- set config_dict = sshd_config -%}
{%- endif -%}
{{ keyword }} {{ config_dict.get(keyword) | join(sep) }}
{% endmacro -%}
{#- macro for handling an option that can be specified as a list or a string -#}
{%- macro option_string_or_list(keyword, sep=',') -%}
{%- if sshd_config.get(keyword, '') is string -%}
{{ option(keyword) }}
{%- else -%}
{{ option_collapselist(keyword, sep) }}
{%- endif -%}
{%- endmacro -%}
{#- macro for conditionally joining a string, list or dict(keys) to just a string -#}
{%- macro join_to_string(src, keyword, sep=',') -%}
{%- set srcval = src.get(keyword, '') -%}
{%- if srcval is string -%}
{{ srcval }}
{%- elif srcval is mapping -%}
{{ srcval.keys() | sort | join(sep) }}
{%- else -%}
{{ srcval | join(sep) }}
{%- endif -%}
{%- endmacro -%}
{%- if sshd_config.get('ConfigBanner', False) -%}
{%- do processed_options.append('ConfigBanner') -%}
{{ sshd_config['ConfigBanner'] }}
{%- else -%}
# This file is managed by salt. Manual changes risk being overwritten.
{%- endif %}
{%- set global_src_url = salt ['pillar.get']('__formulas:print_template_url', None) %}
{%- set local_src_url = salt ['pillar.get']('openssh-formula:print_template_url', None) %}
{%- if (global_src_url and local_src_url is none) or local_src_url %}
#
# Template used to generate this file:
# {{ source }}
#
{%- endif %}
# The contents of the original sshd_config are kept on the bottom for
# quick reference.
# See the sshd_config(5) manpage for details
{# Specifies which address family should be used by sshd(8). -#}
{#- Valid arguments are any, inet (use IPv4 only), or inet6 (use IPv6 only) -#}
{{- option('AddressFamily') -}}
{#- What ports, IPs and protocols we listen for -#}
{{- option('Port') -}}
{#- Use these options to restrict which interfaces/protocols sshd will bind to -#}
{{- option('ListenAddress') -}}
{{- option('Protocol') -}}
{#- HostKeys for protocol version 2 -#}
{{- option('HostKey') -}}
{#- Privilege Separation is turned on for security -#}
{{- option('UsePrivilegeSeparation') -}}
{#- Logging -#}
{{- option('SyslogFacility') -}}
{{- option('LogLevel') -}}
{#- Session idle time out -#}
{{- option('ClientAliveInterval') -}}
{{- option('ClientAliveCountMax') -}}
{#- Authentication: -#}
{{- option('LoginGraceTime') -}}
{{- option('PermitRootLogin') -}}
{{- option('StrictModes') -}}
{{- option('MaxAuthTries') -}}
{{- option('MaxSessions') -}}
{{- option('PubkeyAuthentication') -}}
{{- option('AuthorizedKeysFile') -}}
{{- option('AuthorizedKeysCommand') -}}
{{- option('AuthorizedKeysCommandUser') -}}
{#- Don't read the user's ~/.rhosts and ~/.shosts files -#}
{{- option('IgnoreRhosts') -}}
{#- similar for protocol version 2 -#}
{{- option('HostbasedAuthentication') -}}
{#- Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication -#}
{{- option('IgnoreUserKnownHosts') -}}
{#- To enable empty passwords, change to yes (NOT RECOMMENDED) -#}
{{- option('PermitEmptyPasswords') -}}
{#- Change to yes to enable challenge-response passwords (beware issues with -#}
{#- some PAM modules and threads) -#}
{{- option('ChallengeResponseAuthentication') -}}
{{- option('AuthenticationMethods') -}}
{#- Change to no to disable tunnelled clear text passwords -#}
{{- option('PasswordAuthentication') -}}
{#- Kerberos options -#}
{{- option('KerberosAuthentication') -}}
{{- option('KerberosGetAFSToken') -}}
{{- option('KerberosOrLocalPasswd') -}}
{{- option('KerberosTicketCleanup') -}}
{#- GSSAPI options -#}
{{- option('GSSAPIAuthentication') -}}
{{- option('GSSAPICleanupCredentials') -}}
{{- option('X11Forwarding') -}}
{{- option('AllowTcpForwarding') -}}
{{- option('X11DisplayOffset') -}}
{{- option('PrintMotd') -}}
{#- Bug in FreeBSD 10.3 (?) See https://lists.freebsd.org/pipermail/freebsd-stable/2016-April/084501.html -#}
{% if not (salt['grains.get']('os') == 'FreeBSD' and salt['grains.get']('osrelease')|float >= 10.3) -%}
{{- option('PrintLastLog') -}}
{% endif -%}
{{- option('TCPKeepAlive') -}}
{{- option('UseLogin') -}}
{{- option('MaxStartups') -}}
{{- option('Banner') -}}
{#- Allow client to pass locale environment variables -#}
{{- option('AcceptEnv') -}}
{{- option('Subsystem') -}}
{% if not salt['grains.get']('os') == 'OpenBSD' -%}
{#- Set this to 'yes' to enable PAM authentication, account processing, -#}
{#- and session processing. If this is enabled, PAM authentication will -#}
{#- be allowed through the ChallengeResponseAuthentication and -#}
{#- PasswordAuthentication. Depending on your PAM configuration, -#}
{#- PAM authentication via ChallengeResponseAuthentication may bypass -#}
{#- the setting of "PermitRootLogin without-password". -#}
{#- If you just want the PAM account and session checks to run without -#}
{#- PAM authentication, then enable this but set PasswordAuthentication -#}
{#- and ChallengeResponseAuthentication to 'no'. -#}
{{- option('UsePAM') -}}
{%- endif %}
{#- DNS resolve and map remote IP addresses -#}
{{- option('UseDNS') -}}
{#- Restricting Users and Hosts -#}
{#- example: -#}
{#- AllowUsers vader@10.0.0.1 maul@sproing.evil.com luke -#}
{#- AllowGroups wheel staff -#}
{#- Keep in mind that using AllowUsers or AllowGroups means that anyone -#}
{#- not Matching one of the supplied patterns will be denied access by default. -#}
{#- Also, in order for sshd to allow access based on full or partial hostnames it -#}
{#- needs to to a DNS lookup -#}
{# DenyUsers -#}
{{- option_string_or_list('DenyUsers', sep=' ') }}
{# AllowUsers -#}
{{- option_string_or_list('AllowUsers', sep=' ') }}
{# DenyGroups -#}
{{- option_string_or_list('DenyGroups', sep=' ') }}
{# AllowGroups -#}
{{- option_string_or_list('AllowGroups', sep=' ') }}{{ "\n" -}}
{#- Specifies the available KEX (Key Exchange) algorithms. -#}
{{- option_string_or_list('KexAlgorithms') -}}
{#- Specifies the ciphers allowed for protocol version 2. -#}
{{- option_string_or_list('Ciphers') -}}
{#- Specifies the available MAC (message authentication code) algorithms. -#}
{{- option_string_or_list('MACs') -}}
{#- Handling unknown in salt template options -#}
{%- for keyword in sshd_config.keys() %}
{#- Matches have to be at the bottom and should be handled differently -#}
{%- if not keyword in processed_options and keyword != 'matches' -%}
{{- render_option(keyword) -}}
{% endif -%}
{%- endfor %}
{#- Handle matches last as they need to go at the bottom -#}
{%- if 'matches' in sshd_config %}
{%- for name, match in sshd_config['matches']|dictsort(true) %}
Match
{#- Set up the match criteria -#}
{%- for criteria in match['type'].keys()|sort() -%}
{{ ' ' -}}{{criteria }} {{ join_to_string(match['type'], criteria) }}
{%- endfor %} #{{- name }}
{# Set up the applied options -#}
{%- for keyword in match['options'].keys()|sort() -%}
{%- if keyword in ['AllowUsers', 'DenyUsers', 'AllowGroups', 'DenyGroups'] -%}
{{ option_collapselist(keyword, ' ', config_dict=match['options']) | indent(4, true) }}
{% else -%}
{{ render_option(keyword, config_dict=match['options']) | indent(4, true) }}
{% endif -%}
{%- endfor %}
{%- endfor %}
{%- endif %}
{#- vim: set ft=jinja : -#}