mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-07 06:16:18 +02:00
pkg: add grpcutil package (#1032)
This commit is contained in:
parent
fae02791f5
commit
09621ee263
2 changed files with 60 additions and 0 deletions
31
pkg/grpcutil/grpcutil.go
Normal file
31
pkg/grpcutil/grpcutil.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
// Package grpcutil contains functions for interacting with gRPC.
|
||||||
|
package grpcutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SessionIDMetadataKey is the key in the metadata.
|
||||||
|
const SessionIDMetadataKey = "sessionid"
|
||||||
|
|
||||||
|
// WithOutgoingSessionID appends a metadata header for the session ID to a context.
|
||||||
|
func WithOutgoingSessionID(ctx context.Context, sessionID string) context.Context {
|
||||||
|
return metadata.AppendToOutgoingContext(ctx, SessionIDMetadataKey, sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionIDFromGRPCRequest returns the session id from the gRPC request.
|
||||||
|
func SessionIDFromGRPCRequest(ctx context.Context) (sessionID string, ok bool) {
|
||||||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionIDs := md.Get(SessionIDMetadataKey)
|
||||||
|
if len(sessionIDs) == 0 {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return sessionIDs[0], true
|
||||||
|
}
|
29
pkg/grpcutil/grpcutil_test.go
Normal file
29
pkg/grpcutil/grpcutil_test.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package grpcutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWithOutgoingSessionID(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
ctx = WithOutgoingSessionID(ctx, "EXAMPLE")
|
||||||
|
md, ok := metadata.FromOutgoingContext(ctx)
|
||||||
|
if !assert.True(t, ok) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.Equal(t, []string{"EXAMPLE"}, md.Get("sessionid"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSessionIDFromGRPCRequest(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
ctx = metadata.NewIncomingContext(ctx, metadata.MD{
|
||||||
|
"sessionid": {"EXAMPLE"},
|
||||||
|
})
|
||||||
|
sessionID, ok := SessionIDFromGRPCRequest(ctx)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, "EXAMPLE", sessionID)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue