helm.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package pki
  2. import (
  3. "io"
  4. "text/template"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/authority"
  7. authconfig "github.com/smallstep/certificates/authority/config"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/certificates/templates"
  10. "go.step.sm/linkedca"
  11. )
  12. type helmVariables struct {
  13. *linkedca.Configuration
  14. Defaults *linkedca.Defaults
  15. Password string
  16. EnableSSH bool
  17. TLS authconfig.TLSOptions
  18. Provisioners []provisioner.Interface
  19. }
  20. // WriteHelmTemplate a helm template to configure the
  21. // smallstep/step-certificates helm chart.
  22. func (p *PKI) WriteHelmTemplate(w io.Writer) error {
  23. tmpl, err := template.New("helm").Funcs(templates.StepFuncMap()).Parse(helmTemplate)
  24. if err != nil {
  25. return errors.Wrap(err, "error writing helm template")
  26. }
  27. // Delete ssh section if it is not enabled
  28. if !p.options.enableSSH {
  29. p.Ssh = nil
  30. }
  31. // Convert provisioner to ca.json
  32. provisioners := make([]provisioner.Interface, len(p.Authority.Provisioners))
  33. for i, p := range p.Authority.Provisioners {
  34. pp, err := authority.ProvisionerToCertificates(p)
  35. if err != nil {
  36. return err
  37. }
  38. provisioners[i] = pp
  39. }
  40. if err := tmpl.Execute(w, helmVariables{
  41. Configuration: &p.Configuration,
  42. Defaults: &p.Defaults,
  43. Password: "",
  44. EnableSSH: p.options.enableSSH,
  45. TLS: authconfig.DefaultTLSOptions,
  46. Provisioners: provisioners,
  47. }); err != nil {
  48. return errors.Wrap(err, "error executing helm template")
  49. }
  50. return nil
  51. }
  52. const helmTemplate = `# Helm template
  53. inject:
  54. enabled: true
  55. # Config contains the configuration files ca.json and defaults.json
  56. config:
  57. files:
  58. ca.json:
  59. root: {{ first .Root }}
  60. federateRoots: []
  61. crt: {{ .Intermediate }}
  62. key: {{ .IntermediateKey }}
  63. {{- if .Kms }}
  64. kms:
  65. type: {{ lower (.Kms.Type | toString) }}
  66. {{- end }}
  67. {{- if .EnableSSH }}
  68. ssh:
  69. hostKey: {{ .Ssh.HostKey }}
  70. userKey: {{ .Ssh.UserKey }}
  71. {{- end }}
  72. address: {{ .Address }}
  73. dnsNames:
  74. {{- range .DnsNames }}
  75. - {{ . }}
  76. {{- end }}
  77. logger:
  78. format: json
  79. db:
  80. type: badgerv2
  81. dataSource: /home/step/db
  82. authority:
  83. provisioners:
  84. {{- range .Provisioners }}
  85. - {{ . | toJson }}
  86. {{- end }}
  87. tls:
  88. cipherSuites:
  89. {{- range .TLS.CipherSuites }}
  90. - {{ . }}
  91. {{- end }}
  92. minVersion: {{ .TLS.MinVersion }}
  93. maxVersion: {{ .TLS.MaxVersion }}
  94. renegotiation: {{ .TLS.Renegotiation }}
  95. defaults.json:
  96. ca-url: {{ .Defaults.CaUrl }}
  97. ca-config: {{ .Defaults.CaConfig }}
  98. fingerprint: {{ .Defaults.Fingerprint }}
  99. root: {{ .Defaults.Root }}
  100. # Certificates contains the root and intermediate certificate and
  101. # optionally the SSH host and user public keys
  102. certificates:
  103. # intermediate_ca contains the text of the intermediate CA Certificate
  104. intermediate_ca: |
  105. {{- index .Files .Intermediate | toString | nindent 6 }}
  106. # root_ca contains the text of the root CA Certificate
  107. root_ca: |
  108. {{- first .Root | index .Files | toString | nindent 6 }}
  109. {{- if .Ssh }}
  110. # ssh_host_ca contains the text of the public ssh key for the SSH root CA
  111. ssh_host_ca: {{ index .Files .Ssh.HostPublicKey | toString }}
  112. # ssh_user_ca contains the text of the public ssh key for the SSH root CA
  113. ssh_user_ca: {{ index .Files .Ssh.UserPublicKey | toString }}
  114. {{- end }}
  115. # Secrets contains the root and intermediate keys and optionally the SSH
  116. # private keys
  117. secrets:
  118. # ca_password contains the password used to encrypt x509.intermediate_ca_key, ssh.host_ca_key and ssh.user_ca_key
  119. # This value must be base64 encoded.
  120. ca_password: {{ .Password | b64enc }}
  121. provisioner_password: {{ .Password | b64enc}}
  122. x509:
  123. # intermediate_ca_key contains the contents of your encrypted intermediate CA key
  124. intermediate_ca_key: |
  125. {{- index .Files .IntermediateKey | toString | nindent 8 }}
  126. # root_ca_key contains the contents of your encrypted root CA key
  127. # Note that this value can be omitted without impacting the functionality of step-certificates
  128. # If supplied, this should be encrypted using a unique password that is not used for encrypting
  129. # the intermediate_ca_key, ssh.host_ca_key or ssh.user_ca_key.
  130. root_ca_key: |
  131. {{- first .RootKey | index .Files | toString | nindent 8 }}
  132. {{- if .Ssh }}
  133. ssh:
  134. # ssh_host_ca_key contains the contents of your encrypted SSH Host CA key
  135. host_ca_key: |
  136. {{- index .Files .Ssh.HostKey | toString | nindent 8 }}
  137. # ssh_user_ca_key contains the contents of your encrypted SSH User CA key
  138. user_ca_key: |
  139. {{- index .Files .Ssh.UserKey | toString | nindent 8 }}
  140. {{- end }}
  141. `