values.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package templates
  2. import (
  3. "golang.org/x/crypto/ssh"
  4. )
  5. // Step represents the default variables available in the CA.
  6. type Step struct {
  7. SSH StepSSH
  8. }
  9. // StepSSH holds SSH-related values for the CA.
  10. type StepSSH struct {
  11. HostKey ssh.PublicKey
  12. UserKey ssh.PublicKey
  13. HostFederatedKeys []ssh.PublicKey
  14. UserFederatedKeys []ssh.PublicKey
  15. }
  16. // DefaultSSHTemplates contains the configuration of default templates used on ssh.
  17. // Relative paths are relative to the StepPath.
  18. var DefaultSSHTemplates = SSHTemplates{
  19. User: []Template{
  20. {
  21. Name: "include.tpl",
  22. Type: Snippet,
  23. TemplatePath: "templates/ssh/include.tpl",
  24. Path: "~/.ssh/config",
  25. Comment: "#",
  26. },
  27. {
  28. Name: "config.tpl",
  29. Type: File,
  30. TemplatePath: "templates/ssh/config.tpl",
  31. Path: "ssh/config",
  32. Comment: "#",
  33. },
  34. {
  35. Name: "known_hosts.tpl",
  36. Type: File,
  37. TemplatePath: "templates/ssh/known_hosts.tpl",
  38. Path: "ssh/known_hosts",
  39. Comment: "#",
  40. },
  41. },
  42. Host: []Template{
  43. {
  44. Name: "sshd_config.tpl",
  45. Type: Snippet,
  46. TemplatePath: "templates/ssh/sshd_config.tpl",
  47. Path: "/etc/ssh/sshd_config",
  48. Comment: "#",
  49. RequiredData: []string{"Certificate", "Key"},
  50. },
  51. {
  52. Name: "ca.tpl",
  53. Type: Snippet,
  54. TemplatePath: "templates/ssh/ca.tpl",
  55. Path: "/etc/ssh/ca.pub",
  56. Comment: "#",
  57. },
  58. },
  59. }
  60. // DefaultSSHTemplateData contains the data of the default templates used on ssh.
  61. var DefaultSSHTemplateData = map[string]string{
  62. // include.tpl adds the step ssh config file.
  63. //
  64. // Note: on windows `Include C:\...` is treated as a relative path.
  65. "include.tpl": `Host *
  66. {{- if or .User.GOOS "none" | eq "windows" }}
  67. Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
  68. {{- else }}
  69. Include "{{.User.StepPath}}/ssh/config"
  70. {{- end }}`,
  71. // config.tpl is the step ssh config file, it includes the Match rule and
  72. // references the step known_hosts file.
  73. //
  74. // Note: on windows ProxyCommand requires the full path
  75. "config.tpl": `Match exec "step ssh check-host %h"
  76. {{- if .User.User }}
  77. User {{.User.User}}
  78. {{- end }}
  79. {{- if or .User.GOOS "none" | eq "windows" }}
  80. UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts"
  81. ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand %r %h %p
  82. {{- else }}
  83. UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts"
  84. ProxyCommand step ssh proxycommand %r %h %p
  85. {{- end }}
  86. `,
  87. // known_hosts.tpl authorizes the ssh hosts key
  88. "known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
  89. {{- range .Step.SSH.HostFederatedKeys}}
  90. @cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
  91. {{- end }}
  92. `,
  93. // sshd_config.tpl adds the configuration to support certificates
  94. "sshd_config.tpl": `TrustedUserCAKeys /etc/ssh/ca.pub
  95. HostCertificate /etc/ssh/{{.User.Certificate}}
  96. HostKey /etc/ssh/{{.User.Key}}`,
  97. // ca.tpl contains the public key used to authorized clients
  98. "ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
  99. {{- range .Step.SSH.UserFederatedKeys}}
  100. {{.Type}} {{.Marshal | toString | b64enc}}
  101. {{- end }}
  102. `,
  103. }
  104. // DefaultTemplates returns the default templates.
  105. func DefaultTemplates() *Templates {
  106. sshTemplates := DefaultSSHTemplates
  107. for i, t := range sshTemplates.User {
  108. sshTemplates.User[i].TemplatePath = ""
  109. sshTemplates.User[i].Content = []byte(DefaultSSHTemplateData[t.Name])
  110. }
  111. for i, t := range sshTemplates.Host {
  112. sshTemplates.Host[i].TemplatePath = ""
  113. sshTemplates.Host[i].Content = []byte(DefaultSSHTemplateData[t.Name])
  114. }
  115. return &Templates{
  116. SSH: &sshTemplates,
  117. Data: map[string]interface{}{},
  118. }
  119. }