templates.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package pki
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/templates"
  7. "github.com/smallstep/cli/config"
  8. "github.com/smallstep/cli/errs"
  9. "github.com/smallstep/cli/utils"
  10. )
  11. // SSHTemplates contains the configuration of default templates used on ssh.
  12. // Relative paths are relative to the StepPath.
  13. var SSHTemplates = &templates.SSHTemplates{
  14. User: []templates.Template{
  15. {Name: "include.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/include.tpl", Path: "~/.ssh/config", Comment: "#"},
  16. {Name: "config.tpl", Type: templates.File, TemplatePath: "templates/ssh/config.tpl", Path: "ssh/config", Comment: "#"},
  17. {Name: "known_hosts.tpl", Type: templates.File, TemplatePath: "templates/ssh/known_hosts.tpl", Path: "ssh/known_hosts", Comment: "#"},
  18. },
  19. Host: []templates.Template{
  20. {Name: "sshd_config.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/sshd_config.tpl", Path: "/etc/ssh/sshd_config", Comment: "#"},
  21. {Name: "ca.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
  22. },
  23. }
  24. // SSHTemplateData contains the data of the default templates used on ssh.
  25. var SSHTemplateData = map[string]string{
  26. // include.tpl adds the step ssh config file
  27. "include.tpl": `Host *
  28. Include {{.User.StepPath}}/ssh/config`,
  29. // config.tpl is the step ssh config file, it includes the Match rule
  30. // and references the step known_hosts file
  31. "config.tpl": `Match exec "step ssh check-host %h"
  32. ForwardAgent yes
  33. {{- if .User.User }}
  34. User {{.User.User}}
  35. {{- end }}
  36. UserKnownHostsFile {{.User.StepPath}}/ssh/known_hosts
  37. ProxyCommand step ssh proxycommand %r %h %p`,
  38. // known_hosts.tpl authorizes the ssh hosts key
  39. "known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
  40. {{- range .Step.SSH.HostFederatedKeys}}
  41. @cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
  42. {{- end}}
  43. `,
  44. // sshd_config.tpl adds the configuration to support certificates
  45. "sshd_config.tpl": `TrustedUserCAKeys /etc/ssh/ca.pub
  46. HostCertificate /etc/ssh/{{.User.Certificate}}
  47. HostKey /etc/ssh/{{.User.Key}}`,
  48. // ca.tpl contains the public key used to authorized clients
  49. "ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
  50. {{- range .Step.SSH.UserFederatedKeys}}
  51. {{.Type}} {{.Marshal | toString | b64enc}}
  52. {{- end}}
  53. `,
  54. }
  55. // getTemplates returns all the templates enabled
  56. func (p *PKI) getTemplates() *templates.Templates {
  57. if !p.enableSSH {
  58. return nil
  59. }
  60. return &templates.Templates{
  61. SSH: SSHTemplates,
  62. Data: map[string]interface{}{},
  63. }
  64. }
  65. // generateTemplates generates given templates.
  66. func generateTemplates(t *templates.Templates) error {
  67. if t == nil {
  68. return nil
  69. }
  70. base := GetTemplatesPath()
  71. // Generate SSH templates
  72. if t.SSH != nil {
  73. // all ssh templates are under ssh:
  74. sshDir := filepath.Join(base, "ssh")
  75. if _, err := os.Stat(sshDir); os.IsNotExist(err) {
  76. if err = os.MkdirAll(sshDir, 0700); err != nil {
  77. return errs.FileError(err, sshDir)
  78. }
  79. }
  80. // Create all templates
  81. for _, t := range t.SSH.User {
  82. data, ok := SSHTemplateData[t.Name]
  83. if !ok {
  84. return errors.Errorf("template %s does not exists", t.Name)
  85. }
  86. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  87. return err
  88. }
  89. }
  90. for _, t := range t.SSH.Host {
  91. data, ok := SSHTemplateData[t.Name]
  92. if !ok {
  93. return errors.Errorf("template %s does not exists", t.Name)
  94. }
  95. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  96. return err
  97. }
  98. }
  99. }
  100. return nil
  101. }