2020-07-01 02:19:52 +00:00
|
|
|
package x509util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"text/template"
|
|
|
|
|
2020-07-08 02:09:29 +00:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2020-07-01 02:19:52 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/cli/config"
|
|
|
|
)
|
|
|
|
|
2020-07-08 01:59:18 +00:00
|
|
|
// Options are the options that can be passed to NewCertificate.
|
2020-07-01 02:19:52 +00:00
|
|
|
type Options struct {
|
|
|
|
CertBuffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) apply(opts []Option) (*Options, error) {
|
|
|
|
for _, fn := range opts {
|
|
|
|
if err := fn(o); err != nil {
|
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2020-07-08 01:59:18 +00:00
|
|
|
// Option is the type used as a variadic argument in NewCertificate.
|
2020-07-01 02:19:52 +00:00
|
|
|
type Option func(o *Options) error
|
|
|
|
|
2020-07-08 01:59:18 +00:00
|
|
|
// WithTemplate is an options that executes the given template text with the
|
|
|
|
// given data.
|
2020-07-02 01:30:04 +00:00
|
|
|
func WithTemplate(text string, data map[string]interface{}) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
tmpl, err := template.New("template").Funcs(sprig.TxtFuncMap()).Parse(text)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing template")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := tmpl.Execute(buf, data); err != nil {
|
|
|
|
return errors.Wrapf(err, "error executing template")
|
|
|
|
}
|
|
|
|
o.CertBuffer = buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 01:59:18 +00:00
|
|
|
// WithTemplateFile is an options that reads the template file and executes it
|
|
|
|
// with the given data.
|
2020-07-02 01:30:04 +00:00
|
|
|
func WithTemplateFile(path string, data map[string]interface{}) Option {
|
2020-07-01 02:19:52 +00:00
|
|
|
return func(o *Options) error {
|
|
|
|
filename := config.StepAbs(path)
|
|
|
|
b, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New(path).Funcs(sprig.TxtFuncMap()).Parse(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := tmpl.Execute(buf, data); err != nil {
|
|
|
|
return errors.Wrapf(err, "error executing %s", path)
|
|
|
|
}
|
|
|
|
o.CertBuffer = buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|