Skip to content

Commit f61e666

Browse files
committed
fix: address PR review comments
- Reword doc comment: remove misleading 'Exactly one' phrasing; clarify that only asymmetric algorithms require at least one key - Extend CEL rule to treat empty algorithm string (size == 0) as symmetric, consistent with APISIX's default of HS256 - Replace PEM-like key fixtures with simple placeholders to avoid secret scanner false positives - Add TestApisixConsumer_JwtAuth_EmptyAlgorithmTreatedAsSymmetric - Regenerate CRD manifest and reference docs
1 parent f0132e6 commit f61e666

4 files changed

Lines changed: 37 additions & 15 deletions

File tree

api/v2/apisixconsumer_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ type ApisixConsumerJwtAuth struct {
130130
}
131131

132132
// ApisixConsumerJwtAuthValue defines configuration for JWT authentication.
133-
// Exactly one of the following must be provided depending on the algorithm:
134-
// - For symmetric algorithms (HS256, HS384, HS512): use secret. private_key and public_key are not required.
135-
// - For asymmetric algorithms (RS*, ES*, PS*, EdDSA): at least one of public_key or private_key must be provided.
133+
// For asymmetric algorithms (RS*, ES*, PS*, EdDSA), at least one of public_key
134+
// or private_key must be provided. Symmetric algorithms (HS256, HS384, HS512)
135+
// and unset algorithm do not require any key field.
136136
//
137-
// +kubebuilder:validation:XValidation:rule="!has(self.algorithm) || self.algorithm in ['HS256','HS384','HS512'] || (has(self.public_key) && size(self.public_key) > 0) || (has(self.private_key) && size(self.private_key) > 0)",message="asymmetric JWT algorithms (RS*/ES*/PS*/EdDSA) require at least one of public_key or private_key"
137+
// +kubebuilder:validation:XValidation:rule="!has(self.algorithm) || size(self.algorithm) == 0 || self.algorithm in ['HS256','HS384','HS512'] || (has(self.public_key) && size(self.public_key) > 0) || (has(self.private_key) && size(self.private_key) > 0)",message="asymmetric JWT algorithms (RS*/ES*/PS*/EdDSA) require at least one of public_key or private_key"
138138
type ApisixConsumerJwtAuthValue struct {
139139
// Key is the unique identifier for the JWT credential.
140140
Key string `json:"key" yaml:"key"`

api/v2/apisixconsumer_validation_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestApisixConsumer_JwtAuth_AsymmetricRS256WithPublicKey(t *testing.T) {
184184
JwtAuth: &apisixv2.ApisixConsumerJwtAuth{
185185
Value: &apisixv2.ApisixConsumerJwtAuthValue{
186186
Key: "my-key",
187-
PublicKey: "-----BEGIN PUBLIC KEY-----\nMFww\n-----END PUBLIC KEY-----",
187+
PublicKey: "test-public-key",
188188
Algorithm: "RS256",
189189
},
190190
},
@@ -202,7 +202,7 @@ func TestApisixConsumer_JwtAuth_AsymmetricRS256WithPrivateKey(t *testing.T) {
202202
JwtAuth: &apisixv2.ApisixConsumerJwtAuth{
203203
Value: &apisixv2.ApisixConsumerJwtAuthValue{
204204
Key: "my-key",
205-
PrivateKey: "-----BEGIN RSA PRIVATE KEY-----\nMIIE\n-----END RSA PRIVATE KEY-----",
205+
PrivateKey: "test-private-key",
206206
Algorithm: "RS256",
207207
},
208208
},
@@ -220,8 +220,8 @@ func TestApisixConsumer_JwtAuth_AsymmetricRS256WithBothKeys(t *testing.T) {
220220
JwtAuth: &apisixv2.ApisixConsumerJwtAuth{
221221
Value: &apisixv2.ApisixConsumerJwtAuthValue{
222222
Key: "my-key",
223-
PublicKey: "-----BEGIN PUBLIC KEY-----\nMFww\n-----END PUBLIC KEY-----",
224-
PrivateKey: "-----BEGIN RSA PRIVATE KEY-----\nMIIE\n-----END RSA PRIVATE KEY-----",
223+
PublicKey: "test-public-key",
224+
PrivateKey: "test-private-key",
225225
Algorithm: "RS256",
226226
},
227227
},
@@ -308,3 +308,25 @@ func TestApisixConsumer_JwtAuth_AsymmetricWithEmptyPublicKey(t *testing.T) {
308308
require.Error(t, err)
309309
assert.Contains(t, err.Error(), "asymmetric JWT algorithms")
310310
}
311+
312+
// TestApisixConsumer_JwtAuth_EmptyAlgorithmTreatedAsSymmetric verifies that an
313+
// explicitly empty algorithm string is treated the same as an unset algorithm
314+
// (defaults to HS256) and does not require public_key or private_key.
315+
func TestApisixConsumer_JwtAuth_EmptyAlgorithmTreatedAsSymmetric(t *testing.T) {
316+
v := loadApisixConsumerSchema(t)
317+
ac := &apisixv2.ApisixConsumer{
318+
Spec: apisixv2.ApisixConsumerSpec{
319+
AuthParameter: apisixv2.ApisixConsumerAuthParameter{
320+
JwtAuth: &apisixv2.ApisixConsumerJwtAuth{
321+
Value: &apisixv2.ApisixConsumerJwtAuthValue{
322+
Key: "my-key",
323+
Secret: "my-secret",
324+
// Algorithm is explicitly empty string — should be treated as
325+
// unset and not require asymmetric keys.
326+
},
327+
},
328+
},
329+
},
330+
}
331+
assert.NoError(t, v.Validate(t, ac))
332+
}

config/crd/bases/apisix.apache.org_apisixconsumers.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ spec:
214214
x-kubernetes-validations:
215215
- message: asymmetric JWT algorithms (RS*/ES*/PS*/EdDSA) require
216216
at least one of public_key or private_key
217-
rule: '!has(self.algorithm) || self.algorithm in [''HS256'',''HS384'',''HS512'']
218-
|| (has(self.public_key) && size(self.public_key) > 0)
219-
|| (has(self.private_key) && size(self.private_key) >
220-
0)'
217+
rule: '!has(self.algorithm) || size(self.algorithm) == 0
218+
|| self.algorithm in [''HS256'',''HS384'',''HS512''] ||
219+
(has(self.public_key) && size(self.public_key) > 0) ||
220+
(has(self.private_key) && size(self.private_key) > 0)'
221221
type: object
222222
keyAuth:
223223
description: KeyAuth configures the key authentication details.

docs/en/latest/reference/api-reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,9 @@ _Appears in:_
781781

782782

783783
ApisixConsumerJwtAuthValue defines configuration for JWT authentication.
784-
Exactly one of the following must be provided depending on the algorithm:
785-
- For symmetric algorithms (HS256, HS384, HS512): use secret. private_key and public_key are not required.
786-
- For asymmetric algorithms (RS*, ES*, PS*, EdDSA): at least one of public_key or private_key must be provided.
784+
For asymmetric algorithms (RS*, ES*, PS*, EdDSA), at least one of public_key
785+
or private_key must be provided. Symmetric algorithms (HS256, HS384, HS512)
786+
and unset algorithm do not require any key field.
787787

788788

789789

0 commit comments

Comments
 (0)