helm.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. EnableAdmin bool
  18. TLS authconfig.TLSOptions
  19. Provisioners []provisioner.Interface
  20. }
  21. // WriteHelmTemplate a helm template to configure the
  22. // smallstep/step-certificates helm chart.
  23. func (p *PKI) WriteHelmTemplate(w io.Writer) error {
  24. tmpl, err := template.New("helm").Funcs(templates.StepFuncMap()).Parse(helmTemplate)
  25. if err != nil {
  26. return errors.Wrap(err, "error writing helm template")
  27. }
  28. // Delete ssh section if it is not enabled
  29. if !p.options.enableSSH {
  30. p.Ssh = nil
  31. }
  32. // Convert provisioners to ca.json representation
  33. provisioners := []provisioner.Interface{}
  34. for _, p := range p.Authority.Provisioners {
  35. pp, err := authority.ProvisionerToCertificates(p)
  36. if err != nil {
  37. return err
  38. }
  39. provisioners = append(provisioners, pp)
  40. }
  41. // Add default ACME provisioner if enabled. Note that this logic is similar
  42. // to what's in p.GenerateConfig(), but that codepath isn't taken when
  43. // writing the Helm template. The default JWK provisioner is added earlier in
  44. // the process and that's part of the provisioners above.
  45. // TODO(hs): consider refactoring the initialization, so that this becomes
  46. // easier to reason about and maintain.
  47. if p.options.enableACME {
  48. provisioners = append(provisioners, &provisioner.ACME{
  49. Type: "ACME",
  50. Name: "acme",
  51. })
  52. }
  53. // Add default SSHPOP provisioner if enabled. Similar to the above, this is
  54. // the same as what happens in p.GenerateConfig().
  55. if p.options.enableSSH {
  56. provisioners = append(provisioners, &provisioner.SSHPOP{
  57. Type: "SSHPOP",
  58. Name: "sshpop",
  59. Claims: &provisioner.Claims{
  60. EnableSSHCA: &p.options.enableSSH,
  61. },
  62. })
  63. }
  64. if err := tmpl.Execute(w, helmVariables{
  65. Configuration: &p.Configuration,
  66. Defaults: &p.Defaults,
  67. Password: "",
  68. EnableSSH: p.options.enableSSH,
  69. EnableAdmin: p.options.enableAdmin,
  70. TLS: authconfig.DefaultTLSOptions,
  71. Provisioners: provisioners,
  72. }); err != nil {
  73. return errors.Wrap(err, "error executing helm template")
  74. }
  75. return nil
  76. }
  77. const helmTemplate = `# Helm template
  78. inject:
  79. enabled: true
  80. # Config contains the configuration files ca.json and defaults.json
  81. config:
  82. files:
  83. ca.json:
  84. root: {{ first .Root }}
  85. federateRoots: []
  86. crt: {{ .Intermediate }}
  87. key: {{ .IntermediateKey }}
  88. {{- if .Kms }}
  89. kms:
  90. type: {{ lower (.Kms.Type | toString) }}
  91. {{- end }}
  92. {{- if .EnableSSH }}
  93. ssh:
  94. hostKey: {{ .Ssh.HostKey }}
  95. userKey: {{ .Ssh.UserKey }}
  96. {{- end }}
  97. address: {{ .Address }}
  98. dnsNames:
  99. {{- range .DnsNames }}
  100. - {{ . }}
  101. {{- end }}
  102. logger:
  103. format: json
  104. db:
  105. type: badgerv2
  106. dataSource: /home/step/db
  107. authority:
  108. enableAdmin: {{ .EnableAdmin }}
  109. provisioners:
  110. {{- range .Provisioners }}
  111. - {{ . | toJson }}
  112. {{- end }}
  113. tls:
  114. cipherSuites:
  115. {{- range .TLS.CipherSuites }}
  116. - {{ . }}
  117. {{- end }}
  118. minVersion: {{ .TLS.MinVersion }}
  119. maxVersion: {{ .TLS.MaxVersion }}
  120. renegotiation: {{ .TLS.Renegotiation }}
  121. defaults.json:
  122. ca-url: {{ .Defaults.CaUrl }}
  123. ca-config: {{ .Defaults.CaConfig }}
  124. fingerprint: {{ .Defaults.Fingerprint }}
  125. root: {{ .Defaults.Root }}
  126. # Certificates contains the root and intermediate certificate and
  127. # optionally the SSH host and user public keys
  128. certificates:
  129. # intermediate_ca contains the text of the intermediate CA Certificate
  130. intermediate_ca: |
  131. {{- index .Files .Intermediate | toString | nindent 6 }}
  132. # root_ca contains the text of the root CA Certificate
  133. root_ca: |
  134. {{- first .Root | index .Files | toString | nindent 6 }}
  135. {{- if .Ssh }}
  136. # ssh_host_ca contains the text of the public ssh key for the SSH root CA
  137. ssh_host_ca: {{ index .Files .Ssh.HostPublicKey | toString }}
  138. # ssh_user_ca contains the text of the public ssh key for the SSH root CA
  139. ssh_user_ca: {{ index .Files .Ssh.UserPublicKey | toString }}
  140. {{- end }}
  141. # Secrets contains the root and intermediate keys and optionally the SSH
  142. # private keys
  143. secrets:
  144. # ca_password contains the password used to encrypt x509.intermediate_ca_key, ssh.host_ca_key and ssh.user_ca_key
  145. # This value must be base64 encoded.
  146. ca_password: {{ .Password | b64enc }}
  147. provisioner_password: {{ .Password | b64enc}}
  148. x509:
  149. # intermediate_ca_key contains the contents of your encrypted intermediate CA key
  150. intermediate_ca_key: |
  151. {{- index .Files .IntermediateKey | toString | nindent 8 }}
  152. # root_ca_key contains the contents of your encrypted root CA key
  153. # Note that this value can be omitted without impacting the functionality of step-certificates
  154. # If supplied, this should be encrypted using a unique password that is not used for encrypting
  155. # the intermediate_ca_key, ssh.host_ca_key or ssh.user_ca_key.
  156. root_ca_key: |
  157. {{- first .RootKey | index .Files | toString | nindent 8 }}
  158. {{- if .Ssh }}
  159. ssh:
  160. # ssh_host_ca_key contains the contents of your encrypted SSH Host CA key
  161. host_ca_key: |
  162. {{- index .Files .Ssh.HostKey | toString | nindent 8 }}
  163. # ssh_user_ca_key contains the contents of your encrypted SSH User CA key
  164. user_ca_key: |
  165. {{- index .Files .Ssh.UserKey | toString | nindent 8 }}
  166. {{- end }}
  167. `