@@ -32,6 +32,8 @@ import (
32
32
"go.opentelemetry.io/collector/component"
33
33
"go.opentelemetry.io/collector/extension/extensionauth"
34
34
"golang.org/x/crypto/pbkdf2"
35
+ "google.golang.org/grpc/codes"
36
+ "google.golang.org/grpc/status"
35
37
36
38
"github.com/elastic/go-elasticsearch/v8"
37
39
"github.com/elastic/go-elasticsearch/v8/typedapi/security/hasprivileges"
@@ -222,10 +224,14 @@ func (a *authenticator) getCacheKey(id string, headers map[string][]string) (str
222
224
223
225
// Authenticate validates an ApiKey scheme Authorization header,
224
226
// passing it to Elasticsearch for checking privileges.
227
+ //
228
+ // Callers can use status.FromError(err) to get the status code
229
+ // and message from the returned error. If no status.Status is returned,
230
+ // the error should be considered an internal error.
225
231
func (a * authenticator ) Authenticate (ctx context.Context , headers map [string ][]string ) (context.Context , error ) {
226
232
authHeaderValue , id , err := a .parseAuthorizationHeader (headers )
227
233
if err != nil {
228
- return ctx , err
234
+ return ctx , status . Error ( codes . Unauthenticated , err . Error ())
229
235
}
230
236
231
237
cacheKey , err := a .getCacheKey (id , headers )
@@ -245,27 +251,29 @@ func (a *authenticator) Authenticate(ctx context.Context, headers map[string][]s
245
251
// Client has specified an API Key with a colliding ID,
246
252
// but whose secret component does not match the one in
247
253
// the cache.
248
- return ctx , fmt .Errorf ("API Key %q unauthorized" , id )
254
+ return ctx , status .Errorf (codes .Unauthenticated ,
255
+ "API Key %q unauthorized" , id ,
256
+ )
249
257
}
250
258
if cacheEntry .err != nil {
251
- return ctx , cacheEntry .err
259
+ return ctx , status . Error ( codes . Unauthenticated , cacheEntry .err . Error ())
252
260
}
253
- clientInfo := client .FromContext (ctx )
254
- clientInfo .Auth = cacheEntry .data
255
- return client .NewContext (ctx , clientInfo ), nil
261
+ return newCtxWithAuthData (ctx , cacheEntry .data ), nil
256
262
}
257
263
258
264
hasPrivileges , username , err := a .hasPrivileges (ctx , authHeaderValue )
259
265
if err != nil {
260
- return ctx , err
266
+ return ctx , fmt .Errorf (
267
+ "error checking privileges for API Key %q: %v" , id , err ,
268
+ )
261
269
}
262
270
if ! hasPrivileges {
263
271
cacheEntry := & cacheEntry {
264
272
key : derivedKey ,
265
273
err : fmt .Errorf ("API Key %q unauthorized" , id ),
266
274
}
267
275
a .cache .Add (cacheKey , cacheEntry )
268
- return ctx , cacheEntry .err
276
+ return ctx , status . Error ( codes . PermissionDenied , cacheEntry .err . Error ())
269
277
}
270
278
cacheEntry := & cacheEntry {
271
279
key : derivedKey ,
@@ -275,7 +283,11 @@ func (a *authenticator) Authenticate(ctx context.Context, headers map[string][]s
275
283
},
276
284
}
277
285
a .cache .Add (cacheKey , cacheEntry )
286
+ return newCtxWithAuthData (ctx , cacheEntry .data ), nil
287
+ }
288
+
289
+ func newCtxWithAuthData (ctx context.Context , authData * authData ) context.Context {
278
290
clientInfo := client .FromContext (ctx )
279
- clientInfo .Auth = cacheEntry . data
280
- return client .NewContext (ctx , clientInfo ), nil
291
+ clientInfo .Auth = authData
292
+ return client .NewContext (ctx , clientInfo )
281
293
}
0 commit comments