Files
archie/modules/connectors/credentials_test.go
T

72 lines
1.9 KiB
Go

package connectors
import (
"context"
json "encoding/json/v2"
"log/slog"
"net/http"
"net/http/httptest"
"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(http.ResponseWriter, *http.Request)
}{
{name: "list", call: module.listCredentials},
{name: "read", call: module.getCredential},
} {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/credentials", nil)
req.SetPathValue("name", "ops")
tt.call(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
body := rec.Body.String()
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)
}
})
}
}