Files
archie/modules/connectors/credentials_test.go
T

209 lines
9.0 KiB
Go

package connectors
import (
"context"
json "encoding/json/v2"
"log/slog"
"strings"
"testing"
"filippo.io/age"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// TestCredentialRoutesNeverReturnASecret guards the credential boundary: a
// list or a read says where each field comes from and never what it holds.
func TestCredentialRoutesNeverReturnASecret(t *testing.T) {
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
sealer, err := seal.New(id.String(), nil)
if err != nil {
t.Fatal(err)
}
const secret = "plainly-fake-value-the-route-must-not-return"
plain, err := json.Marshal(map[string]Field{"token": {Value: secret}})
if err != nil {
t.Fatal(err)
}
sealed, err := sealer.Seal("instance:/ops/bearer", plain)
if err != nil {
t.Fatal(err)
}
module := &Module{
log: slog.New(slog.DiscardHandler),
store: &fakeRegistry{creds: []store.Credential{
{Scope: store.ScopeInstance, Name: "ops", Kind: "bearer", Sealed: sealed},
}},
sealer: sealer,
}
for _, tt := range []struct {
name string
call func(context.Context) (any, error)
}{
{name: "list", call: func(ctx context.Context) (any, error) { return module.listCredentials(ctx, nil) }},
{name: "read", call: func(ctx context.Context) (any, error) { return module.getCredential(ctx, &nameInput{Name: "ops"}) }},
} {
t.Run(tt.name, func(t *testing.T) {
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: "admin", Kind: governance.KindUser, Org: governance.DefaultOrgID})
reply, err := tt.call(ctx)
if err != nil {
t.Fatalf("read: %v", err)
}
raw, err := json.Marshal(reply)
if err != nil {
t.Fatal(err)
}
body := string(raw)
if strings.Contains(body, secret) {
t.Fatalf("the route returned the secret: %s", body)
}
if !strings.Contains(body, "stored") {
t.Fatalf("the route didn't say the field is stored: %s", body)
}
})
}
}
// TestBearerTokenByIDRespectsScope guards the credential ID resolution boundary:
// BearerTokenByID and the bearer name lookups enforce the caller's scope, so an
// organisation scope with no person reaches only that organisation's and the
// instance's credentials, never another organisation's or any person's.
func TestBearerTokenByIDRespectsScope(t *testing.T) {
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
sealer, err := seal.New(id.String(), nil)
if err != nil {
t.Fatal(err)
}
sealBearer := func(owner store.Owner, name, token string) store.Credential {
plain, err := json.Marshal(map[string]Field{"token": {Value: token}})
if err != nil {
t.Fatal(err)
}
sealed, err := sealer.Seal(credentialLabel(owner, name, "bearer"), plain)
if err != nil {
t.Fatal(err)
}
return store.Credential{
ID: uuid.New(), Scope: owner.Scope, OrgID: owner.OrgID, PersonID: owner.PersonID,
Name: name, Kind: "bearer", Sealed: sealed,
}
}
sealBasic := func(owner store.Owner, name string) store.Credential {
plain, err := json.Marshal(map[string]Field{"username": {Value: "u"}, "password": {Value: "plainly-fake-password-for-test"}}) //nolint:gosec // test fixture for non-bearer refusal
if err != nil {
t.Fatal(err)
}
sealed, err := sealer.Seal(credentialLabel(owner, name, "basic"), plain)
if err != nil {
t.Fatal(err)
}
return store.Credential{
ID: uuid.New(), Scope: owner.Scope, OrgID: owner.OrgID, PersonID: owner.PersonID,
Name: name, Kind: "basic", Sealed: sealed,
}
}
instanceCred := sealBearer(store.Owner{Scope: store.ScopeInstance}, "inst-key", "plainly-fake-inst-token")
ownOrgCred := sealBearer(store.Owner{Scope: store.ScopeOrg, OrgID: "own"}, "own-org-key", "plainly-fake-own-org-token")
otherOrgCred := sealBearer(store.Owner{Scope: store.ScopeOrg, OrgID: "other"}, "other-org-key", "plainly-fake-other-org-token")
ownPersonCred := sealBearer(store.Owner{Scope: store.ScopePerson, PersonID: "me"}, "me-key", "plainly-fake-me-token")
otherPersonCred := sealBearer(store.Owner{Scope: store.ScopePerson, PersonID: "them"}, "them-key", "plainly-fake-them-token")
basicCred := sealBasic(store.Owner{Scope: store.ScopeOrg, OrgID: "own"}, "own-basic")
module := &Module{
log: slog.New(slog.DiscardHandler),
store: &fakeRegistry{creds: []store.Credential{
instanceCred, ownOrgCred, otherOrgCred, ownPersonCred, otherPersonCred, basicCred,
}},
sealer: sealer,
}
orgOwn := func(ctx context.Context) context.Context { return WithScope(ctx, Scope{OrgID: "own"}) }
personMe := func(ctx context.Context) context.Context {
return WithScope(ctx, Scope{OrgID: "own", PersonID: "me"})
}
instanceOnly := func(ctx context.Context) context.Context { return WithScope(ctx, Scope{}) }
principalMe := func(ctx context.Context) context.Context {
return governance.WithOrg(
governance.WithPrincipal(ctx, governance.Principal{IdentityID: "me", Kind: governance.KindUser, Org: "own"}),
"own",
)
}
stripped := func(ctx context.Context) context.Context {
return governance.WithOrg(governance.WithoutPrincipal(principalMe(ctx)), "own")
}
tests := []struct {
name string
scope func(context.Context) context.Context
credID uuid.UUID
wantToken string
wantName string
wantOK bool
}{
{name: "org scope reads instance credential", scope: orgOwn, credID: instanceCred.ID, wantToken: "plainly-fake-inst-token", wantName: "inst-key", wantOK: true},
{name: "org scope reads its own org credential", scope: orgOwn, credID: ownOrgCred.ID, wantToken: "plainly-fake-own-org-token", wantName: "own-org-key", wantOK: true},
{name: "org scope refuses another org credential", scope: orgOwn, credID: otherOrgCred.ID, wantOK: false},
{name: "org scope refuses any person credential", scope: orgOwn, credID: ownPersonCred.ID, wantOK: false},
{name: "org scope refuses non-bearer credential", scope: orgOwn, credID: basicCred.ID, wantOK: false},
{name: "org scope refuses nil UUID", scope: orgOwn, credID: uuid.Nil, wantOK: false},
{name: "org scope refuses unknown UUID", scope: orgOwn, credID: uuid.New(), wantOK: false},
{name: "person scope reads instance credential", scope: personMe, credID: instanceCred.ID, wantToken: "plainly-fake-inst-token", wantName: "inst-key", wantOK: true},
{name: "person scope reads own org credential", scope: personMe, credID: ownOrgCred.ID, wantToken: "plainly-fake-own-org-token", wantName: "own-org-key", wantOK: true},
{name: "person scope reads own person credential", scope: personMe, credID: ownPersonCred.ID, wantToken: "plainly-fake-me-token", wantName: "me-key", wantOK: true},
{name: "person scope refuses another person credential", scope: personMe, credID: otherPersonCred.ID, wantOK: false},
{name: "person scope refuses another org credential", scope: personMe, credID: otherOrgCred.ID, wantOK: false},
{name: "instance-only scope reads instance credential", scope: instanceOnly, credID: instanceCred.ID, wantToken: "plainly-fake-inst-token", wantName: "inst-key", wantOK: true},
{name: "instance-only scope refuses org credential", scope: instanceOnly, credID: ownOrgCred.ID, wantOK: false},
{name: "instance-only scope refuses person credential", scope: instanceOnly, credID: ownPersonCred.ID, wantOK: false},
{name: "principal context reads own person credential", scope: principalMe, credID: ownPersonCred.ID, wantToken: "plainly-fake-me-token", wantName: "me-key", wantOK: true},
{name: "principal context refuses another person credential", scope: principalMe, credID: otherPersonCred.ID, wantOK: false},
{name: "principal stripped context refuses person credential", scope: stripped, credID: ownPersonCred.ID, wantOK: false},
{name: "principal stripped context reads own org credential", scope: stripped, credID: ownOrgCred.ID, wantToken: "plainly-fake-own-org-token", wantName: "own-org-key", wantOK: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.scope(t.Context())
tok, err := module.BearerTokenByID(ctx, tt.credID)
if tt.wantOK {
if err != nil {
t.Fatalf("BearerTokenByID() error = %v, want nil", err)
}
if tok != tt.wantToken {
t.Errorf("BearerTokenByID() = %q, want %q", tok, tt.wantToken)
}
} else if err == nil {
t.Fatalf("BearerTokenByID() succeeded with %q, want error", tok)
}
name, ok, err := module.BearerCredentialName(ctx, tt.credID)
if err != nil {
t.Fatalf("BearerCredentialName() error = %v", err)
}
if ok != tt.wantOK || (ok && name != tt.wantName) {
t.Errorf("BearerCredentialName() = (%q, %v), want (%q, %v)", name, ok, tt.wantName, tt.wantOK)
}
names, err := module.BearerCredentialNames(ctx)
if err != nil {
t.Fatalf("BearerCredentialNames() error = %v", err)
}
gotName, inMap := names[tt.credID]
if inMap != tt.wantOK || (inMap && gotName != tt.wantName) {
t.Errorf("BearerCredentialNames()[%s] = (%q, %v), want (%q, %v)", tt.credID, gotName, inMap, tt.wantName, tt.wantOK)
}
})
}
}