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.

138 lines
4.1KB

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