beads/internal/httpclient/open_with.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

84 lines
3.7 KiB
Go

// Written fresh for OSS beads S6 (no bd-enterprise source copied). A sibling
// enterprise implementation takes over credential selection with its own
// Options.Credentials hook; OSS instead threads the backends.OpenOptions.Credential
// seam S1 defined, which backend_credential.go's ResolveCredential (S2) was
// built to resolve but which, until this file, had no production caller.
// OpenWith is that caller.
package httpclient
import (
"context"
"fmt"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/backends"
)
// OpenWith is the backends.Backend.OpenWith hook backend/http (bdhttp)
// installs: the per-open seam that honors opts.Credential, opts.HTTPClient and
// opts.UserAgent for exactly THIS open, rather than the process-wide dialer
// Open/OpenReadOnly (NewFromConfig/NewReadOnlyFromConfig) use via the
// package-level registered dialer.
//
// That distinction is the whole reason this is a separate code path: the
// registered dialer is installed once, at process start, from one fixed
// DialOptions — exactly what a single-tenant CLI process wants, but useless
// to a multi-tenant embedder (gc, Gas City) that needs a different credential
// per workspace it opens in the same process. OpenWith dials fresh every call.
//
// base supplies the Register-time defaults (bdhttp.Options.UserAgent /
// HTTPClient) that a non-empty opts.UserAgent or non-nil opts.HTTPClient
// overrides for this one open.
//
// require is bdhttp.Options.RequireCredential: when true, a nil opts.Credential
// is ErrCredentialRequired rather than a silent fall-through to the ambient
// bearer ladder (BEADS_HTTP_TOKEN, BEADS_HTTP_TOKEN_COMMAND, the credentials
// file) — process-global ambient state a multi-tenant embedder cannot trust to
// name the right tenant. See ResolveCredential.
//
// A non-nil opts.Credential is the ONLY credential this open uses and the open
// reads no process environment on its account (DialWithCredential): two
// stores opened in one process against the same host, each with its own
// ProvidedCredential, each send their own token, and neither is affected by
// BEADS_HTTP_TOKEN, BEADS_HTTP_TOKEN_COMMAND, the credentials file or
// BEADS_HTTP_ALLOW_INSECURE. The provider is held by this store's wire client
// alone; nothing about it is cached per host.
//
// Unlike Open/OpenReadOnly, OpenWith never consults BEADS_HTTP_CA_FILE: CA
// resolution is pinned to target.CAFile alone via DialOptionsForTarget,
// because an embedder juggling several targets in one process must not have
// one silently renarrowed (or widened) by an ambient value an unrelated open
// exported. See DialOptionsForTarget's own doc for why that rung is skipped
// here specifically.
func OpenWith(ctx context.Context, beadsDir string, opts backends.OpenOptions, base DialOptions, require bool) (storage.DoltStorage, error) {
target, err := LoadTarget(beadsDir)
if err != nil {
return nil, err
}
creds, err := ResolveCredential(opts, target.BaseURL, require)
if err != nil {
return nil, err
}
dialOpts := base
if opts.UserAgent != "" {
dialOpts.UserAgent = opts.UserAgent
}
if opts.HTTPClient != nil {
dialOpts.HTTPClient = opts.HTTPClient
}
var conn *Conn
if opts.Credential != nil {
// An explicit credential is the ONLY one this open uses: no ambient
// env, no credentials file, no host:port ladder, and no
// BEADS_HTTP_ALLOW_INSECURE — see DialWithCredential.
conn, err = DialWithCredential(target, creds, dialOpts)
} else {
conn, err = DialWith(target, creds, DialOptionsForTarget(target, dialOpts))
}
if err != nil {
return nil, fmt.Errorf("dialing %s: %w", target, err)
}
s := New(target, conn, nil)
s.local = newLocalMetadata(beadsDir)
return s, nil
}