Skip to content

Commit 81e6368

Browse files
committed
add test, fix template, remove pointers, generate crd
Signed-off-by: Haywood Shannon <[email protected]> Signed-off-by: Haywood Shannon <[email protected]>
1 parent 029e02e commit 81e6368

File tree

7 files changed

+335
-18
lines changed

7 files changed

+335
-18
lines changed

config/crd/bases/k8s.nginx.org_policies.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ spec:
179179
properties:
180180
burst:
181181
type: integer
182+
condition:
183+
properties:
184+
default:
185+
type: boolean
186+
jwt:
187+
properties:
188+
claim:
189+
type: string
190+
match:
191+
type: string
192+
type: object
193+
type: object
182194
delay:
183195
type: integer
184196
dryRun:

deploy/crds.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ spec:
341341
properties:
342342
burst:
343343
type: integer
344+
condition:
345+
properties:
346+
default:
347+
type: boolean
348+
jwt:
349+
properties:
350+
claim:
351+
type: string
352+
match:
353+
type: string
354+
type: object
355+
type: object
344356
delay:
345357
type: integer
346358
dryRun:

internal/configs/version2/__snapshots__/templates_test.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ server {
22632263

22642264
[TestExecuteVirtualServerTemplate_RendersTemplateWithRateLimitJWTClaim - 1]
22652265

2266-
auth_jwt_claim_set $jwt_default_webapp_group_consumer_group_type consumer_group type
2266+
auth_jwt_claim_set $jwt_default_webapp_group_consumer_group_type consumer_group type;
22672267
map $jwt_default_webapp_group_consumer_group_type $rate_limit_default_webapp_group_consumer_group_type {
22682268
default Group3;
22692269
Gold Group1;

internal/configs/version2/nginx-plus.virtualserver.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ split_clients {{ $sc.Source }} {{ $sc.Variable }} {
5151
{{- end }}
5252

5353
{{- range $claim := .AuthJWTClaimSets }}
54-
auth_jwt_claim_set {{ $claim.Variable }} {{ $claim.Claim}}
54+
auth_jwt_claim_set {{ $claim.Variable }} {{ $claim.Claim}};
5555
{{- end }}
5656

5757
{{- range $m := .Maps }}

internal/configs/virtualserver.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
453453
var statusMatches []version2.StatusMatch
454454
var healthChecks []version2.HealthCheck
455455
var limitReqZones []version2.LimitReqZone
456-
var authJWTClaimSets []*version2.AuthJWTClaimSet
456+
var authJWTClaimSets []version2.AuthJWTClaimSet
457457

458458
limitReqZones = append(limitReqZones, policiesCfg.RateLimit.Zones...)
459459

@@ -695,7 +695,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
695695
}
696696

697697
locSnippets := r.LocationSnippets
698-
// use the VirtualServer location snippet if the route does not define any
698+
// use the VirtualServer location snippet if the route does not define any
699699
if r.LocationSnippets == "" {
700700
locSnippets = vsrLocationSnippetsFromVs[vsrNamespaceName]
701701
}
@@ -926,7 +926,7 @@ type policiesCfg struct {
926926
Deny []string
927927
RateLimit rateLimit
928928
JWTAuth jwtAuth
929-
AuthJWTClaimSets []*version2.AuthJWTClaimSet
929+
AuthJWTClaimSets []version2.AuthJWTClaimSet
930930
BasicAuth *version2.BasicAuth
931931
IngressMTLS *version2.IngressMTLS
932932
EgressMTLS *version2.EgressMTLS
@@ -1020,8 +1020,8 @@ func (p *policiesCfg) addRateLimitConfig(
10201020
rlZoneName := fmt.Sprintf("pol_rl_%v_%v_%v_%v", polNamespace, polName, vsNamespace, vsName)
10211021
p.RateLimit.Reqs = append(p.RateLimit.Reqs, generateLimitReq(rlZoneName, rateLimit))
10221022
p.RateLimit.Zones = append(p.RateLimit.Zones, generateLimitReqZone(rlZoneName, rateLimit, podReplicas))
1023-
if rateLimit.Condition != nil && rateLimit.Condition.JWT != nil {
1024-
p.AuthJWTClaimSets = append(p.AuthJWTClaimSets, generateAuthJwtClaimSet(*rateLimit.Condition.JWT, vsNamespace, vsName))
1023+
if rateLimit.Condition != nil && rateLimit.Condition.JWT.Claim != "" && rateLimit.Condition.JWT.Match != "" {
1024+
p.AuthJWTClaimSets = append(p.AuthJWTClaimSets, generateAuthJwtClaimSet(rateLimit.Condition.JWT, vsNamespace, vsName))
10251025
}
10261026
if len(p.RateLimit.Reqs) == 1 {
10271027
p.RateLimit.Options = generateLimitReqOptions(rateLimit)
@@ -1679,30 +1679,30 @@ func removeDuplicateLimitReqZones(rlz []version2.LimitReqZone) []version2.LimitR
16791679
return result
16801680
}
16811681

1682-
func removeDuplicateAuthJWTClaimSets(ajcs []*version2.AuthJWTClaimSet) []version2.AuthJWTClaimSet {
1682+
func removeDuplicateAuthJWTClaimSets(ajcs []version2.AuthJWTClaimSet) []version2.AuthJWTClaimSet {
16831683
encountered := make(map[string]bool)
16841684
var result []version2.AuthJWTClaimSet
16851685

16861686
for _, v := range ajcs {
16871687
if !encountered[v.Variable] {
16881688
encountered[v.Variable] = true
1689-
result = append(result, *v)
1689+
result = append(result, v)
16901690
}
16911691
}
16921692

16931693
return result
16941694
}
16951695

1696-
func generateAuthJwtClaimSet(jwtCondition conf_v1.JWTCondition, vsNamespace string, vsName string) *version2.AuthJWTClaimSet {
1697-
return &version2.AuthJWTClaimSet{
1696+
func generateAuthJwtClaimSet(jwtCondition conf_v1.JWTCondition, vsNamespace string, vsName string) version2.AuthJWTClaimSet {
1697+
return version2.AuthJWTClaimSet{
16981698
Variable: generateAuthJwtClaimSetVariable(jwtCondition.Claim, vsNamespace, vsName),
16991699
Claim: generateAuthJwtClaimSetClaim(jwtCondition.Claim),
17001700
}
17011701
}
17021702

17031703
// TODO: process claim with spaces
17041704
func generateAuthJwtClaimSetVariable(claim string, vsNamespace string, vsName string) string {
1705-
return fmt.Sprintf("jwt_%v_%v_%v", vsNamespace, vsName, strings.Join(strings.Split(claim, "."), "_"))
1705+
return fmt.Sprintf("$jwt_%v_%v_%v", vsNamespace, vsName, strings.Join(strings.Split(claim, "."), "_"))
17061706
}
17071707

17081708
// TODO: process claim with spaces

0 commit comments

Comments
 (0)