Skip to content

Commit b2b7422

Browse files
authored
apikeyauthextension: Elasticsearch check error codes explicitly (#732)
* apikeyauthextension: check es auth error explicitly
1 parent f2a2133 commit b2b7422

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

extension/apikeyauthextension/authenticator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"errors"
2727
"fmt"
2828
"hash/fnv"
29+
"net/http"
2930
"strings"
3031

3132
"go.opentelemetry.io/collector/client"
@@ -263,6 +264,11 @@ func (a *authenticator) Authenticate(ctx context.Context, headers map[string][]s
263264

264265
hasPrivileges, username, err := a.hasPrivileges(ctx, authHeaderValue)
265266
if err != nil {
267+
if elasticsearchErr, ok := err.(*types.ElasticsearchError); ok {
268+
if elasticsearchErr.Status == http.StatusUnauthorized || elasticsearchErr.Status == http.StatusForbidden {
269+
return ctx, status.Error(codes.Unauthenticated, err.Error())
270+
}
271+
}
266272
return ctx, fmt.Errorf(
267273
"error checking privileges for API Key %q: %v", id, err,
268274
)

extension/apikeyauthextension/authenticator_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ func TestAuthenticator(t *testing.T) {
7676
}),
7777
expectedErr: `error checking privileges for API Key "id": status: 400, failed: [a_type], reason: a_reason`,
7878
},
79+
"auth_error": {
80+
handler: newCannedErrorHandler(types.ElasticsearchError{
81+
ErrorCause: types.ErrorCause{
82+
Type: "auth_reason",
83+
Reason: func() *string {
84+
reason := "auth_reason"
85+
return &reason
86+
}(),
87+
},
88+
Status: 401,
89+
}),
90+
expectedErr: `rpc error: code = Unauthenticated desc = status: 401, failed: [auth_reason], reason: auth_reason`,
91+
},
7992
"missing_privileges": {
8093
handler: newCannedHasPrivilegesHandler(hasprivileges.Response{HasAllRequested: false}),
8194
expectedErr: `rpc error: code = PermissionDenied desc = API Key "id" unauthorized`,

0 commit comments

Comments
 (0)