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