Browse Source

Merge pull request #88 from alxwr/force_key_length

Opt-in to enforce RSA key length
master
alxwr 7 years ago
parent
commit
844e96b57b
2 changed files with 34 additions and 17 deletions
  1. +32
    -17
      openssh/config.sls
  2. +2
    -0
      pillar.example

+ 32
- 17
openssh/config.sls View File

- 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) %}
- mode: {{ openssh.ssh_config_mode }} - mode: {{ openssh.ssh_config_mode }}
{% endif %} {% endif %}


{% for keyType in ['ecdsa', 'dsa', 'rsa', 'ed25519'] %}
{% if salt['pillar.get']('openssh:generate_' ~ keyType ~ '_keys', False) %}
{%- for keyType in ['ecdsa', 'dsa', 'rsa', 'ed25519'] %}
{%- 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) %}
{%- set keySize = salt['pillar.get']('openssh:generate_' ~ keyType ~ '_size', 4096) %}
- name: ssh-keygen -t {{ keyType }} -b {{ keySize }} -N '' -f /etc/ssh/ssh_host_{{ keyType }}_key
{%- if keySize %}
- name: ssh-keygen -t {{ keyType }} -b {{ keySize }} -N '' -f {{ keyFile }}
{%- 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 %}
{% endfor %}
{%- endif %}
{%- endfor %}

+ 2
- 0
pillar.example View File



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:

Loading…
Cancel
Save