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
7.0KB

  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. {%- if grains['saltversioninfo'][0] >= 2017 %}
  85. - retry:
  86. attepmts: 5
  87. until: True
  88. interval: 60
  89. {%- endif %}
  90. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  91. {{ cert_file }}_cert_permissions:
  92. file.managed:
  93. - name: {{ cert_file }}
  94. - mode: {{ cert.get("mode", 0600) }}
  95. {%- if salt['user.info'](cert.get("user", "root")) %}
  96. - user: {{ cert.get("user", "root") }}
  97. {%- endif %}
  98. {%- if salt['group.info'](cert.get("group", "root")) %}
  99. - group: {{ cert.get("group", "root") }}
  100. {%- endif %}
  101. - replace: false
  102. - require:
  103. - x509: {{ cert_file }}
  104. {%- if cert.host is defined and ca_file not in created_ca_files %}
  105. {%- for ca_path,ca_cert in salt['mine.get'](cert.host, 'x509.get_pem_entries').get(cert.host, {}).iteritems() %}
  106. {%- if '/etc/pki/ca/'+cert.authority in ca_path %}
  107. {{ ca_file }}:
  108. x509.pem_managed:
  109. - name: {{ ca_file }}
  110. - text: {{ ca_cert|replace('\n', '') }}
  111. - watch:
  112. - x509: {{ cert_file }}
  113. {%- if cert.all_file is defined %}
  114. - watch_in:
  115. - cmd: salt_minion_cert_{{ cert_name }}_all
  116. {%- endif %}
  117. # TODO: Squash this with the previous state after switch to Salt version >= 2016.11.2
  118. {{ ca_file }}_cert_permissions:
  119. file.managed:
  120. - name: {{ ca_file }}
  121. - mode: 0644
  122. {%- if salt['user.info'](cert.get("user", "root")) %}
  123. - user: {{ cert.get("user", "root") }}
  124. {%- endif %}
  125. {%- if salt['group.info'](cert.get("group", "root")) %}
  126. - group: {{ cert.get("group", "root") }}
  127. {%- endif %}
  128. - require:
  129. - x509: {{ ca_file }}
  130. {%- endif %}
  131. {%- endfor %}
  132. {%- do created_ca_files.append(ca_file) %}
  133. {%- endif %}
  134. {%- if cert.all_file is defined %}
  135. salt_minion_cert_{{ cert_name }}_all:
  136. cmd.wait:
  137. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  138. {{ cert.all_file }}_cert_permissions:
  139. file.managed:
  140. - name: {{ cert.all_file }}
  141. - mode: {{ cert.get("mode", 0600) }}
  142. {%- if salt['user.info'](cert.get("user", "root")) %}
  143. - user: {{ cert.get("user", "root") }}
  144. {%- endif %}
  145. {%- if salt['group.info'](cert.get("group", "root")) %}
  146. - group: {{ cert.get("group", "root") }}
  147. {%- endif %}
  148. - replace: false
  149. - require:
  150. - cmd: salt_minion_cert_{{ cert_name }}_all
  151. {%- endif %}
  152. {%- endfor %}
  153. {%- endif %}
  154. salt_ca_certificates_packages:
  155. pkg.installed:
  156. - names: {{ minion.cert_pkgs }}
  157. salt_update_certificates:
  158. cmd.wait:
  159. {%- if grains.os_family == 'Debian' %}
  160. - name: "update-ca-certificates{% if minion.get('ca_certificates_cleanup') %} --fresh {% endif %}"
  161. {%- elif grains.os_family == 'RedHat' %}
  162. - name: "update-ca-trust extract"
  163. {%- else %}
  164. - name: true
  165. {%- endif %}
  166. - require:
  167. - pkg: salt_ca_certificates_packages
  168. {%- if minion.get('trust_salt_ca', True) %}
  169. {%- for trusted_ca_minion in minion.get('trusted_ca_minions', []) %}
  170. {%- for ca_host, certs in salt['mine.get'](trusted_ca_minion+'*', 'x509.get_pem_entries').iteritems() %}
  171. {%- for ca_path, ca_cert in certs.iteritems() %}
  172. {%- if ca_path.endswith('ca.crt') %}
  173. {# authority name can be obtained only from a cacert path in case of mine.get #}
  174. {%- set ca_authority = ca_path.split("/")[-2] %}
  175. {%- set cacert_file="%s/ca-%s.crt" % (cacerts_dir,ca_authority) %}
  176. salt_trust_ca_{{ cacert_file }}:
  177. x509.pem_managed:
  178. - name: {{ cacert_file }}
  179. - text: {{ ca_cert|replace('\n', '') }}
  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 %}