@@ -35,15 +35,18 @@ const (
3535
3636// APIKeyPolicy implements API Key Authentication
3737type APIKeyPolicy struct {
38+ logger * slog.Logger
3839}
3940
40- var ins = & APIKeyPolicy {}
41-
4241func GetPolicy (
4342 metadata policy.PolicyMetadata ,
4443 params map [string ]interface {},
44+ logger * slog.Logger ,
4545) (policy.Policy , error ) {
46- return ins , nil
46+ p := & APIKeyPolicy {
47+ logger : logger ,
48+ }
49+ return p , nil
4750}
4851
4952// Mode returns the processing mode for this policy
@@ -58,7 +61,8 @@ func (p *APIKeyPolicy) Mode() policy.ProcessingMode {
5861
5962// OnRequest performs API Key Authentication
6063func (p * APIKeyPolicy ) OnRequest (ctx * policy.RequestContext , params map [string ]interface {}) policy.RequestAction {
61- slog .Debug ("API Key Auth Policy: OnRequest started" ,
64+ log := policy .WithRequestID (p .logger , ctx .RequestID )
65+ log .Debug ("OnRequest started" ,
6266 "path" , ctx .Path ,
6367 "method" , ctx .Method ,
6468 "apiId" , ctx .APIId ,
@@ -69,7 +73,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
6973 // Get configuration parameters
7074 keyName , ok := params ["key" ].(string )
7175 if ! ok || keyName == "" {
72- slog .Debug ("API Key Auth Policy: Missing or invalid 'key' configuration" ,
76+ log .Debug ("Missing or invalid 'key' configuration" ,
7377 "keyName" , keyName ,
7478 "ok" , ok ,
7579 )
@@ -79,7 +83,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
7983
8084 location , ok := params ["in" ].(string )
8185 if ! ok || location == "" {
82- slog .Debug ("API Key Auth Policy: Missing or invalid 'in' configuration" ,
86+ log .Debug ("Missing or invalid 'in' configuration" ,
8387 "location" , location ,
8488 "ok" , ok ,
8589 )
@@ -94,7 +98,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
9498 }
9599 }
96100
97- slog .Debug ("API Key Auth Policy: Configuration loaded" ,
101+ log .Debug ("Configuration loaded" ,
98102 "keyName" , keyName ,
99103 "location" , location ,
100104 "valuePrefix" , valuePrefix ,
@@ -107,7 +111,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
107111 // Check header (case-insensitive)
108112 if headerValues := ctx .Headers .Get (http .CanonicalHeaderKey (keyName )); len (headerValues ) > 0 {
109113 providedKey = headerValues [0 ]
110- slog .Debug ("API Key Auth Policy: Found API key in header" ,
114+ log .Debug ("Found API key in header" ,
111115 "headerName" , keyName ,
112116 "keyLength" , len (providedKey ),
113117 )
@@ -116,7 +120,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
116120 // Extract query parameters from the full path
117121 providedKey = extractQueryParam (ctx .Path , keyName )
118122 if providedKey != "" {
119- slog .Debug ("API Key Auth Policy: Found API key in query parameter" ,
123+ log .Debug ("Found API key in query parameter" ,
120124 "paramName" , keyName ,
121125 "keyLength" , len (providedKey ),
122126 )
@@ -125,7 +129,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
125129
126130 // If no API key provided
127131 if providedKey == "" {
128- slog .Debug ("API Key Auth Policy: No API key found" ,
132+ log .Debug ("No API key found" ,
129133 "location" , location ,
130134 "keyName" , keyName ,
131135 )
@@ -137,15 +141,15 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
137141 if valuePrefix != "" {
138142 originalLength := len (providedKey )
139143 providedKey = stripPrefix (providedKey , valuePrefix )
140- slog .Debug ("API Key Auth Policy: Processed value prefix" ,
144+ log .Debug ("Processed value prefix" ,
141145 "prefix" , valuePrefix ,
142146 "originalLength" , originalLength ,
143147 "processedLength" , len (providedKey ),
144148 )
145149
146150 // If after stripping prefix, the key is empty, treat as missing
147151 if providedKey == "" {
148- slog .Debug ("API Key Auth Policy: API key became empty after prefix removal" )
152+ log .Debug ("API key became empty after prefix removal" )
149153 return p .handleAuthFailure (ctx , 401 , "json" , "Valid API key required" ,
150154 "missing API key" )
151155 }
@@ -158,7 +162,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
158162 operationMethod := ctx .Method
159163
160164 if apiId == "" || apiName == "" || apiVersion == "" || apiOperation == "" || operationMethod == "" {
161- slog .Debug ("API Key Auth Policy: Missing API details for validation" ,
165+ log .Debug ("Missing API details for validation" ,
162166 "apiId" , apiId ,
163167 "apiName" , apiName ,
164168 "apiVersion" , apiVersion ,
@@ -169,7 +173,7 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
169173 "missing API details for validation" )
170174 }
171175
172- slog .Debug ("API Key Auth Policy: Starting validation" ,
176+ log .Debug ("Starting validation" ,
173177 "apiId" , apiId ,
174178 "apiName" , apiName ,
175179 "apiVersion" , apiVersion ,
@@ -181,26 +185,27 @@ func (p *APIKeyPolicy) OnRequest(ctx *policy.RequestContext, params map[string]i
181185 // API key was provided - validate it using external validation
182186 isValid , err := p .validateAPIKey (apiId , apiOperation , operationMethod , providedKey )
183187 if err != nil {
184- slog .Debug ("API Key Auth Policy: Validation error" ,
188+ log .Debug ("Validation error" ,
185189 "error" , err ,
186190 )
187191 return p .handleAuthFailure (ctx , 401 , "json" , "Valid API key required" ,
188192 "error validating API key" )
189193 }
190194 if ! isValid {
191- slog .Debug ("API Key Auth Policy: Invalid API key" )
195+ log .Debug ("Invalid API key" )
192196 return p .handleAuthFailure (ctx , 401 , "json" , "Valid API key required" ,
193197 "invalid API key" )
194198 }
195199
196200 // Authentication successful
197- slog .Debug ("API Key Auth Policy: Authentication successful" )
201+ log .Debug ("Authentication successful" )
198202 return p .handleAuthSuccess (ctx )
199203}
200204
201205// handleAuthSuccess handles successful authentication
202206func (p * APIKeyPolicy ) handleAuthSuccess (ctx * policy.RequestContext ) policy.RequestAction {
203- slog .Debug ("API Key Auth Policy: handleAuthSuccess called" ,
207+ log := policy .WithRequestID (p .logger , ctx .RequestID )
208+ log .Debug ("handleAuthSuccess called" ,
204209 "apiId" , ctx .APIId ,
205210 "apiName" , ctx .APIName ,
206211 "apiVersion" , ctx .APIVersion ,
@@ -212,7 +217,7 @@ func (p *APIKeyPolicy) handleAuthSuccess(ctx *policy.RequestContext) policy.Requ
212217 ctx .Metadata [MetadataKeyAuthSuccess ] = true
213218 ctx .Metadata [MetadataKeyAuthMethod ] = "api-key"
214219
215- slog .Debug ("API Key Auth Policy: Authentication metadata set" ,
220+ log .Debug ("Authentication metadata set" ,
216221 "authSuccess" , true ,
217222 "authMethod" , "api-key" ,
218223 )
@@ -229,7 +234,8 @@ func (p *APIKeyPolicy) OnResponse(_ctx *policy.ResponseContext, _params map[stri
229234// handleAuthFailure handles authentication failure
230235func (p * APIKeyPolicy ) handleAuthFailure (ctx * policy.RequestContext , statusCode int , errorFormat , errorMessage ,
231236 reason string ) policy.RequestAction {
232- slog .Debug ("API Key Auth Policy: handleAuthFailure called" ,
237+ log := policy .WithRequestID (p .logger , ctx .RequestID )
238+ log .Debug ("handleAuthFailure called" ,
233239 "statusCode" , statusCode ,
234240 "errorFormat" , errorFormat ,
235241 "errorMessage" , errorMessage ,
@@ -263,7 +269,7 @@ func (p *APIKeyPolicy) handleAuthFailure(ctx *policy.RequestContext, statusCode
263269 body = string (bodyBytes )
264270 }
265271
266- slog .Debug ("API Key Auth Policy: Returning immediate response" ,
272+ log .Debug ("Returning immediate response" ,
267273 "statusCode" , statusCode ,
268274 "contentType" , headers ["content-type" ],
269275 "bodyLength" , len (body ),
0 commit comments