Skip to content

Commit b583d8d

Browse files
committed
Move default templates to the template package.
1 parent 2ebfc73 commit b583d8d

File tree

2 files changed

+112
-67
lines changed

2 files changed

+112
-67
lines changed

pki/templates.go

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,13 @@ import (
1111
"github.com/smallstep/cli/utils"
1212
)
1313

14-
// SSHTemplates contains the configuration of default templates used on ssh.
15-
// Relative paths are relative to the StepPath.
16-
var SSHTemplates = &templates.SSHTemplates{
17-
User: []templates.Template{
18-
{Name: "include.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/include.tpl", Path: "~/.ssh/config", Comment: "#"},
19-
{Name: "config.tpl", Type: templates.File, TemplatePath: "templates/ssh/config.tpl", Path: "ssh/config", Comment: "#"},
20-
{Name: "known_hosts.tpl", Type: templates.File, TemplatePath: "templates/ssh/known_hosts.tpl", Path: "ssh/known_hosts", Comment: "#"},
21-
},
22-
Host: []templates.Template{
23-
{Name: "sshd_config.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/sshd_config.tpl", Path: "/etc/ssh/sshd_config", Comment: "#"},
24-
{Name: "ca.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
25-
},
26-
}
27-
28-
// SSHTemplateData contains the data of the default templates used on ssh.
29-
var SSHTemplateData = map[string]string{
30-
// include.tpl adds the step ssh config file.
31-
//
32-
// Note: on windows `Include C:\...` is treated as a relative path.
33-
"include.tpl": `Host *
34-
{{- if or .User.GOOS "none" | eq "windows" }}
35-
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
36-
{{- else }}
37-
Include "{{.User.StepPath}}/ssh/config"
38-
{{- end }}`,
39-
40-
// config.tpl is the step ssh config file, it includes the Match rule and
41-
// references the step known_hosts file.
42-
//
43-
// Note: on windows ProxyCommand requires the full path
44-
"config.tpl": `Match exec "step ssh check-host %h"
45-
{{- if .User.User }}
46-
User {{.User.User}}
47-
{{- end }}
48-
{{- if or .User.GOOS "none" | eq "windows" }}
49-
UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts"
50-
ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand %r %h %p
51-
{{- else }}
52-
UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts"
53-
ProxyCommand step ssh proxycommand %r %h %p
54-
{{- end }}
55-
`,
56-
57-
// known_hosts.tpl authorizes the ssh hosts key
58-
"known_hosts.tpl": `@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}
59-
{{- range .Step.SSH.HostFederatedKeys}}
60-
@cert-authority * {{.Type}} {{.Marshal | toString | b64enc}}
61-
{{- end }}
62-
`,
63-
64-
// sshd_config.tpl adds the configuration to support certificates
65-
"sshd_config.tpl": `TrustedUserCAKeys /etc/ssh/ca.pub
66-
HostCertificate /etc/ssh/{{.User.Certificate}}
67-
HostKey /etc/ssh/{{.User.Key}}`,
68-
69-
// ca.tpl contains the public key used to authorized clients
70-
"ca.tpl": `{{.Step.SSH.UserKey.Type}} {{.Step.SSH.UserKey.Marshal | toString | b64enc}}
71-
{{- range .Step.SSH.UserFederatedKeys}}
72-
{{.Type}} {{.Marshal | toString | b64enc}}
73-
{{- end }}
74-
`,
75-
}
76-
7714
// getTemplates returns all the templates enabled
7815
func (p *PKI) getTemplates() *templates.Templates {
7916
if !p.enableSSH {
8017
return nil
8118
}
82-
8319
return &templates.Templates{
84-
SSH: SSHTemplates,
20+
SSH: &templates.DefaultSSHTemplates,
8521
Data: map[string]interface{}{},
8622
}
8723
}
@@ -104,7 +40,7 @@ func generateTemplates(t *templates.Templates) error {
10440
}
10541
// Create all templates
10642
for _, t := range t.SSH.User {
107-
data, ok := SSHTemplateData[t.Name]
43+
data, ok := templates.DefaultSSHTemplateData[t.Name]
10844
if !ok {
10945
return errors.Errorf("template %s does not exists", t.Name)
11046
}
@@ -113,7 +49,7 @@ func generateTemplates(t *templates.Templates) error {
11349
}
11450
}
11551
for _, t := range t.SSH.Host {
116-
data, ok := SSHTemplateData[t.Name]
52+
data, ok := templates.DefaultSSHTemplateData[t.Name]
11753
if !ok {
11854
return errors.Errorf("template %s does not exists", t.Name)
11955
}

templates/values.go

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

0 commit comments

Comments
 (0)