Skip to content

Commit 331ed41

Browse files
authored
fix(api): invalidate all team's api keys (#2274)
* fix: invalidate all api keys * fix: type
1 parent c491d01 commit 331ed41

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

packages/api/internal/handlers/admin_kill_team_sandboxes.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ func (a *APIStore) PostAdminTeamsTeamIDSandboxesKill(c *gin.Context, teamID uuid
1919
ctx, span := tracer.Start(ctx, "admin-kill-team-sandboxes")
2020
defer span.End()
2121

22+
err := a.authService.InvalidateTeamCache(ctx, teamID)
23+
if err != nil {
24+
logger.L().Error(ctx, "Failed to invalidate auth cache for team",
25+
logger.WithTeamID(teamID.String()),
26+
zap.Error(err))
27+
}
28+
2229
logger.L().Info(ctx, "Admin killing all sandboxes for team", logger.WithTeamID(teamID.String()))
2330

2431
// Get all running sandboxes for the team
@@ -68,6 +75,13 @@ func (a *APIStore) PostAdminTeamsTeamIDSandboxesKill(c *gin.Context, teamID uuid
6875
return
6976
}
7077

78+
// Invalidate auth cache for this team so subsequent requests re-check against DB
79+
if err := a.authService.InvalidateTeamCache(ctx, teamID); err != nil {
80+
logger.L().Error(ctx, "Failed to invalidate auth cache for team",
81+
logger.WithTeamID(teamID.String()),
82+
zap.Error(err))
83+
}
84+
7185
logger.L().Info(ctx, "Completed killing team sandboxes",
7286
zap.String("teamID", teamID.String()),
7387
zap.Int64("killed", killedCount.Load()),

packages/auth/pkg/auth/auth_store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ func (s *AuthStoreImpl) GetTeamByIDAndUserID(ctx context.Context, userID uuid.UU
9696
func (s *AuthStoreImpl) GetUserIDByHashedAccessToken(ctx context.Context, hashedToken string) (uuid.UUID, error) {
9797
return s.authDB.Read.GetUserIDFromAccessToken(ctx, hashedToken)
9898
}
99+
100+
func (s *AuthStoreImpl) GetTeamAPIKeyHashes(ctx context.Context, teamID uuid.UUID) ([]string, error) {
101+
return s.authDB.Read.GetTeamAPIKeyHashes(ctx, teamID)
102+
}

packages/auth/pkg/auth/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func (c *AuthCache[T]) GetOrSet(ctx context.Context, key string, dataCallback fu
3838
return c.cache.GetOrSet(ctx, key, dataCallback)
3939
}
4040

41+
// Invalidate removes a single entry from the cache by key.
42+
func (c *AuthCache[T]) Invalidate(key string) {
43+
c.cache.Delete(key)
44+
}
45+
4146
// Close stops the cache's background refresh goroutines.
4247
func (c *AuthCache[T]) Close(ctx context.Context) error {
4348
return c.cache.Close(ctx)

packages/auth/pkg/auth/service.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type AuthStore[T TeamItem] interface {
2222
GetTeamByHashedAPIKey(ctx context.Context, hashedKey string) (T, error)
2323
GetTeamByIDAndUserID(ctx context.Context, userID uuid.UUID, teamID string) (T, error)
2424
GetUserIDByHashedAccessToken(ctx context.Context, hashedToken string) (uuid.UUID, error)
25+
GetTeamAPIKeyHashes(ctx context.Context, teamID uuid.UUID) ([]string, error)
2526
}
2627

2728
// AuthService encapsulates the cache, store, and JWT secrets for auth validation.
@@ -196,6 +197,20 @@ func (s *AuthService[T]) ValidateSupabaseTeam(ctx context.Context, ginCtx *gin.C
196197
return result, nil
197198
}
198199

200+
// InvalidateTeamCache queries the team's API key hashes and removes their cached entries.
201+
func (s *AuthService[T]) InvalidateTeamCache(ctx context.Context, teamID uuid.UUID) error {
202+
hashes, err := s.store.GetTeamAPIKeyHashes(ctx, teamID)
203+
if err != nil {
204+
return fmt.Errorf("failed to get team API key hashes: %w", err)
205+
}
206+
207+
for _, hash := range hashes {
208+
s.teamCache.Invalidate(hash)
209+
}
210+
211+
return nil
212+
}
213+
199214
// Close stops the underlying cache's background refresh goroutines.
200215
func (s *AuthService[T]) Close(ctx context.Context) error {
201216
return s.teamCache.Close(ctx)

packages/db/pkg/auth/queries/get_team_api_key_hashes.sql.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- name: GetTeamAPIKeyHashes :many
2+
SELECT tak.api_key_hash
3+
FROM "public"."team_api_keys" tak
4+
WHERE tak.team_id = @team_id;

0 commit comments

Comments
 (0)