helm.go 4.7 KB

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