4
0

values.go 5.1 KB

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