pomerium/pkg/grpcutil/grpcutil.go
Caleb Doxsey 5d60cff21e
databroker: refactor databroker to sync all changes (#1879)
* refactor backend, implement encrypted store

* refactor in-memory store

* wip

* wip

* wip

* add syncer test

* fix redis expiry

* fix linting issues

* fix test by skipping non-config records

* fix backoff import

* fix init issues

* fix query

* wait for initial sync before starting directory sync

* add type to SyncLatest

* add more log messages, fix deadlock in in-memory store, always return server version from SyncLatest

* update sync types and tests

* add redis tests

* skip macos in github actions

* add comments to proto

* split getBackend into separate methods

* handle errors in initVersion

* return different error for not found vs other errors in get

* use exponential backoff for redis transaction retry

* rename raw to result

* use context instead of close channel

* store type urls as constants in databroker

* use timestampb instead of ptypes

* fix group merging not waiting

* change locked names

* update GetAll to return latest record version

* add method to grpcutil to get the type url for a protobuf type
2021-02-18 15:24:33 -07:00

72 lines
1.9 KiB
Go

// Package grpcutil contains functions for interacting with gRPC.
package grpcutil
import (
"context"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/proto"
)
// 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
}
// JWTMetadataKey is the key in the metadata.
const JWTMetadataKey = "jwt"
// WithOutgoingJWT appends a metadata header for the JWT to a context.
func WithOutgoingJWT(ctx context.Context, rawjwt string) context.Context {
return metadata.AppendToOutgoingContext(ctx, JWTMetadataKey, rawjwt)
}
// JWTFromGRPCRequest returns the JWT from the gRPC request.
func JWTFromGRPCRequest(ctx context.Context) (rawjwt string, ok bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", false
}
rawjwts := md.Get(JWTMetadataKey)
if len(rawjwts) == 0 {
return "", false
}
return rawjwts[0], true
}
// GetPeerAddr returns the peer address.
func GetPeerAddr(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if ok {
return p.Addr.String()
}
return ""
}
// GetTypeURL gets the TypeURL for a protobuf message.
func GetTypeURL(msg proto.Message) string {
// taken from the anypb package
const urlPrefix = "type.googleapis.com/"
return urlPrefix + string(msg.ProtoReflect().Descriptor().FullName())
}