mirror of
https://github.com/smallstep/certificates.git
synced 2024-11-17 15:29:21 +00:00
Allow to send errors from template to cli.
This commit is contained in:
parent
1a04d458ae
commit
8f0dd811af
@ -108,6 +108,9 @@ func (a *Authority) Sign(csr *x509.CertificateRequest, signOpts provisioner.Opti
|
||||
|
||||
cert, err := x509cert.NewCertificate(csr, certOptions...)
|
||||
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...)
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,15 @@ import (
|
||||
"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.
|
||||
type Options struct {
|
||||
CertBuffer *bytes.Buffer
|
||||
@ -33,7 +42,10 @@ type Option func(cr *x509.CertificateRequest, o *Options) error
|
||||
// given data.
|
||||
func WithTemplate(text string, data TemplateData) Option {
|
||||
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 {
|
||||
return errors.Wrapf(err, "error parsing template")
|
||||
}
|
||||
@ -41,6 +53,9 @@ func WithTemplate(text string, data TemplateData) Option {
|
||||
buf := new(bytes.Buffer)
|
||||
data.SetCertificateRequest(cr)
|
||||
if err := tmpl.Execute(buf, data); err != nil {
|
||||
if terr.Message != "" {
|
||||
return terr
|
||||
}
|
||||
return errors.Wrapf(err, "error executing template")
|
||||
}
|
||||
o.CertBuffer = buf
|
||||
|
@ -13,6 +13,14 @@ const (
|
||||
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
|
||||
// passed to the templates.
|
||||
type TemplateData map[string]interface{}
|
||||
|
Loading…
Reference in New Issue
Block a user