Files
archie/internal/infrastructure/rpcidentity/identity.go
T

136 lines
4.5 KiB
Go

// Package rpcidentity attributes authenticated service calls to their principal.
package rpcidentity
import (
"context"
"errors"
"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"
// OrgKey carries the org the principal acts in, which differs from the one it
// serves when the caller selected another org it is a member of.
const OrgKey = "state-store-org"
// 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)
md.Delete(OrgKey)
if p, ok := access.PrincipalFromContext(ctx); ok {
md.Set(PrincipalKey, string(p.IdentityID))
if p.Org != "" {
md.Set(OrgKey, string(p.Org))
}
}
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
// Workspaces lets a call act in an org it selected; without it any
// selection other than the principal's own org is refused.
Workspaces access.WorkspaceLister
}
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)
}
// The forwarded org is a claim: the membership check, not the metadata,
// decides whether the call may act there.
p, err = p.Select(org.WithOrg(access.WithPrincipal(ctx, p), p.Org), org.OrgID(first(md, OrgKey)), c.Workspaces)
if err != nil {
if errors.Is(err, access.ErrNotMember) {
return nil, status.Error(codes.PermissionDenied, err.Error())
}
return nil, status.Errorf(codes.Unavailable, "select org: %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
}