Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: albertteoh <see.kwang.teoh@gmail.com>
  • Loading branch information
albertteoh committed Nov 2, 2021
1 parent 4d7e1c5 commit 696cb5f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
13 changes: 4 additions & 9 deletions pkg/bearertoken/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ package bearertoken

import "context"

type contextKeyType string
type contextKeyType int

const (
// Key is the key name for the bearer token context value.
Key = "bearer.token"
const contextKey = iota

// StoragePropagationKey is a key for viper configuration to pass this option to storage plugins.
StoragePropagationKey = "storage.propagate.token"

contextKey = contextKeyType(Key)
)
// StoragePropagationKey is a key for viper configuration to pass this option to storage plugins.
const StoragePropagationKey = "storage.propagate.token"

// ContextWithBearerToken set bearer token in context.
func ContextWithBearerToken(ctx context.Context, token string) context.Context {
Expand Down
5 changes: 2 additions & 3 deletions pkg/bearertoken/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
)

// PropagationHandler returns a http.Handler containing the logic to extract
// the Authorization token from the http.Request and inserts it into the http.Request
// context for easier access to the request token via GetBearerToken for bearer token
// propagation use cases.
// the Bearer token from the Authorization header of the http.Request and insert it into request.Context
// for propagation. The token can be accessed via GetBearerToken.
func PropagationHandler(logger *zap.Logger, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
5 changes: 3 additions & 2 deletions plugin/storage/grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ When using `--query.bearer-token-propagation=true`, the bearer token will be pro
import (
// ... other imports
"fmt"
"github.com/jaegertracing/jaeger/storage/spanstore"
"google.golang.org/grpc/metadata"

"github.com/jaegertracing/jaeger/plugin/storage/grpc"
)

// ... spanReader type declared here

func (r *spanReader) extractBearerToken(ctx context.Context) (string, bool) {
if md, ok := metadata.FromIncomingContext(ctx); ok {
values := md.Get(spanstore.BearerTokenKey)
values := md.Get(grpc.BearerTokenKey)
if len(values) > 0 {
return values[0], true
}
Expand Down
5 changes: 4 additions & 1 deletion plugin/storage/grpc/shared/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"github.com/jaegertracing/jaeger/storage/spanstore"
)

// BearerTokenKey is the key name for the bearer token context value.
const BearerTokenKey = "bearer.token"

var (
_ StoragePlugin = (*grpcClient)(nil)
_ ArchiveStoragePlugin = (*grpcClient)(nil)
Expand Down Expand Up @@ -74,7 +77,7 @@ func upgradeContextWithBearerToken(ctx context.Context) context.Context {
if !ok {
md = metadata.New(nil)
}
md.Set(bearertoken.Key, bearerToken)
md.Set(BearerTokenKey, bearerToken)
return metadata.NewOutgoingContext(ctx, md)
}
return ctx
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/grpc/shared/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestContextUpgradeWithToken(t *testing.T) {
upgradedToken := upgradeContextWithBearerToken(ctx)
md, ok := metadata.FromOutgoingContext(upgradedToken)
assert.Truef(t, ok, "Expected metadata in context")
bearerTokenFromMetadata := md.Get(bearertoken.Key)
bearerTokenFromMetadata := md.Get(BearerTokenKey)
assert.Equal(t, []string{testBearerToken}, bearerTokenFromMetadata)
}

Expand Down

0 comments on commit 696cb5f

Please sign in to comment.