Opt-in to enforce RSA key length

This commit is contained in:
Alexander Weidinger 2017-03-04 12:41:28 +01:00
parent 66c954ed66
commit 6b23b28f52
2 changed files with 34 additions and 17 deletions

View File

@ -13,7 +13,7 @@ sshd_config:
- group: {{ openssh.sshd_config_group }} - group: {{ openssh.sshd_config_group }}
- mode: {{ openssh.sshd_config_mode }} - mode: {{ openssh.sshd_config_mode }}
- watch_in: - watch_in:
- service: openssh - service: {{ openssh.service }}
{% endif %} {% endif %}
{% if salt['pillar.get']('ssh_config', False) %} {% if salt['pillar.get']('ssh_config', False) %}
@ -27,45 +27,60 @@ ssh_config:
- mode: {{ openssh.ssh_config_mode }} - mode: {{ openssh.ssh_config_mode }}
{% endif %} {% endif %}
{% for keyType in ['ecdsa', 'dsa', 'rsa', 'ed25519'] %} {%- for keyType in ['ecdsa', 'dsa', 'rsa', 'ed25519'] %}
{% if salt['pillar.get']('openssh:generate_' ~ keyType ~ '_keys', False) %} {%- set keyFile = "/etc/ssh/ssh_host_" ~ keyType ~ "_key" %}
{%- set keySize = salt['pillar.get']('openssh:generate_' ~ keyType ~ '_size', False) %}
{%- if salt['pillar.get']('openssh:generate_' ~ keyType ~ '_keys', False) %}
{%- if keySize and salt['pillar.get']('openssh:enforce_' ~ keyType ~ '_size', False) %}
ssh_remove_short_{{ keyType }}_key:
cmd.run:
- name: "rm -f {{ keyFile }} {{ keyFile }}.pub"
- onlyif: "test -f {{ keyFile }}.pub && test `ssh-keygen -l -f {{ keyFile }}.pub 2>/dev/null | awk '{print $1}'` -lt {{ keySize }}"
- require_in:
- cmd: ssh_generate_host_{{ keyType }}_key
{%- endif %}
ssh_generate_host_{{ keyType }}_key: ssh_generate_host_{{ keyType }}_key:
cmd.run: cmd.run:
{%- if salt['pillar.get']('openssh:generate_' ~ keyType ~ '_size', False) %} {%- if keySize %}
{%- set keySize = salt['pillar.get']('openssh:generate_' ~ keyType ~ '_size', 4096) %} - name: ssh-keygen -t {{ keyType }} -b {{ keySize }} -N '' -f {{ keyFile }}
- name: ssh-keygen -t {{ keyType }} -b {{ keySize }} -N '' -f /etc/ssh/ssh_host_{{ keyType }}_key
{%- else %} {%- else %}
- name: ssh-keygen -t {{ keyType }} -N '' -f /etc/ssh/ssh_host_{{ keyType }}_key - name: ssh-keygen -t {{ keyType }} -N '' -f {{ keyFile }}
{%- endif %} {%- endif %}
- creates: /etc/ssh/ssh_host_{{ keyType }}_key - creates: /etc/ssh/ssh_host_{{ keyType }}_key
- user: root - user: root
- watch_in:
- service: {{ openssh.service }}
{% elif salt['pillar.get']('openssh:absent_' ~ keyType ~ '_keys', False) %} {%- elif salt['pillar.get']('openssh:absent_' ~ keyType ~ '_keys', False) %}
ssh_host_{{ keyType }}_key: ssh_host_{{ keyType }}_key:
file.absent: file.absent:
- name: /etc/ssh/ssh_host_{{ keyType }}_key - name: {{ keyFile }}
- watch_in:
- service: {{ openssh.service }}
ssh_host_{{ keyType }}_key.pub: ssh_host_{{ keyType }}_key.pub:
file.absent: file.absent:
- name: /etc/ssh/ssh_host_{{ keyType }}_key.pub - name: {{ keyFile }}.pub
- watch_in:
- service: {{ openssh.service }}
{% elif salt['pillar.get']('openssh:provide_' ~ keyType ~ '_keys', False) %} {%- elif salt['pillar.get']('openssh:provide_' ~ keyType ~ '_keys', False) %}
ssh_host_{{ keyType }}_key: ssh_host_{{ keyType }}_key:
file.managed: file.managed:
- name: /etc/ssh/ssh_host_{{ keyType }}_key - name: {{ keyFile }}
- contents_pillar: 'openssh:{{ keyType }}:private_key' - contents_pillar: 'openssh:{{ keyType }}:private_key'
- user: root - user: root
- mode: 600 - mode: 600
- require_in: - watch_in:
- service: {{ openssh.service }} - service: {{ openssh.service }}
ssh_host_{{ keyType }}_key.pub: ssh_host_{{ keyType }}_key.pub:
file.managed: file.managed:
- name: /etc/ssh/ssh_host_{{ keyType }}_key.pub - name: {{ keyFile }}.pub
- contents_pillar: 'openssh:{{ keyType }}:public_key' - contents_pillar: 'openssh:{{ keyType }}:public_key'
- user: root - user: root
- mode: 600 - mode: 600
- require_in: - watch_in:
- service: {{ openssh.service }} - service: {{ openssh.service }}
{% endif %} {%- endif %}
{% endfor %} {%- endfor %}

View File

@ -191,6 +191,8 @@ openssh:
generate_rsa_keys: False generate_rsa_keys: False
generate_rsa_size: 4096 generate_rsa_size: 4096
# Will remove the old key if it is to short and generate a new one.
enforce_rsa_size: False
absent_rsa_keys: False absent_rsa_keys: False
provide_rsa_keys: False provide_rsa_keys: False
rsa: rsa: