chat: attach to the operator's own OpenCode by pairing, fall back honestly
Brokered chat accepted an operator-supplied endpoint (-opencode-endpoint /
PIPELINES_OPENCODE_ENDPOINT) but failed closed against the operator's running
V2 service: that API authenticates every request with HTTP Basic auth (fixed
user "opencode" plus a password), the broker had no credential, so the health
probe answered 401 and startup logged "OpenCode endpoint ... is unreachable or
is not an OpenCode service", disabling chat entirely.
Established from the installed binary (opencode v2.0.20), not from docs:
`opencode --help` advertises `api`, `pair`, `serve`, `service` and `auth`;
`GET /api/info` is unauthenticated-401 with www-authenticate Basic realm
"Secure Area"; Basic auth user `opencode` with the password from
~/.local/state/opencode/service.json answers 200; `POST /api/pair` returns a
code and `GET /auth/connect/{code}` returns a token that is then accepted as
the Basic password.
- internal/opencode/pair.go (new): PairEndpoint does the pair exchange over the
shared nativeRequest transport (same auth, bounds and JSON checks);
ReadServiceCredential reads the operator's local service password under the
private-file rule; OpenEndpointAttached resolves the credential in precedence
order - explicit password file, a paired token stored by an earlier run, then
a fresh pairing bootstrapped by the local service credential - stores the
token owner-only and re-pairs once when a stored token is refused (401).
- internal/opencode/endpoint.go: OpenEndpoint wraps probeEndpoint, which returns
the connection even on failure; Connection.unauthorized (set on HTTP 401 by
the request paths, cleared per probe) distinguishes an authentication refusal
from any other refusal.
- cmd/pipelines-server/main.go: the endpoint attach uses OpenEndpointAttached
with the paired-credential file beside the pipeline database. An endpoint that
cannot be attached logs a line naming it and falls back to the server's own
OpenCode instance instead of leaving chat refused.
- internal/protocol/status.go, internal/server/websocket.go: get_status gains an
opencode object (health = endpoint | fallback | spawned | unavailable, plus
the endpoint url), the honest status evidence for the fallback. No credential
is ever placed there.
- Tests: internal/opencode/pair_test.go, internal/protocol/status_test.go and a
TestStatusReportsOpenCodeAttachment case in internal/server/status_test.go.
Repository checks: go build ./..., go vet ./... and gofmt -l . clean;
go test ./internal/... ./cmd/... green.
Packaged templates and skill references: the repository has no template tree
that mentions the OpenCode endpoint, and no packaged reference file is added, so
internal/author/install.go's managed inventory is unchanged. The packaged
authoring reference internal/author/skill/references/operations.md (the endpoint
paragraph around line 289, the "clear refusal and no fallback" wording and the
password-file/username-only credential story, and the missing opencode status
field) is now stale; per the maintenance convention the item does not edit the
shared document and instead records the stale points, with the exact wording
they should take, in docs/notes/chat-persistence.md for the docs-fold writer.
This commit is contained in:
@@ -42,6 +42,11 @@ type Connection struct {
|
||||
LastHTTPStatus int
|
||||
LastHTTPOperation string
|
||||
MessageReadbackDiagnostic string
|
||||
// unauthorized records that at least one request through this connection was
|
||||
// refused with HTTP 401, so an attach that probed both backend families can
|
||||
// tell an authentication refusal (worth re-pairing) from any other refusal.
|
||||
// It is set only by the request path and cleared by the attach probe.
|
||||
unauthorized bool
|
||||
}
|
||||
|
||||
// Reference records only our ephemeral loopback service credentials, never provider auth.
|
||||
@@ -356,6 +361,9 @@ func (c *Connection) nativeRequest(ctx context.Context, method, path string, bod
|
||||
}
|
||||
defer response.Body.Close()
|
||||
c.LastHTTPStatus = response.StatusCode
|
||||
if response.StatusCode == http.StatusUnauthorized {
|
||||
c.unauthorized = true
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
// The stage adapter stores bounded, redacted failure evidence separately;
|
||||
// the public error string remains free of provider bodies and credentials.
|
||||
@@ -409,6 +417,9 @@ func (c *Connection) EventStream(ctx context.Context) (io.ReadCloser, error) {
|
||||
return nil, fmt.Errorf("OpenCode event stream request failed")
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
if response.StatusCode == http.StatusUnauthorized {
|
||||
c.unauthorized = true
|
||||
}
|
||||
evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10))
|
||||
response.Body.Close()
|
||||
return nil, fmt.Errorf("OpenCode event stream failed (HTTP %d): %s", response.StatusCode, strings.TrimSpace(string(evidence)))
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -64,15 +63,15 @@ func ReadEndpointCredentialFile(path string) (string, error) {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return "", nil
|
||||
}
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read the OpenCode endpoint password file")
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > endpointPasswordFileLimit {
|
||||
return "", fmt.Errorf("invalid OpenCode endpoint password file")
|
||||
}
|
||||
if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) {
|
||||
return "", fmt.Errorf("the OpenCode endpoint password file belongs to another user")
|
||||
if err := checkPrivateFile(path, endpointPasswordFileLimit); err != nil {
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
return "", fmt.Errorf("cannot read the OpenCode endpoint password file")
|
||||
case strings.Contains(err.Error(), "another user"):
|
||||
return "", fmt.Errorf("the OpenCode endpoint password file belongs to another user")
|
||||
default:
|
||||
return "", fmt.Errorf("invalid OpenCode endpoint password file")
|
||||
}
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -88,6 +87,20 @@ func ReadEndpointCredentialFile(path string) (string, error) {
|
||||
// backend-specific health contract, then a streamable event path) and every
|
||||
// failure names the endpoint; there is no fallback and no retry loop.
|
||||
func OpenEndpoint(ctx context.Context, rawURL, password string) (*Connection, error) {
|
||||
conn, err := probeEndpoint(ctx, rawURL, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// probeEndpoint is the shared body of OpenEndpoint. Unlike OpenEndpoint it
|
||||
// returns the connection even when the probe fails, so a caller that supplied a
|
||||
// credential can tell an authentication refusal (Connection.unauthorized, set on
|
||||
// HTTP 401) from any other refusal without a second probe or a second judgement
|
||||
// of the endpoint contract. A caller that does not care discards the connection
|
||||
// through OpenEndpoint.
|
||||
func probeEndpoint(ctx context.Context, rawURL, password string) (*Connection, error) {
|
||||
base, err := NormalizeEndpointURL(rawURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -99,6 +112,10 @@ func OpenEndpoint(ctx context.Context, rawURL, password string) (*Connection, er
|
||||
models: map[string]map[string]string{},
|
||||
responses: map[string][]byte{},
|
||||
}
|
||||
// A fresh probe judges this connection's credential only: any 401 recorded
|
||||
// here belongs to the password supplied now, not to a connection reused
|
||||
// across a re-pair.
|
||||
conn.unauthorized = false
|
||||
probeCtx, cancel := context.WithTimeout(ctx, endpointProbeTimeout)
|
||||
defer cancel()
|
||||
switch {
|
||||
@@ -107,7 +124,7 @@ func OpenEndpoint(ctx context.Context, rawURL, password string) (*Connection, er
|
||||
case conn.probeHealth(probeCtx, "v1") == nil:
|
||||
conn.Backend = "v1"
|
||||
default:
|
||||
return nil, fmt.Errorf("OpenCode endpoint %s is unreachable or is not an OpenCode service", base)
|
||||
return conn, fmt.Errorf("OpenCode endpoint %s is unreachable or is not an OpenCode service", base)
|
||||
}
|
||||
// Require a streamable event endpoint before the endpoint is accepted: a
|
||||
// healthy service without SSE cannot broker chat, so it is a clear refusal,
|
||||
@@ -116,7 +133,7 @@ func OpenEndpoint(ctx context.Context, rawURL, password string) (*Connection, er
|
||||
defer cancelStream()
|
||||
body, err := conn.EventStream(streamCtx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("OpenCode endpoint %s exposes no streamable event endpoint: %w", base, err)
|
||||
return conn, fmt.Errorf("OpenCode endpoint %s exposes no streamable event endpoint: %w", base, err)
|
||||
}
|
||||
body.Close()
|
||||
return conn, nil
|
||||
|
||||
@@ -0,0 +1,332 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// The V2 OpenCode service authenticates every /api request with HTTP Basic
|
||||
// auth: the fixed user "opencode" (unless PIPELINES_OPENCODE_ENDPOINT_USERNAME
|
||||
// overrides it) and a password. A client that does not already hold the
|
||||
// service password obtains its own by pairing: POST /api/pair returns a
|
||||
// single-use code and GET /auth/connect/{code} returns the paired token, which
|
||||
// is then sent as the Basic password exactly like the service password. There
|
||||
// is no bearer or api-key header, verified against the installed 2.0.20 binary
|
||||
// (`opencode api POST /api/pair`, then an unauthenticated `curl` of the
|
||||
// `/auth/connect/{code}` link `opencode pair` prints).
|
||||
//
|
||||
// The broker pairs once, stores the token in an owner-only file beside the
|
||||
// other Pipelines configuration, and reuses it. The bootstrap for the pair call
|
||||
// is the operator's own local service credential (the file `opencode service
|
||||
// start` writes); it is read, never stored and never logged.
|
||||
|
||||
// OpenCodeServiceStateEnvKey overrides the directory holding the OpenCode
|
||||
// service state file. It is the XDG state location, so the usual override
|
||||
// applies for tests and for a service that keeps its state elsewhere.
|
||||
const OpenCodeServiceStateEnvKey = "XDG_STATE_HOME"
|
||||
|
||||
// serviceCredentialLimit bounds the service state file read.
|
||||
const serviceCredentialLimit = 64 << 10
|
||||
|
||||
// EndpointAttach is the outcome of attaching the chat broker to the operator's
|
||||
// OpenCode endpoint: the ready, non-owning connection plus exactly how the
|
||||
// credential was obtained, so the caller can report the attach (and any
|
||||
// non-fatal caveat) without ever touching the credential itself.
|
||||
type EndpointAttach struct {
|
||||
Connection *Connection
|
||||
// Mode is "password-file" when the operator supplied the Basic password
|
||||
// directly, "paired" when a token from this run's pairing was stored, and
|
||||
// "paired-stored" when a token stored by an earlier run was reused.
|
||||
Mode string
|
||||
// Note carries a non-fatal caveat such as a paired credential that could not
|
||||
// be persisted; empty when there is none. It never contains a credential.
|
||||
Note string
|
||||
}
|
||||
|
||||
// OpenCodeStateDirectory is the directory `opencode` keeps its service state
|
||||
// (including service.json, the owner-only file `opencode service start`
|
||||
// writes) in: $XDG_STATE_HOME/opencode or the XDG default
|
||||
// ~/.local/state/opencode.
|
||||
func OpenCodeStateDirectory() (string, error) {
|
||||
if dir := strings.TrimSpace(os.Getenv(OpenCodeServiceStateEnvKey)); dir != "" {
|
||||
return filepath.Join(dir, "opencode"), nil
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if home == "" {
|
||||
return "", fmt.Errorf("operator home directory is empty")
|
||||
}
|
||||
return filepath.Join(home, ".local", "state", "opencode"), nil
|
||||
}
|
||||
|
||||
// ServiceCredentialPath is the local OpenCode service state file the pair
|
||||
// bootstrap reads. It is the operator's own machine configuration.
|
||||
func ServiceCredentialPath() (string, error) {
|
||||
dir, err := OpenCodeStateDirectory()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(dir, "service.json"), nil
|
||||
}
|
||||
|
||||
// ReadServiceCredential reads the service password from the operator's local
|
||||
// OpenCode service state file. The file is held to the same private-file rule
|
||||
// as the other credential files (a regular, single-owner, 0600 file with a
|
||||
// bounded read), so a world-readable or foreign-owned file is refused rather
|
||||
// than trusted. An empty path yields no credential.
|
||||
func ReadServiceCredential(path string) (string, error) {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return "", nil
|
||||
}
|
||||
if err := checkPrivateFile(path, serviceCredentialLimit); err != nil {
|
||||
return "", fmt.Errorf("cannot read the local OpenCode service credential")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot read the local OpenCode service credential")
|
||||
}
|
||||
var state struct {
|
||||
URL string `json:"url"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &state); err != nil || strings.TrimSpace(state.Password) == "" {
|
||||
return "", fmt.Errorf("the local OpenCode service credential is not usable")
|
||||
}
|
||||
return strings.TrimSpace(state.Password), nil
|
||||
}
|
||||
|
||||
// PairEndpoint exchanges one fresh credential with the running service: it
|
||||
// calls POST /api/pair on the already-authenticated connection and completes
|
||||
// the returned single-use code at GET /auth/connect/{code}. It reuses
|
||||
// nativeRequest, so the pair request carries the same Basic auth, bounds and
|
||||
// JSON checks as every other call and no second HTTP client is introduced. A
|
||||
// blank bootstrap is a clear refusal rather than a guessed credential.
|
||||
func PairEndpoint(ctx context.Context, rawURL, bootstrapPassword string) (string, error) {
|
||||
base, err := NormalizeEndpointURL(rawURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(bootstrapPassword) == "" {
|
||||
return "", fmt.Errorf("OpenCode endpoint %s needs a password or a local OpenCode service credential to pair", base)
|
||||
}
|
||||
conn := &Connection{
|
||||
Backend: "v2",
|
||||
URL: base,
|
||||
password: bootstrapPassword,
|
||||
username: strings.TrimSpace(os.Getenv(EndpointUsernameEnvKey)),
|
||||
models: map[string]map[string]string{},
|
||||
responses: map[string][]byte{},
|
||||
}
|
||||
codeCtx, cancel := context.WithTimeout(ctx, endpointProbeTimeout)
|
||||
defer cancel()
|
||||
data, err := conn.nativeRequest(codeCtx, "post", "/api/pair", nil, "")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("OpenCode endpoint %s could not start a pairing: %w", base, err)
|
||||
}
|
||||
var issued struct {
|
||||
Code string `json:"code"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
if json.Unmarshal(data, &issued) != nil || strings.TrimSpace(issued.Code) == "" {
|
||||
return "", fmt.Errorf("OpenCode endpoint %s returned no pairing code", base)
|
||||
}
|
||||
tokenCtx, cancelToken := context.WithTimeout(ctx, endpointProbeTimeout)
|
||||
defer cancelToken()
|
||||
tokenData, err := conn.nativeRequest(tokenCtx, "get", "/auth/connect/"+url.PathEscape(issued.Code), nil, "")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("OpenCode endpoint %s refused the pairing code: %w", base, err)
|
||||
}
|
||||
var paired struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if json.Unmarshal(tokenData, &paired) != nil || strings.TrimSpace(paired.Token) == "" {
|
||||
return "", fmt.Errorf("OpenCode endpoint %s returned no paired credential", base)
|
||||
}
|
||||
return strings.TrimSpace(paired.Token), nil
|
||||
}
|
||||
|
||||
// OpenEndpointAttached attaches chat to the operator's endpoint with the first
|
||||
// credential that works, in precedence order: an explicit password (the
|
||||
// password-file reference), a paired token stored by an earlier run, and a
|
||||
// freshly paired token. The returned connection never owns a process. When a
|
||||
// stored token is refused with 401 it is cleared and the broker pairs once
|
||||
// more, so a credential the service has rotated heals without operator action;
|
||||
// any other refusal is returned as-is.
|
||||
//
|
||||
// credentialPath is where the paired token is kept (owner-only). No credential
|
||||
// is ever written to the log or the returned error; only the endpoint and the
|
||||
// mode are.
|
||||
func OpenEndpointAttached(ctx context.Context, rawURL, explicitPassword, bootstrapPassword, credentialPath string) (EndpointAttach, error) {
|
||||
base, err := NormalizeEndpointURL(rawURL)
|
||||
if err != nil {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
// 1. An explicit password (the operator's password-file reference) is used
|
||||
// as given: it needs no pairing and is never replaced.
|
||||
if strings.TrimSpace(explicitPassword) != "" {
|
||||
conn, err := probeEndpoint(ctx, base, explicitPassword)
|
||||
if err != nil {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
return EndpointAttach{Connection: conn, Mode: "password-file"}, nil
|
||||
}
|
||||
// 2. A token a previous run paired and stored.
|
||||
stored, found, err := ReadPairedCredential(credentialPath, base)
|
||||
if err != nil {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
if found {
|
||||
conn, err := probeEndpoint(ctx, base, stored)
|
||||
if err == nil {
|
||||
return EndpointAttach{Connection: conn, Mode: "paired-stored"}, nil
|
||||
}
|
||||
if conn == nil || !conn.unauthorized {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
// The stored token was refused: drop it and pair a fresh one below.
|
||||
if clearErr := ClearPairedCredential(credentialPath); clearErr != nil {
|
||||
return EndpointAttach{}, clearErr
|
||||
}
|
||||
}
|
||||
// 3. Pair a fresh token with the local service credential as bootstrap.
|
||||
token, err := PairEndpoint(ctx, base, bootstrapPassword)
|
||||
if err != nil {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
conn, err := probeEndpoint(ctx, base, token)
|
||||
if err != nil {
|
||||
return EndpointAttach{}, err
|
||||
}
|
||||
attach := EndpointAttach{Connection: conn, Mode: "paired"}
|
||||
if credentialPath != "" {
|
||||
if writeErr := WritePairedCredential(credentialPath, base, token); writeErr != nil {
|
||||
// The attached connection works for this run; only persistence
|
||||
// failed, so the next start re-pairs instead of failing closed.
|
||||
attach.Note = "the paired credential could not be stored; chat will re-pair on the next start"
|
||||
}
|
||||
}
|
||||
return attach, nil
|
||||
}
|
||||
|
||||
// pairedCredential is the owner-only record of a token a previous run paired
|
||||
// with one endpoint. The endpoint is stored so a token paired for one service
|
||||
// is never offered to another.
|
||||
type pairedCredential struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// ReadPairedCredential returns the stored token for endpoint, found false when
|
||||
// no credential file exists or it belongs to a different endpoint. A file that
|
||||
// exists but is not a private, owner-held regular file is an error, never a
|
||||
// silently ignored credential.
|
||||
func ReadPairedCredential(path, endpoint string) (string, bool, error) {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", false, nil
|
||||
}
|
||||
return "", false, fmt.Errorf("cannot read the stored OpenCode pairing")
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 {
|
||||
return "", false, fmt.Errorf("the stored OpenCode pairing is not a private file")
|
||||
}
|
||||
if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) {
|
||||
return "", false, fmt.Errorf("the stored OpenCode pairing belongs to another user")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", false, fmt.Errorf("cannot read the stored OpenCode pairing")
|
||||
}
|
||||
var credential pairedCredential
|
||||
if err := json.Unmarshal(data, &credential); err != nil {
|
||||
return "", false, fmt.Errorf("the stored OpenCode pairing is not usable")
|
||||
}
|
||||
if credential.Endpoint != endpoint || strings.TrimSpace(credential.Token) == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
return credential.Token, true, nil
|
||||
}
|
||||
|
||||
// WritePairedCredential stores the paired token in an owner-only file: the
|
||||
// directory is created 0700 and the file is written 0600 through a temporary
|
||||
// file in the same directory and renamed over the target, so a reader never
|
||||
// sees a partial record.
|
||||
func WritePairedCredential(path, endpoint, token string) error {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return fmt.Errorf("no location to store the OpenCode pairing")
|
||||
}
|
||||
if strings.TrimSpace(token) == "" {
|
||||
return fmt.Errorf("no OpenCode pairing token to store")
|
||||
}
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
return fmt.Errorf("cannot create the OpenCode pairing directory")
|
||||
}
|
||||
data, err := json.Marshal(pairedCredential{Endpoint: endpoint, Token: token})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
temp, err := os.CreateTemp(dir, "opencode-pairing-*.json")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create the OpenCode pairing file")
|
||||
}
|
||||
name := temp.Name()
|
||||
if err := temp.Chmod(0600); err != nil {
|
||||
temp.Close()
|
||||
os.Remove(name)
|
||||
return fmt.Errorf("cannot secure the OpenCode pairing file")
|
||||
}
|
||||
if _, err := temp.Write(data); err != nil {
|
||||
temp.Close()
|
||||
os.Remove(name)
|
||||
return fmt.Errorf("cannot write the OpenCode pairing file")
|
||||
}
|
||||
if err := temp.Close(); err != nil {
|
||||
os.Remove(name)
|
||||
return fmt.Errorf("cannot write the OpenCode pairing file")
|
||||
}
|
||||
if err := os.Rename(name, path); err != nil {
|
||||
os.Remove(name)
|
||||
return fmt.Errorf("cannot store the OpenCode pairing file")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClearPairedCredential removes a stored pairing that the service refused.
|
||||
func ClearPairedCredential(path string) error {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return nil
|
||||
}
|
||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("cannot clear the refused OpenCode pairing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkPrivateFile applies the shared private-file rule: a regular file with
|
||||
// exact 0600 permissions, owned by the current user and no larger than limit.
|
||||
func checkPrivateFile(path string, limit int64) error {
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > limit {
|
||||
return fmt.Errorf("not a private file")
|
||||
}
|
||||
if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) {
|
||||
return fmt.Errorf("belongs to another user")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// pairService is a minimal V2 OpenCode service: it accepts the fixed "opencode"
|
||||
// basic-auth user with either the bootstrap service password or a token it has
|
||||
// paired, issues single-use pair codes, exchanges them for tokens, and counts
|
||||
// pair calls so a test can prove a stored token is reused rather than re-paired.
|
||||
type pairService struct {
|
||||
mu sync.Mutex
|
||||
password string
|
||||
tokens map[string]bool
|
||||
codes map[string]bool
|
||||
pairs int
|
||||
seq int
|
||||
}
|
||||
|
||||
func newPairService(t *testing.T, password string) (*pairService, *httptest.Server) {
|
||||
t.Helper()
|
||||
s := &pairService{password: password, tokens: map[string]bool{}, codes: map[string]bool{}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(s.handle))
|
||||
t.Cleanup(srv.Close)
|
||||
return s, srv
|
||||
}
|
||||
|
||||
func (s *pairService) authorized(r *http.Request) bool {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if !ok || user != "opencode" {
|
||||
return false
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return pass == s.password || s.tokens[pass]
|
||||
}
|
||||
|
||||
func (s *pairService) handle(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/pair" && r.Method == http.MethodPost:
|
||||
if !s.authorized(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.seq++
|
||||
code := "code" + string(rune('0'+s.seq))
|
||||
s.codes[code] = true
|
||||
s.pairs++
|
||||
s.mu.Unlock()
|
||||
writeJSON(w, map[string]any{"code": code, "expires_in": 300})
|
||||
case strings.HasPrefix(r.URL.Path, "/auth/connect/"):
|
||||
code := strings.TrimPrefix(r.URL.Path, "/auth/connect/")
|
||||
s.mu.Lock()
|
||||
ok := s.codes[code]
|
||||
delete(s.codes, code)
|
||||
token := "token" + string(rune('0'+s.seq))
|
||||
if ok {
|
||||
s.tokens[token] = true
|
||||
}
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"token": token})
|
||||
case r.URL.Path == "/api/info":
|
||||
if !s.authorized(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"version": "2.0.20", "urls": []string{}})
|
||||
case r.URL.Path == "/api/event":
|
||||
if !s.authorized(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *pairService) pairCalls() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.pairs
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, body any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
|
||||
// TestPairEndpointExchangesCodeForToken pins the pairing contract the installed
|
||||
// 2.0.20 service exposes: POST /api/pair (authenticated) issues a single-use
|
||||
// code and GET /auth/connect/{code} returns the token used as the basic
|
||||
// password.
|
||||
func TestPairEndpointExchangesCodeForToken(t *testing.T) {
|
||||
service, srv := newPairService(t, "service-password")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
token, err := PairEndpoint(ctx, srv.URL, "service-password")
|
||||
if err != nil {
|
||||
t.Fatalf("PairEndpoint: %v", err)
|
||||
}
|
||||
if token == "" {
|
||||
t.Fatal("PairEndpoint returned an empty token")
|
||||
}
|
||||
if service.pairCalls() != 1 {
|
||||
t.Fatalf("pair calls = %d, want 1", service.pairCalls())
|
||||
}
|
||||
if _, err := PairEndpoint(ctx, srv.URL, ""); err == nil {
|
||||
t.Fatal("PairEndpoint with no bootstrap password succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenEndpointAttachedPairsStoresAndReuses pins the whole point: with no
|
||||
// explicit password the broker pairs once, stores the token in an owner-only
|
||||
// file beside the other configuration, and a later start reuses it instead of
|
||||
// pairing again.
|
||||
func TestOpenEndpointAttachedPairsStoresAndReuses(t *testing.T) {
|
||||
service, srv := newPairService(t, "service-password")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
credentialPath := filepath.Join(t.TempDir(), "opencode-endpoint.json")
|
||||
|
||||
first, err := OpenEndpointAttached(ctx, srv.URL, "", "service-password", credentialPath)
|
||||
if err != nil {
|
||||
t.Fatalf("first attach: %v", err)
|
||||
}
|
||||
if first.Mode != "paired" {
|
||||
t.Fatalf("first attach mode = %q, want paired", first.Mode)
|
||||
}
|
||||
first.Connection.Close()
|
||||
info, err := os.Stat(credentialPath)
|
||||
if err != nil {
|
||||
t.Fatalf("paired credential was not stored: %v", err)
|
||||
}
|
||||
if info.Mode().Perm() != 0600 {
|
||||
t.Fatalf("stored credential mode = %v, want 0600", info.Mode().Perm())
|
||||
}
|
||||
if service.pairCalls() != 1 {
|
||||
t.Fatalf("pair calls after first attach = %d, want 1", service.pairCalls())
|
||||
}
|
||||
|
||||
second, err := OpenEndpointAttached(ctx, srv.URL, "", "service-password", credentialPath)
|
||||
if err != nil {
|
||||
t.Fatalf("second attach: %v", err)
|
||||
}
|
||||
if second.Mode != "paired-stored" {
|
||||
t.Fatalf("second attach mode = %q, want paired-stored", second.Mode)
|
||||
}
|
||||
second.Connection.Close()
|
||||
if service.pairCalls() != 1 {
|
||||
t.Fatalf("a reused token triggered a re-pair: pair calls = %d, want 1", service.pairCalls())
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenEndpointAttachedRepairsRefusedStoredCredential pins the recovery: a
|
||||
// token the service has refused is cleared and replaced by one fresh pairing,
|
||||
// never left to fail closed.
|
||||
func TestOpenEndpointAttachedRepairsRefusedStoredCredential(t *testing.T) {
|
||||
service, srv := newPairService(t, "service-password")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
credentialPath := filepath.Join(t.TempDir(), "opencode-endpoint.json")
|
||||
if err := WritePairedCredential(credentialPath, srv.URL, "stale-token"); err != nil {
|
||||
t.Fatalf("seed stale credential: %v", err)
|
||||
}
|
||||
|
||||
attach, err := OpenEndpointAttached(ctx, srv.URL, "", "service-password", credentialPath)
|
||||
if err != nil {
|
||||
t.Fatalf("attach after refusal: %v", err)
|
||||
}
|
||||
attach.Connection.Close()
|
||||
if attach.Mode != "paired" {
|
||||
t.Fatalf("attach mode = %q, want paired after a refused stored token", attach.Mode)
|
||||
}
|
||||
if service.pairCalls() != 1 {
|
||||
t.Fatalf("pair calls = %d, want exactly one re-pair", service.pairCalls())
|
||||
}
|
||||
token, found, err := ReadPairedCredential(credentialPath, srv.URL)
|
||||
if err != nil || !found || token == "stale-token" {
|
||||
t.Fatalf("stored credential after repair = %q,%v,%v want a fresh token", token, found, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenEndpointAttachedExplicitPasswordSkipsPairing pins that an explicit
|
||||
// password (the operator's password-file reference) is used as given and no
|
||||
// pairing is attempted.
|
||||
func TestOpenEndpointAttachedExplicitPasswordSkipsPairing(t *testing.T) {
|
||||
service, srv := newPairService(t, "service-password")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
attach, err := OpenEndpointAttached(ctx, srv.URL, "service-password", "service-password", filepath.Join(t.TempDir(), "unused.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("attach with explicit password: %v", err)
|
||||
}
|
||||
attach.Connection.Close()
|
||||
if attach.Mode != "password-file" {
|
||||
t.Fatalf("attach mode = %q, want password-file", attach.Mode)
|
||||
}
|
||||
if service.pairCalls() != 0 {
|
||||
t.Fatalf("an explicit password triggered a pairing: pair calls = %d", service.pairCalls())
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadServiceCredential pins the bootstrap read: the service state file is
|
||||
// held to the private-file rule and only its password is returned.
|
||||
func TestReadServiceCredential(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "service.json")
|
||||
if err := os.WriteFile(path, []byte(`{"url":"http://0.0.0.0:49374","password":"bootstrap"}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := ReadServiceCredential(path); err != nil || got != "bootstrap" {
|
||||
t.Fatalf("ReadServiceCredential = %q,%v want bootstrap", got, err)
|
||||
}
|
||||
if err := os.Chmod(path, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ReadServiceCredential(path); err == nil {
|
||||
t.Fatal("a world-readable service credential was accepted")
|
||||
}
|
||||
if got, err := ReadServiceCredential(""); err != nil || got != "" {
|
||||
t.Fatalf("empty path = %q,%v want no credential", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadPairedCredentialIsEndpointScoped pins that a token paired for one
|
||||
// endpoint is never offered to another.
|
||||
func TestReadPairedCredentialIsEndpointScoped(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "opencode-endpoint.json")
|
||||
if err := WritePairedCredential(path, "http://127.0.0.1:1", "token"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, found, err := ReadPairedCredential(path, "http://127.0.0.1:2"); err != nil || found {
|
||||
t.Fatalf("foreign endpoint = found %v, err %v; want not found", found, err)
|
||||
}
|
||||
if token, found, err := ReadPairedCredential(path, "http://127.0.0.1:1"); err != nil || !found || token != "token" {
|
||||
t.Fatalf("matching endpoint = %q,%v,%v want token", token, found, err)
|
||||
}
|
||||
if _, found, err := ReadPairedCredential(filepath.Join(t.TempDir(), "absent.json"), "http://127.0.0.1:1"); err != nil || found {
|
||||
t.Fatalf("absent file = found %v, err %v; want not found", found, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user