Skip to content

Commit f93f364

Browse files
Merge pull request #950 from Thushani-Jayasekera/api-key
Gateway Controller - API Key Update Restrictions
2 parents 8b71bad + 1259406 commit f93f364

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,12 @@ func (s *APIServer) UpdateAPIKey(c *gin.Context, id string, apiKeyName string) {
25502550
result, err := s.apiKeyService.UpdateAPIKey(params)
25512551
if err != nil {
25522552
// Check error type to determine appropriate status code
2553-
if strings.Contains(err.Error(), "not found") {
2553+
if storage.IsOperationNotAllowedError(err) {
2554+
c.JSON(http.StatusBadRequest, api.ErrorResponse{
2555+
Status: "error",
2556+
Message: err.Error(),
2557+
})
2558+
} else if strings.Contains(err.Error(), "not found") {
25542559
c.JSON(http.StatusNotFound, api.ErrorResponse{
25552560
Status: "error",
25562561
Message: err.Error(),

gateway/gateway-controller/pkg/storage/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ var (
3333

3434
// ErrDatabaseUnavailable is returned when the database storage is unavailable
3535
ErrDatabaseUnavailable = errors.New("database storage is unavailable")
36+
37+
// ErrOperationNotAllowed is returned when an operation is not permitted
38+
ErrOperationNotAllowed = errors.New("operation not allowed")
3639
)
3740

3841
// IsConflictError checks if an error is a conflict error
@@ -50,3 +53,8 @@ func IsNotFoundError(err error) bool {
5053
func IsDatabaseUnavailableError(err error) bool {
5154
return errors.Is(err, ErrDatabaseUnavailable)
5255
}
56+
57+
// IsOperationNotAllowedError checks if an error is an operation not allowed error
58+
func IsOperationNotAllowedError(err error) bool {
59+
return errors.Is(err, ErrOperationNotAllowed)
60+
}

gateway/gateway-controller/pkg/storage/sqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ func (s *SQLiteStorage) UpdateAPIKey(apiKey *models.APIKey) error {
15441544
LIMIT 1
15451545
`
15461546
var duplicateID, duplicateName string
1547-
err := s.db.QueryRow(duplicateCheckQuery, apiKey.APIId, apiKey.IndexKey, apiKey.Name).Scan(&duplicateID, &duplicateName)
1547+
err := tx.QueryRow(duplicateCheckQuery, apiKey.APIId, apiKey.IndexKey, apiKey.Name).Scan(&duplicateID, &duplicateName)
15481548
if err != nil && !errors.Is(err, sql.ErrNoRows) {
15491549
tx.Rollback()
15501550
return fmt.Errorf("failed to check for duplicate API key: %w", err)

gateway/gateway-controller/pkg/utils/api_key.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,14 @@ func (s *APIKeyService) UpdateAPIKey(params APIKeyUpdateParams) (*APIKeyUpdateRe
507507
return nil, fmt.Errorf("API key '%s' not found for API '%s'", params.APIKeyName, params.Handle)
508508
}
509509

510+
// Validate that only external API keys can be updated
511+
if existingKey.Source != "external" {
512+
logger.Warn("Attempted to update a locally generated API key",
513+
slog.String("source", existingKey.Source),
514+
slog.String("api_key_name", params.APIKeyName))
515+
return nil, fmt.Errorf("%w: updates are only allowed for externally generated API keys. For locally generated keys, please use the regenerate endpoint to create a new key", storage.ErrOperationNotAllowed)
516+
}
517+
510518
// Check authorization - only creator can update their own key (unless admin)
511519
err = s.canRegenerateAPIKey(user, existingKey, logger)
512520
if err != nil {

0 commit comments

Comments
 (0)