Skip to content

Commit 4784e5a

Browse files
authored
feat: convert IR APIKeyAuth Credentials Map into a Slice of Struct (#7584)
* feat: convert IR APIKeyAuth Credentials Map into a Slice of Struct Signed-off-by: Lalet Scaria <[email protected]> * refactor: rename Credential to APIKeyCredential for clarity Signed-off-by: Lalet Scaria <[email protected]>
1 parent 0623ca5 commit 4784e5a

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

internal/gatewayapi/securitypolicy.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,8 +1672,10 @@ func (t *Translator) buildAPIKeyAuth(
16721672
namespace: policy.Namespace,
16731673
}
16741674

1675-
credentials := make(map[string]ir.PrivateBytes)
1675+
expected := len(policy.Spec.APIKeyAuth.CredentialRefs)
1676+
apiKeyCredentials := make([]ir.APIKeyCredential, 0, expected)
16761677
seenKeys := make(sets.Set[string])
1678+
seenClients := make(sets.Set[string])
16771679

16781680
for _, ref := range policy.Spec.APIKeyAuth.CredentialRefs {
16791681
credentialsSecret, err := t.validateSecretRef(
@@ -1682,7 +1684,7 @@ func (t *Translator) buildAPIKeyAuth(
16821684
return nil, err
16831685
}
16841686
for clientid, key := range credentialsSecret.Data {
1685-
if _, ok := credentials[clientid]; ok {
1687+
if seenClients.Has(clientid) {
16861688
continue
16871689
}
16881690

@@ -1692,7 +1694,11 @@ func (t *Translator) buildAPIKeyAuth(
16921694
}
16931695

16941696
seenKeys.Insert(keyString)
1695-
credentials[clientid] = key
1697+
seenClients.Insert(clientid)
1698+
apiKeyCredentials = append(apiKeyCredentials, ir.APIKeyCredential{
1699+
Client: []byte(clientid),
1700+
Key: key,
1701+
})
16961702
}
16971703
}
16981704

@@ -1706,7 +1712,7 @@ func (t *Translator) buildAPIKeyAuth(
17061712
}
17071713

17081714
return &ir.APIKeyAuth{
1709-
Credentials: credentials,
1715+
Credentials: apiKeyCredentials,
17101716
ExtractFrom: extractFrom,
17111717
ForwardClientIDHeader: policy.Spec.APIKeyAuth.ForwardClientIDHeader,
17121718
Sanitize: policy.Spec.APIKeyAuth.Sanitize,

internal/ir/xds.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,13 +1232,23 @@ type BasicAuth struct {
12321232
ForwardUsernameHeader *string `json:"forwardUsernameHeader,omitempty" yaml:"forwardUsernameHeader,omitempty"`
12331233
}
12341234

1235+
// APIKeyCredential defines a single API key credential.
1236+
//
1237+
// +k8s:deepcopy-gen=true
1238+
type APIKeyCredential struct {
1239+
// Client is the client ID.
1240+
Client PrivateBytes `json:"client" yaml:"client"`
1241+
// Key is the API key associated with the client.
1242+
Key PrivateBytes `json:"key" yaml:"key"`
1243+
}
1244+
12351245
// APIKeyAuth defines the schema for the API Key Authentication.
12361246
//
12371247
// +k8s:deepcopy-gen=true
12381248
type APIKeyAuth struct {
1239-
// The API key to be used for authentication.
1240-
// Key is the client id and the value is the API key to be used for authentication.
1241-
Credentials map[string]PrivateBytes `json:"credentials,omitempty" yaml:"credentials,omitempty"`
1249+
// Credentials is the list of API key credentials.
1250+
// Each credential contains a client ID and the associated API key.
1251+
Credentials []APIKeyCredential `json:"credentials,omitempty" yaml:"credentials,omitempty"`
12421252

12431253
// ExtractFrom is where to fetch the key from the coming request.
12441254
// The value from the first source that has a key will be used.

internal/ir/xds_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ func TestRedaction(t *testing.T) {
14101410
HMACSecret: []byte("secret"),
14111411
},
14121412
APIKeyAuth: &APIKeyAuth{
1413-
Credentials: map[string]PrivateBytes{"client-id": []byte("secret")},
1413+
Credentials: []APIKeyCredential{{Client: []byte("client-id"), Key: []byte("secret")}},
14141414
},
14151415
BasicAuth: &BasicAuth{
14161416
Users: []byte("secret"),
@@ -1427,7 +1427,7 @@ func TestRedaction(t *testing.T) {
14271427
`"routes":[{` +
14281428
`"name":"","hostname":"","isHTTP2":false,"security":{` +
14291429
`"oidc":{"name":"","provider":{"authorizationEndpoint":"","tokenEndpoint":""},"clientID":"","clientSecret":"[redacted]","hmacSecret":"[redacted]"},` +
1430-
`"apiKeyAuth":{"credentials":{"client-id":"[redacted]"},"extractFrom":null},` +
1430+
`"apiKeyAuth":{"credentials":[{"client":"[redacted]","key":"[redacted]"}],"extractFrom":null},` +
14311431
`"basicAuth":{"name":"","users":"[redacted]"}` +
14321432
`}}],` +
14331433
`"isHTTP2":false,"path":{"mergeSlashes":false,"escapedSlashesAction":""}}],` +

internal/ir/zz_generated.deepcopy.go

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/xds/translator/api_key_auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ func buildAPIKeyAuthFilterConfig(apiKeyAuth *ir.APIKeyAuth) *apikeyauthv3.ApiKey
128128
apiKeyAuthProto := &apikeyauthv3.ApiKeyAuth{
129129
Credentials: make([]*apikeyauthv3.Credential, 0, len(apiKeyAuth.Credentials)),
130130
}
131-
for clientid, key := range apiKeyAuth.Credentials {
131+
for _, cred := range apiKeyAuth.Credentials {
132132
apiKeyAuthProto.Credentials = append(apiKeyAuthProto.Credentials, &apikeyauthv3.Credential{
133-
Client: clientid,
134-
Key: string(key),
133+
Client: string(cred.Client),
134+
Key: string(cred.Key),
135135
})
136136
}
137137

internal/xds/translator/testdata/in/xds-ir/api-key-auth.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ http:
3333
apiKeyAuth:
3434
name: securitypolicy/default/policy-for-http-route-1
3535
credentials:
36-
client-1: "a2V5MQ=="
36+
- client: "Y2xpZW50LTE="
37+
key: "a2V5MQ=="
3738
extractFrom:
3839
- headers: ["X-API-KEY", "X-API-KEY-2"]
3940
forwardClientIDHeader: "X-API-KEY-CLIENT-ID"
@@ -62,7 +63,8 @@ http:
6263
apiKeyAuth:
6364
name: securitypolicy/default/policy-for-http-route-1
6465
credentials:
65-
client-2: "a2V5Mg=="
66+
- client: "Y2xpZW50LTI="
67+
key: "a2V5Mg=="
6668
extractFrom:
6769
- params: ["X-API-KEY", "X-API-KEY-2"]
6870
- name: httproute/default/httproute-2/rule/0/match/0/www_bar_com
@@ -89,7 +91,8 @@ http:
8991
apiKeyAuth:
9092
name: securitypolicy/default/policy-for-http-route-1
9193
credentials:
92-
client-3: "a2V5Mw=="
94+
- client: "Y2xpZW50LTM="
95+
key: "a2V5Mw=="
9396
extractFrom:
9497
- cookies: ["X-API-KEY", "X-API-KEY-2"]
9598
- name: httproute/default/httproute-2/rule/0/match/0/www_bar_com
@@ -116,7 +119,8 @@ http:
116119
apiKeyAuth:
117120
name: securitypolicy/default/policy-for-http-route-1
118121
credentials:
119-
client-3: "a2V5Mw=="
122+
- client: "Y2xpZW50LTM="
123+
key: "a2V5Mw=="
120124
extractFrom:
121125
# multiple kind of extractFrom with multiple values
122126
- cookies: ["X-API-KEY", "X-API-KEY-2"]

0 commit comments

Comments
 (0)