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.

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