| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package validation |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"net/url" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"strings" |
| 26 |
"time" |
| 27 |
|
| 28 |
celgo "github.com/google/cel-go/cel" |
| 29 |
"github.com/google/cel-go/common/operators" |
| 30 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 31 |
|
| 32 |
v1 "k8s.io/api/authorization/v1" |
| 33 |
"k8s.io/api/authorization/v1beta1" |
| 34 |
"k8s.io/apimachinery/pkg/util/sets" |
| 35 |
utilvalidation "k8s.io/apimachinery/pkg/util/validation" |
| 36 |
"k8s.io/apimachinery/pkg/util/validation/field" |
| 37 |
api "k8s.io/apiserver/pkg/apis/apiserver" |
| 38 |
authenticationcel "k8s.io/apiserver/pkg/authentication/cel" |
| 39 |
authorizationcel "k8s.io/apiserver/pkg/authorization/cel" |
| 40 |
"k8s.io/apiserver/pkg/cel" |
| 41 |
"k8s.io/apiserver/pkg/features" |
| 42 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 43 |
"k8s.io/client-go/util/cert" |
| 44 |
) |
| 45 |
|
| 46 |
// ValidateAuthenticationConfiguration validates a given AuthenticationConfiguration. |
| 47 |
func ValidateAuthenticationConfiguration(compiler authenticationcel.Compiler, c *api.AuthenticationConfiguration, disallowedIssuers []string) field.ErrorList { |
| 48 |
root := field.NewPath("jwt") |
| 49 |
var allErrs field.ErrorList |
| 50 |
|
| 51 |
// We allow 0 authenticators in the authentication configuration. |
| 52 |
// This allows us to support scenarios where the API server is initially set up without |
| 53 |
// any authenticators and then authenticators are added later via dynamic config. |
| 54 |
|
| 55 |
if len(c.JWT) > 64 { |
| 56 |
allErrs = append(allErrs, field.TooMany(root, len(c.JWT), 64)) |
| 57 |
return allErrs |
| 58 |
} |
| 59 |
|
| 60 |
seenIssuers := sets.New[string]() |
| 61 |
seenDiscoveryURLs := sets.New[string]() |
| 62 |
for i, a := range c.JWT { |
| 63 |
fldPath := root.Index(i) |
| 64 |
_, errs := validateJWTAuthenticator(compiler, a, fldPath, sets.New(disallowedIssuers...), utilfeature.DefaultFeatureGate.Enabled(features.StructuredAuthenticationConfiguration)) |
| 65 |
allErrs = append(allErrs, errs...) |
| 66 |
|
| 67 |
if seenIssuers.Has(a.Issuer.URL) { |
| 68 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("issuer").Child("url"), a.Issuer.URL)) |
| 69 |
} |
| 70 |
seenIssuers.Insert(a.Issuer.URL) |
| 71 |
|
| 72 |
if len(a.Issuer.DiscoveryURL) > 0 { |
| 73 |
if seenDiscoveryURLs.Has(a.Issuer.DiscoveryURL) { |
| 74 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("issuer").Child("discoveryURL"), a.Issuer.DiscoveryURL)) |
| 75 |
} |
| 76 |
seenDiscoveryURLs.Insert(a.Issuer.DiscoveryURL) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if c.Anonymous != nil { |
| 81 |
if !utilfeature.DefaultFeatureGate.Enabled(features.AnonymousAuthConfigurableEndpoints) { |
| 82 |
allErrs = append(allErrs, field.Forbidden(field.NewPath("anonymous"), "anonymous is not supported when AnonymousAuthConfigurableEnpoints feature gate is disabled")) |
| 83 |
} |
| 84 |
if !c.Anonymous.Enabled && len(c.Anonymous.Conditions) > 0 { |
| 85 |
allErrs = append(allErrs, field.Invalid(field.NewPath("anonymous", "conditions"), c.Anonymous.Conditions, "enabled should be set to true when conditions are defined")) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return allErrs |
| 90 |
} |
| 91 |
|
| 92 |
// CompileAndValidateJWTAuthenticator validates a given JWTAuthenticator and returns a CELMapper with the compiled |
| 93 |
// CEL expressions for claim mappings and validation rules. |
| 94 |
// This is exported for use in oidc package. |
| 95 |
func CompileAndValidateJWTAuthenticator(compiler authenticationcel.Compiler, authenticator api.JWTAuthenticator, disallowedIssuers []string) (authenticationcel.CELMapper, field.ErrorList) { |
| 96 |
return validateJWTAuthenticator(compiler, authenticator, nil, sets.New(disallowedIssuers...), utilfeature.DefaultFeatureGate.Enabled(features.StructuredAuthenticationConfiguration)) |
| 97 |
} |
| 98 |
|
| 99 |
func validateJWTAuthenticator(compiler authenticationcel.Compiler, authenticator api.JWTAuthenticator, fldPath *field.Path, disallowedIssuers sets.Set[string], structuredAuthnFeatureEnabled bool) (authenticationcel.CELMapper, field.ErrorList) { |
| 100 |
var allErrs field.ErrorList |
| 101 |
|
| 102 |
state := &validationState{} |
| 103 |
|
| 104 |
allErrs = append(allErrs, validateIssuer(authenticator.Issuer, disallowedIssuers, fldPath.Child("issuer"), structuredAuthnFeatureEnabled)...) |
| 105 |
allErrs = append(allErrs, validateClaimValidationRules(compiler, state, authenticator.ClaimValidationRules, fldPath.Child("claimValidationRules"), structuredAuthnFeatureEnabled)...) |
| 106 |
allErrs = append(allErrs, validateClaimMappings(compiler, state, authenticator.ClaimMappings, fldPath.Child("claimMappings"), structuredAuthnFeatureEnabled)...) |
| 107 |
allErrs = append(allErrs, validateUserValidationRules(compiler, state, authenticator.UserValidationRules, fldPath.Child("userValidationRules"), structuredAuthnFeatureEnabled)...) |
| 108 |
|
| 109 |
return state.mapper, allErrs |
| 110 |
} |
| 111 |
|
| 112 |
type validationState struct { |
| 113 |
mapper authenticationcel.CELMapper |
| 114 |
usesEmailClaim bool |
| 115 |
usesEmailVerifiedClaim bool |
| 116 |
} |
| 117 |
|
| 118 |
func validateIssuer(issuer api.Issuer, disallowedIssuers sets.Set[string], fldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 119 |
var allErrs field.ErrorList |
| 120 |
|
| 121 |
allErrs = append(allErrs, validateIssuerURL(issuer.URL, disallowedIssuers, fldPath.Child("url"))...) |
| 122 |
allErrs = append(allErrs, validateIssuerDiscoveryURL(issuer.URL, issuer.DiscoveryURL, fldPath.Child("discoveryURL"), structuredAuthnFeatureEnabled)...) |
| 123 |
allErrs = append(allErrs, validateAudiences(issuer.Audiences, issuer.AudienceMatchPolicy, fldPath.Child("audiences"), fldPath.Child("audienceMatchPolicy"), structuredAuthnFeatureEnabled)...) |
| 124 |
allErrs = append(allErrs, validateCertificateAuthority(issuer.CertificateAuthority, fldPath.Child("certificateAuthority"))...) |
| 125 |
|
| 126 |
return allErrs |
| 127 |
} |
| 128 |
|
| 129 |
func validateIssuerURL(issuerURL string, disallowedIssuers sets.Set[string], fldPath *field.Path) field.ErrorList { |
| 130 |
if len(issuerURL) == 0 { |
| 131 |
return field.ErrorList{field.Required(fldPath, "URL is required")} |
| 132 |
} |
| 133 |
|
| 134 |
return validateURL(issuerURL, disallowedIssuers, fldPath) |
| 135 |
} |
| 136 |
|
| 137 |
func validateIssuerDiscoveryURL(issuerURL, issuerDiscoveryURL string, fldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 138 |
var allErrs field.ErrorList |
| 139 |
|
| 140 |
if len(issuerDiscoveryURL) == 0 { |
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
if !structuredAuthnFeatureEnabled { |
| 145 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerDiscoveryURL, "discoveryURL is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 146 |
} |
| 147 |
|
| 148 |
if len(issuerURL) > 0 && strings.TrimRight(issuerURL, "/") == strings.TrimRight(issuerDiscoveryURL, "/") { |
| 149 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerDiscoveryURL, "discoveryURL must be different from URL")) |
| 150 |
} |
| 151 |
|
| 152 |
// issuerDiscoveryURL is not an issuer URL and does not need to validated against any set of disallowed issuers |
| 153 |
allErrs = append(allErrs, validateURL(issuerDiscoveryURL, nil, fldPath)...) |
| 154 |
return allErrs |
| 155 |
} |
| 156 |
|
| 157 |
func validateURL(issuerURL string, disallowedIssuers sets.Set[string], fldPath *field.Path) field.ErrorList { |
| 158 |
var allErrs field.ErrorList |
| 159 |
|
| 160 |
if disallowedIssuers.Has(issuerURL) { |
| 161 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, fmt.Sprintf("URL must not overlap with disallowed issuers: %s", sets.List(disallowedIssuers)))) |
| 162 |
} |
| 163 |
|
| 164 |
u, err := url.Parse(issuerURL) |
| 165 |
if err != nil { |
| 166 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, err.Error())) |
| 167 |
return allErrs |
| 168 |
} |
| 169 |
if u.Scheme != "https" { |
| 170 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, "URL scheme must be https")) |
| 171 |
} |
| 172 |
if u.User != nil { |
| 173 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, "URL must not contain a username or password")) |
| 174 |
} |
| 175 |
if len(u.RawQuery) > 0 { |
| 176 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, "URL must not contain a query")) |
| 177 |
} |
| 178 |
if len(u.Fragment) > 0 { |
| 179 |
allErrs = append(allErrs, field.Invalid(fldPath, issuerURL, "URL must not contain a fragment")) |
| 180 |
} |
| 181 |
|
| 182 |
return allErrs |
| 183 |
} |
| 184 |
|
| 185 |
func validateAudiences(audiences []string, audienceMatchPolicy api.AudienceMatchPolicyType, fldPath, audienceMatchPolicyFldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 186 |
var allErrs field.ErrorList |
| 187 |
|
| 188 |
if len(audiences) == 0 { |
| 189 |
allErrs = append(allErrs, field.Required(fldPath, fmt.Sprintf(atLeastOneRequiredErrFmt, fldPath))) |
| 190 |
return allErrs |
| 191 |
} |
| 192 |
|
| 193 |
if len(audiences) > 1 && !structuredAuthnFeatureEnabled { |
| 194 |
allErrs = append(allErrs, field.Invalid(fldPath, audiences, "multiple audiences are not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 195 |
} |
| 196 |
|
| 197 |
seenAudiences := sets.NewString() |
| 198 |
for i, audience := range audiences { |
| 199 |
fldPath := fldPath.Index(i) |
| 200 |
if len(audience) == 0 { |
| 201 |
allErrs = append(allErrs, field.Required(fldPath, "audience can't be empty")) |
| 202 |
} |
| 203 |
if seenAudiences.Has(audience) { |
| 204 |
allErrs = append(allErrs, field.Duplicate(fldPath, audience)) |
| 205 |
} |
| 206 |
seenAudiences.Insert(audience) |
| 207 |
} |
| 208 |
|
| 209 |
if len(audiences) > 1 && audienceMatchPolicy != api.AudienceMatchPolicyMatchAny { |
| 210 |
allErrs = append(allErrs, field.Invalid(audienceMatchPolicyFldPath, audienceMatchPolicy, "audienceMatchPolicy must be MatchAny for multiple audiences")) |
| 211 |
} |
| 212 |
if len(audiences) == 1 && (len(audienceMatchPolicy) > 0 && audienceMatchPolicy != api.AudienceMatchPolicyMatchAny) { |
| 213 |
allErrs = append(allErrs, field.Invalid(audienceMatchPolicyFldPath, audienceMatchPolicy, "audienceMatchPolicy must be empty or MatchAny for single audience")) |
| 214 |
} |
| 215 |
|
| 216 |
return allErrs |
| 217 |
} |
| 218 |
|
| 219 |
func validateCertificateAuthority(certificateAuthority string, fldPath *field.Path) field.ErrorList { |
| 220 |
var allErrs field.ErrorList |
| 221 |
|
| 222 |
if len(certificateAuthority) == 0 { |
| 223 |
return allErrs |
| 224 |
} |
| 225 |
_, err := cert.NewPoolFromBytes([]byte(certificateAuthority)) |
| 226 |
if err != nil { |
| 227 |
allErrs = append(allErrs, field.Invalid(fldPath, "<omitted>", err.Error())) |
| 228 |
} |
| 229 |
|
| 230 |
return allErrs |
| 231 |
} |
| 232 |
|
| 233 |
func validateClaimValidationRules(compiler authenticationcel.Compiler, state *validationState, rules []api.ClaimValidationRule, fldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 234 |
var allErrs field.ErrorList |
| 235 |
|
| 236 |
seenClaims := sets.NewString() |
| 237 |
seenExpressions := sets.NewString() |
| 238 |
var compilationResults []authenticationcel.CompilationResult |
| 239 |
|
| 240 |
for i, rule := range rules { |
| 241 |
fldPath := fldPath.Index(i) |
| 242 |
|
| 243 |
if len(rule.Expression) > 0 && !structuredAuthnFeatureEnabled { |
| 244 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("expression"), rule.Expression, "expression is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 245 |
} |
| 246 |
|
| 247 |
switch { |
| 248 |
case len(rule.Claim) > 0 && len(rule.Expression) > 0: |
| 249 |
allErrs = append(allErrs, field.Invalid(fldPath, rule.Claim, "claim and expression can't both be set")) |
| 250 |
case len(rule.Claim) == 0 && len(rule.Expression) == 0: |
| 251 |
allErrs = append(allErrs, field.Required(fldPath, "claim or expression is required")) |
| 252 |
case len(rule.Claim) > 0: |
| 253 |
if len(rule.Message) > 0 { |
| 254 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("message"), rule.Message, "message can't be set when claim is set")) |
| 255 |
} |
| 256 |
if seenClaims.Has(rule.Claim) { |
| 257 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("claim"), rule.Claim)) |
| 258 |
} |
| 259 |
seenClaims.Insert(rule.Claim) |
| 260 |
case len(rule.Expression) > 0: |
| 261 |
if len(rule.RequiredValue) > 0 { |
| 262 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("requiredValue"), rule.RequiredValue, "requiredValue can't be set when expression is set")) |
| 263 |
} |
| 264 |
if seenExpressions.Has(rule.Expression) { |
| 265 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("expression"), rule.Expression)) |
| 266 |
continue |
| 267 |
} |
| 268 |
seenExpressions.Insert(rule.Expression) |
| 269 |
|
| 270 |
compilationResult, err := compileClaimsCELExpression(compiler, &authenticationcel.ClaimValidationCondition{ |
| 271 |
Expression: rule.Expression, |
| 272 |
Message: rule.Message, |
| 273 |
}, fldPath.Child("expression")) |
| 274 |
|
| 275 |
if err != nil { |
| 276 |
allErrs = append(allErrs, err) |
| 277 |
continue |
| 278 |
} |
| 279 |
if compilationResult != nil { |
| 280 |
compilationResults = append(compilationResults, *compilationResult) |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if structuredAuthnFeatureEnabled && len(compilationResults) > 0 { |
| 286 |
state.mapper.ClaimValidationRules = authenticationcel.NewClaimsMapper(compilationResults) |
| 287 |
state.usesEmailVerifiedClaim = state.usesEmailVerifiedClaim || anyUsesEmailVerifiedClaim(compilationResults) |
| 288 |
} |
| 289 |
|
| 290 |
return allErrs |
| 291 |
} |
| 292 |
|
| 293 |
func validateClaimMappings(compiler authenticationcel.Compiler, state *validationState, m api.ClaimMappings, fldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 294 |
var allErrs field.ErrorList |
| 295 |
|
| 296 |
if !structuredAuthnFeatureEnabled { |
| 297 |
if len(m.Username.Expression) > 0 { |
| 298 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("username").Child("expression"), m.Username.Expression, "expression is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 299 |
} |
| 300 |
if len(m.Groups.Expression) > 0 { |
| 301 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("groups").Child("expression"), m.Groups.Expression, "expression is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 302 |
} |
| 303 |
if len(m.UID.Claim) > 0 || len(m.UID.Expression) > 0 { |
| 304 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("uid"), "", "uid claim mapping is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 305 |
} |
| 306 |
if len(m.Extra) > 0 { |
| 307 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("extra"), "", "extra claim mapping is not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
compilationResult, err := validatePrefixClaimOrExpression(compiler, m.Username, fldPath.Child("username"), true) |
| 312 |
if err != nil { |
| 313 |
allErrs = append(allErrs, err...) |
| 314 |
} else if compilationResult != nil && structuredAuthnFeatureEnabled { |
| 315 |
state.usesEmailClaim = state.usesEmailClaim || usesEmailClaim(compilationResult.AST) |
| 316 |
state.usesEmailVerifiedClaim = state.usesEmailVerifiedClaim || usesEmailVerifiedClaim(compilationResult.AST) |
| 317 |
state.mapper.Username = authenticationcel.NewClaimsMapper([]authenticationcel.CompilationResult{*compilationResult}) |
| 318 |
} |
| 319 |
|
| 320 |
compilationResult, err = validatePrefixClaimOrExpression(compiler, m.Groups, fldPath.Child("groups"), false) |
| 321 |
if err != nil { |
| 322 |
allErrs = append(allErrs, err...) |
| 323 |
} else if compilationResult != nil && structuredAuthnFeatureEnabled { |
| 324 |
state.mapper.Groups = authenticationcel.NewClaimsMapper([]authenticationcel.CompilationResult{*compilationResult}) |
| 325 |
} |
| 326 |
|
| 327 |
switch { |
| 328 |
case len(m.UID.Claim) > 0 && len(m.UID.Expression) > 0: |
| 329 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("uid"), "", "claim and expression can't both be set")) |
| 330 |
case len(m.UID.Expression) > 0: |
| 331 |
compilationResult, err := compileClaimsCELExpression(compiler, &authenticationcel.ClaimMappingExpression{ |
| 332 |
Expression: m.UID.Expression, |
| 333 |
}, fldPath.Child("uid").Child("expression")) |
| 334 |
|
| 335 |
if err != nil { |
| 336 |
allErrs = append(allErrs, err) |
| 337 |
} else if structuredAuthnFeatureEnabled && compilationResult != nil { |
| 338 |
state.mapper.UID = authenticationcel.NewClaimsMapper([]authenticationcel.CompilationResult{*compilationResult}) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
var extraCompilationResults []authenticationcel.CompilationResult |
| 343 |
seenExtraKeys := sets.NewString() |
| 344 |
|
| 345 |
for i, mapping := range m.Extra { |
| 346 |
fldPath := fldPath.Child("extra").Index(i) |
| 347 |
// Key should be namespaced to the authenticator or authenticator/authorizer pair making use of them. |
| 348 |
// For instance: "example.org/foo" instead of "foo". |
| 349 |
// xref: https://github.com/kubernetes/kubernetes/blob/3825e206cb162a7ad7431a5bdf6a065ae8422cf7/staging/src/k8s.io/apiserver/pkg/authentication/user/user.go#L31-L41 |
| 350 |
// IsDomainPrefixedPath checks for non-empty key and that the key is prefixed with a domain name. |
| 351 |
allErrs = append(allErrs, utilvalidation.IsDomainPrefixedPath(fldPath.Child("key"), mapping.Key)...) |
| 352 |
if mapping.Key != strings.ToLower(mapping.Key) { |
| 353 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("key"), mapping.Key, "key must be lowercase")) |
| 354 |
} |
| 355 |
|
| 356 |
if isKubernetesDomainPrefix(mapping.Key) { |
| 357 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("key"), mapping.Key, "k8s.io, kubernetes.io and their subdomains are reserved for Kubernetes use")) |
| 358 |
} |
| 359 |
|
| 360 |
if seenExtraKeys.Has(mapping.Key) { |
| 361 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("key"), mapping.Key)) |
| 362 |
continue |
| 363 |
} |
| 364 |
seenExtraKeys.Insert(mapping.Key) |
| 365 |
|
| 366 |
if len(mapping.ValueExpression) == 0 { |
| 367 |
allErrs = append(allErrs, field.Required(fldPath.Child("valueExpression"), "valueExpression is required")) |
| 368 |
continue |
| 369 |
} |
| 370 |
|
| 371 |
compilationResult, err := compileClaimsCELExpression(compiler, &authenticationcel.ExtraMappingExpression{ |
| 372 |
Key: mapping.Key, |
| 373 |
Expression: mapping.ValueExpression, |
| 374 |
}, fldPath.Child("valueExpression")) |
| 375 |
|
| 376 |
if err != nil { |
| 377 |
allErrs = append(allErrs, err) |
| 378 |
continue |
| 379 |
} |
| 380 |
|
| 381 |
if compilationResult != nil { |
| 382 |
extraCompilationResults = append(extraCompilationResults, *compilationResult) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
if structuredAuthnFeatureEnabled && len(extraCompilationResults) > 0 { |
| 387 |
state.mapper.Extra = authenticationcel.NewClaimsMapper(extraCompilationResults) |
| 388 |
state.usesEmailVerifiedClaim = state.usesEmailVerifiedClaim || anyUsesEmailVerifiedClaim(extraCompilationResults) |
| 389 |
} |
| 390 |
|
| 391 |
if structuredAuthnFeatureEnabled && state.usesEmailClaim && !state.usesEmailVerifiedClaim { |
| 392 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("username", "expression"), m.Username.Expression, |
| 393 |
"claims.email_verified must be used in claimMappings.username.expression or claimMappings.extra[*].valueExpression or claimValidationRules[*].expression when claims.email is used in claimMappings.username.expression")) |
| 394 |
} |
| 395 |
|
| 396 |
return allErrs |
| 397 |
} |
| 398 |
|
| 399 |
func isKubernetesDomainPrefix(key string) bool { |
| 400 |
domainPrefix := getDomainPrefix(key) |
| 401 |
if domainPrefix == "kubernetes.io" || strings.HasSuffix(domainPrefix, ".kubernetes.io") { |
| 402 |
return true |
| 403 |
} |
| 404 |
if domainPrefix == "k8s.io" || strings.HasSuffix(domainPrefix, ".k8s.io") { |
| 405 |
return true |
| 406 |
} |
| 407 |
return false |
| 408 |
} |
| 409 |
|
| 410 |
func getDomainPrefix(key string) string { |
| 411 |
if parts := strings.SplitN(key, "/", 2); len(parts) == 2 { |
| 412 |
return parts[0] |
| 413 |
} |
| 414 |
return "" |
| 415 |
} |
| 416 |
|
| 417 |
func usesEmailClaim(ast *celgo.Ast) bool { |
| 418 |
return hasSelectExp(ast.Expr(), "claims", "email") |
| 419 |
} |
| 420 |
|
| 421 |
func anyUsesEmailVerifiedClaim(results []authenticationcel.CompilationResult) bool { |
| 422 |
for _, result := range results { |
| 423 |
if usesEmailVerifiedClaim(result.AST) { |
| 424 |
return true |
| 425 |
} |
| 426 |
} |
| 427 |
return false |
| 428 |
} |
| 429 |
|
| 430 |
func usesEmailVerifiedClaim(ast *celgo.Ast) bool { |
| 431 |
return hasSelectExp(ast.Expr(), "claims", "email_verified") |
| 432 |
} |
| 433 |
|
| 434 |
func hasSelectExp(exp *exprpb.Expr, operand, field string) bool { |
| 435 |
if exp == nil { |
| 436 |
return false |
| 437 |
} |
| 438 |
switch e := exp.ExprKind.(type) { |
| 439 |
case *exprpb.Expr_ConstExpr, |
| 440 |
*exprpb.Expr_IdentExpr: |
| 441 |
return false |
| 442 |
case *exprpb.Expr_SelectExpr: |
| 443 |
s := e.SelectExpr |
| 444 |
if s == nil { |
| 445 |
return false |
| 446 |
} |
| 447 |
if isIdentOperand(s.Operand, operand) && s.Field == field { |
| 448 |
return true |
| 449 |
} |
| 450 |
return hasSelectExp(s.Operand, operand, field) |
| 451 |
case *exprpb.Expr_CallExpr: |
| 452 |
c := e.CallExpr |
| 453 |
if c == nil { |
| 454 |
return false |
| 455 |
} |
| 456 |
if c.Target == nil && c.Function == operators.OptSelect && len(c.Args) == 2 && |
| 457 |
isIdentOperand(c.Args[0], operand) && isConstField(c.Args[1], field) { |
| 458 |
return true |
| 459 |
} |
| 460 |
for _, arg := range c.Args { |
| 461 |
if hasSelectExp(arg, operand, field) { |
| 462 |
return true |
| 463 |
} |
| 464 |
} |
| 465 |
return hasSelectExp(c.Target, operand, field) |
| 466 |
case *exprpb.Expr_ListExpr: |
| 467 |
l := e.ListExpr |
| 468 |
if l == nil { |
| 469 |
return false |
| 470 |
} |
| 471 |
for _, element := range l.Elements { |
| 472 |
if hasSelectExp(element, operand, field) { |
| 473 |
return true |
| 474 |
} |
| 475 |
} |
| 476 |
return false |
| 477 |
case *exprpb.Expr_StructExpr: |
| 478 |
s := e.StructExpr |
| 479 |
if s == nil { |
| 480 |
return false |
| 481 |
} |
| 482 |
for _, entry := range s.Entries { |
| 483 |
if hasSelectExp(entry.GetMapKey(), operand, field) { |
| 484 |
return true |
| 485 |
} |
| 486 |
if hasSelectExp(entry.Value, operand, field) { |
| 487 |
return true |
| 488 |
} |
| 489 |
} |
| 490 |
return false |
| 491 |
case *exprpb.Expr_ComprehensionExpr: |
| 492 |
c := e.ComprehensionExpr |
| 493 |
if c == nil { |
| 494 |
return false |
| 495 |
} |
| 496 |
return hasSelectExp(c.IterRange, operand, field) || |
| 497 |
hasSelectExp(c.AccuInit, operand, field) || |
| 498 |
hasSelectExp(c.LoopCondition, operand, field) || |
| 499 |
hasSelectExp(c.LoopStep, operand, field) || |
| 500 |
hasSelectExp(c.Result, operand, field) |
| 501 |
default: |
| 502 |
return false |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
func isIdentOperand(exp *exprpb.Expr, operand string) bool { |
| 507 |
if len(operand) == 0 { |
| 508 |
return false // sanity check against default values |
| 509 |
} |
| 510 |
id := exp.GetIdentExpr() // does not panic even if exp is nil |
| 511 |
return id != nil && id.Name == operand |
| 512 |
} |
| 513 |
|
| 514 |
func isConstField(exp *exprpb.Expr, field string) bool { |
| 515 |
if len(field) == 0 { |
| 516 |
return false // sanity check against default values |
| 517 |
} |
| 518 |
c := exp.GetConstExpr() // does not panic even if exp is nil |
| 519 |
return c != nil && c.GetStringValue() == field // does not panic even if c is not a string |
| 520 |
} |
| 521 |
|
| 522 |
func validatePrefixClaimOrExpression(compiler authenticationcel.Compiler, mapping api.PrefixedClaimOrExpression, fldPath *field.Path, claimOrExpressionRequired bool) (*authenticationcel.CompilationResult, field.ErrorList) { |
| 523 |
var allErrs field.ErrorList |
| 524 |
|
| 525 |
var compilationResult *authenticationcel.CompilationResult |
| 526 |
switch { |
| 527 |
case len(mapping.Expression) > 0 && len(mapping.Claim) > 0: |
| 528 |
allErrs = append(allErrs, field.Invalid(fldPath, "", "claim and expression can't both be set")) |
| 529 |
case len(mapping.Expression) == 0 && len(mapping.Claim) == 0 && claimOrExpressionRequired: |
| 530 |
allErrs = append(allErrs, field.Required(fldPath, "claim or expression is required")) |
| 531 |
case len(mapping.Expression) > 0: |
| 532 |
var err *field.Error |
| 533 |
|
| 534 |
if mapping.Prefix != nil { |
| 535 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("prefix"), *mapping.Prefix, "prefix can't be set when expression is set")) |
| 536 |
} |
| 537 |
compilationResult, err = compileClaimsCELExpression(compiler, &authenticationcel.ClaimMappingExpression{ |
| 538 |
Expression: mapping.Expression, |
| 539 |
}, fldPath.Child("expression")) |
| 540 |
|
| 541 |
if err != nil { |
| 542 |
allErrs = append(allErrs, err) |
| 543 |
} |
| 544 |
|
| 545 |
case len(mapping.Claim) > 0: |
| 546 |
if mapping.Prefix == nil { |
| 547 |
allErrs = append(allErrs, field.Required(fldPath.Child("prefix"), "prefix is required when claim is set. It can be set to an empty string to disable prefixing")) |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
return compilationResult, allErrs |
| 552 |
} |
| 553 |
|
| 554 |
func validateUserValidationRules(compiler authenticationcel.Compiler, state *validationState, rules []api.UserValidationRule, fldPath *field.Path, structuredAuthnFeatureEnabled bool) field.ErrorList { |
| 555 |
var allErrs field.ErrorList |
| 556 |
var compilationResults []authenticationcel.CompilationResult |
| 557 |
|
| 558 |
if len(rules) > 0 && !structuredAuthnFeatureEnabled { |
| 559 |
allErrs = append(allErrs, field.Invalid(fldPath, "", "user validation rules are not supported when StructuredAuthenticationConfiguration feature gate is disabled")) |
| 560 |
} |
| 561 |
|
| 562 |
seenExpressions := sets.NewString() |
| 563 |
for i, rule := range rules { |
| 564 |
fldPath := fldPath.Index(i) |
| 565 |
|
| 566 |
if len(rule.Expression) == 0 { |
| 567 |
allErrs = append(allErrs, field.Required(fldPath.Child("expression"), "expression is required")) |
| 568 |
continue |
| 569 |
} |
| 570 |
|
| 571 |
if seenExpressions.Has(rule.Expression) { |
| 572 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("expression"), rule.Expression)) |
| 573 |
continue |
| 574 |
} |
| 575 |
seenExpressions.Insert(rule.Expression) |
| 576 |
|
| 577 |
compilationResult, err := compileUserCELExpression(compiler, &authenticationcel.UserValidationCondition{ |
| 578 |
Expression: rule.Expression, |
| 579 |
Message: rule.Message, |
| 580 |
}, fldPath.Child("expression")) |
| 581 |
|
| 582 |
if err != nil { |
| 583 |
allErrs = append(allErrs, err) |
| 584 |
continue |
| 585 |
} |
| 586 |
|
| 587 |
if compilationResult != nil { |
| 588 |
compilationResults = append(compilationResults, *compilationResult) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
if structuredAuthnFeatureEnabled && len(compilationResults) > 0 { |
| 593 |
state.mapper.UserValidationRules = authenticationcel.NewUserMapper(compilationResults) |
| 594 |
} |
| 595 |
|
| 596 |
return allErrs |
| 597 |
} |
| 598 |
|
| 599 |
func compileClaimsCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) { |
| 600 |
compilationResult, err := compiler.CompileClaimsExpression(expression) |
| 601 |
if err != nil { |
| 602 |
return nil, convertCELErrorToValidationError(fldPath, expression.GetExpression(), err) |
| 603 |
} |
| 604 |
return &compilationResult, nil |
| 605 |
} |
| 606 |
|
| 607 |
func compileUserCELExpression(compiler authenticationcel.Compiler, expression authenticationcel.ExpressionAccessor, fldPath *field.Path) (*authenticationcel.CompilationResult, *field.Error) { |
| 608 |
compilationResult, err := compiler.CompileUserExpression(expression) |
| 609 |
if err != nil { |
| 610 |
return nil, convertCELErrorToValidationError(fldPath, expression.GetExpression(), err) |
| 611 |
} |
| 612 |
return &compilationResult, nil |
| 613 |
} |
| 614 |
|
| 615 |
// ValidateAuthorizationConfiguration validates a given AuthorizationConfiguration. |
| 616 |
func ValidateAuthorizationConfiguration(compiler authorizationcel.Compiler, fldPath *field.Path, c *api.AuthorizationConfiguration, knownTypes sets.Set[string], repeatableTypes sets.Set[string]) field.ErrorList { |
| 617 |
allErrs := field.ErrorList{} |
| 618 |
|
| 619 |
if len(c.Authorizers) == 0 { |
| 620 |
allErrs = append(allErrs, field.Required(fldPath.Child("authorizers"), "at least one authorization mode must be defined")) |
| 621 |
} |
| 622 |
|
| 623 |
seenAuthorizerTypes := sets.NewString() |
| 624 |
seenAuthorizerNames := sets.NewString() |
| 625 |
for i, a := range c.Authorizers { |
| 626 |
fldPath := fldPath.Child("authorizers").Index(i) |
| 627 |
aType := string(a.Type) |
| 628 |
if aType == "" { |
| 629 |
allErrs = append(allErrs, field.Required(fldPath.Child("type"), "")) |
| 630 |
continue |
| 631 |
} |
| 632 |
if !knownTypes.Has(aType) { |
| 633 |
allErrs = append(allErrs, field.NotSupported(fldPath.Child("type"), aType, sets.List(knownTypes))) |
| 634 |
continue |
| 635 |
} |
| 636 |
if seenAuthorizerTypes.Has(aType) && !repeatableTypes.Has(aType) { |
| 637 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("type"), aType)) |
| 638 |
continue |
| 639 |
} |
| 640 |
seenAuthorizerTypes.Insert(aType) |
| 641 |
|
| 642 |
if len(a.Name) == 0 { |
| 643 |
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "")) |
| 644 |
} else if seenAuthorizerNames.Has(a.Name) { |
| 645 |
allErrs = append(allErrs, field.Duplicate(fldPath.Child("name"), a.Name)) |
| 646 |
} else if errs := utilvalidation.IsDNS1123Subdomain(a.Name); len(errs) != 0 { |
| 647 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), a.Name, fmt.Sprintf("authorizer name is invalid: %s", strings.Join(errs, ", ")))) |
| 648 |
} |
| 649 |
seenAuthorizerNames.Insert(a.Name) |
| 650 |
|
| 651 |
switch a.Type { |
| 652 |
case api.TypeWebhook: |
| 653 |
if a.Webhook == nil { |
| 654 |
allErrs = append(allErrs, field.Required(fldPath.Child("webhook"), "required when type=Webhook")) |
| 655 |
continue |
| 656 |
} |
| 657 |
allErrs = append(allErrs, ValidateWebhookConfiguration(compiler, fldPath, a.Webhook)...) |
| 658 |
default: |
| 659 |
if a.Webhook != nil { |
| 660 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("webhook"), "non-null", "may only be specified when type=Webhook")) |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
return allErrs |
| 666 |
} |
| 667 |
|
| 668 |
func ValidateWebhookConfiguration(compiler authorizationcel.Compiler, fldPath *field.Path, c *api.WebhookConfiguration) field.ErrorList { |
| 669 |
allErrs := field.ErrorList{} |
| 670 |
|
| 671 |
if c.Timeout.Duration == 0 { |
| 672 |
allErrs = append(allErrs, field.Required(fldPath.Child("timeout"), "")) |
| 673 |
} else if c.Timeout.Duration > 30*time.Second || c.Timeout.Duration < 0 { |
| 674 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("timeout"), c.Timeout.Duration.String(), "must be > 0s and <= 30s")) |
| 675 |
} |
| 676 |
|
| 677 |
if c.AuthorizedTTL.Duration == 0 { |
| 678 |
allErrs = append(allErrs, field.Required(fldPath.Child("authorizedTTL"), "")) |
| 679 |
} else if c.AuthorizedTTL.Duration < 0 { |
| 680 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("authorizedTTL"), c.AuthorizedTTL.Duration.String(), "must be > 0s")) |
| 681 |
} |
| 682 |
|
| 683 |
if c.UnauthorizedTTL.Duration == 0 { |
| 684 |
allErrs = append(allErrs, field.Required(fldPath.Child("unauthorizedTTL"), "")) |
| 685 |
} else if c.UnauthorizedTTL.Duration < 0 { |
| 686 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("unauthorizedTTL"), c.UnauthorizedTTL.Duration.String(), "must be > 0s")) |
| 687 |
} |
| 688 |
|
| 689 |
switch c.SubjectAccessReviewVersion { |
| 690 |
case "": |
| 691 |
allErrs = append(allErrs, field.Required(fldPath.Child("subjectAccessReviewVersion"), "")) |
| 692 |
case "v1": |
| 693 |
_ = &v1.SubjectAccessReview{} |
| 694 |
case "v1beta1": |
| 695 |
_ = &v1beta1.SubjectAccessReview{} |
| 696 |
default: |
| 697 |
allErrs = append(allErrs, field.NotSupported(fldPath.Child("subjectAccessReviewVersion"), c.SubjectAccessReviewVersion, []string{"v1", "v1beta1"})) |
| 698 |
} |
| 699 |
|
| 700 |
switch c.MatchConditionSubjectAccessReviewVersion { |
| 701 |
case "": |
| 702 |
if len(c.MatchConditions) > 0 { |
| 703 |
allErrs = append(allErrs, field.Required(fldPath.Child("matchConditionSubjectAccessReviewVersion"), "required if match conditions are specified")) |
| 704 |
} |
| 705 |
case "v1": |
| 706 |
_ = &v1.SubjectAccessReview{} |
| 707 |
default: |
| 708 |
allErrs = append(allErrs, field.NotSupported(fldPath.Child("matchConditionSubjectAccessReviewVersion"), c.MatchConditionSubjectAccessReviewVersion, []string{"v1"})) |
| 709 |
} |
| 710 |
|
| 711 |
switch c.FailurePolicy { |
| 712 |
case "": |
| 713 |
allErrs = append(allErrs, field.Required(fldPath.Child("failurePolicy"), "")) |
| 714 |
case api.FailurePolicyNoOpinion, api.FailurePolicyDeny: |
| 715 |
default: |
| 716 |
allErrs = append(allErrs, field.NotSupported(fldPath.Child("failurePolicy"), c.FailurePolicy, []string{"NoOpinion", "Deny"})) |
| 717 |
} |
| 718 |
|
| 719 |
switch c.ConnectionInfo.Type { |
| 720 |
case "": |
| 721 |
allErrs = append(allErrs, field.Required(fldPath.Child("connectionInfo", "type"), "")) |
| 722 |
case api.AuthorizationWebhookConnectionInfoTypeInCluster: |
| 723 |
if c.ConnectionInfo.KubeConfigFile != nil { |
| 724 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("connectionInfo", "kubeConfigFile"), *c.ConnectionInfo.KubeConfigFile, "can only be set when type=KubeConfigFile")) |
| 725 |
} |
| 726 |
case api.AuthorizationWebhookConnectionInfoTypeKubeConfigFile: |
| 727 |
if c.ConnectionInfo.KubeConfigFile == nil || *c.ConnectionInfo.KubeConfigFile == "" { |
| 728 |
allErrs = append(allErrs, field.Required(fldPath.Child("connectionInfo", "kubeConfigFile"), "")) |
| 729 |
} else if !filepath.IsAbs(*c.ConnectionInfo.KubeConfigFile) { |
| 730 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("connectionInfo", "kubeConfigFile"), *c.ConnectionInfo.KubeConfigFile, "must be an absolute path")) |
| 731 |
} else if info, err := os.Stat(*c.ConnectionInfo.KubeConfigFile); err != nil { |
| 732 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("connectionInfo", "kubeConfigFile"), *c.ConnectionInfo.KubeConfigFile, fmt.Sprintf("error loading file: %v", err))) |
| 733 |
} else if !info.Mode().IsRegular() { |
| 734 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("connectionInfo", "kubeConfigFile"), *c.ConnectionInfo.KubeConfigFile, "must be a regular file")) |
| 735 |
} |
| 736 |
default: |
| 737 |
allErrs = append(allErrs, field.NotSupported(fldPath.Child("connectionInfo", "type"), c.ConnectionInfo, []string{api.AuthorizationWebhookConnectionInfoTypeInCluster, api.AuthorizationWebhookConnectionInfoTypeKubeConfigFile})) |
| 738 |
} |
| 739 |
|
| 740 |
_, errs := compileMatchConditions(compiler, c.MatchConditions, fldPath, utilfeature.DefaultFeatureGate.Enabled(features.StructuredAuthorizationConfiguration)) |
| 741 |
allErrs = append(allErrs, errs...) |
| 742 |
|
| 743 |
return allErrs |
| 744 |
} |
| 745 |
|
| 746 |
// ValidateAndCompileMatchConditions validates a given webhook's matchConditions. |
| 747 |
// This is exported for use in authz package. |
| 748 |
func ValidateAndCompileMatchConditions(compiler authorizationcel.Compiler, matchConditions []api.WebhookMatchCondition) (*authorizationcel.CELMatcher, field.ErrorList) { |
| 749 |
return compileMatchConditions(compiler, matchConditions, nil, utilfeature.DefaultFeatureGate.Enabled(features.StructuredAuthorizationConfiguration)) |
| 750 |
} |
| 751 |
|
| 752 |
func compileMatchConditions(compiler authorizationcel.Compiler, matchConditions []api.WebhookMatchCondition, fldPath *field.Path, structuredAuthzFeatureEnabled bool) (*authorizationcel.CELMatcher, field.ErrorList) { |
| 753 |
var allErrs field.ErrorList |
| 754 |
// should fail when match conditions are used without feature enabled |
| 755 |
if len(matchConditions) > 0 && !structuredAuthzFeatureEnabled { |
| 756 |
allErrs = append(allErrs, field.Invalid(fldPath.Child("matchConditions"), "", "matchConditions are not supported when StructuredAuthorizationConfiguration feature gate is disabled")) |
| 757 |
} |
| 758 |
if len(matchConditions) > 64 { |
| 759 |
allErrs = append(allErrs, field.TooMany(fldPath.Child("matchConditions"), len(matchConditions), 64)) |
| 760 |
return nil, allErrs |
| 761 |
} |
| 762 |
|
| 763 |
seenExpressions := sets.NewString() |
| 764 |
var compilationResults []authorizationcel.CompilationResult |
| 765 |
var usesFieldSelector, usesLabelSelector bool |
| 766 |
|
| 767 |
for i, condition := range matchConditions { |
| 768 |
fldPath := fldPath.Child("matchConditions").Index(i).Child("expression") |
| 769 |
if len(strings.TrimSpace(condition.Expression)) == 0 { |
| 770 |
allErrs = append(allErrs, field.Required(fldPath, "")) |
| 771 |
continue |
| 772 |
} |
| 773 |
if seenExpressions.Has(condition.Expression) { |
| 774 |
allErrs = append(allErrs, field.Duplicate(fldPath, condition.Expression)) |
| 775 |
continue |
| 776 |
} |
| 777 |
seenExpressions.Insert(condition.Expression) |
| 778 |
compilationResult, err := compileMatchConditionsExpression(fldPath, compiler, condition.Expression) |
| 779 |
if err != nil { |
| 780 |
allErrs = append(allErrs, err) |
| 781 |
continue |
| 782 |
} |
| 783 |
compilationResults = append(compilationResults, compilationResult) |
| 784 |
usesFieldSelector = usesFieldSelector || compilationResult.UsesFieldSelector |
| 785 |
usesLabelSelector = usesLabelSelector || compilationResult.UsesLabelSelector |
| 786 |
} |
| 787 |
if len(compilationResults) == 0 { |
| 788 |
return nil, allErrs |
| 789 |
} |
| 790 |
return &authorizationcel.CELMatcher{ |
| 791 |
CompilationResults: compilationResults, |
| 792 |
UsesFieldSelector: usesFieldSelector, |
| 793 |
UsesLabelSelector: usesLabelSelector, |
| 794 |
}, allErrs |
| 795 |
} |
| 796 |
|
| 797 |
func compileMatchConditionsExpression(fldPath *field.Path, compiler authorizationcel.Compiler, expression string) (authorizationcel.CompilationResult, *field.Error) { |
| 798 |
authzExpression := &authorizationcel.SubjectAccessReviewMatchCondition{ |
| 799 |
Expression: expression, |
| 800 |
} |
| 801 |
compilationResult, err := compiler.CompileCELExpression(authzExpression) |
| 802 |
if err != nil { |
| 803 |
return compilationResult, convertCELErrorToValidationError(fldPath, authzExpression.GetExpression(), err) |
| 804 |
} |
| 805 |
return compilationResult, nil |
| 806 |
} |
| 807 |
|
| 808 |
func convertCELErrorToValidationError(fldPath *field.Path, expression string, err error) *field.Error { |
| 809 |
var celErr *cel.Error |
| 810 |
if errors.As(err, &celErr) { |
| 811 |
switch celErr.Type { |
| 812 |
case cel.ErrorTypeRequired: |
| 813 |
return field.Required(fldPath, celErr.Detail) |
| 814 |
case cel.ErrorTypeInvalid: |
| 815 |
return field.Invalid(fldPath, expression, celErr.Detail) |
| 816 |
default: |
| 817 |
return field.InternalError(fldPath, celErr) |
| 818 |
} |
| 819 |
} |
| 820 |
return field.InternalError(fldPath, fmt.Errorf("error is not cel error: %w", err)) |
| 821 |
} |