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.

209 lines
6.2KB

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