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.

127 lines
3.7KB

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