Skip to content

Commit 1b0c30e

Browse files
committed
Add in memory map for api key caching
1 parent cf7420b commit 1b0c30e

File tree

6 files changed

+261
-46
lines changed

6 files changed

+261
-46
lines changed

gateway/gateway-controller/api/gateway-controller-internal-api.yaml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ servers:
1616
description: Docker/Kubernetes deployment
1717

1818
paths:
19-
/api/internal/v1/apis/{name}/{version}/validate/{apikey}:
20-
get:
19+
/api/internal/v1/apis/{name}/{version}/validate-apikey:
20+
post:
2121
summary: Validate API key for a specific API
2222
description: |
2323
Validates whether the provided API key is valid for accessing the specified API name and version.
@@ -40,13 +40,13 @@ paths:
4040
schema:
4141
type: string
4242
example: "1.0.0"
43-
- name: apikey
44-
in: path
45-
required: true
46-
description: The API key to validate
47-
schema:
48-
type: string
49-
example: "abc123xyz789"
43+
requestBody:
44+
required: true
45+
description: Request body containing the API key to validate
46+
content:
47+
application/json:
48+
schema:
49+
$ref: '#/components/schemas/ApiKeyValidationRequest'
5050
responses:
5151
"200":
5252
description: API key validation result
@@ -75,6 +75,18 @@ paths:
7575

7676
components:
7777
schemas:
78+
ApiKeyValidationRequest:
79+
type: object
80+
required:
81+
- apiKey
82+
properties:
83+
apiKey:
84+
type: string
85+
description: The API key to validate
86+
example: "gw_abc123xyz789"
87+
minLength: 8
88+
additionalProperties: false
89+
7890
ApiKeyValidationResponse:
7991
type: object
8092
required:

gateway/gateway-controller/pkg/api/handlers/handlers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,38 @@ func (s *APIServer) DeleteAPI(c *gin.Context, name string, version string) {
548548
})
549549
return
550550
}
551+
552+
// Delete associated API keys from database
553+
apiKeys, err := s.db.GetAPIKeysByAPI(name, version)
554+
if err != nil {
555+
log.Warn("Failed to retrieve API keys for deletion",
556+
zap.String("name", name),
557+
zap.String("version", version),
558+
zap.Error(err))
559+
} else {
560+
for _, apiKey := range apiKeys {
561+
if err := s.db.DeleteAPIKey(apiKey.APIKey); err != nil {
562+
log.Error("Failed to delete API key from database",
563+
zap.String("keyId", apiKey.ID),
564+
zap.String("name", name),
565+
zap.String("version", version),
566+
zap.Error(err))
567+
} else {
568+
log.Debug("API key deleted from database",
569+
zap.String("keyId", apiKey.ID),
570+
zap.String("name", name),
571+
zap.String("version", version))
572+
}
573+
}
574+
}
575+
}
576+
577+
// Remove API keys from ConfigStore
578+
if err := s.store.RemoveAPIKeysByAPI(name, version); err != nil {
579+
log.Warn("Failed to remove API keys from ConfigStore",
580+
zap.String("name", name),
581+
zap.String("version", version),
582+
zap.Error(err))
551583
}
552584

553585
if cfg.Configuration.Kind == api.APIConfigurationKindAsyncwebsub {
@@ -1918,6 +1950,19 @@ func (s *APIServer) GenerateAPIKey(c *gin.Context, name string, version string)
19181950
}
19191951
}
19201952

1953+
// Store the generated API key in the ConfigStore
1954+
if err := s.store.StoreAPIKey(apiKey); err != nil {
1955+
log.Error("Failed to store API key in ConfigStore",
1956+
zap.Error(err),
1957+
zap.String("name", name),
1958+
zap.String("version", version))
1959+
c.JSON(http.StatusInternalServerError, api.ErrorResponse{
1960+
Status: "error",
1961+
Message: "Failed to store API key",
1962+
})
1963+
return
1964+
}
1965+
19211966
log.Info("API key generated successfully",
19221967
zap.String("name", name),
19231968
zap.String("version", version),

gateway/gateway-controller/pkg/internalapi/generated/generated.go

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

gateway/gateway-controller/pkg/internalapi/handlers/gateway_internal_handlers.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,29 @@ func NewInternalAPIServer(
5454
}
5555

5656
// ValidateApiKey validates whether the provided API key is valid for accessing the specified API name and version
57-
func (s *InternalAPIServer) ValidateApiKey(c *gin.Context, name string, version string, apikey string) {
57+
func (s *InternalAPIServer) ValidateApiKey(c *gin.Context, name string, version string) {
5858
// Get correlation-aware logger from context
5959
log := middleware.GetLogger(c, s.logger)
6060

61+
var req internalapi.ValidateApiKeyJSONRequestBody
62+
if err := c.ShouldBindJSON(&req); err != nil {
63+
log.Warn("Failed to bind request body", zap.Error(err))
64+
c.JSON(http.StatusBadRequest, internalapi.ErrorResponse{
65+
Status: "error",
66+
Message: "Invalid request body",
67+
})
68+
return
69+
}
70+
apikey := req.ApiKey
71+
if apikey == "" {
72+
log.Warn("API key is missing in request body")
73+
c.JSON(http.StatusBadRequest, internalapi.ErrorResponse{
74+
Status: "error",
75+
Message: "API key cannot be empty",
76+
})
77+
return
78+
}
79+
6180
log.Info("Validating API key",
6281
zap.String("apiName", name),
6382
zap.String("apiVersion", version),

gateway/gateway-controller/pkg/internalapi/services/apikey_validator.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package services
2020

2121
import (
22-
"fmt"
22+
"github.com/wso2/api-platform/gateway/gateway-controller/pkg/models"
2323
"strings"
2424
"time"
2525

@@ -88,13 +88,31 @@ func (v *APIKeyValidator) validateGatewayAPIKey(apiName, apiVersion, apiKey stri
8888
zap.String("apiVersion", apiVersion),
8989
)
9090

91-
// Look up the API key in the database
92-
storedAPIKey, err := v.db.GetAPIKeyByKey(apiKey)
91+
// Look up the API key
92+
var storedAPIKey *models.APIKey
93+
var err error
94+
storedAPIKey, err = v.store.GetAPIKeyByKey(apiKey)
9395
if err != nil {
94-
v.logger.Debug("API key not found in database",
96+
v.logger.Debug("API key not found in memory",
9597
zap.String("apiName", apiName),
9698
zap.String("apiVersion", apiVersion),
9799
zap.Error(err))
100+
if v.db != nil {
101+
// Fallback to persistent storage
102+
storedAPIKey, err = v.db.GetAPIKeyByKey(apiKey)
103+
if err != nil {
104+
v.logger.Debug("API key not found in persistent storage",
105+
zap.String("apiName", apiName),
106+
zap.String("apiVersion", apiVersion),
107+
zap.Error(err))
108+
return false, nil // API key doesn't exist
109+
}
110+
} else {
111+
return false, nil // API key doesn't exist
112+
}
113+
}
114+
115+
if storedAPIKey == nil {
98116
return false, nil // API key doesn't exist
99117
}
100118

@@ -135,7 +153,7 @@ func (v *APIKeyValidator) validateManagementPortalAPIKey(apiName, apiVersion, ap
135153
// 4. Parse response and return validation result
136154

137155
v.logger.Warn("Management portal API key validation not yet implemented")
138-
return false, fmt.Errorf("management portal API key validation not implemented")
156+
return false, nil
139157
}
140158

141159
// validateDevPortalAPIKey validates API keys with "dev_" prefix against the developer portal
@@ -154,5 +172,5 @@ func (v *APIKeyValidator) validateDevPortalAPIKey(apiName, apiVersion, apiKey st
154172
// 4. Parse response and return validation result
155173

156174
v.logger.Warn("Developer portal API key validation not yet implemented")
157-
return false, fmt.Errorf("developer portal API key validation not implemented")
175+
return false, nil
158176
}

0 commit comments

Comments
 (0)