New version of salt-formula from Saltstack
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

202 行
6.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 = 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. {%- endif %}
  103. {%- endfor %}
  104. {%- do created_ca_files.append(ca_file) %}
  105. {%- endif %}
  106. {%- if cert.all_file is defined %}
  107. salt_minion_cert_{{ cert_name }}_all:
  108. cmd.wait:
  109. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  110. - watch:
  111. - x509: {{ key_file }}
  112. - x509: {{ cert_file }}
  113. - x509: {{ ca_file }}
  114. {{ cert.all_file }}_cert_permissions:
  115. file.managed:
  116. - name: {{ cert.all_file }}
  117. - mode: {{ cert.get("mode", 0600) }}
  118. {%- if salt['user.info'](cert.get("user", "root")) %}
  119. - user: {{ cert.get("user", "root") }}
  120. {%- endif %}
  121. {%- if salt['group.info'](cert.get("group", "root")) %}
  122. - group: {{ cert.get("group", "root") }}
  123. {%- endif %}
  124. - replace: false
  125. - watch:
  126. - cmd: salt_minion_cert_{{ cert_name }}_all
  127. {%- endif %}
  128. {%- endfor %}
  129. {%- endif %}
  130. salt_ca_certificates_packages:
  131. pkg.installed:
  132. {%- if grains.os_family == 'Debian' %}
  133. - name: ca-certificates
  134. {%- elif grains.os_family == 'RedHat' %}
  135. - name: ca-certificates
  136. {%- else %}
  137. - name: []
  138. {%- endif %}
  139. salt_update_certificates:
  140. cmd.wait:
  141. {%- if grains.os_family == 'Debian' %}
  142. - name: "update-ca-certificates{% if minion.get('ca_certificates_cleanup') %} --fresh {% endif %}"
  143. {%- elif grains.os_family == 'RedHat' %}
  144. - name: "update-ca-trust extract"
  145. {%- else %}
  146. - name: true
  147. {%- endif %}
  148. - require:
  149. - pkg: salt_ca_certificates_packages
  150. {%- if minion.get('cert', {}).get('trust_salt_ca', 'True') %}
  151. {%- for trusted_ca_minion in minion.get('trusted_ca_minions', []) %}
  152. {%- for ca_host, certs in salt['mine.get'](trusted_ca_minion+'*', 'x509.get_pem_entries').iteritems() %}
  153. {%- for ca_path, ca_cert in certs.iteritems() %}
  154. {%- if not 'ca.crt' in ca_path %}{% continue %}{% endif %}
  155. {%- set cacert_file="ca-"+ca_path.split("/")[4]+".crt" %}
  156. salt_cert_{{ cacerts_dir }}/{{ cacert_file }}:
  157. file.managed:
  158. - name: {{ cacerts_dir }}/{{ cacert_file }}
  159. - contents: |
  160. {{ ca_cert|replace(' ', '')|indent(6) }}
  161. - makedirs: True
  162. - show_changes: True
  163. - follow_symlinks: True
  164. - watch_in:
  165. - cmd: salt_update_certificates
  166. {%- endfor %}
  167. {%- endfor %}
  168. {%- endfor %}
  169. {%- endif %}
  170. {%- endif %}