Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.3KB

  1. {% from "openssh/map.jinja" import openssh with context %}
  2. include:
  3. - openssh
  4. {% if salt['pillar.get']('sshd_config', False) %}
  5. sshd_config:
  6. file.managed:
  7. - name: {{ openssh.sshd_config }}
  8. - source: {{ openssh.sshd_config_src }}
  9. - template: jinja
  10. - user: {{ openssh.sshd_config_user }}
  11. - group: {{ openssh.sshd_config_group }}
  12. - mode: {{ openssh.sshd_config_mode }}
  13. - check_cmd: {{ openssh.sshd_binary }} -t -f
  14. - watch_in:
  15. - service: {{ openssh.service }}
  16. {% endif %}
  17. {% if salt['pillar.get']('ssh_config', False) %}
  18. ssh_config:
  19. file.managed:
  20. - name: {{ openssh.ssh_config }}
  21. - source: {{ openssh.ssh_config_src }}
  22. - template: jinja
  23. - user: {{ openssh.ssh_config_user }}
  24. - group: {{ openssh.ssh_config_group }}
  25. - mode: {{ openssh.ssh_config_mode }}
  26. {% endif %}
  27. {%- for keyType in ['ecdsa', 'dsa', 'rsa', 'ed25519'] %}
  28. {%- set keyFile = "/etc/ssh/ssh_host_" ~ keyType ~ "_key" %}
  29. {%- set keySize = salt['pillar.get']('openssh:generate_' ~ keyType ~ '_size', False) %}
  30. {%- if salt['pillar.get']('openssh:generate_' ~ keyType ~ '_keys', False) %}
  31. {%- if keySize and salt['pillar.get']('openssh:enforce_' ~ keyType ~ '_size', False) %}
  32. ssh_remove_short_{{ keyType }}_key:
  33. cmd.run:
  34. - name: "rm -f {{ keyFile }} {{ keyFile }}.pub"
  35. - onlyif: "test -f {{ keyFile }}.pub && test `ssh-keygen -l -f {{ keyFile }}.pub 2>/dev/null | awk '{print $1}'` -lt {{ keySize }}"
  36. - require_in:
  37. - cmd: ssh_generate_host_{{ keyType }}_key
  38. {%- endif %}
  39. ssh_generate_host_{{ keyType }}_key:
  40. cmd.run:
  41. {%- set keySizePart = "-b {}".format(keySize) if keySize else "" %}
  42. - name: "rm {{ keyFile }}*; ssh-keygen -t {{ keyType }} {{ keySizePart }} -N '' -f {{ keyFile }}"
  43. - unless: "test -s {{ keyFile }}"
  44. - user: root
  45. - require_in:
  46. - file: sshd_config
  47. - watch_in:
  48. - service: {{ openssh.service }}
  49. ssh_host_{{ keyType }}_key: # set permissions
  50. file.managed:
  51. - name: {{ keyFile }}
  52. - replace: false
  53. - mode: 0600
  54. - require:
  55. - cmd: ssh_generate_host_{{ keyType }}_key
  56. - require_in:
  57. - file: sshd_config
  58. {%- elif salt['pillar.get']('openssh:absent_' ~ keyType ~ '_keys', False) %}
  59. ssh_host_{{ keyType }}_key:
  60. file.absent:
  61. - name: {{ keyFile }}
  62. - watch_in:
  63. - service: {{ openssh.service }}
  64. ssh_host_{{ keyType }}_key.pub:
  65. file.absent:
  66. - name: {{ keyFile }}.pub
  67. - watch_in:
  68. - service: {{ openssh.service }}
  69. {%- elif salt['pillar.get']('openssh:provide_' ~ keyType ~ '_keys', False) %}
  70. ssh_host_{{ keyType }}_key:
  71. file.managed:
  72. - name: {{ keyFile }}
  73. - contents_pillar: 'openssh:{{ keyType }}:private_key'
  74. - user: root
  75. - mode: 600
  76. - require_in:
  77. - file: sshd_config
  78. - watch_in:
  79. - service: {{ openssh.service }}
  80. ssh_host_{{ keyType }}_key.pub:
  81. file.managed:
  82. - name: {{ keyFile }}.pub
  83. - contents_pillar: 'openssh:{{ keyType }}:public_key'
  84. - user: root
  85. - mode: 600
  86. - require_in:
  87. - file: sshd_config
  88. - watch_in:
  89. - service: {{ openssh.service }}
  90. {%- endif %}
  91. {%- endfor %}
  92. {%- if salt['pillar.get']('sshd_config:UsePrivilegeSeparation', '')|lower == 'yes' %}
  93. /var/run/sshd:
  94. file.directory:
  95. - user: root
  96. - mode: 755
  97. - require_in:
  98. - file: sshd_config
  99. - watch_in:
  100. - service: {{ openssh.service }}
  101. {% endif %}