templates.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. UserKnownHostsFile {{.User.StepPath}}/ssh/known_hosts
  34. ProxyCommand step ssh proxycommand %r %h %p`,
  35. // known_hosts.tpl authorizes the ssh hosts key
  36. "known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
  37. {{- range .Step.SSH.HostFederatedKeys}}
  38. @cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
  39. {{- end}}
  40. `,
  41. // sshd_config.tpl adds the configuration to support certificates
  42. "sshd_config.tpl": `TrustedUserCAKeys /etc/ssh/ca.pub
  43. HostCertificate /etc/ssh/{{.User.Certificate}}
  44. HostKey /etc/ssh/{{.User.Key}}`,
  45. // ca.tpl contains the public key used to authorized clients
  46. "ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
  47. {{- range .Step.SSH.UserFederatedKeys}}
  48. {{.Type}} {{.Marshal | toString | b64enc}}
  49. {{- end}}
  50. `,
  51. }
  52. // getTemplates returns all the templates enabled
  53. func (p *PKI) getTemplates() *templates.Templates {
  54. if !p.enableSSH {
  55. return nil
  56. }
  57. return &templates.Templates{
  58. SSH: SSHTemplates,
  59. Data: map[string]interface{}{},
  60. }
  61. }
  62. // generateTemplates generates given templates.
  63. func generateTemplates(t *templates.Templates) error {
  64. if t == nil {
  65. return nil
  66. }
  67. base := GetTemplatesPath()
  68. // Generate SSH templates
  69. if t.SSH != nil {
  70. // all ssh templates are under ssh:
  71. sshDir := filepath.Join(base, "ssh")
  72. if _, err := os.Stat(sshDir); os.IsNotExist(err) {
  73. if err = os.MkdirAll(sshDir, 0700); err != nil {
  74. return errs.FileError(err, sshDir)
  75. }
  76. }
  77. // Create all templates
  78. for _, t := range t.SSH.User {
  79. data, ok := SSHTemplateData[t.Name]
  80. if !ok {
  81. return errors.Errorf("template %s does not exists", t.Name)
  82. }
  83. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  84. return err
  85. }
  86. }
  87. for _, t := range t.SSH.Host {
  88. data, ok := SSHTemplateData[t.Name]
  89. if !ok {
  90. return errors.Errorf("template %s does not exists", t.Name)
  91. }
  92. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  93. return err
  94. }
  95. }
  96. }
  97. return nil
  98. }