2019-10-04 02:03:38 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2021-11-13 22:22:23 +00:00
|
|
|
// SSHTemplateVersionKey is a key that can be submitted by a client to select
|
|
|
|
// the template version that will be returned by the server.
|
|
|
|
var SSHTemplateVersionKey = "StepSSHTemplateVersion"
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// Step represents the default variables available in the CA.
|
|
|
|
type Step struct {
|
|
|
|
SSH StepSSH
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:42:56 +00:00
|
|
|
// StepSSH holds SSH-related values for the CA.
|
2019-10-04 02:03:38 +00:00
|
|
|
type StepSSH struct {
|
2019-10-12 02:26:59 +00:00
|
|
|
HostKey ssh.PublicKey
|
|
|
|
UserKey ssh.PublicKey
|
|
|
|
HostFederatedKeys []ssh.PublicKey
|
|
|
|
UserFederatedKeys []ssh.PublicKey
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
2020-06-17 00:24:47 +00:00
|
|
|
|
|
|
|
// DefaultSSHTemplates contains the configuration of default templates used on ssh.
|
|
|
|
// Relative paths are relative to the StepPath.
|
|
|
|
var DefaultSSHTemplates = SSHTemplates{
|
|
|
|
User: []Template{
|
2021-11-10 08:42:57 +00:00
|
|
|
{
|
2021-11-12 06:05:47 +00:00
|
|
|
Name: "config.tpl",
|
2021-11-10 08:42:57 +00:00
|
|
|
Type: Snippet,
|
2021-11-12 06:05:47 +00:00
|
|
|
TemplatePath: "templates/ssh/config.tpl",
|
2021-11-10 08:42:57 +00:00
|
|
|
Path: "~/.ssh/config",
|
|
|
|
Comment: "#",
|
|
|
|
},
|
2020-06-17 00:24:47 +00:00
|
|
|
{
|
2021-11-12 06:05:47 +00:00
|
|
|
Name: "step_includes.tpl",
|
|
|
|
Type: PrependLine,
|
|
|
|
TemplatePath: "templates/ssh/step_includes.tpl",
|
2021-11-10 22:53:44 +00:00
|
|
|
Path: "${STEPPATH}/ssh/includes",
|
2020-06-17 00:24:47 +00:00
|
|
|
Comment: "#",
|
|
|
|
},
|
|
|
|
{
|
2021-11-12 06:05:47 +00:00
|
|
|
Name: "step_config.tpl",
|
2020-06-17 00:24:47 +00:00
|
|
|
Type: File,
|
2021-11-12 06:05:47 +00:00
|
|
|
TemplatePath: "templates/ssh/step_config.tpl",
|
2020-06-17 00:24:47 +00:00
|
|
|
Path: "ssh/config",
|
|
|
|
Comment: "#",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "known_hosts.tpl",
|
|
|
|
Type: File,
|
|
|
|
TemplatePath: "templates/ssh/known_hosts.tpl",
|
|
|
|
Path: "ssh/known_hosts",
|
|
|
|
Comment: "#",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Host: []Template{
|
|
|
|
{
|
|
|
|
Name: "sshd_config.tpl",
|
|
|
|
Type: Snippet,
|
|
|
|
TemplatePath: "templates/ssh/sshd_config.tpl",
|
|
|
|
Path: "/etc/ssh/sshd_config",
|
|
|
|
Comment: "#",
|
|
|
|
RequiredData: []string{"Certificate", "Key"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ca.tpl",
|
|
|
|
Type: Snippet,
|
|
|
|
TemplatePath: "templates/ssh/ca.tpl",
|
|
|
|
Path: "/etc/ssh/ca.pub",
|
|
|
|
Comment: "#",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultSSHTemplateData contains the data of the default templates used on ssh.
|
|
|
|
var DefaultSSHTemplateData = map[string]string{
|
2021-11-10 08:42:57 +00:00
|
|
|
// base_config.tpl adds the step ssh config file.
|
2020-06-17 00:24:47 +00:00
|
|
|
//
|
|
|
|
// Note: on windows `Include C:\...` is treated as a relative path.
|
2021-11-12 06:28:25 +00:00
|
|
|
"config.tpl": `Host *
|
2020-06-17 00:24:47 +00:00
|
|
|
{{- if or .User.GOOS "none" | eq "windows" }}
|
2021-11-12 06:05:47 +00:00
|
|
|
{{- if .User.StepBasePath }}
|
2021-11-10 22:53:44 +00:00
|
|
|
Include "{{ .User.StepBasePath | replace "\\" "/" | trimPrefix "C:" }}/ssh/includes"
|
2021-11-10 08:42:57 +00:00
|
|
|
{{- else }}
|
2021-11-12 06:05:47 +00:00
|
|
|
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/includes"
|
2021-11-10 08:42:57 +00:00
|
|
|
{{- end }}
|
|
|
|
{{- else }}
|
2021-11-12 06:05:47 +00:00
|
|
|
{{- if .User.StepBasePath }}
|
2021-11-10 22:53:44 +00:00
|
|
|
Include "{{.User.StepBasePath}}/ssh/includes"
|
2020-06-17 00:24:47 +00:00
|
|
|
{{- else }}
|
2021-11-12 06:05:47 +00:00
|
|
|
Include "{{.User.StepPath}}/ssh/includes"
|
2021-11-10 08:42:57 +00:00
|
|
|
{{- end }}
|
|
|
|
{{- end }}`,
|
|
|
|
|
2021-11-10 22:53:44 +00:00
|
|
|
// includes.tpl adds the step ssh config file.
|
2021-11-10 08:42:57 +00:00
|
|
|
//
|
|
|
|
// Note: on windows `Include C:\...` is treated as a relative path.
|
2021-11-12 06:28:25 +00:00
|
|
|
"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 }}`,
|
2020-06-17 00:24:47 +00:00
|
|
|
|
|
|
|
// config.tpl is the step ssh config file, it includes the Match rule and
|
|
|
|
// references the step known_hosts file.
|
|
|
|
//
|
|
|
|
// Note: on windows ProxyCommand requires the full path
|
2021-11-12 06:51:46 +00:00
|
|
|
"step_config.tpl": `Match exec "step ssh check-host{{- if .User.Context }} --context {{ .User.Context }}{{- end }} %h"
|
2020-06-17 00:24:47 +00:00
|
|
|
{{- if .User.User }}
|
|
|
|
User {{.User.User}}
|
|
|
|
{{- end }}
|
|
|
|
{{- if or .User.GOOS "none" | eq "windows" }}
|
|
|
|
UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts"
|
2021-11-10 08:42:57 +00:00
|
|
|
ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand{{- if .User.Context }} --context {{ .User.Context }}{{- end }} %r %h %p
|
2020-06-17 00:24:47 +00:00
|
|
|
{{- else }}
|
|
|
|
UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts"
|
2021-11-10 08:42:57 +00:00
|
|
|
ProxyCommand step ssh proxycommand{{- if .User.Context }} --context {{ .User.Context }}{{- end }} %r %h %p
|
2020-06-17 00:24:47 +00:00
|
|
|
{{- end }}
|
|
|
|
`,
|
|
|
|
|
|
|
|
// known_hosts.tpl authorizes the ssh hosts key
|
|
|
|
"known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
|
|
|
|
{{- range .Step.SSH.HostFederatedKeys}}
|
|
|
|
@cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
|
|
|
|
{{- end }}
|
|
|
|
`,
|
|
|
|
|
|
|
|
// sshd_config.tpl adds the configuration to support certificates
|
2021-04-13 01:37:10 +00:00
|
|
|
"sshd_config.tpl": `Match all
|
|
|
|
TrustedUserCAKeys /etc/ssh/ca.pub
|
|
|
|
HostCertificate /etc/ssh/{{.User.Certificate}}
|
|
|
|
HostKey /etc/ssh/{{.User.Key}}`,
|
2020-06-17 00:24:47 +00:00
|
|
|
|
|
|
|
// ca.tpl contains the public key used to authorized clients
|
|
|
|
"ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
|
|
|
|
{{- range .Step.SSH.UserFederatedKeys}}
|
|
|
|
{{.Type}} {{.Marshal | toString | b64enc}}
|
|
|
|
{{- end }}
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultTemplates returns the default templates.
|
|
|
|
func DefaultTemplates() *Templates {
|
|
|
|
sshTemplates := DefaultSSHTemplates
|
|
|
|
for i, t := range sshTemplates.User {
|
2020-06-17 00:56:22 +00:00
|
|
|
sshTemplates.User[i].TemplatePath = ""
|
2020-06-17 00:24:47 +00:00
|
|
|
sshTemplates.User[i].Content = []byte(DefaultSSHTemplateData[t.Name])
|
|
|
|
}
|
|
|
|
for i, t := range sshTemplates.Host {
|
2020-06-17 00:56:22 +00:00
|
|
|
sshTemplates.Host[i].TemplatePath = ""
|
2020-06-17 00:24:47 +00:00
|
|
|
sshTemplates.Host[i].Content = []byte(DefaultSSHTemplateData[t.Name])
|
|
|
|
}
|
|
|
|
return &Templates{
|
|
|
|
SSH: &sshTemplates,
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
}
|