certificates/pki/templates.go

64 lines
1.5 KiB
Go
Raw Permalink Normal View History

2019-10-11 19:49:09 +00:00
package pki
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/smallstep/certificates/templates"
"go.step.sm/cli-utils/errs"
"go.step.sm/cli-utils/fileutil"
2021-08-10 07:38:30 +00:00
"go.step.sm/cli-utils/step"
2019-10-11 19:49:09 +00:00
)
// getTemplates returns all the templates enabled
func (p *PKI) getTemplates() *templates.Templates {
2021-08-02 23:07:30 +00:00
if !p.options.enableSSH {
2019-10-11 19:49:09 +00:00
return nil
}
return &templates.Templates{
SSH: &templates.DefaultSSHTemplates,
2019-10-11 19:49:09 +00:00
Data: map[string]interface{}{},
}
}
// generateTemplates generates given templates.
func generateTemplates(t *templates.Templates) error {
if t == nil {
return nil
}
base := GetTemplatesPath()
// Generate SSH templates
if t.SSH != nil {
// all ssh templates are under ssh:
sshDir := filepath.Join(base, "ssh")
if _, err := os.Stat(sshDir); os.IsNotExist(err) {
if err = os.MkdirAll(sshDir, 0700); err != nil {
return errs.FileError(err, sshDir)
}
}
// Create all templates
for _, t := range t.SSH.User {
data, ok := templates.DefaultSSHTemplateData[t.Name]
2019-10-11 19:49:09 +00:00
if !ok {
return errors.Errorf("template %s does not exists", t.Name)
}
2021-08-10 07:38:30 +00:00
if err := fileutil.WriteFile(step.Abs(t.TemplatePath), []byte(data), 0644); err != nil {
2019-10-11 19:49:09 +00:00
return err
}
}
for _, t := range t.SSH.Host {
data, ok := templates.DefaultSSHTemplateData[t.Name]
2019-10-11 19:49:09 +00:00
if !ok {
return errors.Errorf("template %s does not exists", t.Name)
}
2021-08-10 07:38:30 +00:00
if err := fileutil.WriteFile(step.Abs(t.TemplatePath), []byte(data), 0644); err != nil {
2019-10-11 19:49:09 +00:00
return err
}
}
}
return nil
}