Files
archie/internal/infrastructure/staterpc/callers_test.go
T
sam cf3455ef2e feat(rpc): mTLS between services from operator-issued certificates
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.
2026-10-16 02:37:51 +11:00

102 lines
3.8 KiB
Go

package staterpc
import (
"context"
"strings"
"testing"
"time"
"google.golang.org/grpc"
grpccreds "google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"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/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity"
)
// credentials is an in-memory RunCredentialStore.
type credentials map[[32]byte]int64
func (c credentials) PutRunCredential(_ context.Context, d [32]byte, id int64, _ time.Time) error {
c[d] = id
return nil
}
func (c credentials) DeleteRunCredential(_ context.Context, d [32]byte) error {
delete(c, d)
return nil
}
func (c credentials) RunCredentialTask(_ context.Context, d [32]byte, _ time.Time) (int64, error) {
if id, ok := c[d]; ok {
return id, nil
}
return 0, storecontract.ErrRunCredentialUnknown
}
func (credentials) DeleteExpiredRunCredentials(context.Context, time.Time) error { return nil }
type principalsByID map[identity.IdentityID]org.OrgID
func (p principalsByID) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id, Org: p[id]}, nil
}
// TestCallerAttribution pins who a call acts as: a trusted service's
// metadata is trusted, a task grant's is not, and a caller with neither a
// certificate nor a grant is refused.
func TestCallerAttribution(t *testing.T) {
taskToken := strings.Repeat("ab", 32)
grants := &TaskGrants{Store: credentials{}}
if err := grants.register(context.Background(), 7, taskToken, time.Hour); err != nil {
t.Fatal(err)
}
callers := rpcidentity.Callers{Principals: principalsByID{"alice": "acme"}}
update := &grpc.UnaryServerInfo{FullMethod: pb.StateStoreService_Update_FullMethodName}
// A plaintext peer reached a loopback listener; a TLS peer without a
// verified chain presented no client certificate.
plaintext := &peer.Peer{}
noCert := &peer.Peer{AuthInfo: grpccreds.TLSInfo{}}
tests := []struct {
name string
peer *peer.Peer
md []string
wantActor string
wantOrg org.OrgID
wantErr bool
}{
{"service alone acts in the default org", plaintext, []string{rpcidentity.CallerKey, "archied"}, "archied", org.DefaultOrgID, false},
{"forwarded principal sets actor and org", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice"}, "alice", "acme", false},
{"task grant cannot claim a principal", noCert, []string{tokenMetadataKey, taskToken, rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice"}, "task/7", org.DefaultOrgID, false},
{"no certificate and no grant is refused", noCert, []string{rpcidentity.CallerKey, "archied"}, "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := peer.NewContext(metadata.NewIncomingContext(context.Background(), metadata.Pairs(tt.md...)), tt.peer)
var gotActor string
var gotOrg org.OrgID
handler := func(ctx context.Context, _ any) (any, error) {
gotActor, gotOrg = access.ActorFromContext(ctx), org.OrgFromContext(ctx)
return nil, nil
}
chained := func(ctx context.Context, req any) (any, error) {
return callers.Unary()(ctx, req, update, handler)
}
req := &pb.UpdateRequest{Task: &pb.Task{Id: 7}}
_, err := grants.UnaryInterceptor()(ctx, req, update, chained)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if gotActor != tt.wantActor || gotOrg != tt.wantOrg {
t.Fatalf("got actor %q org %q, want %q %q", gotActor, gotOrg, tt.wantActor, tt.wantOrg)
}
})
}
}