Skip to content

Commit ff04873

Browse files
committed
Change the default error type to forbidden in Sign.
The errors will also be propagated from sign options.
1 parent b9beab0 commit ff04873

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Diff for: authority/tls.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func (a *Authority) Sign(csr *x509.CertificateRequest, signOpts provisioner.Sign
9494
// Validate the given certificate request.
9595
case provisioner.CertificateRequestValidator:
9696
if err := k.Valid(csr); err != nil {
97-
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
97+
return nil, errs.ApplyOptions(
98+
errs.ForbiddenErr(err, "error validating certificate"),
99+
opts...,
100+
)
98101
}
99102

100103
// Validates the unsigned certificate template.
@@ -131,26 +134,38 @@ func (a *Authority) Sign(csr *x509.CertificateRequest, signOpts provisioner.Sign
131134

132135
// Set default subject
133136
if err := withDefaultASN1DN(a.config.AuthorityConfig.Template).Modify(leaf, signOpts); err != nil {
134-
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
137+
return nil, errs.ApplyOptions(
138+
errs.ForbiddenErr(err, "error creating certificate"),
139+
opts...,
140+
)
135141
}
136142

137143
for _, m := range certModifiers {
138144
if err := m.Modify(leaf, signOpts); err != nil {
139-
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
145+
return nil, errs.ApplyOptions(
146+
errs.ForbiddenErr(err, "error creating certificate"),
147+
opts...,
148+
)
140149
}
141150
}
142151

143152
// Certificate validation.
144153
for _, v := range certValidators {
145154
if err := v.Valid(leaf, signOpts); err != nil {
146-
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
155+
return nil, errs.ApplyOptions(
156+
errs.ForbiddenErr(err, "error validating certificate"),
157+
opts...,
158+
)
147159
}
148160
}
149161

150162
// Certificate modifiers after validation
151163
for _, m := range certEnforcers {
152164
if err := m.Enforce(leaf); err != nil {
153-
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
165+
return nil, errs.ApplyOptions(
166+
errs.ForbiddenErr(err, "error creating certificate"),
167+
opts...,
168+
)
154169
}
155170
}
156171

Diff for: authority/tls_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func TestAuthority_Sign(t *testing.T) {
281281
csr: csr,
282282
extraOpts: extraOpts,
283283
signOpts: signOpts,
284-
err: errors.New("authority.Sign: default ASN1DN template cannot be nil"),
285-
code: http.StatusUnauthorized,
284+
err: errors.New("default ASN1DN template cannot be nil"),
285+
code: http.StatusForbidden,
286286
}
287287
},
288288
"fail create cert": func(t *testing.T) *signTest {
@@ -309,7 +309,7 @@ func TestAuthority_Sign(t *testing.T) {
309309
csr: csr,
310310
extraOpts: extraOpts,
311311
signOpts: _signOpts,
312-
err: errors.New("authority.Sign: requested duration of 25h0m0s is more than the authorized maximum certificate duration of 24h1m0s"),
312+
err: errors.New("requested duration of 25h0m0s is more than the authorized maximum certificate duration of 24h1m0s"),
313313
code: http.StatusBadRequest,
314314
}
315315
},
@@ -322,7 +322,7 @@ func TestAuthority_Sign(t *testing.T) {
322322
csr: csr,
323323
extraOpts: extraOpts,
324324
signOpts: signOpts,
325-
err: errors.New("authority.Sign: certificate request does not contain the valid DNS names - got [test.smallstep.com smallstep test], want [test.smallstep.com]"),
325+
err: errors.New("certificate request does not contain the valid DNS names - got [test.smallstep.com smallstep test], want [test.smallstep.com]"),
326326
code: http.StatusBadRequest,
327327
}
328328
},
@@ -348,7 +348,7 @@ ZYtQ9Ot36qc=
348348
csr: csr,
349349
extraOpts: extraOpts,
350350
signOpts: signOpts,
351-
err: errors.New("authority.Sign: certificate request RSA key must be at least 2048 bits (256 bytes)"),
351+
err: errors.New("certificate request RSA key must be at least 2048 bits (256 bytes)"),
352352
code: http.StatusForbidden,
353353
}
354354
},

0 commit comments

Comments
 (0)