-
Notifications
You must be signed in to change notification settings - Fork 47
[Discussion WIP] Refactor API key validation to return API key details #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,8 +31,8 @@ const ( | |||||||||||||||||||||||||||||
| AIProviderNameMetadataKey = "ai:providername" | ||||||||||||||||||||||||||||||
| AIProviderDisplayNameMetadataKey = "ai:providerdisplayname" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // AuthContext key for user ID (used for analytics) | ||||||||||||||||||||||||||||||
| AuthContextKeyUserID = "x-wso2-user-id" | ||||||||||||||||||||||||||||||
| // Analytics metadata key for user ID | ||||||||||||||||||||||||||||||
| AuthContextKeyUserID = "x-wso2-user-id" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Lazy resource type for LLM provider templates | ||||||||||||||||||||||||||||||
| lazyResourceTypeLLMProviderTemplate = "LlmProviderTemplate" | ||||||||||||||||||||||||||||||
|
|
@@ -243,14 +243,12 @@ func (p *AnalyticsPolicy) OnResponse(ctx *policy.ResponseContext, params map[str | |||||||||||||||||||||||||||||
| // Store tokenInfo in analytics metadata for publishing | ||||||||||||||||||||||||||||||
| analyticsMetadata := make(map[string]any) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Extract user ID from AuthContext if available (set by jwt-auth policy) | ||||||||||||||||||||||||||||||
| if ctx.SharedContext.AuthContext != nil { | ||||||||||||||||||||||||||||||
| if userID, ok := ctx.SharedContext.AuthContext[AuthContextKeyUserID]; ok && userID != "" { | ||||||||||||||||||||||||||||||
| analyticsMetadata[AuthContextKeyUserID] = userID | ||||||||||||||||||||||||||||||
| slog.Debug("Analytics system policy: User ID extracted from AuthContext", | ||||||||||||||||||||||||||||||
| "userID", userID, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Extract user ID from AuthContext if available (set by auth policies) | ||||||||||||||||||||||||||||||
| if ctx.SharedContext.AuthContext != nil && ctx.SharedContext.AuthContext.UserID != "" { | ||||||||||||||||||||||||||||||
| analyticsMetadata[AuthContextKeyUserID] = ctx.SharedContext.AuthContext.UserID | ||||||||||||||||||||||||||||||
| slog.Debug("Analytics system policy: User ID extracted from AuthContext", | ||||||||||||||||||||||||||||||
| "userID", ctx.SharedContext.AuthContext.Properties[AuthContextKeyUserID], | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+246
to
252
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug log reads from Line 248 correctly stores 🐛 Proposed fix if ctx.SharedContext.AuthContext != nil && ctx.SharedContext.AuthContext.UserID != "" {
analyticsMetadata[AuthContextKeyUserID] = ctx.SharedContext.AuthContext.UserID
slog.Debug("Analytics system policy: User ID extracted from AuthContext",
- "userID", ctx.SharedContext.AuthContext.Properties[AuthContextKeyUserID],
+ "userID", ctx.SharedContext.AuthContext.UserID,
)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Based on the API kind, collect the analytics data | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning a pointer to the internal store entry leaks mutable state outside the lock.
targetAPIKeyon lines 271/279 is the same*APIKeypointer held inapiKeysByAPI. Callers that mutate the returned object afterValidateAPIKeyreturns will race with any concurrent store operation (theRWMutexno longer protects it). Consider returning a shallow copy instead.♻️ Suggested approach
Note: A shallow copy is sufficient here since the mutable fields (
Status, scalar strings, etc.) are value types. The only pointer field isExpiresAt *time.Time; if callers might mutate it, a deep copy of that field would also be needed.🤖 Prompt for AI Agents