helm.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 .EnableSSH }}
  64. ssh:
  65. hostKey: {{ .Ssh.HostKey }}
  66. userKey: {{ .Ssh.UserKey }}
  67. {{- end }}
  68. address: {{ .Address }}
  69. dnsNames:
  70. {{- range .DnsNames }}
  71. - {{ . }}
  72. {{- end }}
  73. logger:
  74. format: json
  75. db:
  76. type: badgerv2
  77. dataSource: /home/step/db
  78. authority:
  79. provisioners:
  80. {{- range .Provisioners }}
  81. - {{ . | toJson }}
  82. {{- end }}
  83. tls:
  84. cipherSuites:
  85. {{- range .TLS.CipherSuites }}
  86. - {{ . }}
  87. {{- end }}
  88. minVersion: {{ .TLS.MinVersion }}
  89. maxVersion: {{ .TLS.MaxVersion }}
  90. renegotiation: {{ .TLS.Renegotiation }}
  91. defaults.json:
  92. ca-url: {{ .Defaults.CaUrl }}
  93. ca-config: {{ .Defaults.CaConfig }}
  94. fingerprint: {{ .Defaults.Fingerprint }}
  95. root: {{ .Defaults.Root }}
  96. # Certificates contains the root and intermediate certificate and
  97. # optionally the SSH host and user public keys
  98. certificates:
  99. # intermediate_ca contains the text of the intermediate CA Certificate
  100. intermediate_ca: |
  101. {{- index .Files .Intermediate | toString | nindent 6 }}
  102. # root_ca contains the text of the root CA Certificate
  103. root_ca: |
  104. {{- first .Root | index .Files | toString | nindent 6 }}
  105. {{- if .Ssh }}
  106. # ssh_host_ca contains the text of the public ssh key for the SSH root CA
  107. ssh_host_ca: {{ index .Files .Ssh.HostPublicKey | toString }}
  108. # ssh_user_ca contains the text of the public ssh key for the SSH root CA
  109. ssh_user_ca: {{ index .Files .Ssh.UserPublicKey | toString }}
  110. {{- end }}
  111. # Secrets contains the root and intermediate keys and optionally the SSH
  112. # private keys
  113. secrets:
  114. # ca_password contains the password used to encrypt x509.intermediate_ca_key, ssh.host_ca_key and ssh.user_ca_key
  115. # This value must be base64 encoded.
  116. ca_password: {{ .Password | b64enc }}
  117. provisioner_password: {{ .Password | b64enc}}
  118. x509:
  119. # intermediate_ca_key contains the contents of your encrypted intermediate CA key
  120. intermediate_ca_key: |
  121. {{- index .Files .IntermediateKey | toString | nindent 8 }}
  122. # root_ca_key contains the contents of your encrypted root CA key
  123. # Note that this value can be omitted without impacting the functionality of step-certificates
  124. # If supplied, this should be encrypted using a unique password that is not used for encrypting
  125. # the intermediate_ca_key, ssh.host_ca_key or ssh.user_ca_key.
  126. root_ca_key: |
  127. {{- first .RootKey | index .Files | toString | nindent 8 }}
  128. {{- if .Ssh }}
  129. ssh:
  130. # ssh_host_ca_key contains the contents of your encrypted SSH Host CA key
  131. host_ca_key: |
  132. {{- index .Files .Ssh.HostKey | toString | nindent 8 }}
  133. # ssh_user_ca_key contains the contents of your encrypted SSH User CA key
  134. user_ca_key: |
  135. {{- index .Files .Ssh.UserKey | toString | nindent 8 }}
  136. {{- end }}
  137. `