2020-07-08 02:09:29 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-07-15 00:13:06 +00:00
|
|
|
"strings"
|
2020-07-08 02:09:29 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/x509util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CertificateOptions is an interface that returns a list of options passed when
|
|
|
|
// creating a new certificate.
|
|
|
|
type CertificateOptions interface {
|
|
|
|
Options(Options) []x509util.Option
|
|
|
|
}
|
|
|
|
|
|
|
|
type certificateOptionsFunc func(Options) []x509util.Option
|
|
|
|
|
|
|
|
func (fn certificateOptionsFunc) Options(so Options) []x509util.Option {
|
|
|
|
return fn(so)
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:12:07 +00:00
|
|
|
// ProvisionerOptions are a collection of custom options that can be added to
|
|
|
|
// each provisioner.
|
2020-07-08 02:09:29 +00:00
|
|
|
type ProvisionerOptions struct {
|
2020-07-15 00:12:07 +00:00
|
|
|
// Template contains a X.509 certificate template. It can be a JSON template
|
|
|
|
// escaped in a string or it can be also encoded in base64.
|
|
|
|
Template string `json:"template"`
|
|
|
|
|
|
|
|
// TemplateFile points to a file containing a X.509 certificate template.
|
|
|
|
TemplateFile string `json:"templateFile"`
|
|
|
|
|
|
|
|
// TemplateData is a JSON object with variables that can be used in custom
|
|
|
|
// templates.
|
2020-07-08 02:09:29 +00:00
|
|
|
TemplateData json.RawMessage `json:"templateData"`
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:30:04 +00:00
|
|
|
// HasTemplate returns true if a template is defined in the provisioner options.
|
|
|
|
func (o *ProvisionerOptions) HasTemplate() bool {
|
|
|
|
return o != nil && (o.Template != "" || o.TemplateFile != "")
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:47:26 +00:00
|
|
|
// TemplateOptions generates a CertificateOptions with the template and data
|
2020-07-08 02:09:29 +00:00
|
|
|
// defined in the ProvisionerOptions, the provisioner generated data, and the
|
2020-07-13 19:47:26 +00:00
|
|
|
// user data provided in the request. If no template has been provided,
|
|
|
|
// x509util.DefaultLeafTemplate will be used.
|
2020-07-08 02:09:29 +00:00
|
|
|
func TemplateOptions(o *ProvisionerOptions, data x509util.TemplateData) (CertificateOptions, error) {
|
2020-07-13 19:47:26 +00:00
|
|
|
return CustomTemplateOptions(o, data, x509util.DefaultLeafTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomTemplateOptions generates a CertificateOptions with the template, data
|
|
|
|
// defined in the ProvisionerOptions, the provisioner generated data and the
|
|
|
|
// user data provided in the request. If no template has been provided in the
|
|
|
|
// ProvisionerOptions, the given template will be used.
|
|
|
|
func CustomTemplateOptions(o *ProvisionerOptions, data x509util.TemplateData, defaultTemplate string) (CertificateOptions, error) {
|
2020-07-08 02:09:29 +00:00
|
|
|
if o != nil {
|
|
|
|
if data == nil {
|
2020-07-09 22:22:00 +00:00
|
|
|
data = x509util.NewTemplateData()
|
2020-07-08 02:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add template data if any.
|
|
|
|
if len(o.TemplateData) > 0 {
|
2020-07-08 22:54:25 +00:00
|
|
|
if err := json.Unmarshal(o.TemplateData, &data); err != nil {
|
2020-07-08 02:09:29 +00:00
|
|
|
return nil, errors.Wrap(err, "error unmarshaling template data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificateOptionsFunc(func(so Options) []x509util.Option {
|
|
|
|
// We're not provided user data without custom templates.
|
2020-07-15 01:30:04 +00:00
|
|
|
if !o.HasTemplate() {
|
2020-07-08 02:09:29 +00:00
|
|
|
return []x509util.Option{
|
2020-07-13 19:47:26 +00:00
|
|
|
x509util.WithTemplate(defaultTemplate, data),
|
2020-07-08 02:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add user provided data.
|
2020-07-08 22:54:25 +00:00
|
|
|
if len(so.TemplateData) > 0 {
|
2020-07-08 02:09:29 +00:00
|
|
|
userObject := make(map[string]interface{})
|
2020-07-08 22:54:25 +00:00
|
|
|
if err := json.Unmarshal(so.TemplateData, &userObject); err != nil {
|
2020-07-15 00:12:07 +00:00
|
|
|
data.SetUserData(map[string]interface{}{})
|
2020-07-08 02:09:29 +00:00
|
|
|
} else {
|
2020-07-15 00:12:07 +00:00
|
|
|
data.SetUserData(userObject)
|
2020-07-08 02:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-15 00:12:07 +00:00
|
|
|
|
|
|
|
// Load a template from a file if Template is not defined.
|
2020-07-08 02:09:29 +00:00
|
|
|
if o.Template == "" && o.TemplateFile != "" {
|
|
|
|
return []x509util.Option{
|
|
|
|
x509util.WithTemplateFile(o.TemplateFile, data),
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 00:13:06 +00:00
|
|
|
|
|
|
|
// Load a template from the Template fields
|
|
|
|
// 1. As a JSON in a string.
|
|
|
|
template := strings.TrimSpace(o.Template)
|
|
|
|
if strings.HasPrefix(template, "{") {
|
|
|
|
return []x509util.Option{
|
|
|
|
x509util.WithTemplate(template, data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2. As a base64 encoded JSON.
|
2020-07-08 02:09:29 +00:00
|
|
|
return []x509util.Option{
|
2020-07-15 00:13:06 +00:00
|
|
|
x509util.WithTemplateBase64(template, data),
|
2020-07-08 02:09:29 +00:00
|
|
|
}
|
|
|
|
}), nil
|
|
|
|
}
|