helm.go 6.6 KB

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