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.

130 lines
3.8KB

  1. {%- from "salt/map.jinja" import minion with context %}
  2. {%- if minion.enabled %}
  3. {%- for cert_name,cert in minion.get('cert', {}).iteritems() %}
  4. {%- set rowloop = loop %}
  5. {%- set key_file = cert.get('key_file', '/etc/ssl/private/' + cert.common_name + '.key') %}
  6. {%- set cert_file = cert.get('cert_file', '/etc/ssl/certs/' + cert.common_name + '.crt') %}
  7. {%- set ca_file = cert.get('ca_file', '/etc/ssl/certs/ca-' + cert.authority + '.crt') %}
  8. {%- set key_dir = key_file|replace(key_file.split('/')[-1], "") %}
  9. {%- set cert_dir = cert_file|replace(cert_file.split('/')[-1], "") %}
  10. {%- set ca_dir = ca_file|replace(ca_file.split('/')[-1], "") %}
  11. {# Only ensure directories exists, don't touch permissions, etc. #}
  12. salt_minion_cert_{{ cert_name }}_dirs:
  13. file.directory:
  14. - names:
  15. - {{ key_dir }}
  16. - {{ cert_dir }}
  17. - {{ ca_dir }}
  18. - makedirs: true
  19. - replace: false
  20. {{ key_file }}:
  21. x509.private_key_managed:
  22. - bits: {{ cert.get('bits', 4096) }}
  23. require:
  24. - file: salt_minion_cert_{{ cert_name }}_dirs
  25. {{ key_file }}_key_permissions:
  26. file.managed:
  27. - name: {{ key_file }}
  28. - mode: {{ cert.get("mode", 0600) }}
  29. {%- if salt['user.info'](cert.get("user", "root")) %}
  30. - user: {{ cert.get("user", "root") }}
  31. {%- endif %}
  32. {%- if salt['group.info'](cert.get("group", "root")) %}
  33. - group: {{ cert.get("group", "root") }}
  34. {%- endif %}
  35. - replace: false
  36. - watch:
  37. - x509: {{ key_file }}
  38. {{ cert_file }}:
  39. x509.certificate_managed:
  40. - ca_server: {{ cert.host }}
  41. - signing_policy: {{ cert.authority }}_{{ cert.signing_policy }}
  42. - public_key: {{ key_file }}
  43. - CN: "{{ cert.common_name }}"
  44. {%- if cert.alternative_names is defined %}
  45. - subjectAltName: "{{ cert.alternative_names }}"
  46. {%- endif %}
  47. {%- if cert.extended_key_usage is defined %}
  48. - extendedKeyUsage: "{{ cert.extended_key_usage }}"
  49. {%- endif %}
  50. {%- if cert.key_usage is defined %}
  51. - keyUsage: "{{ cert.key_usage }}"
  52. {%- endif %}
  53. - days_remaining: 30
  54. - backup: True
  55. - watch:
  56. - x509: {{ key_file }}
  57. {{ cert_file }}_cert_permissions:
  58. file.managed:
  59. - name: {{ cert_file }}
  60. - mode: {{ cert.get("mode", 0600) }}
  61. {%- if salt['user.info'](cert.get("user", "root")) %}
  62. - user: {{ cert.get("user", "root") }}
  63. {%- endif %}
  64. {%- if salt['group.info'](cert.get("group", "root")) %}
  65. - group: {{ cert.get("group", "root") }}
  66. {%- endif %}
  67. - replace: false
  68. - watch:
  69. - x509: {{ cert_file }}
  70. {%- for ca_path,ca_cert in salt['mine.get'](cert.host, 'x509.get_pem_entries').get(cert.host, {}).iteritems() %}
  71. {%- if '/etc/pki/ca/'+cert.authority in ca_path %}
  72. {{ ca_file }}_{{ rowloop.index }}:
  73. x509.pem_managed:
  74. - name: {{ ca_file }}
  75. - text: {{ ca_cert|replace('\n', '') }}
  76. - watch:
  77. - x509: {{ cert_file }}
  78. {%- if cert.all_file is defined %}
  79. - watch_in:
  80. - cmd: salt_minion_cert_{{ cert_name }}_all
  81. {%- endif %}
  82. {{ ca_file }}_cert_permissions_{{ rowloop.index }}:
  83. file.managed:
  84. - name: {{ ca_file }}
  85. - mode: 0644
  86. - watch:
  87. - x509: {{ ca_file }}
  88. {%- endif %}
  89. {%- endfor %}
  90. {%- if cert.all_file is defined %}
  91. salt_minion_cert_{{ cert_name }}_all:
  92. cmd.wait:
  93. - name: cat {{ key_file }} {{ cert_file }} {{ ca_file }} > {{ cert.all_file }}
  94. - watch:
  95. - x509: {{ key_file }}
  96. - x509: {{ cert_file }}
  97. {{ cert.all_file }}_cert_permissions:
  98. file.managed:
  99. - name: {{ cert.all_file }}
  100. - mode: {{ cert.get("mode", 0600) }}
  101. {%- if salt['user.info'](cert.get("user", "root")) %}
  102. - user: {{ cert.get("user", "root") }}
  103. {%- endif %}
  104. {%- if salt['group.info'](cert.get("group", "root")) %}
  105. - group: {{ cert.get("group", "root") }}
  106. {%- endif %}
  107. - replace: false
  108. - watch:
  109. - cmd: salt_minion_cert_{{ cert_name }}_all
  110. {%- endif %}
  111. {%- endfor %}
  112. {%- endif %}