-
Notifications
You must be signed in to change notification settings - Fork 4
fix: relax jwtAuth private_key requirement and add CEL validation #406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlinsRan
wants to merge
8
commits into
master
Choose a base branch
from
fix/jwt-auth-private-key-required
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
06efa8c
fix: relax jwtAuth private_key requirement and add CEL validation
AlinsRan b50d0f6
test: add CEL validation unit tests for ApisixConsumerJwtAuthValue
AlinsRan 6073371
fix: use single quotes in CEL rule to avoid Unicode quote corruption
AlinsRan 9e62471
test: rewrite validation tests using full ApisixConsumer CRD schema
AlinsRan 97af003
fix: use size() in CEL rule to avoid gofmt Unicode quote conversion
AlinsRan f0132e6
docs: regenerate CRD reference docs for ApisixConsumerJwtAuthValue
AlinsRan f61e666
fix: address PR review comments
AlinsRan d8c4d62
fix: tighten CEL rule to reject whitespace-only keys, fix ADC omitempty
AlinsRan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,337 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package v2_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" | ||
| "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel" | ||
| "k8s.io/apiextensions-apiserver/pkg/apiserver/validation" | ||
| celconfig "k8s.io/apiserver/pkg/apis/cel" | ||
| sigsyaml "sigs.k8s.io/yaml" | ||
|
|
||
| apisixv2 "github.com/apache/apisix-ingress-controller/api/v2" | ||
| ) | ||
|
|
||
| // consumerSchemaValidator holds the parsed CRD schema for ApisixConsumer | ||
| // and provides a Validate method for use in tests. | ||
| type consumerSchemaValidator struct { | ||
| structural *structuralschema.Structural | ||
| internal *apiextensions.JSONSchemaProps | ||
| } | ||
|
|
||
| func (v *consumerSchemaValidator) Validate(t *testing.T, ac *apisixv2.ApisixConsumer) error { | ||
| t.Helper() | ||
|
|
||
| data, err := json.Marshal(ac) | ||
| require.NoError(t, err, "failed to marshal ApisixConsumer") | ||
|
|
||
| var obj map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(data, &obj), "failed to unmarshal to map") | ||
|
|
||
| schemaValidator, _, err := validation.NewSchemaValidator(v.internal) | ||
| require.NoError(t, err, "failed to build schema validator") | ||
|
|
||
| if errs := validation.ValidateCustomResource(nil, obj, schemaValidator); len(errs) > 0 { | ||
| return errs.ToAggregate() | ||
| } | ||
|
|
||
| celValidator := cel.NewValidator(v.structural, false, celconfig.PerCallLimit) | ||
| celErrs, _ := celValidator.Validate(context.Background(), nil, v.structural, obj, nil, celconfig.RuntimeCELCostBudget) | ||
| if len(celErrs) > 0 { | ||
| return celErrs.ToAggregate() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // loadApisixConsumerSchema reads the ApisixConsumer CRD YAML and returns a | ||
| // validator backed by the real generated schema. | ||
| func loadApisixConsumerSchema(t *testing.T) *consumerSchemaValidator { | ||
| t.Helper() | ||
|
|
||
| _, thisFile, _, _ := runtime.Caller(0) | ||
| crdPath := filepath.Join(filepath.Dir(thisFile), "..", "..", | ||
| "config", "crd", "bases", "apisix.apache.org_apisixconsumers.yaml") | ||
|
|
||
| data, err := os.ReadFile(crdPath) | ||
| require.NoError(t, err, "failed to read CRD file: %s", crdPath) | ||
|
|
||
| jsonData, err := sigsyaml.YAMLToJSON(data) | ||
| require.NoError(t, err, "failed to convert CRD YAML to JSON") | ||
|
|
||
| var crd apiextensionsv1.CustomResourceDefinition | ||
| require.NoError(t, json.Unmarshal(jsonData, &crd), "failed to unmarshal CRD") | ||
|
|
||
| var v1Schema *apiextensionsv1.JSONSchemaProps | ||
| for _, v := range crd.Spec.Versions { | ||
| if v.Name == "v2" { | ||
| v1Schema = v.Schema.OpenAPIV3Schema | ||
| break | ||
| } | ||
| } | ||
| require.NotNil(t, v1Schema, "v2 schema not found in CRD") | ||
|
|
||
| var internal apiextensions.JSONSchemaProps | ||
| require.NoError(t, | ||
| apiextensionsv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(v1Schema, &internal, nil), | ||
| "failed to convert v1 schema to internal", | ||
| ) | ||
|
|
||
| structural, err := structuralschema.NewStructural(&internal) | ||
| require.NoError(t, err, "failed to build structural schema") | ||
| return &consumerSchemaValidator{structural: structural, internal: &internal} | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_SymmetricHS256(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Secret: "my-secret", | ||
| Algorithm: "HS256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| // TestApisixConsumer_JwtAuth_AsymmetricWithWhitespaceOnlyPublicKey verifies | ||
| // that a whitespace-only public_key is treated as absent and rejected for | ||
| // asymmetric algorithms. | ||
| func TestApisixConsumer_JwtAuth_AsymmetricWithWhitespaceOnlyPublicKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Algorithm: "RS256", | ||
| PublicKey: " ", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := v.Validate(t, ac) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "algorithms other than HS256/HS384/HS512") | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_SymmetricHS512(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Secret: "my-secret", | ||
| Algorithm: "HS512", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_NoAlgorithmDefaultsToSymmetric(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Secret: "my-secret", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricRS256WithPublicKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| PublicKey: "test-public-key", | ||
| Algorithm: "RS256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricRS256WithPrivateKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| PrivateKey: "test-private-key", | ||
| Algorithm: "RS256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricRS256WithBothKeys(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| PublicKey: "test-public-key", | ||
| PrivateKey: "test-private-key", | ||
| Algorithm: "RS256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricRS256WithoutAnyKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Algorithm: "RS256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := v.Validate(t, ac) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "algorithms other than HS256/HS384/HS512") | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricES256WithoutAnyKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Algorithm: "ES256", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := v.Validate(t, ac) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "algorithms other than HS256/HS384/HS512") | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricEdDSAWithoutAnyKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Algorithm: "EdDSA", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| err := v.Validate(t, ac) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "algorithms other than HS256/HS384/HS512") | ||
| } | ||
|
|
||
| func TestApisixConsumer_JwtAuth_AsymmetricWithEmptyPublicKey(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Algorithm: "RS256", | ||
| // PublicKey is empty string — omitempty means it won't appear | ||
| // in the serialized JSON, same effect as not set | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
Comment on lines
+298
to
+311
|
||
| err := v.Validate(t, ac) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "algorithms other than HS256/HS384/HS512") | ||
| } | ||
|
|
||
| // TestApisixConsumer_JwtAuth_EmptyAlgorithmTreatedAsSymmetric verifies that an | ||
| // explicitly empty algorithm string is treated the same as an unset algorithm | ||
| // (defaults to HS256) and does not require public_key or private_key. | ||
| func TestApisixConsumer_JwtAuth_EmptyAlgorithmTreatedAsSymmetric(t *testing.T) { | ||
| v := loadApisixConsumerSchema(t) | ||
| ac := &apisixv2.ApisixConsumer{ | ||
| Spec: apisixv2.ApisixConsumerSpec{ | ||
| AuthParameter: apisixv2.ApisixConsumerAuthParameter{ | ||
| JwtAuth: &apisixv2.ApisixConsumerJwtAuth{ | ||
| Value: &apisixv2.ApisixConsumerJwtAuthValue{ | ||
| Key: "my-key", | ||
| Secret: "my-secret", | ||
| // Algorithm is explicitly empty string — should be treated as | ||
| // unset and not require asymmetric keys. | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| assert.NoError(t, v.Validate(t, ac)) | ||
|
Comment on lines
+317
to
+336
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.