@@ -105,11 +105,8 @@ func (par *PolicyAdminResponder) CreateAuthorityPolicy(w http.ResponseWriter, r
105
105
106
106
var createdPolicy * linkedca.Policy
107
107
if createdPolicy , err = par .auth .CreateAuthorityPolicy (ctx , adm , newPolicy ); err != nil {
108
- var pe * authority.PolicyError
109
- isPolicyError := errors .As (err , & pe )
110
-
111
- if isPolicyError && pe .Typ == authority .AdminLockOut || pe .Typ == authority .EvaluationFailure || pe .Typ == authority .ConfigurationFailure {
112
- render .Error (w , admin .WrapError (admin .ErrorBadRequestType , pe , "error storing authority policy" ))
108
+ if isBadRequest (err ) {
109
+ render .Error (w , admin .WrapError (admin .ErrorBadRequestType , err , "error storing authority policy" ))
113
110
return
114
111
}
115
112
@@ -153,10 +150,8 @@ func (par *PolicyAdminResponder) UpdateAuthorityPolicy(w http.ResponseWriter, r
153
150
154
151
var updatedPolicy * linkedca.Policy
155
152
if updatedPolicy , err = par .auth .UpdateAuthorityPolicy (ctx , adm , newPolicy ); err != nil {
156
- var pe * authority.PolicyError
157
- isPolicyError := errors .As (err , & pe )
158
- if isPolicyError && pe .Typ == authority .AdminLockOut || pe .Typ == authority .EvaluationFailure || pe .Typ == authority .ConfigurationFailure {
159
- render .Error (w , admin .WrapError (admin .ErrorBadRequestType , pe , "error updating authority policy" ))
153
+ if isBadRequest (err ) {
154
+ render .Error (w , admin .WrapError (admin .ErrorBadRequestType , err , "error updating authority policy" ))
160
155
return
161
156
}
162
157
@@ -246,10 +241,8 @@ func (par *PolicyAdminResponder) CreateProvisionerPolicy(w http.ResponseWriter,
246
241
prov .Policy = newPolicy
247
242
248
243
if err := par .auth .UpdateProvisioner (ctx , prov ); err != nil {
249
- var pe * authority.PolicyError
250
- isPolicyError := errors .As (err , & pe )
251
- if isPolicyError && pe .Typ == authority .AdminLockOut || pe .Typ == authority .EvaluationFailure || pe .Typ == authority .ConfigurationFailure {
252
- render .Error (w , admin .WrapError (admin .ErrorBadRequestType , pe , "error creating provisioner policy" ))
244
+ if isBadRequest (err ) {
245
+ render .Error (w , admin .WrapError (admin .ErrorBadRequestType , err , "error creating provisioner policy" ))
253
246
return
254
247
}
255
248
@@ -286,10 +279,8 @@ func (par *PolicyAdminResponder) UpdateProvisionerPolicy(w http.ResponseWriter,
286
279
287
280
prov .Policy = newPolicy
288
281
if err := par .auth .UpdateProvisioner (ctx , prov ); err != nil {
289
- var pe * authority.PolicyError
290
- isPolicyError := errors .As (err , & pe )
291
- if isPolicyError && pe .Typ == authority .AdminLockOut || pe .Typ == authority .EvaluationFailure || pe .Typ == authority .ConfigurationFailure {
292
- render .Error (w , admin .WrapError (admin .ErrorBadRequestType , pe , "error updating provisioner policy" ))
282
+ if isBadRequest (err ) {
283
+ render .Error (w , admin .WrapError (admin .ErrorBadRequestType , err , "error updating provisioner policy" ))
293
284
return
294
285
}
295
286
@@ -456,6 +447,14 @@ func (par *PolicyAdminResponder) blockLinkedCA() error {
456
447
return nil
457
448
}
458
449
450
+ // isBadRequest checks if an error should result in a bad request error
451
+ // returned to the client.
452
+ func isBadRequest (err error ) bool {
453
+ var pe * authority.PolicyError
454
+ isPolicyError := errors .As (err , & pe )
455
+ return isPolicyError && (pe .Typ == authority .AdminLockOut || pe .Typ == authority .EvaluationFailure || pe .Typ == authority .ConfigurationFailure )
456
+ }
457
+
459
458
// applyConditionalDefaults applies default settings in case they're not provided
460
459
// in the request body.
461
460
func applyConditionalDefaults (p * linkedca.Policy ) {
0 commit comments