@@ -416,18 +416,18 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
416
416
vsNamespace : vsEx .VirtualServer .Namespace ,
417
417
vsName : vsEx .VirtualServer .Name ,
418
418
}
419
- policiesCfg , mapsFromPolicies := vsc .generatePolicies (ownerDetails , vsEx .VirtualServer .Spec .Policies , vsEx .Policies , specContext , policyOpts )
420
-
421
- maps = append (maps , mapsFromPolicies ... )
419
+ policiesCfg := vsc .generatePolicies (ownerDetails , vsEx .VirtualServer .Spec .Policies , vsEx .Policies , specContext , policyOpts )
422
420
if policiesCfg .JWKSAuthEnabled {
423
421
jwtAuthKey := policiesCfg .JWTAuth .Key
424
422
policiesCfg .JWTAuthList = make (map [string ]* version2.JWTAuth )
425
423
policiesCfg .JWTAuthList [jwtAuthKey ] = policiesCfg .JWTAuth
426
424
}
427
- //
428
- //if policiesCfg.APIKey {
429
- // apiKey := policiesCfg.APIKey.MapName
430
- //}
425
+
426
+ if policiesCfg .APIKeyEnabled {
427
+ apiMapName := policiesCfg .APIKey .MapName
428
+ policiesCfg .APIKeyClientMap = make (map [string ][]APIKeyClient )
429
+ policiesCfg .APIKeyClientMap [apiMapName ] = policiesCfg .APIKeyClients
430
+ }
431
431
432
432
dosCfg := generateDosCfg (dosResources ["" ])
433
433
@@ -571,9 +571,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
571
571
vsNamespace : vsEx .VirtualServer .Namespace ,
572
572
vsName : vsEx .VirtualServer .Name ,
573
573
}
574
- routePoliciesCfg , mapsFromPolicies := vsc .generatePolicies (ownerDetails , r .Policies , vsEx .Policies , routeContext , policyOpts )
575
- maps = append (maps , mapsFromPolicies ... )
576
-
574
+ routePoliciesCfg := vsc .generatePolicies (ownerDetails , r .Policies , vsEx .Policies , routeContext , policyOpts )
577
575
if policiesCfg .OIDC {
578
576
routePoliciesCfg .OIDC = policiesCfg .OIDC
579
577
}
@@ -589,6 +587,16 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
589
587
policiesCfg .JWTAuthList [jwtAuthKey ] = routePoliciesCfg .JWTAuth
590
588
}
591
589
}
590
+ if routePoliciesCfg .APIKeyEnabled {
591
+ policiesCfg .APIKeyEnabled = routePoliciesCfg .APIKeyEnabled
592
+ apiMapName := routePoliciesCfg .APIKey .MapName
593
+ if policiesCfg .APIKeyClientMap == nil {
594
+ policiesCfg .APIKeyClientMap = make (map [string ][]APIKeyClient )
595
+ }
596
+ if _ , exists := policiesCfg .APIKeyClientMap [apiMapName ]; ! exists {
597
+ policiesCfg .APIKeyClientMap [apiMapName ] = routePoliciesCfg .APIKeyClients
598
+ }
599
+ }
592
600
limitReqZones = append (limitReqZones , routePoliciesCfg .LimitReqZones ... )
593
601
594
602
dosRouteCfg := generateDosCfg (dosResources [r .Path ])
@@ -703,8 +711,7 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
703
711
policyRefs = r .Policies
704
712
context = subRouteContext
705
713
}
706
- routePoliciesCfg , mapsFromPolicies := vsc .generatePolicies (ownerDetails , policyRefs , vsEx .Policies , context , policyOpts )
707
- maps = append (maps , mapsFromPolicies ... )
714
+ routePoliciesCfg := vsc .generatePolicies (ownerDetails , policyRefs , vsEx .Policies , context , policyOpts )
708
715
if policiesCfg .OIDC {
709
716
routePoliciesCfg .OIDC = policiesCfg .OIDC
710
717
}
@@ -720,6 +727,17 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
720
727
policiesCfg .JWTAuthList [jwtAuthKey ] = routePoliciesCfg .JWTAuth
721
728
}
722
729
}
730
+ if routePoliciesCfg .APIKeyEnabled {
731
+ policiesCfg .APIKeyEnabled = routePoliciesCfg .APIKeyEnabled
732
+ apiMapName := routePoliciesCfg .APIKey .MapName
733
+ if policiesCfg .APIKeyClientMap == nil {
734
+ policiesCfg .APIKeyClientMap = make (map [string ][]APIKeyClient )
735
+ }
736
+ if _ , exists := policiesCfg .APIKeyClientMap [apiMapName ]; ! exists {
737
+ policiesCfg .APIKeyClientMap [apiMapName ] = routePoliciesCfg .APIKeyClients
738
+ }
739
+ }
740
+
723
741
limitReqZones = append (limitReqZones , routePoliciesCfg .LimitReqZones ... )
724
742
725
743
dosRouteCfg := generateDosCfg (dosResources [r .Path ])
@@ -787,6 +805,10 @@ func (vsc *virtualServerConfigurator) GenerateVirtualServerConfig(
787
805
}
788
806
}
789
807
808
+ for mapName , apiKeyClients := range policiesCfg .APIKeyClientMap {
809
+ maps = append (maps , * generateAPIKeyClientMap (mapName , apiKeyClients ))
810
+ }
811
+
790
812
httpSnippets := generateSnippets (vsc .enableSnippets , vsEx .VirtualServer .Spec .HTTPSnippets , []string {})
791
813
serverSnippets := generateSnippets (
792
814
vsc .enableSnippets ,
@@ -870,8 +892,10 @@ type policiesCfg struct {
870
892
IngressMTLS * version2.IngressMTLS
871
893
EgressMTLS * version2.EgressMTLS
872
894
OIDC bool
895
+ APIKeyEnabled bool
873
896
APIKey * version2.APIKey
874
- APIKeyList map [string ]* version2.APIKey
897
+ APIKeyClients []APIKeyClient
898
+ APIKeyClientMap map [string ][]APIKeyClient
875
899
WAF * version2.WAF
876
900
ErrorReturn * version2.Return
877
901
BundleValidator bundleValidator
@@ -886,6 +910,11 @@ type internalBundleValidator struct {
886
910
bundlePath string
887
911
}
888
912
913
+ type APIKeyClient struct {
914
+ ClientID string
915
+ HashedKey string
916
+ }
917
+
889
918
func (i internalBundleValidator ) validate (bundle string ) (string , error ) {
890
919
bundle = path .Join (i .bundlePath , bundle )
891
920
_ , err := os .Stat (bundle )
@@ -1308,19 +1337,17 @@ func (p *policiesCfg) addAPIKeyConfig(
1308
1337
polNamespace string ,
1309
1338
secretRefs map [string ]* secrets.SecretReference ,
1310
1339
context string ,
1311
- ) ( * validationResults , * version2. Map ) {
1340
+ ) * validationResults {
1312
1341
res := newValidationResults ()
1313
- var clients []version2.Client
1314
1342
if p .APIKey != nil {
1315
1343
res .addWarningf (
1316
1344
"Multiple APIKey policies in the same context is not valid. APIKey policy %s will be ignored" ,
1317
1345
polKey ,
1318
1346
)
1319
1347
res .isError = true
1320
- return res , nil
1348
+ return res
1321
1349
}
1322
1350
1323
- // if apiKey.ClientSecret != "" {
1324
1351
secretKey := fmt .Sprintf ("%v/%v" , polNamespace , apiKey .ClientSecret )
1325
1352
glog .Infof ("secretKey: %v" , secretKey )
1326
1353
secretRef := secretRefs [secretKey ]
@@ -1333,57 +1360,65 @@ func (p *policiesCfg) addAPIKeyConfig(
1333
1360
if secretType != "" && secretType != secrets .SecretTypeAPIKey {
1334
1361
res .addWarningf ("API Key policy %s references a secret %s of a wrong type '%s', must be '%s'" , polKey , secretKey , secretType , secrets .SecretTypeAPIKey )
1335
1362
res .isError = true
1336
- return res , nil
1363
+ return res
1337
1364
} else if secretRef .Error != nil {
1338
1365
res .addWarningf ("API Key %s references an invalid secret %s: %v" , polKey , secretKey , secretRef .Error )
1339
1366
res .isError = true
1340
- return res , nil
1367
+ return res
1368
+ }
1369
+
1370
+ p .APIKeyClients = generateAPIKeyClients (secretRef .Secret .Data )
1371
+
1372
+ mapName := fmt .Sprintf ("apikey_auth_client_name_%s" , strings .Split (strings .Replace (polKey , "-" , "_" , - 1 ), "/" )[1 ])
1373
+ p .APIKey = & version2.APIKey {
1374
+ Header : apiKey .SuppliedIn .Header ,
1375
+ Query : apiKey .SuppliedIn .Query ,
1376
+ MapName : mapName ,
1341
1377
}
1378
+ p .APIKeyEnabled = true
1379
+ return res
1380
+ }
1342
1381
1343
- for clientID , clientSecret := range secretRef .Secret .Data {
1382
+ func generateAPIKeyClients (secretData map [string ][]byte ) []APIKeyClient {
1383
+ var clients []APIKeyClient
1384
+ for clientID , apiKey := range secretData {
1344
1385
1345
1386
h := sha256 .New ()
1346
- h .Write ([]byte (clientSecret ))
1387
+ h .Write ([]byte (apiKey ))
1347
1388
sha256Hash := hex .EncodeToString (h .Sum (nil ))
1348
1389
base64Str := base64 .URLEncoding .EncodeToString (h .Sum (nil ))
1349
1390
1350
- glog .Infof ("clientSecret %s" , clientSecret )
1391
+ glog .Infof ("apiKey %s" , apiKey )
1351
1392
glog .Infof ("sha %s" , sha256Hash )
1352
1393
glog .Infof ("base64Str %s" , base64Str )
1353
- clients = append (clients , version2. Client {ClientID : clientID , EncryptedKey : sha256Hash }) //
1394
+ clients = append (clients , APIKeyClient {ClientID : clientID , HashedKey : sha256Hash }) //
1354
1395
}
1396
+ return clients
1397
+ }
1355
1398
1356
- default_parameter := version2.Parameter {
1399
+ func generateAPIKeyClientMap (mapName string , apiKeyClients []APIKeyClient ) * version2.Map {
1400
+ glog .Infof ("mapName: %v, apiKeyClients: %v" , mapName , apiKeyClients )
1401
+
1402
+ defaultParam := version2.Parameter {
1357
1403
Value : "default" ,
1358
1404
Result : "\" \" " ,
1359
1405
}
1360
1406
1361
- params := []version2.Parameter {default_parameter }
1362
- for _ , client := range clients {
1407
+ params := []version2.Parameter {defaultParam }
1408
+ for _ , client := range apiKeyClients {
1363
1409
params = append (params , version2.Parameter {
1364
- Value : fmt .Sprintf ("\" %s\" " , client .EncryptedKey ),
1410
+ Value : fmt .Sprintf ("\" %s\" " , client .HashedKey ),
1365
1411
Result : fmt .Sprintf ("\" %s\" " , client .ClientID ),
1366
1412
})
1367
1413
}
1368
1414
1369
1415
sourceName := "$apikey_auth_token"
1370
- mapName := fmt .Sprintf ("apikey_auth_client_name_%s" , strings .Split (strings .Replace (polKey , "-" , "_" , - 1 ), "/" )[1 ])
1371
1416
1372
- shaToClient := & version2.Map {
1417
+ return & version2.Map {
1373
1418
Source : sourceName ,
1374
1419
Variable : fmt .Sprintf ("$%s" , mapName ),
1375
1420
Parameters : params ,
1376
1421
}
1377
-
1378
- p .APIKey = & version2.APIKey {
1379
- Header : apiKey .SuppliedIn .Header ,
1380
- Query : apiKey .SuppliedIn .Query ,
1381
- RejectCodeNotSupplied : generateIntFromPointer (apiKey .RejectCodes .NotSupplied , 401 ),
1382
- RejectCodeNoMatch : generateIntFromPointer (apiKey .RejectCodes .NoMatch , 403 ),
1383
- Clients : clients ,
1384
- MapName : mapName ,
1385
- }
1386
- return res , shaToClient
1387
1422
}
1388
1423
1389
1424
func (p * policiesCfg ) addWAFConfig (
@@ -1474,9 +1509,8 @@ func (vsc *virtualServerConfigurator) generatePolicies(
1474
1509
policies map [string ]* conf_v1.Policy ,
1475
1510
context string ,
1476
1511
policyOpts policyOptions ,
1477
- ) ( policiesCfg , []version2. Map ) {
1512
+ ) policiesCfg {
1478
1513
config := newPoliciesConfig (vsc .bundleValidator )
1479
- maps := []version2.Map {}
1480
1514
1481
1515
for _ , p := range policyRefs {
1482
1516
polNamespace := p .Namespace
@@ -1518,44 +1552,27 @@ func (vsc *virtualServerConfigurator) generatePolicies(
1518
1552
case pol .Spec .OIDC != nil :
1519
1553
res = config .addOIDCConfig (pol .Spec .OIDC , key , polNamespace , policyOpts .secretRefs , vsc .oidcPolCfg )
1520
1554
case pol .Spec .APIKey != nil :
1521
- res , shaToClientMap := config .addAPIKeyConfig (pol .Spec .APIKey , key , polNamespace , policyOpts .secretRefs , context )
1522
- // TODO: refactor
1523
- if res != nil && len (res .warnings ) > 0 {
1524
- vsc .addWarnings (ownerDetails .owner , res .warnings )
1525
- }
1526
-
1527
- if res != nil && res .isError {
1528
- return policiesCfg {
1529
- ErrorReturn : & version2.Return {Code : 500 },
1530
- }, maps
1531
- }
1532
- if res != nil && ! res .isError && shaToClientMap != nil {
1533
- maps = append (maps , * shaToClientMap )
1534
- }
1535
-
1555
+ res = config .addAPIKeyConfig (pol .Spec .APIKey , key , polNamespace , policyOpts .secretRefs , context )
1536
1556
case pol .Spec .WAF != nil :
1537
1557
res = config .addWAFConfig (pol .Spec .WAF , key , polNamespace , policyOpts .apResources )
1538
1558
default :
1539
1559
res = newValidationResults ()
1540
1560
}
1541
- if res != nil && len (res .warnings ) > 0 {
1542
- vsc .addWarnings (ownerDetails .owner , res .warnings )
1543
- }
1544
-
1545
- if res != nil && res .isError {
1561
+ vsc .addWarnings (ownerDetails .owner , res .warnings )
1562
+ if res .isError {
1546
1563
return policiesCfg {
1547
1564
ErrorReturn : & version2.Return {Code : 500 },
1548
- }, maps
1565
+ }
1549
1566
}
1550
1567
} else {
1551
1568
vsc .addWarningf (ownerDetails .owner , "Policy %s is missing or invalid" , key )
1552
1569
return policiesCfg {
1553
1570
ErrorReturn : & version2.Return {Code : 500 },
1554
- }, maps
1571
+ }
1555
1572
}
1556
1573
}
1557
1574
1558
- return * config , maps
1575
+ return * config
1559
1576
}
1560
1577
1561
1578
func generateLimitReq (zoneName string , rateLimitPol * conf_v1.RateLimit ) version2.LimitReq {
0 commit comments