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.

226 lines
6.9KB

  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.signing_private_key is defined and cert.signing_cert is defined %}
  63. - signing_private_key: "{{ cert.signing_private_key }}"
  64. - signing_cert: "{{ cert.signing_cert }}"
  65. {%- endif %}
  66. {% if cert.alternative_names is defined %}
  67. - subjectAltName: "{{ cert.alternative_names }}"
  68. {%- endif %}
  69. {%- if cert.extended_key_usage is defined %}
  70. - extendedKeyUsage: "{{ cert.extended_key_usage }}"
  71. {%- endif %}
  72. {%- if cert.key_usage is defined %}
  73. - keyUsage: "{{ cert.key_usage }}"
  74. {%- endif %}
  75. - days_remaining: 30
  76. - backup: True
  77. - watch:
  78. - x509: {{ key_file }}
  79. {%- if cert.all_file is defined %}
  80. - watch_in:
  81. - cmd: salt_minion_cert_{{ cert_name }}_all
  82. {%- endif %}
  83. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  84. {{ cert_file }}_cert_permissions:
  85. file.managed:
  86. - name: {{ cert_file }}
  87. - mode: {{ cert.get("mode", 0600) }}
  88. {%- if salt['user.info'](cert.get("user", "root")) %}
  89. - user: {{ cert.get("user", "root") }}
  90. {%- endif %}
  91. {%- if salt['group.info'](cert.get("group", "root")) %}
  92. - group: {{ cert.get("group", "root") }}
  93. {%- endif %}
  94. - replace: false
  95. - require:
  96. - x509: {{ cert_file }}
  97. {%- if cert.host is defined and ca_file not in created_ca_files %}
  98. {%- for ca_path,ca_cert in salt['mine.get'](cert.host, 'x509.get_pem_entries').get(cert.host, {}).iteritems() %}
  99. {%- if '/etc/pki/ca/'+cert.authority in ca_path %}
  100. {{ ca_file }}:
  101. x509.pem_managed:
  102. - name: {{ ca_file }}
  103. - text: {{ ca_cert|replace('\n', '') }}
  104. - watch:
  105. - x509: {{ cert_file }}
  106. {%- if cert.all_file is defined %}
  107. - watch_in:
  108. - cmd: salt_minion_cert_{{ cert_name }}_all
  109. {%- endif %}
  110. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  111. {{ ca_file }}_cert_permissions:
  112. file.managed:
  113. - name: {{ ca_file }}
  114. - mode: 0644
  115. {%- if salt['user.info'](cert.get("user", "root")) %}
  116. - user: {{ cert.get("user", "root") }}
  117. {%- endif %}
  118. {%- if salt['group.info'](cert.get("group", "root")) %}
  119. - group: {{ cert.get("group", "root") }}
  120. {%- endif %}
  121. - require:
  122. - x509: {{ ca_file }}
  123. {%- endif %}
  124. {%- endfor %}
  125. {%- do created_ca_files.append(ca_file) %}
  126. {%- endif %}
  127. {%- if cert.all_file is defined %}
  128. salt_minion_cert_{{ cert_name }}_all:
  129. cmd.wait:
  130. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  131. {{ cert.all_file }}_cert_permissions:
  132. file.managed:
  133. - name: {{ cert.all_file }}
  134. - mode: {{ cert.get("mode", 0600) }}
  135. {%- if salt['user.info'](cert.get("user", "root")) %}
  136. - user: {{ cert.get("user", "root") }}
  137. {%- endif %}
  138. {%- if salt['group.info'](cert.get("group", "root")) %}
  139. - group: {{ cert.get("group", "root") }}
  140. {%- endif %}
  141. - replace: false
  142. - require:
  143. - cmd: salt_minion_cert_{{ cert_name }}_all
  144. {%- endif %}
  145. {%- endfor %}
  146. {%- endif %}
  147. salt_ca_certificates_packages:
  148. pkg.installed:
  149. {%- if grains.os_family == 'Debian' %}
  150. - name: ca-certificates
  151. {%- elif grains.os_family == 'RedHat' %}
  152. - name: ca-certificates
  153. {%- else %}
  154. - name: []
  155. {%- endif %}
  156. salt_update_certificates:
  157. cmd.wait:
  158. {%- if grains.os_family == 'Debian' %}
  159. - name: "update-ca-certificates{% if minion.get('ca_certificates_cleanup') %} --fresh {% endif %}"
  160. {%- elif grains.os_family == 'RedHat' %}
  161. - name: "update-ca-trust extract"
  162. {%- else %}
  163. - name: true
  164. {%- endif %}
  165. - require:
  166. - pkg: salt_ca_certificates_packages
  167. {%- if minion.get('trust_salt_ca', True) %}
  168. {%- for trusted_ca_minion in minion.get('trusted_ca_minions', []) %}
  169. {%- for ca_host, certs in salt['mine.get'](trusted_ca_minion+'*', 'x509.get_pem_entries').iteritems() %}
  170. {%- for ca_path, ca_cert in certs.iteritems() %}
  171. {%- if ca_path.endswith('ca.crt') %}
  172. {# authority name can be obtained only from a cacert path in case of mine.get #}
  173. {%- set ca_authority = ca_path.split("/")[-2] %}
  174. {%- set cacert_file="%s/ca-%s.crt" % (cacerts_dir,ca_authority) %}
  175. salt_trust_ca_{{ cacert_file }}:
  176. x509.pem_managed:
  177. - name: {{ cacert_file }}
  178. - text: {{ ca_cert|replace('\n', '') }}
  179. - makedirs: True
  180. - watch_in:
  181. - file: salt_trust_ca_{{ cacert_file }}_permissions
  182. - cmd: salt_update_certificates
  183. salt_trust_ca_{{ cacert_file }}_permissions:
  184. file.managed:
  185. - name: {{ cacert_file }}
  186. - mode: 0444
  187. {%- endif %}
  188. {%- endfor %}
  189. {%- endfor %}
  190. {%- endfor %}
  191. {%- endif %}
  192. {%- endif %}