2019-10-04 02:03:38 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2019-10-12 01:59:50 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-10-08 18:59:57 +00:00
|
|
|
"strings"
|
2019-10-04 02:03:38 +00:00
|
|
|
"text/template"
|
|
|
|
|
2019-10-23 01:41:54 +00:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2019-10-04 02:03:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-10-29 20:10:03 +00:00
|
|
|
"go.step.sm/cli-utils/fileutil"
|
2021-08-05 21:00:58 +00:00
|
|
|
"go.step.sm/cli-utils/step"
|
2019-10-04 02:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TemplateType defines how a template will be written in disk.
|
|
|
|
type TemplateType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Snippet will mark a template as a part of a file.
|
|
|
|
Snippet TemplateType = "snippet"
|
2021-11-09 23:26:34 +00:00
|
|
|
// FullSnippet will mark a template that includes header and footer as a part of a file.
|
|
|
|
FullSnippet TemplateType = "fullSnippet"
|
2019-10-04 02:03:38 +00:00
|
|
|
// File will mark a templates as a full file.
|
|
|
|
File TemplateType = "file"
|
2019-10-12 01:59:50 +00:00
|
|
|
// Directory will mark a template as a directory.
|
|
|
|
Directory TemplateType = "directory"
|
2019-10-04 02:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Templates is a collection of templates and variables.
|
|
|
|
type Templates struct {
|
2019-10-05 00:08:42 +00:00
|
|
|
SSH *SSHTemplates `json:"ssh,omitempty"`
|
|
|
|
Data map[string]interface{} `json:"data,omitempty"`
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if a template is not valid.
|
|
|
|
func (t *Templates) Validate() (err error) {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate members
|
|
|
|
if err = t.SSH.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-05 00:08:42 +00:00
|
|
|
// Do not allow "Step" and "User"
|
|
|
|
if t.Data != nil {
|
|
|
|
if _, ok := t.Data["Step"]; ok {
|
|
|
|
return errors.New("templates variables cannot contain 'Step' as a property")
|
|
|
|
}
|
|
|
|
if _, ok := t.Data["User"]; ok {
|
|
|
|
return errors.New("templates variables cannot contain 'User' as a property")
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAll preloads all templates in memory. It returns an error if an error is
|
|
|
|
// found parsing at least one template.
|
|
|
|
func LoadAll(t *Templates) (err error) {
|
2019-10-16 01:00:46 +00:00
|
|
|
if t != nil {
|
|
|
|
if t.SSH != nil {
|
|
|
|
for _, tt := range t.SSH.User {
|
|
|
|
if err = tt.Load(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
2019-10-16 01:00:46 +00:00
|
|
|
for _, tt := range t.SSH.Host {
|
|
|
|
if err = tt.Load(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 01:00:46 +00:00
|
|
|
return
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSHTemplates contains the templates defining ssh configuration files.
|
|
|
|
type SSHTemplates struct {
|
|
|
|
User []Template `json:"user"`
|
|
|
|
Host []Template `json:"host"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if a template is not valid.
|
|
|
|
func (t *SSHTemplates) Validate() (err error) {
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, tt := range t.User {
|
|
|
|
if err = tt.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tt := range t.Host {
|
|
|
|
if err = tt.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-09 23:26:34 +00:00
|
|
|
// Template represents a template file.
|
2019-10-04 02:03:38 +00:00
|
|
|
type Template struct {
|
|
|
|
*template.Template
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type TemplateType `json:"type"`
|
|
|
|
TemplatePath string `json:"template"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Comment string `json:"comment"`
|
2020-06-17 02:03:33 +00:00
|
|
|
RequiredData []string `json:"requires,omitempty"`
|
2020-02-20 23:48:48 +00:00
|
|
|
Content []byte `json:"-"`
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the template is not valid.
|
|
|
|
func (t *Template) Validate() error {
|
|
|
|
switch {
|
|
|
|
case t == nil:
|
|
|
|
return nil
|
|
|
|
case t.Name == "":
|
|
|
|
return errors.New("template name cannot be empty")
|
2021-11-09 23:26:34 +00:00
|
|
|
case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != FullSnippet:
|
|
|
|
return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, FullSnippet, File, Directory)
|
2020-02-20 23:48:48 +00:00
|
|
|
case t.TemplatePath == "" && t.Type != Directory && len(t.Content) == 0:
|
2019-10-04 02:03:38 +00:00
|
|
|
return errors.New("template template cannot be empty")
|
2019-10-16 01:00:46 +00:00
|
|
|
case t.TemplatePath != "" && t.Type == Directory:
|
|
|
|
return errors.New("template template must be empty with directory type")
|
2020-02-20 23:48:48 +00:00
|
|
|
case t.TemplatePath != "" && len(t.Content) > 0:
|
|
|
|
return errors.New("template template must be empty with content")
|
2019-10-04 02:03:38 +00:00
|
|
|
case t.Path == "":
|
|
|
|
return errors.New("template path cannot be empty")
|
|
|
|
}
|
|
|
|
|
2019-10-16 01:00:46 +00:00
|
|
|
if t.TemplatePath != "" {
|
|
|
|
// Check for file
|
2021-08-05 21:00:58 +00:00
|
|
|
st, err := os.Stat(step.Abs(t.TemplatePath))
|
2019-10-16 01:00:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", t.TemplatePath)
|
|
|
|
}
|
|
|
|
if st.IsDir() {
|
|
|
|
return errors.Errorf("error reading %s: is not a file", t.TemplatePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
if t.Comment == "" {
|
|
|
|
t.Comment = "#"
|
|
|
|
}
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 00:26:54 +00:00
|
|
|
// ValidateRequiredData checks that the given data contains all the keys
|
|
|
|
// required.
|
|
|
|
func (t *Template) ValidateRequiredData(data map[string]string) error {
|
|
|
|
for _, key := range t.RequiredData {
|
|
|
|
if _, ok := data[key]; !ok {
|
|
|
|
return errors.Errorf("required variable '%s' is missing", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// Load loads the template in memory, returns an error if the parsing of the
|
|
|
|
// template fails.
|
|
|
|
func (t *Template) Load() error {
|
2019-10-16 01:00:46 +00:00
|
|
|
if t.Template == nil && t.Type != Directory {
|
2020-02-20 23:48:48 +00:00
|
|
|
switch {
|
|
|
|
case t.TemplatePath != "":
|
2021-08-05 21:00:58 +00:00
|
|
|
filename := step.Abs(t.TemplatePath)
|
2020-02-20 23:48:48 +00:00
|
|
|
b, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", filename)
|
|
|
|
}
|
|
|
|
return t.LoadBytes(b)
|
|
|
|
default:
|
|
|
|
return t.LoadBytes(t.Content)
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 00:26:54 +00:00
|
|
|
// LoadBytes loads the template in memory, returns an error if the parsing of
|
|
|
|
// the template fails.
|
2020-02-20 23:48:48 +00:00
|
|
|
func (t *Template) LoadBytes(b []byte) error {
|
2020-06-17 00:26:54 +00:00
|
|
|
t.backfill(b)
|
2021-10-27 23:11:47 +00:00
|
|
|
tmpl, err := template.New(t.Name).Funcs(StepFuncMap()).Parse(string(b))
|
2020-02-20 23:48:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing template %s", t.Name)
|
|
|
|
}
|
|
|
|
t.Template = tmpl
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// Render executes the template with the given data and returns the rendered
|
|
|
|
// version.
|
|
|
|
func (t *Template) Render(data interface{}) ([]byte, error) {
|
2019-10-16 01:00:46 +00:00
|
|
|
if t.Type == Directory {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
if err := t.Load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := t.Execute(buf, data); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error executing %s", t.TemplatePath)
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output renders the template and returns a template.Output struct or an error.
|
|
|
|
func (t *Template) Output(data interface{}) (Output, error) {
|
|
|
|
b, err := t.Render(data)
|
|
|
|
if err != nil {
|
|
|
|
return Output{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output{
|
|
|
|
Name: t.Name,
|
|
|
|
Type: t.Type,
|
|
|
|
Path: t.Path,
|
2019-10-16 01:00:46 +00:00
|
|
|
Comment: t.Comment,
|
2019-10-04 02:03:38 +00:00
|
|
|
Content: b,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-12 01:59:50 +00:00
|
|
|
|
2020-06-17 00:26:54 +00:00
|
|
|
// backfill updates old templates with the required data.
|
|
|
|
func (t *Template) backfill(b []byte) {
|
2021-10-08 18:59:57 +00:00
|
|
|
if strings.EqualFold(t.Name, "sshd_config.tpl") && len(t.RequiredData) == 0 {
|
|
|
|
a := bytes.TrimSpace(b)
|
|
|
|
b := bytes.TrimSpace([]byte(DefaultSSHTemplateData[t.Name]))
|
|
|
|
if bytes.Equal(a, b) {
|
|
|
|
t.RequiredData = []string{"Certificate", "Key"}
|
2020-06-17 00:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 01:59:50 +00:00
|
|
|
// Output represents the text representation of a rendered template.
|
|
|
|
type Output struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type TemplateType `json:"type"`
|
|
|
|
Path string `json:"path"`
|
2019-10-16 01:00:46 +00:00
|
|
|
Comment string `json:"comment"`
|
2019-10-12 01:59:50 +00:00
|
|
|
Content []byte `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the Output to the filesystem as a directory, file or snippet.
|
|
|
|
func (o *Output) Write() error {
|
2021-08-05 21:00:58 +00:00
|
|
|
path := step.Abs(o.Path)
|
2019-10-12 01:59:50 +00:00
|
|
|
if o.Type == Directory {
|
|
|
|
return mkdir(path, 0700)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
if err := mkdir(dir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-09 23:26:34 +00:00
|
|
|
switch o.Type {
|
|
|
|
case File:
|
2020-10-29 20:10:03 +00:00
|
|
|
return fileutil.WriteFile(path, o.Content, 0600)
|
2021-11-09 23:26:34 +00:00
|
|
|
case Snippet:
|
|
|
|
return fileutil.WriteSnippet(path, o.Content, 0600)
|
|
|
|
case FullSnippet:
|
|
|
|
lines := strings.Split(string(o.Content), "\n")
|
|
|
|
return fileutil.WriteFullSnippet(path, o.Content, lines[0], lines[len(lines)-1], 0600)
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unexpected output template type %s", string(o.Type))
|
2019-10-12 01:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkdir(path string, perm os.FileMode) error {
|
|
|
|
if err := os.MkdirAll(path, perm); err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating %s", path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 18:50:55 +00:00
|
|
|
|
2021-10-27 23:11:47 +00:00
|
|
|
// StepFuncMap returns sprig.TxtFuncMap but removing the "env" and "expandenv"
|
2021-10-27 18:50:55 +00:00
|
|
|
// functions to avoid any leak of information.
|
2021-10-27 23:11:47 +00:00
|
|
|
func StepFuncMap() template.FuncMap {
|
2021-10-27 18:50:55 +00:00
|
|
|
m := sprig.TxtFuncMap()
|
|
|
|
delete(m, "env")
|
|
|
|
delete(m, "expandenv")
|
|
|
|
return m
|
|
|
|
}
|