104 lines
4.3 KiB
Go
104 lines
4.3 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], Memberships: []org.Membership{{IdentityID: id, OrgID: p[id], Role: org.RoleDeveloper}}}, 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.TaskService_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},
|
|
{"forwarded org the principal serves is accepted", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice", rpcidentity.OrgKey, "acme"}, "alice", "acme", false},
|
|
{"forwarded org the principal is not a member of is refused", plaintext, []string{rpcidentity.CallerKey, "archie-ui", rpcidentity.PrincipalKey, "alice", rpcidentity.OrgKey, "other"}, "", "", true},
|
|
{"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)
|
|
}
|
|
})
|
|
}
|
|
}
|