Skip to content

Commit 06efa8c

Browse files
committed
fix: relax jwtAuth private_key requirement and add CEL validation
Previously, private_key was marked as required in the CRD schema, which rejected symmetric JWT configurations (e.g. HS256 + secret) before they reached the translator. Changes: - Add omitempty to PrivateKey JSON tag so the CRD schema no longer requires it unconditionally - Add CEL validation rule: asymmetric algorithms (RS*/ES*/PS*/EdDSA) require at least one of public_key or private_key; symmetric algorithms (HS256/HS384/HS512 or unset) have no key requirement - Regenerate CRD manifest Fixes #400
1 parent b02e842 commit 06efa8c

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

api/v2/apisixconsumer_types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +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.
136+
//
137+
// +kubebuilder:validation:XValidation:rule="!has(self.algorithm) || self.algorithm in ['HS256','HS384','HS512'] || (has(self.public_key) && self.public_key != ”) || (has(self.private_key) && self.private_key != ”)",message="asymmetric JWT algorithms (RS*/ES*/PS*/EdDSA) require at least one of public_key or private_key"
133138
type ApisixConsumerJwtAuthValue struct {
134139
// Key is the unique identifier for the JWT credential.
135140
Key string `json:"key" yaml:"key"`
@@ -138,7 +143,7 @@ type ApisixConsumerJwtAuthValue struct {
138143
// PublicKey is the public key used to verify JWT signatures (for asymmetric algorithms).
139144
PublicKey string `json:"public_key,omitempty" yaml:"public_key,omitempty"`
140145
// PrivateKey is the private key used to sign the JWT (for asymmetric algorithms).
141-
PrivateKey string `json:"private_key" yaml:"private_key,omitempty"`
146+
PrivateKey string `json:"private_key,omitempty" yaml:"private_key,omitempty"`
142147
// Algorithm specifies the signing algorithm.
143148
// Can be `HS256`, `HS384`, `HS512`, `RS256`, `RS384`, `RS512`, `ES256`, `ES384`, `ES512`, `PS256`, `PS384`, `PS512`, or `EdDSA`.
144149
// Currently APISIX only supports `HS256`, `HS512`, `RS256`, and `ES256`. API7 Enterprise supports all algorithms.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,13 @@ spec:
210210
type: string
211211
required:
212212
- key
213-
- private_key
214213
type: object
214+
x-kubernetes-validations:
215+
- message: asymmetric JWT algorithms (RS*/ES*/PS*/EdDSA) require
216+
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) && self.public_key != ”) || (has(self.private_key)
219+
&& self.private_key != ”)'
215220
type: object
216221
keyAuth:
217222
description: KeyAuth configures the key authentication details.

0 commit comments

Comments
 (0)