An adversarial fable red-team of the multi-backend PR surfaced 19 findings; the verified, non-regressing subset is fixed here (two were confirmed intentional and left as-is with clarified comments). Dolt-parity (high): - sqlkit tx AddLabel/RemoveLabel/CloseIssue recorded no audit event, unlike both Dolt references, so bd label add/remove and bd batch / mol squash close lost their EventLabel*/EventClosed rows on Postgres/MySQL/SQLite. Delegate to the same issueops with-event helpers the embedded oracle uses. - Heartbeat left updated_at stale on Postgres/SQLite (no ON UPDATE trigger; MySQL/Dolt auto-bump), so bd stale diverged. Stamp updated_at on the heartbeat UPDATE (claim already stamps it, so no double-assign). Correctness / hardening: - postgres/mysql/sqlite stampSchemaVersion: idempotent insert (ON CONFLICT / INSERT IGNORE / INSERT OR IGNORE) + re-read, so concurrent first provisions do not fail one opener with a duplicate-key error. - sqlkit tx SetConfig now syncs custom_statuses/custom_types like the oracle. - pgdialect INSTR->POSITION leaves the call loud when both args carry a placeholder, preventing a silent parameter swap. - creds command runner uses cmd.exe on Windows (sh is POSIX-only). - Dolt gateway credential command is gated on the Dolt backend, not just shared-server mode (was failing closed for SQL workspaces). - Scrub pg/mysql URL passwords from the bd.args telemetry span. Conformance rigor: - E2E normalize() maps zero/epoch timestamps to <ZERO-TS> so a lost timestamp no longer reads as byte-identical to a real one. - E2E logs its candidate set and honors BEADS_CONFORMANCE_REQUIRE so a sqlite-only run cannot masquerade as full coverage. Docs/hygiene: - Scrub dev-machine paths (/data/tmp/pg-recon, proposal_2.md, "proof-wedge") from the Postgres schema DDL comment; clarify the SQLite timestamp comment. Deliberately not changed (would regress): sqlkit UpdateIssue uses the without-event variant to MATCH dolt-server (only embedded differs); sub-second timestamp precision is an intentional per-backend waiver (whole-second round-trip is the portable contract) — forcing round-down would discard PG/SQLite fidelity. Verified: embedded + postgres + mysql + sqlite + server-dolt TestConformance and the pg/mysql/sqlite E2E differential all pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
178 lines
6.4 KiB
Go
178 lines
6.4 KiB
Go
package creds
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// CommandSource runs a configured command and reads a credential from its stdout —
|
|
// the external credential-process idiom (kubectl ExecCredential / AWS
|
|
// credential_process / git credential helper). bd knows nothing of the issuer: the
|
|
// operator points Command at whatever mints the credential — a token-issuing CLI for
|
|
// an identity, or `vault read ...` / `aws rds generate-db-auth-token ...` for a secret.
|
|
// Kind is fixed at construction from the config slot, so the resolved value's role is
|
|
// never ambiguous.
|
|
//
|
|
// The result is cached per-command until near expiry so repeated opens don't
|
|
// re-spawn the helper; the cache lives for the process and dies with it.
|
|
type CommandSource struct {
|
|
Command string
|
|
Kind Kind
|
|
Label string // provenance slug (the config-slot name); defaults to "credential-command"
|
|
}
|
|
|
|
// Name returns the provenance slug.
|
|
func (s CommandSource) Name() string {
|
|
if s.Label != "" {
|
|
return s.Label
|
|
}
|
|
return "credential-command"
|
|
}
|
|
|
|
// Resolve runs the command (or a cached result) and returns the credential. An
|
|
// empty Command means "not configured"; a helper failure is a configured error and
|
|
// aborts the ladder.
|
|
func (s CommandSource) Resolve(ctx context.Context) (Credential, bool, error) {
|
|
if s.Command == "" {
|
|
return Credential{}, false, nil
|
|
}
|
|
tok, user, expiry, err := resolveCredentialToken(ctx, s.Command)
|
|
if err != nil {
|
|
return Credential{}, true, err
|
|
}
|
|
return Credential{Value: tok, Username: user, Kind: s.Kind, Expiry: expiry, Source: s.Name()}, true, nil
|
|
}
|
|
|
|
const (
|
|
credCommandTimeout = 30 * time.Second // a helper that hangs must not wedge an open
|
|
credDefaultTTL = 60 * time.Second // cache window when the helper reports no expiry
|
|
credExpirySkew = 10 * time.Second // refresh this long before the reported expiry
|
|
)
|
|
|
|
// execCredential is the union of stdout envelopes accepted: the kubectl
|
|
// ExecCredential subset {token, expirationTimestamp}, the OAuth-style
|
|
// {access_token, expires_in} shape, and an optional username for dynamic user/password
|
|
// pairs (e.g. Vault). A helper may instead print a bare token — see parseCredential.
|
|
type execCredential struct {
|
|
Token string `json:"token"` //nolint:gosec // G117: ExecCredential/OAuth envelope field name (wire format), not an embedded secret
|
|
AccessToken string `json:"access_token"` //nolint:gosec // G117: ExecCredential/OAuth envelope field name (wire format), not an embedded secret
|
|
Username string `json:"username"`
|
|
ExpirationTimestamp string `json:"expirationTimestamp"`
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
}
|
|
|
|
type cachedCred struct {
|
|
token string
|
|
username string
|
|
expires time.Time
|
|
}
|
|
|
|
var (
|
|
credCacheMu sync.Mutex
|
|
credCache = map[string]cachedCred{}
|
|
|
|
// credRunner runs the helper; a package var so tests can stub it without a shell.
|
|
credRunner = func(ctx context.Context, command string) ([]byte, error) {
|
|
// POSIX shells parse the helper command; native Windows has no `sh`, so
|
|
// dispatch through cmd.exe there so a bare Windows bd does not hard-fail
|
|
// every *_PASSWORD_COMMAND / CREDENTIAL_COMMAND in the fail-closed ladder.
|
|
var cmd *exec.Cmd
|
|
if runtime.GOOS == "windows" {
|
|
cmd = exec.CommandContext(ctx, "cmd.exe", "/C", command)
|
|
} else {
|
|
cmd = exec.CommandContext(ctx, "sh", "-c", command)
|
|
}
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
if msg := strings.TrimSpace(stderr.String()); msg != "" {
|
|
return nil, fmt.Errorf("%w: %s", err, msg)
|
|
}
|
|
return nil, err
|
|
}
|
|
return stdout.Bytes(), nil
|
|
}
|
|
)
|
|
|
|
// resolveCredentialToken returns the token (and any username/expiry) for the given
|
|
// helper command, using a process-level cache keyed by the command so repeated opens
|
|
// don't re-spawn the helper until the token is near expiry. It is concurrency-safe.
|
|
func resolveCredentialToken(ctx context.Context, command string) (token, username string, expiry time.Time, err error) {
|
|
now := time.Now()
|
|
|
|
credCacheMu.Lock()
|
|
if c, ok := credCache[command]; ok && now.Before(c.expires.Add(-credExpirySkew)) {
|
|
tok, user, exp := c.token, c.username, c.expires
|
|
credCacheMu.Unlock()
|
|
return tok, user, exp, nil
|
|
}
|
|
credCacheMu.Unlock()
|
|
|
|
runCtx, cancel := context.WithTimeout(ctx, credCommandTimeout)
|
|
defer cancel()
|
|
raw, err := credRunner(runCtx, command)
|
|
if err != nil {
|
|
return "", "", time.Time{}, fmt.Errorf("credential command failed: %w", err)
|
|
}
|
|
token, username, expiry, err = parseCredential(raw)
|
|
if err != nil {
|
|
return "", "", time.Time{}, err
|
|
}
|
|
if expiry.IsZero() {
|
|
expiry = now.Add(credDefaultTTL)
|
|
}
|
|
|
|
credCacheMu.Lock()
|
|
credCache[command] = cachedCred{token: token, username: username, expires: expiry}
|
|
credCacheMu.Unlock()
|
|
return token, username, expiry, nil
|
|
}
|
|
|
|
// parseCredential extracts the token (and any username/expiry) from a helper's
|
|
// stdout. A JSON object is read as the ExecCredential/getToken envelope; otherwise
|
|
// the trimmed output is taken as a bare token. A bare value containing whitespace is
|
|
// rejected — it is almost always an error message, and using it as a credential
|
|
// would only fail confusingly downstream.
|
|
func parseCredential(raw []byte) (token, username string, expiry time.Time, err error) {
|
|
trimmed := bytes.TrimSpace(raw)
|
|
if len(trimmed) == 0 {
|
|
return "", "", time.Time{}, fmt.Errorf("credential command produced no output")
|
|
}
|
|
|
|
if trimmed[0] == '{' {
|
|
var c execCredential
|
|
if jerr := json.Unmarshal(trimmed, &c); jerr != nil {
|
|
return "", "", time.Time{}, fmt.Errorf("credential command returned unparseable JSON: %w", jerr)
|
|
}
|
|
token = c.Token
|
|
if token == "" {
|
|
token = c.AccessToken
|
|
}
|
|
if token == "" {
|
|
return "", "", time.Time{}, fmt.Errorf("credential command JSON has no token/access_token field")
|
|
}
|
|
switch {
|
|
case c.ExpirationTimestamp != "":
|
|
if t, perr := time.Parse(time.RFC3339, c.ExpirationTimestamp); perr == nil {
|
|
expiry = t
|
|
}
|
|
case c.ExpiresIn > 0:
|
|
expiry = time.Now().Add(time.Duration(c.ExpiresIn) * time.Second)
|
|
}
|
|
return token, c.Username, expiry, nil
|
|
}
|
|
|
|
bare := string(trimmed)
|
|
if strings.ContainsAny(bare, " \t\r\n") {
|
|
return "", "", time.Time{}, fmt.Errorf("credential command output is not a bare token (contains whitespace); expected a token or a JSON {token,expirationTimestamp} envelope")
|
|
}
|
|
return bare, "", time.Time{}, nil
|
|
}
|