beads/backend/http/open_test.go
Julian Knutsen 6c1848a77e
feat(backend/http): per-open credential is the only credential (S6f) (#7469)
* feat(backend/http): per-open credential is the only credential (S6f)

One process may open several beads HTTP projects, on one server or many,
each with its own credential (gc G7: per-city/per-rig credentials). The
ambient ladder is keyed by host[:port] and read from process-global env,
so two projects behind one host would share a token.

- bdhttp.Options gains Credential (CredentialProvider). Open, Handshake
  and Connect with it set go through the new httpclient.DialWithCredential
  / HandshakeWithCredential: no bearer ladder (BEADS_HTTP_TOKEN,
  BEADS_HTTP_TOKEN_COMMAND, credentials file), CA pinned to Target.CAFile
  (never BEADS_HTTP_CA_FILE), and the plaintext grant only from
  Target/DialOptions (never BEADS_HTTP_ALLOW_INSECURE).
- httpclient.OpenWith takes the same explicit path for a
  ProvidedCredential in backends.OpenOptions.
- Register panics on a non-nil Options.Credential: its dialer is
  process-wide.
- All package env reads go through one getenv seam so a test can prove
  the explicit door reads none. No process-wide credential cache exists;
  the provider lives on the store's own wire client.
- Package doc gains a "Credentials: one per open, never one per host"
  section.

Tests: two stores on one host (direct door and registry OpenWith) each
send their own token; explicit credential overrides poisoned ambient
token/command/credentials file; explicit open and handshake survive a
broken ambient env; Handshake with a Credential; getenv spy sees no reads
on the explicit door; Register refuses a credential.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(httpclient): S6f review fixups for the explicit door's plaintext refusal

Apply the two minor findings and two nits from the review of #7469:

- The explicit-credential door (DialWithCredential: OpenWith with a
  ProvidedCredential, and backend/http's Open and Handshake with
  Options.Credential) never reads BEADS_HTTP_ALLOW_INSECURE, but its
  plaintext refusal still said to set it. That refusal now names
  Target.AllowInsecureCredential (what `bd connect --allow-plaintext`
  records) and says an explicit credential takes no plaintext grant from
  the environment. The ambient ladder's refusal is unchanged.
- Pin that property at each production door. Every explicit-door test
  dialed loopback, where the guard returns before it reads the variable,
  so a door reverted to the env-consulting DialWith left them all green.
  TestExplicitCredentialIgnoresTheEnvPlaintextGrant drives OpenWith, Open
  and Handshake against a plain-http, non-loopback stand-in with
  BEADS_HTTP_ALLOW_INSECURE=1: each refuses before the wire and names the
  grant it honors, each sends its token once with
  Target.AllowInsecureCredential, and an ambient control shows the same
  environment does grant the built-in ladder. Reverting any one door to
  DialWith now fails that door's subtest, and the guard unit test checks
  the refusal text.
- Narrow the "reads no environment" docs to what holds: getenv is the
  reader for the variables that choose or grant a credential, and the
  explicit door reads none of them. The proxy variables, and on !unix with
  a CA file BEADS_ALLOW_INSECURE_CA_FILE_PERMISSIONS, are still read on
  every door; neither chooses a credential or grants plaintext.
- TestExplicitCredentialReadsNoEnvironment checked GetMetadata's error,
  which is nil on a failed handshake; it now checks the project id.

Behaviour change against main: OpenWith with a ProvidedCredential to a
plain-http, non-loopback server is refused even with
BEADS_HTTP_ALLOW_INSECURE=1, which main honored there. Grant it per
target with Target.AllowInsecureCredential (`bd connect --allow-plaintext`
records it in the workspace sidecar).

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:16:14 +00:00

234 lines
8.9 KiB
Go

package bdhttp_test
// Adapted from bd-enterprise backend/http/open_test.go under the MIT
// contribution: TestOpenNeedsNoWorkspaceOnDisk, the ServerSnapshot field-set
// drift guard, TestHandshakeReportsAWrongServer, and the activation sidecar
// round trip carry the same reasoning. Dropped or reshaped for OSS: the
// Credentials-hook tests (OSS takes a per-call Options.Credential instead;
// its tests are in per_open_credential_test.go), ServerInfo / lazy ServerContext (the OSS Store
// does not expose that interface), and ServerSnapshot's field set itself,
// which follows DESIGN.txt exactly (WireRevision in place of the ORM-backed
// field bd-enterprise used). No bd-enterprise-specific wording carries over.
import (
"context"
"errors"
"net/http"
"net/url"
"path/filepath"
"reflect"
"testing"
bdhttp "github.com/steveyegge/beads/backend/http"
)
// TestOpenNeedsNoWorkspaceOnDisk is the programmatic door's reason to exist.
// An embedder that already knows the server — it read the URL from its own
// config, or minted a per-tenant one — has no .beads directory to point the
// registry at, and creating one just to open a store would be a file written
// for the benefit of a lookup.
func TestOpenNeedsNoWorkspaceOnDisk(t *testing.T) {
hermeticEnv(t)
t.Setenv(bdhttp.TokenEnv, "127.0.0.1=direct-token")
server := newRecordingServer(t)
store, err := bdhttp.Open(context.Background(), server.target(t), bdhttp.Options{HTTPClient: server.Client()})
if err != nil {
t.Fatalf("Open: %v", err)
}
t.Cleanup(func() { _ = store.Close() })
got, err := store.GetMetadata(context.Background(), "_project_id")
if err != nil {
t.Fatalf("GetMetadata: %v", err)
}
if got != projectID {
t.Errorf("project id = %q, want %q", got, projectID)
}
server.wantAuthorization(t, "Bearer direct-token")
}
// TestOpenWithoutRegistrationStillDials pins the independence of the two
// doors: Open dials from the Options it is handed, with no dependence on
// Register ever having run.
func TestOpenWithoutRegistrationStillDials(t *testing.T) {
hermeticEnv(t)
t.Setenv(bdhttp.TokenEnv, "127.0.0.1=unregistered")
server := newRecordingServer(t)
store, err := bdhttp.Open(context.Background(), server.target(t), bdhttp.Options{HTTPClient: server.Client()})
if err != nil {
t.Fatalf("Open without Register: %v", err)
}
t.Cleanup(func() { _ = store.Close() })
if _, err := store.GetMetadata(context.Background(), "_project_id"); err != nil {
t.Fatalf("GetMetadata: %v", err)
}
server.wantAuthorization(t, "Bearer unregistered")
}
// TestHandshakeVerifiesBeforeAnythingIsWritten is the connect path's probe. A
// caller about to record a server has to be able to check it first, and doing
// that by opening a store would mean describing a workspace before knowing
// whether the server behind it is the right one.
func TestHandshakeVerifiesBeforeAnythingIsWritten(t *testing.T) {
hermeticEnv(t)
server := newRecordingServer(t)
snapshot, err := bdhttp.Handshake(context.Background(), server.target(t), bdhttp.Options{HTTPClient: server.Client()})
if err != nil {
t.Fatalf("Handshake: %v", err)
}
if snapshot.ProjectID != projectID {
t.Errorf("project id = %q, want %q", snapshot.ProjectID, projectID)
}
if snapshot.BdVersion != "9.9.9" {
t.Errorf("bd_version = %q, want the server's own", snapshot.BdVersion)
}
if snapshot.APIVersion != "v0" {
t.Errorf("api_version = %q, want the major this client speaks", snapshot.APIVersion)
}
if snapshot.WireRevision != 1 {
t.Errorf("wire_revision = %d, want the server's own", snapshot.WireRevision)
}
if len(snapshot.Capabilities) != 2 || snapshot.Capabilities[0] != "issues.get" {
t.Errorf("capabilities = %v, want the server's advertised list", snapshot.Capabilities)
}
}
// TestServerSnapshotExportsNoHostFacts is the drift guard on the curated
// snapshot, and it is a guard rather than a comment because the thing it
// prevents arrives by accident.
//
// The wire document is GENERATED from the OpenAPI spec. Aliasing it, or
// widening this struct to match it, would put a codegen bump — a renamed
// field, a new member — on a published Go API, and would hand every embedder
// the server's own filesystem paths. A multi-tenant caller holding another
// tenant's repo root is not a field it chose to read; it is a field it could
// not avoid.
func TestServerSnapshotExportsNoHostFacts(t *testing.T) {
want := map[string]bool{
"APIVersion": true,
"BdVersion": true,
"WireRevision": true,
"ProjectID": true,
"Capabilities": true,
}
typ := reflect.TypeOf(bdhttp.ServerSnapshot{})
if typ.PkgPath() != "github.com/steveyegge/beads/backend/http" {
t.Fatalf("ServerSnapshot is declared in %q: it must be this package's own struct, not an alias of the generated wire type", typ.PkgPath())
}
for i := 0; i < typ.NumField(); i++ {
name := typ.Field(i).Name
if !want[name] {
t.Errorf("ServerSnapshot exports %q, which is not on the curated set: server host paths, storage mode, logical database and the CLI schema version stay behind this door", name)
continue
}
delete(want, name)
}
for name := range want {
t.Errorf("ServerSnapshot no longer carries %q", name)
}
}
// TestHandshakeReportsAWrongServer pins the gate that makes the probe worth
// running: the pinned identity is checked, and the refusal is typed, so a
// caller can tell "that is the wrong server" from "that server is down"
// without matching text.
func TestHandshakeReportsAWrongServer(t *testing.T) {
hermeticEnv(t)
server := newRecordingServer(t)
target := server.target(t)
target.ExpectProjectID = "proj-somewhere-else"
_, err := bdhttp.Handshake(context.Background(), target, bdhttp.Options{HTTPClient: server.Client()})
if !errors.Is(err, bdhttp.ErrProjectMismatch) {
t.Fatalf("Handshake against a wrong server = %v, want ErrProjectMismatch", err)
}
var mismatch *bdhttp.ProjectMismatchError
if !errors.As(err, &mismatch) {
t.Fatalf("the refusal is not a *ProjectMismatchError: %v", err)
}
}
// TestHandshakeUsesTheSameLadderOpenWouldUse keeps the two doors on one
// credential path: a probe that authorized differently from the store it
// precedes would verify a server the store then cannot reach.
func TestHandshakeUsesTheSameLadderOpenWouldUse(t *testing.T) {
hermeticEnv(t)
t.Setenv(bdhttp.TokenEnv, "127.0.0.1=probe-token")
server := newRecordingServer(t)
if _, err := bdhttp.Handshake(context.Background(), server.target(t), bdhttp.Options{HTTPClient: server.Client()}); err != nil {
t.Fatalf("Handshake: %v", err)
}
server.wantAuthorization(t, "Bearer probe-token")
}
// TestTheActivationSidecarRoundTrips covers the workspace half of the
// surface: an embedder that writes a connected workspace for a later `bd`
// invocation, or reads the one `bd connect` already wrote, goes through
// these.
func TestTheActivationSidecarRoundTrips(t *testing.T) {
beadsDir := t.TempDir()
if _, err := bdhttp.LoadTarget(beadsDir); !errors.Is(err, bdhttp.ErrNotConnected) {
t.Fatalf("LoadTarget on an unconnected workspace = %v, want ErrNotConnected", err)
}
if removed, err := bdhttp.RemoveTarget(beadsDir); err != nil || removed {
t.Fatalf("RemoveTarget on an unconnected workspace = (%v, %v), want (false, nil)", removed, err)
}
base, err := url.Parse("https://beads.example.com")
if err != nil {
t.Fatal(err)
}
want := bdhttp.Target{BaseURL: base, ExpectProjectID: projectID}
if err := bdhttp.SaveTarget(beadsDir, want); err != nil {
t.Fatalf("SaveTarget: %v", err)
}
if got := bdhttp.TargetPath(beadsDir); got != filepath.Join(beadsDir, bdhttp.TargetFileName) {
t.Errorf("TargetPath = %q, want the sidecar beside metadata.json", got)
}
got, err := bdhttp.LoadTarget(beadsDir)
if err != nil {
t.Fatalf("LoadTarget: %v", err)
}
if got.String() != want.String() || got.ExpectProjectID != want.ExpectProjectID {
t.Errorf("round trip = %+v, want %+v", got, want)
}
removed, err := bdhttp.RemoveTarget(beadsDir)
if err != nil {
t.Fatalf("RemoveTarget: %v", err)
}
if !removed {
t.Error("RemoveTarget did not report the sidecar it deleted")
}
if _, err := bdhttp.LoadTarget(beadsDir); !errors.Is(err, bdhttp.ErrNotConnected) {
t.Errorf("LoadTarget after RemoveTarget = %v, want ErrNotConnected", err)
}
}
// TestABearerProviderIsBuildableFromThePublicSurface proves the default
// ladder is nameable, which is what lets a caller wrap or inspect it without
// reimplementing it.
func TestABearerProviderIsBuildableFromThePublicSurface(t *testing.T) {
hermeticEnv(t)
t.Setenv(bdhttp.TokenEnv, "127.0.0.1=ladder-token")
server := newRecordingServer(t)
var provider bdhttp.CredentialProvider = bdhttp.NewBearerProvider(server.target(t).BaseURL)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL, nil)
if err != nil {
t.Fatal(err)
}
if err := provider.Authorize(context.Background(), req); err != nil {
t.Fatalf("Authorize: %v", err)
}
if got := req.Header.Get("Authorization"); got != "Bearer ladder-token" {
t.Errorf("Authorization = %q, want the ladder's token", got)
}
}