Skip to content

Commit

Permalink
Change how peer info is cached (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-srhodes authored Nov 17, 2023
1 parent e9ee3ce commit 415d043
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
26 changes: 19 additions & 7 deletions auth/opa/rpcauth/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/x509/pkix"
"encoding/json"
"net"
"reflect"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -177,20 +178,31 @@ func AddPeerToContext(ctx context.Context, p *PeerAuthInput) context.Context {
// PeerInputFromContext populates peer information from the supplied
// context, if available.
func PeerInputFromContext(ctx context.Context) *PeerAuthInput {
// If this runs after rpcauth hooks, we can return richer data that includes
// information added by the hooks.
cached, ok := ctx.Value(peerInfoKey{}).(*PeerAuthInput)
if ok {
return cached
}
cached, _ := ctx.Value(peerInfoKey{}).(*PeerAuthInput)

out := &PeerAuthInput{}
p, ok := peer.FromContext(ctx)
if !ok {
return nil
// If there's no peer info, returned cached data so that invocations
// of AddPeerToContext can work outside of RPC contexts.
return cached
}

out.Net = NetInputFromAddr(p.Addr)
out.Cert = CertInputFrom(p.AuthInfo)

// If this runs after rpcauth hooks, we can return richer data that includes
// information added by the hooks.
// We need to compare cached data to peer info because we might be calling
// PeerInputFromContext on the context of a client stream, which has a peer
// of the target being called and may have the cached value from an earlier
// server authorization.
if cached != nil && cached.Principal != nil && reflect.DeepEqual(out.Net, cached.Net) {
out.Principal = &PrincipalAuthInput{
ID: cached.Principal.ID,
Groups: cached.Principal.Groups,
}
}
return out
}

Expand Down
14 changes: 0 additions & 14 deletions auth/opa/rpcauth/rpcauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func (g *Authorizer) AuthorizeClient(ctx context.Context, method string, req, re
if err := g.Eval(ctx, authInput); err != nil {
return err
}
ctx = AddPeerToContext(ctx, authInput.Peer)
return invoker(ctx, method, req, reply, cc, opts...)
}

Expand All @@ -212,16 +211,6 @@ type wrappedClientStream struct {
grpc.ClientStream
method string
authz *Authorizer

peerMu sync.Mutex
lastPeerAuthInput *PeerAuthInput
}

func (e *wrappedClientStream) Context() context.Context {
e.peerMu.Lock()
ctx := AddPeerToContext(e.ClientStream.Context(), e.lastPeerAuthInput)
e.peerMu.Unlock()
return ctx
}

// see: grpc.ClientStream.SendMsg
Expand All @@ -238,9 +227,6 @@ func (e *wrappedClientStream) SendMsg(req interface{}) error {
if err := e.authz.Eval(ctx, authInput); err != nil {
return err
}
e.peerMu.Lock()
e.lastPeerAuthInput = authInput.Peer
e.peerMu.Unlock()
return e.ClientStream.SendMsg(req)
}

Expand Down

0 comments on commit 415d043

Please sign in to comment.