templates.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. //
  28. // Note: on windows `Include C:\...` is treated as a relative path.
  29. "include.tpl": `Host *
  30. {{- if or .User.GOOS "none" | eq "windows" }}
  31. Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
  32. {{- else }}
  33. Include "{{.User.StepPath}}/ssh/config"
  34. {{- end }}`,
  35. // config.tpl is the step ssh config file, it includes the Match rule and
  36. // references the step known_hosts file.
  37. //
  38. // Note: on windows ProxyCommand requires the full path
  39. "config.tpl": `Match exec "step ssh check-host %h"
  40. ForwardAgent yes
  41. {{- if .User.User }}
  42. User {{.User.User}}
  43. {{- end }}
  44. {{- if or .User.GOOS "none" | eq "windows" }}
  45. UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts"
  46. ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand %r %h %p
  47. {{- else }}
  48. UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts"
  49. ProxyCommand step ssh proxycommand %r %h %p
  50. {{- end }}
  51. `,
  52. // known_hosts.tpl authorizes the ssh hosts key
  53. "known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
  54. {{- range .Step.SSH.HostFederatedKeys}}
  55. @cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
  56. {{- end }}
  57. `,
  58. // sshd_config.tpl adds the configuration to support certificates
  59. "sshd_config.tpl": `TrustedUserCAKeys /etc/ssh/ca.pub
  60. HostCertificate /etc/ssh/{{.User.Certificate}}
  61. HostKey /etc/ssh/{{.User.Key}}`,
  62. // ca.tpl contains the public key used to authorized clients
  63. "ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
  64. {{- range .Step.SSH.UserFederatedKeys}}
  65. {{.Type}} {{.Marshal | toString | b64enc}}
  66. {{- end }}
  67. `,
  68. }
  69. // getTemplates returns all the templates enabled
  70. func (p *PKI) getTemplates() *templates.Templates {
  71. if !p.enableSSH {
  72. return nil
  73. }
  74. return &templates.Templates{
  75. SSH: SSHTemplates,
  76. Data: map[string]interface{}{},
  77. }
  78. }
  79. // generateTemplates generates given templates.
  80. func generateTemplates(t *templates.Templates) error {
  81. if t == nil {
  82. return nil
  83. }
  84. base := GetTemplatesPath()
  85. // Generate SSH templates
  86. if t.SSH != nil {
  87. // all ssh templates are under ssh:
  88. sshDir := filepath.Join(base, "ssh")
  89. if _, err := os.Stat(sshDir); os.IsNotExist(err) {
  90. if err = os.MkdirAll(sshDir, 0700); err != nil {
  91. return errs.FileError(err, sshDir)
  92. }
  93. }
  94. // Create all templates
  95. for _, t := range t.SSH.User {
  96. data, ok := SSHTemplateData[t.Name]
  97. if !ok {
  98. return errors.Errorf("template %s does not exists", t.Name)
  99. }
  100. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  101. return err
  102. }
  103. }
  104. for _, t := range t.SSH.Host {
  105. data, ok := SSHTemplateData[t.Name]
  106. if !ok {
  107. return errors.Errorf("template %s does not exists", t.Name)
  108. }
  109. if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
  110. return err
  111. }
  112. }
  113. }
  114. return nil
  115. }