cleanup structured authn/authz error logic

Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
This commit is contained in:
Anish Ramasekar 2024-01-25 22:45:19 +00:00
parent 5cef5f1a57
commit c2c4f4616d
No known key found for this signature in database
GPG Key ID: F1F7F3518F1ECB0C

View File

@ -405,7 +405,7 @@ func validateUserValidationRules(compiler authenticationcel.Compiler, celMapper
func compileClaimsCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) { func compileClaimsCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) {
compilationResult, err := compiler.CompileClaimsExpression(expression) compilationResult, err := compiler.CompileClaimsExpression(expression)
if err != nil { if err != nil {
return nil, convertCELErrorToValidationError(fldPath, expression, err) return nil, convertCELErrorToValidationError(fldPath, expression.GetExpression(), err)
} }
return &compilationResult, nil return &compilationResult, nil
} }
@ -413,7 +413,7 @@ func compileClaimsCELExpression(compiler authenticationcel.Compiler, expression
func compileUserCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) { func compileUserCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) {
compilationResult, err := compiler.CompileUserExpression(expression) compilationResult, err := compiler.CompileUserExpression(expression)
if err != nil { if err != nil {
return nil, convertCELErrorToValidationError(fldPath, expression, err) return nil, convertCELErrorToValidationError(fldPath, expression.GetExpression(), err)
} }
return &compilationResult, nil return &compilationResult, nil
} }
@ -602,19 +602,19 @@ func compileMatchConditionsExpression(fldPath *field.Path, compiler authorizatio
} }
compilationResult, err := compiler.CompileCELExpression(authzExpression) compilationResult, err := compiler.CompileCELExpression(authzExpression)
if err != nil { if err != nil {
return compilationResult, convertCELErrorToValidationError(fldPath, authzExpression, err) return compilationResult, convertCELErrorToValidationError(fldPath, authzExpression.GetExpression(), err)
} }
return compilationResult, nil return compilationResult, nil
} }
func convertCELErrorToValidationError(fldPath *field.Path, expression authorizationcel.ExpressionAccessor, err error) *field.Error { func convertCELErrorToValidationError(fldPath *field.Path, expression string, err error) *field.Error {
var celErr *cel.Error var celErr *cel.Error
if errors.As(err, &celErr) { if errors.As(err, &celErr) {
switch celErr.Type { switch celErr.Type {
case cel.ErrorTypeRequired: case cel.ErrorTypeRequired:
return field.Required(fldPath, celErr.Detail) return field.Required(fldPath, celErr.Detail)
case cel.ErrorTypeInvalid: case cel.ErrorTypeInvalid:
return field.Invalid(fldPath, expression.GetExpression(), celErr.Detail) return field.Invalid(fldPath, expression, celErr.Detail)
default: default:
return field.InternalError(fldPath, celErr) return field.InternalError(fldPath, celErr)
} }