* feat(backend/http): add the public HTTP backend door, cmd/bd registration, and bd connect internal/httpclient (S1-S5) had no production caller for the per-open backends.OpenOptions.Credential seam and no way for a workspace to actually select it. This slice (S6) closes both gaps in OSS's own idiom, without carrying over anything from bd-enterprise's gateway/box-login/storage-profile machinery: - backend/http (bdhttp) is the public, out-of-tree extension door: Register adds "http" to the backend registry and installs its transport, Open/ Handshake dial a server directly with no workspace on disk, and the registered OpenWith hook threads a per-call credential through beads.OpenBestAvailableWith for a multi-tenant embedder. - internal/httpclient/open_with.go is the backends.Backend.OpenWith hook itself: a fresh dial per open (opts.Credential/HTTPClient/UserAgent), distinct from the process-wide registered dialer Open/OpenReadOnly use. - cmd/bd/http_backend.go registers the backend for the OSS binary, so a workspace whose metadata.json names "http" opens through the ordinary OpenBestAvailable path; any other unregistered backend name still refuses. - cmd/bd/connect.go adds the minimal `bd connect <url>` command: a handshake that must succeed before anything is written, a per-user activation sidecar (.beads/http_target.json, never git-tracked), a plaintext-to- non-loopback refusal without --allow-plaintext, and a backend-switch refusal without --force. No credential is ever accepted on the command line or written to disk. - cmd/bd/doctor/gitignore.go requires the new http_target.json / http_local_metadata.json sidecars be git-ignored, the same way the proxied-server client info already is. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(storage): keep external-dependency blocking over http unless the server enforces it bd ready / bd close / claim hard-failed against the new http backend with "GetAllDependencyRecords not supported by the http backend": cmd/bd's storage chain wraps every opened store in the externaldeps policy decorator, which needs either the narrow ExternalDependencyQueryStore interface or the broad GetAllDependencyRecords fallback to resolve blocking state, and the http client implemented neither. The decorator now stays on for a remote store. It is skipped only when the server's handshake advertises policy.external_dependencies (the server enforces the policy before answering). Without that capability the client resolves blocking itself from the listing and chunked dependency reads, so an issue with an unresolvable external: dependency stays out of bd ready exactly as it does on a local backend; it is never silently skipped (DESIGN 3.6). Server-side enforcement is slice S10. storage.RemoteBackendStore marks a DoltStorage that is a pure network client of a remote bd serve; the http backend's Store satisfies it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(cmd/bd): suppress the CreatedBy stamp on create against a remote backend bd create failed against the http backend with "Issue.CreatedBy: ... is not expressible on the v0 wire": the http client's create role deliberately refuses any POPULATED CreatedBy/CreatedAt (the creation stamp belongs to the server's own journal entry, not a value the caller supplies), but cmd/bd/create.go unconditionally stamped getActorWithGit() onto every created issue regardless of backend. The refusal only fires on a non-zero field, so suppressing the stamp for a registered remote backend is wire- compatible by construction, not a workaround. Add isRemoteBackendWorkspace()/creationActorStamp() to backend_support.go, reusing the existing resolveCommandBeadsDir(dbPath) + backendNameForErrorFraming(beadsDir) + backends.IsRemote(name) pattern already used elsewhere in that file, and use creationActorStamp() at both create.go call sites (the dry-run preview and the real create). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(backend/http): add an embedder linkage test over public packages only Prove the one promise S6 makes to an out-of-tree embedder (gc is the motivating case): importing nothing but github.com/steveyegge/beads, .../backend, and .../backend/http is enough to register the http backend, open a workspace through it with a per-call credential (beads.OpenBestAvailableWith), and read through the role-accessor surface (IssueReader().Ready) against a stand-in server — with no internal/... import in the test file, which is itself the thing under test. Written fresh for OSS beads S6; no bd-enterprise source copied. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * docs: document the HTTP backend, bd connect, and the public backend/http door Add a README section with bd connect usage, the credential ladder, and the plaintext/force refusals, plus a CHANGELOG entry for the new cmd/bd registration, bd connect command, and the public backend/http package. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(httpclient): address bee-ghosttrack review on #7288 (connect --clear restore, persisted --allow-plaintext) Closes the CHANGES_REQUESTED items on the HTTP backend door PR: - Must-fix: `bd connect --force` over a workspace with its own local (dolt) identity now has writes succeed instead of tripping "workspace identity mismatch detected" — validateWorkspaceIdentity (cmd/bd/main.go) and the sidecar (internal/httpclient/metadata.go) now compare against the http backend's own pinned project id. New e2e regression test TestE2E_ForceConnectWritesSucceedDespiteDifferingLocalProjectID (backend/http/e2e_cli_test.go), confirmed to fail without the fix. - Should-fix 1: `bd connect --clear` now restores metadata.json's backend selection to whatever it was before this workspace connected, instead of leaving it pinned to "http" with no sidecar to dial. Target.PreviousBackend (internal/httpclient/target.go) is recorded by Attach (backend/http/attach.go) and round-tripped through the sidecar; connect.go reads it back on --clear. New tests in backend/http/attach_test.go and internal/httpclient/target_test.go. - Should-fix 2: `bd connect --allow-plaintext` is now remembered in the sidecar (Target.AllowInsecureCredential), scoped to the target it was granted for, so every later ordinary command's dial honors the grant without also needing BEADS_HTTP_ALLOW_INSECURE=1 set (internal/httpclient/dial.go). Fixed BearerProvider.warnInsecure's wording (internal/httpclient/credential.go), which used to assert a token was being sent immediately above a refusal saying the opposite. New test TestDialWithWiresTargetAllowInsecureCredentialThroughToTheGuard. - Should-fix 3: README no longer claims HTTP mode works "exactly as with a local database" — it lists what round-trips and points at the divergence ledger (engdocs/design/http-divergence-ledger.md) for what differs. - Verified, no code change needed: Attach already handles an existing metadata.json project_id coherently (only cfg.Backend is touched); bd show's isNotFoundErr already matches a http 404 (issueops.ErrNotFound is storage.ErrNotFound, the same sentinel a local miss uses), so it reads as "not found" rather than an unsupported-operation error. - Doc fix: guardInsecureCredential's doc comment and refusal message were stale against should-fix 2's own change — they named only DialOptions.AllowInsecureCredential and the env var as ways to get past the guard, not the newly-persisted target.AllowInsecureCredential or `bd connect --allow-plaintext` itself. Gates run clean on this branch: go build/vet, go test over internal/httpclient, backend/http (including the full e2e suite under BEADS_TEST_EMBEDDED_DOLT=1) and cmd/bd, make api-check, make ci-pr-lint, make bazel-sync-check, bazel run //:gazelle -- -mode=diff, and internal/storage's TestEveryLegWiresEveryRoleContract. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(backend/http): give the e2e server the BatchGetter role #7248 added to the required set Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix: exact http ready totals, connect capability row, doc corrections Ready totals over http (review major). externaldeps drops externally blocked rows client-side for a store that cannot express ExcludeIDs (today httpclient.Store). dropBlockedIssuesWithCounts compacted the fetched rows in place (`kept := rows[:0]`), and readyWorkWithCountsAndTotal then re-scanned `rows` to count the blocked rows it had dropped. By then the compaction had overwritten them, so whenever a blocked row came back ahead of a kept one the re-scan undercounted the drops: `bd ready --json`'s total and text-mode `bd ready`'s count (CountReadyWork) overstated ready work against any server that does not advertise policy.external_dependencies. Both drop helpers now copy into a fresh slice, and dropBlockedIssuesWithCounts reports how many rows it dropped, so nothing re-scans `rows`. fakeStore gains an excludeIDsUnsupported switch that, like httpclient.Store, refuses any ExcludeIDs. The new TestReadyTotalsWhenStoreCannotExpressExcludeIDs puts the blocked issue first and checks the page and total for an unlimited filter, a Limit whose bumped window holds the whole set, and one the set outgrows, then CountReadyWork. TestServedReadyExcludesUnsatisfiedExternalDependencyWithoutServerCapability now also checks the total and count against a real in-process bd serve. Against the previous decorator the unit test's unlimited case reports total and count 3 beside a 2-row page, and the served test 2 beside a 1-row page. Review minors, all wording: - `bd connect --clear` pointed at `bd config set backend dolt`, which writes the database's config table rather than metadata.json and, in the state that prints the advice (metadata.json says http, sidecar gone), fails the pre-open identity check. It now says to reconnect, or to set "backend" in metadata.json. - CHANGELOG described the superseded skip-at-wrap design; it now describes the always-wrap decorator with its capability probe and client-side drop. - RemoteBackendStore's docs no longer name a consumer; nothing in-tree checks it. - creationActorStamp's comment pointed at backend/http/lifecycle.go; the refusal lives in internal/httpclient/lifecycle.go. Capability row. TestProxyCapabilityRegistryCoversCommandTree fails at 60b7128204: the command tree walk finds `connect`, which this PR adds, with no row in cmd/bd/capability_registry.go ("1 command(s) have no proxied-server capability row: connect"). The test predates the PR (#6582 is an ancestor of its base) and passes at the base dabf20ef07. connect opts out of store init (skipStoreAnnotation) and never reaches the store factory, so it belongs in proxyPermittedPaths beside init and setup. The row is runtime-neutral: validateProxyRegistryBeforeProvider returns nil both for a permitted row and for a path with no row, so this only records the policy decision the coverage test asks for. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(httpclient): regenerate the divergence ledger doc after the S5 rebase The rebase onto feat/http-client-batch-apply's rebased head (S3/S4 merged to main) left the ledger doc's summary line at a conflict-resolution placeholder count. Regenerate it from internal/httpclient/encode.Ledger() so TestDivergenceLedgerDocMatchesLedger passes against the merged ledger.go. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(backend/http): host-scope S6's test BEADS_HTTP_TOKEN values Rebasing onto feat/http-client-batch-apply's rebased head pulled in main's #7247 review-fix round, which made BEADS_HTTP_TOKEN/BEADS_HTTP_TOKEN_COMMAND host-scoped (host[:port]=value, scopedEnvValue/parseScopedEnv in internal/httpclient/credential.go) after S6's own tests were written against the unscoped form. Five S6 tests across backend/http/open_test.go, backend/http/register_test.go and internal/httpclient/insecure_credential_guard_test.go set a bare token and now hit the new ladder's "an unscoped token would be used for whatever server..." refusal. Scope each to the test server's actual host (127.0.0.1 for the httptest.Server-backed recording servers, nonLoopbackTestHost for the insecure-credential-guard integration tests) so they exercise the same ladder rung as before. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * build(bazel): resync go_srcs/repo_files for the new backend/http and test/embedder packages make bazel-sync-check failed after the rebase: tools/bazel/go_srcs.py's managed repo_files filegroups for backend/http and test/embedder (new in this slice) hadn't been generated, and the root BUILD.bazel / tools/bazel's release_cross_build target list hadn't picked up the new package either. Regenerate with `python3 tools/bazel/go_srcs.py`; `bazel run //:gazelle -- -mode=diff` reports no further drift. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(cmd/bd): route swarm's create paths through creationActorStamp after main's currentActor change Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix: police http's served roles, refuse close --claim-next, exact totals External-dependency policy over http (review blocker). cmd/bd wraps every store, http included, in the externaldeps decorator, and its role accessors rebuilt each role over legacy methods httpclient.Store refuses (SearchIssuesWithCounts, GetDependencyTree, ClaimReadyIssue, IsBlocked, GetDependencyRecordsForIssues, BatchCloserWithPolicy). Over http `bd list`, `bd dep tree`, `bd ready --claim` and a policed `bd close` therefore exited 1, even in a workspace with no external dependency. Over a store that reports storage.RemoteBackendStore, each accessor now starts from the inner store's served role and layers the policy around it (remote_roles.go): - IssueReader serves List and Get from the served reader; Ready and a ready-flagged List go through the policy reader. - IssueClaimer refuses an externally blocked claim before the served one. - ReadyClaimer uses the served claim-next while no issue, closed ones included, holds an unsatisfied external ref. Otherwise it claims down the policy's filtered ready page through the served Claimer, because claimNext cannot exclude ids. - BlockingAnnotator and TreeWalker merge external blockers into, or add external leaves to, the served answer. They read only the named issues' edges through the served EdgeReader, 100 anchors per call. - BatchCloser refuses externally blocked open items and sends the rest to the served closer as one request. --force keeps the base path, and a next claim is refused whole (below). RemoteBackendStore now only chooses this composition. Whether the policy runs is still ServerEnforcesExternalDependencyPolicy's answer (design 3.6), and the storage and httpclient docs say so. remote_roles_test.go drives each role over a store that refuses the legacy methods. TestServedRolesKeepTheExternalDependencyPolicy runs every role over a real http server. TestE2E_BDOverHTTP adds `bd list` (JSON, text and --deps), `bd dep tree`, `bd ready`, `bd list --ready`, `bd ready --claim`, a refused `bd close --claim-next`, and a refused then forced close of an externally blocked issue. At the fixup before the composition, the role tests then written (bar one pin of the next-claim refusal, since replaced) and the e2e's list, dep tree, claim and close steps of that round were red. Close with a next claim over http (review major). batchCloseIssues has no member for a next claim, so httpclient refuses any ClaimNext before the dial (ledger W-CloseBatchRequest.ClaimNext). Over http `bd close --claim-next` therefore always exited 1 and closed nothing. With an external ref anywhere in the workspace (a closed holder or --force included) the decorator's base path failed first, with an opaque `BatchCloserWithPolicy` unsupported. remoteBatchCloser now refuses a next claim whole, before it reads or closes anything, and the error says nothing closed: close without --claim-next, then run `bd ready --claim`. A request that is invalid on every backend is still ErrValidation first, and a server that enforces the policy itself gets the request unchanged. Composing close-then-claim was not taken. The role closes and claims in one transaction and answers with an error or with outcomes, never both, so a claim that failed after the closes committed could not report them; httpclient refuses the same request for that reason. A CLI-level close then claim that can report a partial result is the follow-up. The review also suggested leaving closed holders out of the blocking state. They stay in, as on every backend: a closed issue is never a claim candidate, so they change no claim, and leaving them out would cost a status read per holder because the wire has no batch read. TestRemoteBatchCloserRefusesANextClaim covers an open holder, a closed one, none, --force, and a server that enforces the policy; with the previous remote_roles.go the first four are red. TestRemoteBatchCloserValidatesANextClaimFirst keeps ErrValidation ahead of the refusal. Dropping the filter check, or loading the blocking state before refusing, is each caught. TestE2E_BDOverHTTP runs `bd close --claim-next` while the holder is open and again after it is closed; with the previous remote_roles.go both steps are red and print `operation "BatchCloserWithPolicy" not supported`. Ledger F-close no longer says --claim-next is served, and no longer names a renderEscapedHTTPRefusal that does not exist. L14 says the decorator's claim leg applies while any issue, closed or not, holds an unsatisfied ref. The README and CHANGELOG mention the refusal, and the decorator's IsBlocked, IsBlockedBatch and CloseIssueChecked say they refuse over a store that serves its roles. Ready totals with external blockers (review majors, both rounds). CountReadyWork asks for Limit=0, and readyWorkWithCountsAndTotal took any Limit<=0 to mean "the window held the whole ready set". Over http a zero limit never reaches the wire: the server answers with its default page (workapi.DefaultReadyLimit, 100 rows) while the bridge reports the full total, so past 100 ready issues text-mode `bd ready` kept counting externally blocked issues beyond the first page. Exactness is now judged from the rows that came back (total <= len(rows)). A window that holds only part of the set no longer subtracts every externally blocked id in the workspace either: that set includes closed issues and ones the filter excludes, none of them in the total, so the count came out low. It refetches the filtered ready set, ids only, and subtracts the blocked rows in it; the old bound is left as the fallback for a refetch that comes back truncated too. fakeStore gains the bridge's shape: a defaultPage for a zero Limit, with GetReadyWorkWithCountsAndTotal still totalling the whole set. TestReadyTotalsWhenAnUnlimitedFetchComesBackAsOnePage (a blocked issue past the first page) and TestReadyTotalsIgnoreExternallyBlockedIssuesOutsideTheReadySet (a blocked issue outside the ready set) have served counterparts, TestServedReadyCountExcludesAnExternallyBlockedIssuePastTheDefaultPage and TestServedReadyTotalIgnoresAnExternallyBlockedIssueOutsideTheFilter (an assignee filter). The first pair is red on the Limit<=0 condition, the second on the subtract-every-blocked-id branch. Docs: ledger L8 now says which decorator paths take the chunked GetAllDependencyRecords scan and which read only named edges, and L14 records the decorator's composed claim leg; http-divergence-ledger.md is regenerated. The README names the verified commands, and the README and CHANGELOG say how cross-repo dependencies behave over http. Minors from the reviews: - Drop creationActorStamp/isRemoteBackendWorkspace and restore the base stamps at all 11 sites. The arm guarded a refusal the http create role does not make: since #7247 (actorStampedCreateMember) it accepts a CreatedBy naming the actor. Because the arm keyed on the CWD workspace, a create routed from an http workspace into a local one (--repo) stored an empty created_by. TestE2E_BDOverHTTP checks created_by for a direct and a routed create; the routed check is red with the arm restored. The CHANGELOG sentence describing the arm goes with it. - bdhttp.Attach decides a reconnect from the raw metadata.json backend. GetBackend() reads an unregistered "http" as the dolt default, so in an embedder that never called Register a reconnect recorded dolt as the backend --clear restores. The reconnect test now seeds postgres (TestAttachOnAReconnectKeepsTheEarlierPreviousBackend) and is red with the old check. - README, CHANGELOG and `bd connect --help` document the host-scoped BEADS_HTTP_TOKEN=host[:port]=<token> form (and _COMMAND), and say why a bare value is refused. - bdhttp.Attach also gitignores http_local_metadata.json, the per-user file the store writes on first use, so an embedder activating through Attach alone gets the coverage `bd connect` already had. TestAttachGitignoresBothPerUserFiles pins it. - ServerSnapshot.WireRevision's doc names the handshake's actual check instead of a comparison `bd connect` never makes. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend/http): give the e2e server the LeaseReclaimer role #7412 added to the required set main's #7412 made LeaseReclaimer part of httpapi's required role set and updated main's own binders, but backend/http's e2eServeConfig predates it, so every TestE2E_* that binds the in-process server failed with "httpapi: no database source". Same shape as e63f7da36a for BatchGetter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(externaldeps): lead the --claim-next refusal with the wire fact; pin the L14 leg and connect --clear Review i4 minors/nits on the stranded fixup, carried as a separate commit: - remoteBatchCloser's --claim-next refusal now says the served batch close cannot carry a next claim (the real cause), rather than blaming externally blocked work a workspace may not have. The test asserts the reason, that nothing closed, and the remedy. - GetDependencyTree's doc says a served store refuses its reads and that bd dep tree goes through remoteTreeWalker. - README no longer gives `bd ready --claim` local semantics everywhere; it names when ledger row L14's two-step claim applies. - TestE2E_BDOverHTTP claims once more after the external-ref holder is closed, pinning L14's composed leg (a mutant whose composed leg claims nothing turns only that step red). - New TestE2E_ConnectClearRestoresThePreviousBackend: --clear restores the prior dolt backend with its identity intact, removes the sidecar, and a second --clear changes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend/http): run the served e2e under bazel //backend/http:http_test declared no bd binary, so under bazel the e2e files (bazeltest.PrebuiltBD) failed on BEADS_TEST_BD_BINARY with BEADS_HTTP_TEST_REQUIRED=1 and only "passed" by skipping otherwise. It now takes //cmd/bd:bd_for_tests (visibility added), size large / timeout long: the served cases run 4-5 minutes on the farm under BEADS_TEST_EMBEDDED_DOLT=1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend/http): run the e2e tier in the embedded lane as http_served_test The previous commit gives //backend/http:http_test the bd binary, but no lane sets BEADS_TEST_EMBEDDED_DOLT for it, so its TestE2E_* cases still skipped in CI. http_served_test runs the same test binary through //tools/bazel:go_test_variant.sh with BEADS_TEST_EMBEDDED_DOLT=1 and BEADS_HTTP_TEST_REQUIRED=1 (a skip fails it). It is tagged embedded, so the --config=embedded lane runs it and prcore/ci keep their current shape, the same split as //internal/httpclient:httpclient_served_test. Under plain --config=embedded all 8 TestE2E_* cases run, none skipped. TestBazelRetiredLanesCannotBeNarrowed pins every embedded-lane target's args and env; the new target is added to that pin list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(externaldeps): cap the ready rows kept, not the bumped http window An http store cannot carry ExcludeIDs on listReadyWork, so readyExclusion fetches the unfiltered ready set, widened by len(blocked) under a limit, and drops the externally blocked rows client-side. The http ready bridge enforced MaxRows on the rows it fetched rather than the rows kept. So `bd ready --limit N --max-rows N` exited 2 whenever more than N issues were ready and any workspace issue held an unsatisfied external blocker (a closed one counts), even with no blocked row in the window, and a blocked row in the page counted against the cap although the caller never sees it. Locally neither can happen: issueops enforces the cap on the filtered page it delivers, after the Limit trim (finishReadyWorkWithCounts). The client-side path now takes MaxRows off the inner query and enforces the caller's cap on the rows it keeps, after the trim, in all three listings. The inner query keeps the cap whenever nothing is dropped client-side. MaxRows never rode the wire, so the fetch is the size it was; refetchedReadyTotal already clears the cap on its own over-fetch. TestServedReadyMaxRowsCountsTheRowsKept runs the decorator over a served http store holding three free issues and one externally blocked one: a cap equal to the limit, a cap under a wider limit, an unlimited listing, and a kept page wider than the cap, which must still return ErrTooManyRows with the caller's cap and source. Each case runs through GetReadyWork, GetReadyWorkWithCounts and GetReadyWorkWithCountsAndTotal. Leaving the cap on the inner query fails the first three cases in all three methods, dropping the re-applied cap fails the fourth, and enforcing it before the Limit trim fails the first. 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 Sonnet 5 <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
324 lines
15 KiB
Go
324 lines
15 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
bdhttp "github.com/steveyegge/beads/backend/http"
|
|
"github.com/steveyegge/beads/cmd/bd/doctor"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/httpclient"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
)
|
|
|
|
// Written fresh for OSS beads S6 (no bd-enterprise source copied): richer
|
|
// connect flows (interactive login, server discovery, storage-profile
|
|
// prompts) are explicitly out of scope for this lift (see DESIGN.txt sec
|
|
// 3.1). This is the minimal OSS connect DESIGN.txt asks for: verify a
|
|
// server, then record it.
|
|
|
|
var (
|
|
connectExpectProjectID string
|
|
connectCAFile string
|
|
connectForce bool
|
|
connectAllowPlaintext bool
|
|
connectClear bool
|
|
)
|
|
|
|
var connectCmd = &cobra.Command{
|
|
Use: "connect [url]",
|
|
GroupID: "setup",
|
|
Short: "Attach this workspace to a remote bd serve over HTTP",
|
|
Long: `Attach this workspace to a remote bd serve over HTTP.
|
|
|
|
bd connect verifies the server first (a handshake: api_version, wire
|
|
revision, and — when --expect-project-id is given — workspace identity) and
|
|
writes nothing until that succeeds. On success it writes the per-user
|
|
activation sidecar (.beads/http_target.json, never git-tracked) and sets this
|
|
workspace's metadata.json to backend "http".
|
|
|
|
A credential, if the server requires one, is never read from a flag or
|
|
recorded on disk by this command. It comes from the same ladder every http
|
|
request does: the BEADS_HTTP_TOKEN environment variable, then
|
|
BEADS_HTTP_TOKEN_COMMAND (a helper that prints a token), then the credentials
|
|
file's [host:port] section, then no credential at all — which the tip OSS
|
|
server's loopback-trust posture answers legitimately, not as a failure. Both
|
|
variables name the server they are for, host[:port]=value (for example
|
|
BEADS_HTTP_TOKEN=bd.example.com=<token>); a bare value is refused, since it
|
|
would be sent to whatever server the workspace's http_target.json names.
|
|
|
|
--allow-plaintext IS recorded on disk, in the sidecar alongside the server
|
|
url: once granted here, a credential may cross this one server in the clear
|
|
on every later command too, not only this one, without re-passing the flag
|
|
or setting BEADS_HTTP_ALLOW_INSECURE=1 yourself. A later connect to a
|
|
DIFFERENT url starts that grant over; it is never carried forward to a
|
|
server it was not given for.
|
|
|
|
Examples:
|
|
bd connect http://127.0.0.1:8080 # loopback, no TLS needed
|
|
bd connect https://bd.example.com --expect-project-id p1 # pin workspace identity
|
|
bd connect https://bd.example.com --ca-file ./ca.pem # trust only this CA
|
|
bd connect --clear # forget the server, restoring the previous backend
|
|
`,
|
|
Args: cobra.MaximumNArgs(1),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
// connect is how an http workspace gets its FIRST local state: there is
|
|
// no local database to find yet (none ever exists for this backend), so
|
|
// it opts out of PersistentPreRunE's store init the same way doctor,
|
|
// schema and worktree do, via skipStoreAnnotation rather than editing
|
|
// the central noDbCommands list (see that annotation's own doc comment).
|
|
Annotations: map[string]string{skipStoreAnnotation: "1"},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
evt := metrics.NewCommandEvent("connect")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
// connect, like init, is how a workspace gets its FIRST local state:
|
|
// there may be no .beads directory yet anywhere above the CWD, so
|
|
// this resolves the same way init's own target does (BEADS_DIR, then
|
|
// the worktree fallback, then CWD/.beads) rather than
|
|
// beads.GetRepoContextAllowingNoGit(), which answers "no .beads
|
|
// directory found" with a hard error because every OTHER caller of
|
|
// it (bd context, the proxied context route) is read-only and has
|
|
// nothing to create.
|
|
beadsDir := resolveInitBeadsDir()
|
|
if beadsDir == "" {
|
|
return HandleError("cannot resolve a workspace directory for connect (set BEADS_DIR, or run from inside a writable directory)")
|
|
}
|
|
|
|
if connectClear {
|
|
if len(args) != 0 {
|
|
return HandleError("connect --clear takes no url argument")
|
|
}
|
|
// Read the sidecar's PreviousBackend BEFORE removing it: it is
|
|
// the backend metadata.json selected right before this
|
|
// workspace connected (bee-ghosttrack CHANGES_REQUESTED on
|
|
// #7288, should-fix 1). A missing sidecar (ErrNotConnected) or
|
|
// any other read failure just means there is nothing to
|
|
// restore, matching the "nothing was configured" case below.
|
|
priorTarget, loadErr := httpclient.LoadTarget(beadsDir)
|
|
removed, err := httpclient.RemoveTarget(beadsDir)
|
|
if err != nil {
|
|
return HandleError("clearing %s: %v", httpclient.TargetFileName, err)
|
|
}
|
|
restoredBackend := ""
|
|
if removed && loadErr == nil && priorTarget.PreviousBackend != "" {
|
|
if cfg, cfgErr := configfile.Load(beadsDir); cfgErr == nil && cfg != nil && cfg.GetBackend() == httpclient.Backend {
|
|
cfg.Backend = priorTarget.PreviousBackend
|
|
if saveErr := cfg.Save(beadsDir); saveErr == nil {
|
|
restoredBackend = priorTarget.PreviousBackend
|
|
}
|
|
// A save failure is not fatal to --clear: the sidecar is
|
|
// already gone (or never existed), which is the part
|
|
// that actually detaches the workspace from the
|
|
// server; metadata.json just keeps saying "http" and
|
|
// the manual fallback message below still applies.
|
|
}
|
|
}
|
|
if jsonOutput {
|
|
return outputJSON(map[string]any{"cleared": removed, "restored_backend": restoredBackend})
|
|
}
|
|
switch {
|
|
case restoredBackend != "":
|
|
fmt.Printf("Removed %s and restored this workspace's backend selection in metadata.json to %q.\n", httpclient.TargetFileName, restoredBackend)
|
|
case removed:
|
|
// Not `bd config set backend ...`: that writes the database's
|
|
// own config table, never metadata.json, and must first open
|
|
// the very store this workspace can no longer reach.
|
|
fmt.Printf("Removed %s. This workspace's backend selection in metadata.json is unchanged; run `bd connect <url>` to reconnect, or set \"backend\" in %s to pick a different backend.\n", httpclient.TargetFileName, filepath.Join(beadsDir, configfile.ConfigFileName))
|
|
default:
|
|
fmt.Println("No http target was configured; nothing to clear.")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if len(args) != 1 {
|
|
return HandleError("connect requires a <url> argument (or --clear)")
|
|
}
|
|
rawURL := args[0]
|
|
|
|
parsed, err := url.Parse(rawURL)
|
|
if err != nil || parsed.Host == "" {
|
|
// A parse failure has no *url.URL to redact, so there is nothing
|
|
// safer to print than the raw argument; a parse that succeeded
|
|
// but found no host (e.g. "http://user:pass@/path") does have
|
|
// one, and .Redacted() strips its userinfo before this message
|
|
// repeats it.
|
|
if err != nil {
|
|
return HandleError("invalid url %q", rawURL)
|
|
}
|
|
return HandleError("invalid url %q", parsed.Redacted())
|
|
}
|
|
// redactedURL is what every print/wrap of the connect target uses from
|
|
// here on: rawURL is the user's literal argument, which — unlike
|
|
// parsed.Redacted() — still carries a userinfo password verbatim
|
|
// (http://user:pass@host/...) if one was pasted into the URL. Nothing
|
|
// past this point should format rawURL again.
|
|
redactedURL := parsed.Redacted()
|
|
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
|
return HandleError("url %q must use http or https, got %q", redactedURL, parsed.Scheme)
|
|
}
|
|
// L1 (host safety, functional requirement 1): a bearer credential sent
|
|
// in the clear to anything but loopback is a token leaked to every
|
|
// network hop between here and the server. Refuse by default; require
|
|
// an explicit opt-in rather than a warning nobody reads at connect
|
|
// time, since connect time is the one moment a human is looking.
|
|
if parsed.Scheme == "http" && !configfile.IsLocalHostString(parsed.Hostname()) && !connectAllowPlaintext {
|
|
return HandleError(
|
|
"refusing to connect to %s over plain http: %s is not loopback, and any bearer credential this workspace sends would cross the network unencrypted; use https://, or pass --allow-plaintext to connect anyway",
|
|
redactedURL, parsed.Hostname())
|
|
}
|
|
|
|
existing, err := configfile.Load(beadsDir)
|
|
if err != nil {
|
|
return HandleError("reading %s: %v", configfile.ConfigFileName, err)
|
|
}
|
|
if existing != nil {
|
|
if b := existing.GetBackend(); b != "" && b != httpclient.Backend && !connectForce {
|
|
return HandleError("this workspace already selects backend %q; pass --force to switch it to %q (existing local data, if any, is left in place but will no longer be used)", b, httpclient.Backend)
|
|
}
|
|
}
|
|
|
|
// effectiveExpectedProjectID carries the PRIOR sidecar's pin forward
|
|
// into this run, so a bare re-connect (no --expect-project-id) to a
|
|
// DIFFERENT server still gets caught: without this, every connect
|
|
// built a brand-new Target starting from connectExpectProjectID alone
|
|
// (almost always "" on a re-run), which the "nothing was pinned yet"
|
|
// branch below would then adopt from whatever server just answered —
|
|
// silently re-pinning the workspace to a different project with no
|
|
// refusal at all. --force (regardless of --expect-project-id) drops
|
|
// the prior pin entirely and lets the new server's answer win, which
|
|
// is the explicit "I mean it" escape hatch this check otherwise has
|
|
// none of.
|
|
effectiveExpectedProjectID := connectExpectProjectID
|
|
if !connectForce {
|
|
priorTarget, loadErr := httpclient.LoadTarget(beadsDir)
|
|
if loadErr != nil && !errors.Is(loadErr, httpclient.ErrNotConnected) {
|
|
return HandleError("reading existing %s: %v", httpclient.TargetFileName, loadErr)
|
|
}
|
|
if loadErr == nil && priorTarget.ExpectProjectID != "" {
|
|
switch {
|
|
case effectiveExpectedProjectID == "":
|
|
effectiveExpectedProjectID = priorTarget.ExpectProjectID
|
|
case effectiveExpectedProjectID != priorTarget.ExpectProjectID:
|
|
return HandleError(
|
|
"this workspace is already connected to project %q; pass --force to re-pin it to %q, or pass --expect-project-id %q to confirm the existing pin",
|
|
priorTarget.ExpectProjectID, effectiveExpectedProjectID, priorTarget.ExpectProjectID)
|
|
}
|
|
}
|
|
}
|
|
|
|
var caFile string
|
|
dialOpts := httpclient.DialOptions{
|
|
UserAgent: "bd/" + Version + " " + httpclient.WireUserAgentSuffix,
|
|
// The L1 refusal above already gated this exact combination
|
|
// (plain http, non-loopback) on --allow-plaintext; carrying the
|
|
// flag through here keeps this command's own Handshake probe
|
|
// (which dials with the same ambient bearer ladder a credential
|
|
// might come from) from re-refusing what the flag already
|
|
// permitted. See DialOptions.AllowInsecureCredential.
|
|
AllowInsecureCredential: connectAllowPlaintext,
|
|
}
|
|
if connectCAFile != "" {
|
|
abs, err := filepath.Abs(connectCAFile)
|
|
if err != nil {
|
|
return HandleError("resolving --ca-file %q: %v", connectCAFile, err)
|
|
}
|
|
caFile = abs
|
|
// Verify against EXACTLY this file, bypassing BEADS_HTTP_CA_FILE
|
|
// entirely: a connect run with the env var set to a different CA
|
|
// must not verify against the env's CA while writing the flag's
|
|
// unverified one to the sidecar (see DialOptionsForFile's doc).
|
|
dialOpts, err = httpclient.DialOptionsForFile(caFile, dialOpts)
|
|
if err != nil {
|
|
return HandleError("--ca-file %q: %v", connectCAFile, err)
|
|
}
|
|
}
|
|
|
|
target := httpclient.Target{BaseURL: parsed, ExpectProjectID: effectiveExpectedProjectID, CAFile: caFile, AllowInsecureCredential: connectAllowPlaintext}
|
|
|
|
ctx := context.Background()
|
|
snapshot, err := httpclient.Handshake(ctx, target, dialOpts)
|
|
if err != nil {
|
|
return HandleError("connecting to %s: %v", redactedURL, err)
|
|
}
|
|
if target.ExpectProjectID == "" {
|
|
// Nothing was pinned (a genuine first connect, or --force
|
|
// dropped the prior pin above): record what the server said so
|
|
// this workspace is tied to THIS project from here on.
|
|
target.ExpectProjectID = snapshot.ProjectId
|
|
} else if target.ExpectProjectID != snapshot.ProjectId {
|
|
// effectiveExpectedProjectID is either an explicit
|
|
// --expect-project-id or the prior sidecar's pin carried
|
|
// forward above; either way the server disagrees, and --force
|
|
// was not given (the only way to reach this line with a
|
|
// mismatched prior pin AND --force would have cleared
|
|
// effectiveExpectedProjectID's carry-forward, not left it set).
|
|
return HandleError("server at %s owns project %q, not the pinned %q", redactedURL, snapshot.ProjectId, target.ExpectProjectID)
|
|
}
|
|
|
|
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
|
return HandleError("creating %s: %v", beadsDir, err)
|
|
}
|
|
// A fresh .beads directory (the common case: this backend has no
|
|
// local database to have created one earlier) has no .gitignore yet.
|
|
// Ensure one before writing the sidecar, the same way bd init and
|
|
// doctor --fix do, so http_target.json (which carries a bearer
|
|
// credential's host and, soon, DialOptions) is never a candidate for
|
|
// `git add .` in the first commit after connect.
|
|
if err := doctor.EnsureGitignoreForBeadsDir(beadsDir); err != nil {
|
|
return HandleError("ensuring %s/.gitignore: %v", beadsDir, err)
|
|
}
|
|
// bdhttp.Attach writes both of this backend's files: the gitignored
|
|
// identity sidecar (SaveTarget — which server, which project; see
|
|
// NewFromConfig/NewReadOnlyFromConfig in internal/httpclient/store.go,
|
|
// which read identity from the sidecar alone, never from
|
|
// metadata.json) and metadata.json's backend SELECTION alone
|
|
// (git-tracked, the same file every clone of this repo shares). It
|
|
// leaves every other existing metadata.json field untouched — in
|
|
// particular cfg.Database/cfg.ProjectID, which belong to whatever
|
|
// backend this workspace selected before, and which a future
|
|
// `bd connect --clear` switching back to that backend (dolt) still
|
|
// needs intact.
|
|
if err := bdhttp.Attach(beadsDir, target); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
return outputJSON(map[string]any{
|
|
"url": redactedURL,
|
|
"project_id": target.ExpectProjectID,
|
|
"bd_version": snapshot.BdVersion,
|
|
"api_version": snapshot.ApiVersion,
|
|
"wire_revision": snapshot.WireRevision,
|
|
"ca_file": caFile,
|
|
"capabilities": snapshot.Capabilities,
|
|
})
|
|
}
|
|
fmt.Printf("Connected %s to %s\n", beadsDir, redactedURL)
|
|
fmt.Printf(" project: %s\n", target.ExpectProjectID)
|
|
fmt.Printf(" bd serve: %s (api %s, wire revision %d)\n", snapshot.BdVersion, snapshot.ApiVersion, snapshot.WireRevision)
|
|
if caFile != "" {
|
|
fmt.Printf(" ca_file: %s\n", caFile)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
connectCmd.Flags().StringVar(&connectExpectProjectID, "expect-project-id", "", "pin the workspace identity the server must present at every handshake")
|
|
connectCmd.Flags().StringVar(&connectCAFile, "ca-file", "", "trust ONLY this PEM file for this server (resolved and recorded as an absolute path)")
|
|
connectCmd.Flags().BoolVar(&connectForce, "force", false, "allow switching a workspace that already selects a different backend")
|
|
connectCmd.Flags().BoolVar(&connectAllowPlaintext, "allow-plaintext", false, "allow a plain http connection to a non-loopback host; remembered in the sidecar for every later command against this server")
|
|
connectCmd.Flags().BoolVar(&connectClear, "clear", false, "remove this workspace's http activation sidecar and restore its previous backend selection in metadata.json")
|
|
rootCmd.AddCommand(connectCmd)
|
|
}
|