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.

cert.sls 6.3KB

8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. {{ 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. - force: True
  107. - watch_in:
  108. - cmd: salt_update_certificates
  109. {%- endif %}
  110. {%- endfor %}
  111. {%- do created_ca_files.append(ca_file) %}
  112. {%- endif %}
  113. {%- if cert.all_file is defined %}
  114. salt_minion_cert_{{ cert_name }}_all:
  115. cmd.wait:
  116. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  117. - watch:
  118. - x509: {{ key_file }}
  119. - x509: {{ cert_file }}
  120. - x509: {{ ca_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('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 ca_path.startswith('/etc/pki/ca/') and ca_path.endswith('ca.crt') %}
  162. {# authority name can be obtained only from a cacert path in case of mine.get #}
  163. {%- set ca_authority = ca_path.split("/")[4] %}
  164. {%- set cacert_file="%s/ca-%s.crt" % (cacerts_dir,ca_authority) %}
  165. salt_trust_ca_{{ cacert_file }}:
  166. x509.pem_managed:
  167. - name: {{ cacert_file }}
  168. - text: {{ ca_cert|replace('\n', '') }}
  169. - watch_in:
  170. - file: salt_trust_ca_{{ cacert_file }}_permissions
  171. - cmd: salt_update_certificates
  172. salt_trust_ca_{{ cacert_file }}_permissions:
  173. file.managed:
  174. - name: {{ cacert_file }}
  175. - mode: 0444
  176. {%- endif %}
  177. {%- endfor %}
  178. {%- endfor %}
  179. {%- endfor %}
  180. {%- endif %}
  181. {%- endif %}