Files
archie/modules/connectors/credentials_test.go
T
sam 1026d5e3b6 feat(httpapi): the API is described by an OpenAPI document generated with Huma (#2028)
Every /api operation is registered with Huma behind one admin-token
middleware, served at /api/openapi.json with a docs page at /api/docs,
and committed as api/openapi.yaml, which task check keeps current.
2026-11-16 20:05:05 +11:00

71 lines
1.9 KiB
Go

package connectors
import (
"context"
json "encoding/json/v2"
"log/slog"
"strings"
"testing"
"filippo.io/age"
"github.com/samcharles93/archie-core/modules/connectors/internal/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
// 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("default/ops/bearer", plain)
if err != nil {
t.Fatal(err)
}
module := &Module{
orgID: "default",
log: slog.New(slog.DiscardHandler),
store: &fakeRegistry{creds: []store.Credential{
{OrgID: "default", 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) {
reply, err := tt.call(t.Context())
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)
}
})
}
}