Replace the service bearer tokens with [tls] ca_file and cert_dir. Caller identity comes from the verified certificate, not metadata. Without TLS, services listen and dial on loopback only. Task containers get only the CA and keep their grant.
115 lines
3.6 KiB
Go
115 lines
3.6 KiB
Go
// Package rpcidentity attributes authenticated service calls to their principal.
|
|
package rpcidentity
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
|
)
|
|
|
|
// CallerKey names the service making the call.
|
|
const CallerKey = "state-store-caller"
|
|
|
|
// PrincipalKey carries the identity a service acts for, when it acts
|
|
// for one.
|
|
const PrincipalKey = "state-store-principal"
|
|
|
|
// Outgoing forwards the context's principal, replacing any stale attribution.
|
|
func Outgoing(ctx context.Context, caller string) context.Context {
|
|
md, _ := metadata.FromOutgoingContext(ctx)
|
|
md = md.Copy()
|
|
md.Set(CallerKey, caller)
|
|
md.Delete(PrincipalKey)
|
|
if p, ok := access.PrincipalFromContext(ctx); ok {
|
|
md.Set(PrincipalKey, string(p.IdentityID))
|
|
}
|
|
return metadata.NewOutgoingContext(ctx, md)
|
|
}
|
|
|
|
// Callers resolves who a call is made by. It runs after authentication:
|
|
// the caller is the verified certificate's service, else (a plaintext
|
|
// loopback peer) the metadata's claim, and a task grant's metadata is
|
|
// stripped before it gets here. A call naming a principal acts in, and reads only, that principal's
|
|
// org; any other acts in the default org.
|
|
type Callers struct {
|
|
Principals access.PrincipalSource
|
|
}
|
|
|
|
func (c Callers) resolve(ctx context.Context) (context.Context, error) {
|
|
md, _ := metadata.FromIncomingContext(ctx)
|
|
caller := rpctls.PeerService(ctx)
|
|
if caller == "" {
|
|
caller = first(md, CallerKey)
|
|
}
|
|
if caller != "" {
|
|
ctx = access.WithActor(access.WithSource(ctx, caller), caller)
|
|
}
|
|
id := first(md, PrincipalKey)
|
|
if id == "" {
|
|
if servesChannels(caller) {
|
|
ctx = org.WithScope(ctx, org.DefaultOrgID)
|
|
}
|
|
return ctx, nil
|
|
}
|
|
if c.Principals == nil {
|
|
return nil, status.Error(codes.Unavailable, "principals unavailable")
|
|
}
|
|
p, err := c.Principals.PrincipalFor(ctx, identity.IdentityID(id))
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Unavailable, "resolve principal: %v", err)
|
|
}
|
|
ctx = org.WithScope(org.WithOrg(access.WithActor(access.WithPrincipal(ctx, p), id), p.Org), p.Org)
|
|
return ctx, nil
|
|
}
|
|
|
|
// Unary attributes unary calls.
|
|
func (c Callers) Unary() grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
|
ctx, err := c.resolve(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return handler(ctx, req)
|
|
}
|
|
}
|
|
|
|
// Stream attributes streams.
|
|
func (c Callers) Stream() grpc.StreamServerInterceptor {
|
|
return func(srv any, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
ctx, err := c.resolve(stream.Context())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return handler(srv, attributedStream{ServerStream: stream, ctx: ctx})
|
|
}
|
|
}
|
|
|
|
type attributedStream struct {
|
|
grpc.ServerStream
|
|
ctx context.Context //nolint:containedctx // grpc.ServerStream exposes its context only through Context()
|
|
}
|
|
|
|
func (s attributedStream) Context() context.Context { return s.ctx }
|
|
|
|
func first(md metadata.MD, key string) string {
|
|
if values := md.Get(key); len(values) > 0 {
|
|
return values[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// servesChannels reports whether caller serves chat channels. A channel call
|
|
// acting for no one, such as a Telegram message, reads only the system org.
|
|
func servesChannels(caller string) bool {
|
|
return caller == presence.Gateway || caller == presence.Messaging
|
|
}
|