forked from TrueCloudLab/certificates
Allow to send errors from template to cli.
This commit is contained in:
parent
1a04d458ae
commit
8f0dd811af
3 changed files with 27 additions and 1 deletions
|
@ -108,6 +108,9 @@ func (a *Authority) Sign(csr *x509.CertificateRequest, signOpts provisioner.Opti
|
||||||
|
|
||||||
cert, err := x509cert.NewCertificate(csr, certOptions...)
|
cert, err := x509cert.NewCertificate(csr, certOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if _, ok := err.(*x509cert.TemplateError); ok {
|
||||||
|
return nil, errs.NewErr(http.StatusBadRequest, err, errs.WithMessage(err.Error()))
|
||||||
|
}
|
||||||
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Sign", opts...)
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Sign", opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,15 @@ import (
|
||||||
"github.com/smallstep/cli/config"
|
"github.com/smallstep/cli/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getFuncMap(failMessage *string) template.FuncMap {
|
||||||
|
m := sprig.TxtFuncMap()
|
||||||
|
m["fail"] = func(msg string) (string, error) {
|
||||||
|
*failMessage = msg
|
||||||
|
return "", errors.New(msg)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
// Options are the options that can be passed to NewCertificate.
|
// Options are the options that can be passed to NewCertificate.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
CertBuffer *bytes.Buffer
|
CertBuffer *bytes.Buffer
|
||||||
|
@ -33,7 +42,10 @@ type Option func(cr *x509.CertificateRequest, o *Options) error
|
||||||
// given data.
|
// given data.
|
||||||
func WithTemplate(text string, data TemplateData) Option {
|
func WithTemplate(text string, data TemplateData) Option {
|
||||||
return func(cr *x509.CertificateRequest, o *Options) error {
|
return func(cr *x509.CertificateRequest, o *Options) error {
|
||||||
tmpl, err := template.New("template").Funcs(sprig.TxtFuncMap()).Parse(text)
|
terr := new(TemplateError)
|
||||||
|
funcMap := getFuncMap(&terr.Message)
|
||||||
|
|
||||||
|
tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error parsing template")
|
return errors.Wrapf(err, "error parsing template")
|
||||||
}
|
}
|
||||||
|
@ -41,6 +53,9 @@ func WithTemplate(text string, data TemplateData) Option {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
data.SetCertificateRequest(cr)
|
data.SetCertificateRequest(cr)
|
||||||
if err := tmpl.Execute(buf, data); err != nil {
|
if err := tmpl.Execute(buf, data); err != nil {
|
||||||
|
if terr.Message != "" {
|
||||||
|
return terr
|
||||||
|
}
|
||||||
return errors.Wrapf(err, "error executing template")
|
return errors.Wrapf(err, "error executing template")
|
||||||
}
|
}
|
||||||
o.CertBuffer = buf
|
o.CertBuffer = buf
|
||||||
|
|
|
@ -13,6 +13,14 @@ const (
|
||||||
CertificateRequestKey = "CR"
|
CertificateRequestKey = "CR"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TemplateError struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *TemplateError) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
// TemplateData is an alias for map[string]interface{}. It represents the data
|
// TemplateData is an alias for map[string]interface{}. It represents the data
|
||||||
// passed to the templates.
|
// passed to the templates.
|
||||||
type TemplateData map[string]interface{}
|
type TemplateData map[string]interface{}
|
||||||
|
|
Loading…
Reference in a new issue