New version of salt-formula from Saltstack
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.

219 lines
6.8KB

  1. {%- from "salt/map.jinja" import minion with context %}
  2. {%- if minion.enabled %}
  3. {%- if grains.os_family == 'RedHat' %}
  4. {%- set cacerts_dir='/etc/pki/ca-trust/source/anchors' %}
  5. {%- else %}
  6. {%- set cacerts_dir='/usr/local/share/ca-certificates' %}
  7. {%- endif %}
  8. {%- if minion.cert is defined %}
  9. {%- set created_ca_files = [] %}
  10. {%- for cert_name,cert in minion.get('cert', {}).iteritems() %}
  11. {%- set rowloop = loop %}
  12. {%- set key_file = cert.get('key_file', '/etc/ssl/private/' + cert.common_name + '.key') %}
  13. {%- set cert_file = cert.get('cert_file', '/etc/ssl/certs/' + cert.common_name + '.crt') %}
  14. {%- set ca_file = cert.get('ca_file', '/etc/ssl/certs/ca-' + cert.authority + '.crt') %}
  15. {%- set key_dir = salt['file.dirname'](key_file) %}
  16. {%- set cert_dir = salt['file.dirname'](cert_file) %}
  17. {%- set ca_dir = salt['file.dirname'](ca_file) %}
  18. {# Only ensure directories exists, don't touch permissions, etc. #}
  19. salt_minion_cert_{{ cert_name }}_dirs:
  20. file.directory:
  21. - names:
  22. - {{ key_dir }}
  23. - {{ cert_dir }}
  24. - {{ ca_dir }}
  25. - makedirs: true
  26. - replace: false
  27. {{ key_file }}:
  28. x509.private_key_managed:
  29. - bits: {{ cert.get('bits', 4096) }}
  30. - require:
  31. - file: salt_minion_cert_{{ cert_name }}_dirs
  32. {%- if cert.all_file is defined %}
  33. - watch_in:
  34. - cmd: salt_minion_cert_{{ cert_name }}_all
  35. {%- endif %}
  36. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  37. {{ key_file }}_key_permissions:
  38. file.managed:
  39. - name: {{ key_file }}
  40. - mode: {{ cert.get("mode", 0600) }}
  41. {%- if salt['user.info'](cert.get("user", "root")) %}
  42. - user: {{ cert.get("user", "root") }}
  43. {%- endif %}
  44. {%- if salt['group.info'](cert.get("group", "root")) %}
  45. - group: {{ cert.get("group", "root") }}
  46. {%- endif %}
  47. - replace: false
  48. - require:
  49. - x509: {{ key_file }}
  50. {{ cert_file }}:
  51. x509.certificate_managed:
  52. {% if cert.host is defined %}- ca_server: {{ cert.host }}{%- endif %}
  53. {% if cert.authority is defined and cert.signing_policy is defined %}
  54. - signing_policy: {{ cert.authority }}_{{ cert.signing_policy }}
  55. {%- endif %}
  56. - public_key: {{ key_file }}
  57. - CN: "{{ cert.common_name }}"
  58. {% if cert.state is defined %}- ST: {{ cert.state }}{%- endif %}
  59. {% if cert.country is defined %}- C: {{ cert.country }}{%- endif %}
  60. {% if cert.locality is defined %}- L: {{ cert.locality }}{%- endif %}
  61. {% if cert.organization is defined %}- O: {{ cert.organization }}{%- endif %}
  62. {% if cert.organization_name is defined %}- organizationName: {{ cert.organization_name }}{%- endif %}
  63. {% if cert.signing_private_key is defined and cert.signing_cert is defined %}
  64. - signing_private_key: "{{ cert.signing_private_key }}"
  65. - signing_cert: "{{ cert.signing_cert }}"
  66. {%- endif %}
  67. {% if cert.alternative_names is defined %}
  68. - subjectAltName: "{{ cert.alternative_names }}"
  69. {%- endif %}
  70. {%- if cert.extended_key_usage is defined %}
  71. - extendedKeyUsage: "{{ cert.extended_key_usage }}"
  72. {%- endif %}
  73. {%- if cert.key_usage is defined %}
  74. - keyUsage: "{{ cert.key_usage }}"
  75. {%- endif %}
  76. - days_remaining: 30
  77. - backup: True
  78. - watch:
  79. - x509: {{ key_file }}
  80. {%- if cert.all_file is defined %}
  81. - watch_in:
  82. - cmd: salt_minion_cert_{{ cert_name }}_all
  83. {%- endif %}
  84. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  85. {{ cert_file }}_cert_permissions:
  86. file.managed:
  87. - name: {{ cert_file }}
  88. - mode: {{ cert.get("mode", 0600) }}
  89. {%- if salt['user.info'](cert.get("user", "root")) %}
  90. - user: {{ cert.get("user", "root") }}
  91. {%- endif %}
  92. {%- if salt['group.info'](cert.get("group", "root")) %}
  93. - group: {{ cert.get("group", "root") }}
  94. {%- endif %}
  95. - replace: false
  96. - require:
  97. - x509: {{ cert_file }}
  98. {%- if cert.host is defined and ca_file not in created_ca_files %}
  99. {%- for ca_path,ca_cert in salt['mine.get'](cert.host, 'x509.get_pem_entries').get(cert.host, {}).iteritems() %}
  100. {%- if '/etc/pki/all_cas/'+cert.authority in ca_path %}
  101. {{ ca_file }}:
  102. x509.pem_managed:
  103. - name: {{ ca_file }}
  104. - text: {{ ca_cert|replace('\n', '') }}
  105. - watch:
  106. - x509: {{ cert_file }}
  107. {%- if cert.all_file is defined %}
  108. - watch_in:
  109. - cmd: salt_minion_cert_{{ cert_name }}_all
  110. {%- endif %}
  111. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  112. {{ ca_file }}_cert_permissions:
  113. file.managed:
  114. - name: {{ ca_file }}
  115. - mode: 0644
  116. {%- if salt['user.info'](cert.get("user", "root")) %}
  117. - user: {{ cert.get("user", "root") }}
  118. {%- endif %}
  119. {%- if salt['group.info'](cert.get("group", "root")) %}
  120. - group: {{ cert.get("group", "root") }}
  121. {%- endif %}
  122. - require:
  123. - x509: {{ ca_file }}
  124. {%- endif %}
  125. {%- endfor %}
  126. {%- do created_ca_files.append(ca_file) %}
  127. {%- endif %}
  128. {%- if cert.all_file is defined %}
  129. salt_minion_cert_{{ cert_name }}_all:
  130. cmd.wait:
  131. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  132. {{ cert.all_file }}_cert_permissions:
  133. file.managed:
  134. - name: {{ cert.all_file }}
  135. - mode: {{ cert.get("mode", 0600) }}
  136. {%- if salt['user.info'](cert.get("user", "root")) %}
  137. - user: {{ cert.get("user", "root") }}
  138. {%- endif %}
  139. {%- if salt['group.info'](cert.get("group", "root")) %}
  140. - group: {{ cert.get("group", "root") }}
  141. {%- endif %}
  142. - replace: false
  143. - require:
  144. - cmd: salt_minion_cert_{{ cert_name }}_all
  145. {%- endif %}
  146. {%- endfor %}
  147. {%- endif %}
  148. salt_ca_certificates_packages:
  149. pkg.installed:
  150. - names: {{ minion.cert_pkgs }}
  151. salt_update_certificates:
  152. cmd.wait:
  153. {%- if grains.os_family == 'Debian' %}
  154. - name: "update-ca-certificates{% if minion.get('ca_certificates_cleanup') %} --fresh {% endif %}"
  155. {%- elif grains.os_family == 'RedHat' %}
  156. - name: "update-ca-trust extract"
  157. {%- else %}
  158. - name: true
  159. {%- endif %}
  160. - require:
  161. - pkg: salt_ca_certificates_packages
  162. {%- if minion.get('trust_salt_ca', True) %}
  163. {%- for trusted_ca_minion in minion.get('trusted_ca_minions', []) %}
  164. {%- for ca_host, certs in salt['mine.get'](trusted_ca_minion+'*', 'x509.get_pem_entries').iteritems() %}
  165. {%- for ca_path, ca_cert in certs.iteritems() %}
  166. {%- if ca_path.endswith('ca.crt') %}
  167. {# authority name can be obtained only from a cacert path in case of mine.get #}
  168. {%- set ca_authority = ca_path.split("/")[-1].split(".")[0] %}
  169. {%- set cacert_file="%s/ca-%s.crt" % (cacerts_dir,ca_authority) %}
  170. salt_trust_ca_{{ cacert_file }}:
  171. x509.pem_managed:
  172. - name: {{ cacert_file }}
  173. - text: {{ ca_cert|replace('\n', '') }}
  174. - watch_in:
  175. - file: salt_trust_ca_{{ cacert_file }}_permissions
  176. - cmd: salt_update_certificates
  177. salt_trust_ca_{{ cacert_file }}_permissions:
  178. file.managed:
  179. - name: {{ cacert_file }}
  180. - mode: 0444
  181. {%- endif %}
  182. {%- endfor %}
  183. {%- endfor %}
  184. {%- endfor %}
  185. {%- endif %}
  186. {%- endif %}