Compare commits

..

4 Commits

Author SHA1 Message Date
Jon Chery 531b36924c fix(P09): migration + operational safety — job stop, retention, logs cap (REQ-158)
- job stop: real systemctl stop via SSH (was DB-only soft stop)
  resolves node from alloc_history or --peer flag
- doctor db-retention: row count check for jobs/tasks/audit_log
  warns at 100k rows, suggests backup + cleanup
- logs --lines: cap at 50000 (default 1000); --since upper bound 7d
  prevents OOM from unbounded journalctl
- cache DB mode 0600 (was 0644; matches store.Open)
- upgrade cutover: backup file + atomic rename (was sed -i)
  rollback restores from backup on failure

Tests: job stop SSH, DB retention warning, logs lines cap, cache mode,
cutover backup-restore + atomic rename.

---ci---
project: orca
phase: 9
milestone: v0.13
status: complete
requirements:
  covered: [158]
---/ci---
2026-08-10 13:37:28 +00:00
Jon Chery 3a3ea74d76 fix(P08): transport + SSH safety — typed errors, IPv6, timeouts, signal (REQ-157)
- transport.IsTransient: typed sentinels (ErrTransient/ErrPermanent) +
  standard net.Error/io errors.Is; substring matching removed
- sshpush.isTransient: same typed-error classification
- rotateSSHKeys: 2-phase atomic swap (stage peers -> swap local ->
  verify -> cleanup old); no more partial-result window
- known_hosts: dial() reads stored field (was reading v0.8 path directly)
- IPv6: net.JoinHostPort in proxmox SSH dial + drain splitHostPort
- SSH timeouts: context.WithTimeout on peer-setup, drift, txn rollback,
  job restart (default 2m)
- verifyCutover: orca CA pool TLS config (was default http.Client)
- OIDC callback: ReadHeaderTimeout 5s (slowloris defense)
- root Execute: signal.NotifyContext for SIGINT/SIGTERM (clean exit
  for non-watch commands)

Tests: typed-error classification table, IPv6 JoinHostPort, signal
handler context cancellation.

---ci---
project: orca
phase: 8
milestone: v0.13
status: complete
requirements:
  covered: [157]
---/ci---
2026-08-10 13:11:07 +00:00
Jon Chery 0358efe95b fix(P07): concurrency safety — SQLite, flock, cache, atomic writes (REQ-156)
- SQLite busy_timeout(5000) + SetMaxOpenConns(1) on all 4 DSNs
- secrets file flock (concurrent set on same ns no longer loses data)
- upgrade lock file (refuse concurrent orca upgrade)
- backup lock file (refuse concurrent backup)
- cache invalidation by writes (read-after-write consistency)
- Executor.Run mutex scope fix (hold only for DB inserts)
- ns create/inherit/set-constraint atomic writeNSMdAtomic
- writeCurrentLead + rotateSSHKeys atomic
- consolidate 3 writeAtomic impls onto security.WriteAtomic
- WebAuthn session stores guarded with sync.Mutex

Tests: concurrent secrets set, upgrade lock rejection, cache
read-after-write, WebAuthn session thread-safety (pass under -race).

---ci---
project: orca
phase: 7
milestone: v0.13
status: complete
requirements:
  covered: [156]
---/ci---
2026-08-10 12:27:05 +00:00
Jon Chery 978334a4bc feat(P06): auth init-idp real + auth register + doctor oidc (REQ-155)
Implements the v0.12 R-021 load-bearing change's working IdP path:
- orca auth init-idp: renders Dex config + systemd unit + Traefik route
  (atomic deploy, RP ID from --rp-id, C-38)
- orca auth register: opens browser to WebAuthn registration page
- loadOIDCConfig: config-file loading (oidc block + cluster_domain),
  falls back to flags + env vars
- orca doctor oidc: health check (systemctl is-active + .well-known)
- config.go: OIDCConfig block + ClusterDomain field
- markdown.go: oidc block parsing in config frontmatter

---ci---
project: orca
phase: 6
milestone: v0.13
status: complete
requirements:
  covered: [155]
---/ci---
2026-08-10 11:55:01 +00:00
41 changed files with 3106 additions and 176 deletions
+13 -1
View File
@@ -58,14 +58,26 @@ func Open(path string) (*Cache, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return nil, fmt.Errorf("create cache db dir: %w", err)
}
db, err := sql.Open("sqlite", path+"?_pragma=journal_mode(WAL)")
// REQ-156 / P07 T1: busy_timeout(5000) so concurrent cache opens
// (e.g. two `orca node list` invocations racing on the same shell)
// wait up to 5s for the writer instead of failing immediately with
// SQLITE_BUSY. SetMaxOpenConns(1) serializes the connections so the
// busy_timeout is rarely needed but keeps the cache durable under
// contention.
db, err := sql.Open("sqlite", path+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
if err != nil {
return nil, fmt.Errorf("open cache sqlite: %w", err)
}
db.SetMaxOpenConns(1)
if err := db.Ping(); err != nil {
_ = db.Close()
return nil, fmt.Errorf("ping cache sqlite: %w", err)
}
// REQ-158 / P09 T4: enforce 0600 on the cache DB file (SQLite
// creates it at umask, typically 0644). Match store.Open which
// chmods after open+ping (the file exists at this point). Non-fatal
// if chmod fails (e.g. the DB is at a path we don't own).
_ = os.Chmod(path, 0o600)
const schema = `CREATE TABLE IF NOT EXISTS cache_entries (
class TEXT NOT NULL,
key TEXT NOT NULL,
+47
View File
@@ -2,6 +2,7 @@ package cache
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
@@ -225,3 +226,49 @@ func BenchmarkCacheHit(b *testing.B) {
}
}
}
// TestCache_FileMode0600 verifies that the cache DB file is created
// with mode 0600 (not the default umask 0644) (REQ-158, P09 T4).
func TestCache_FileMode0600(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "orca_cache.db")
c, err := Open(path)
if err != nil {
t.Fatalf("open: %v", err)
}
defer c.Close()
info, err := os.Stat(path)
if err != nil {
t.Fatalf("stat cache db: %v", err)
}
got := info.Mode().Perm()
if got != 0o600 {
t.Errorf("cache db mode = %04o, want 0600", got)
}
}
// TestCache_FileMode0600DefaultPath verifies that the cache DB at the
// default path (ORCA_HOME) also gets 0600 (REQ-158, P09 T4).
func TestCache_FileMode0600DefaultPath(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
c, err := Open("")
if err != nil {
t.Fatalf("open default path: %v", err)
}
defer c.Close()
// The default path is paths.CacheDB() which is under ORCA_HOME.
// Find the db file.
dbPath := filepath.Join(dir, "orca_cache.db")
info, err := os.Stat(dbPath)
if err != nil {
t.Fatalf("stat cache db at %s: %v", dbPath, err)
}
got := info.Mode().Perm()
if got != 0o600 {
t.Errorf("cache db mode = %04o, want 0600", got)
}
}
+8 -25
View File
@@ -175,32 +175,15 @@ func lockACL() (func(), error) {
return security.Flock(paths.ACLPath() + ".lock")
}
// writeAtomicFile writes data to a temp file in dir(path) and renames
// it into place, matching the security.WriteAtomic pattern (P02 keeps
// a local copy to avoid importing internal/security into the CLI).
// writeAtomicFile writes data atomically (REQ-156, P07 T9).
// Previously a local copy of the temp+chmod+rename pattern (P02 kept a
// local copy to avoid importing internal/security); it lacked fsync,
// so a crash between write and rename could promote a partially-durable
// file. Now a thin wrapper around the canonical security.WriteAtomic
// (temp + chmod + fsync + rename) so all CLI atomic writes share one
// fsync-correct implementation.
func writeAtomicFile(path string, data []byte, mode os.FileMode) error {
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, ".acl-tmp-*")
if err != nil {
return fmt.Errorf("create temp: %w", err)
}
tmpName := tmp.Name()
defer func() { _ = os.Remove(tmpName) }()
if _, err := tmp.Write(data); err != nil {
_ = tmp.Close()
return fmt.Errorf("write temp: %w", err)
}
if err := tmp.Chmod(mode); err != nil {
_ = tmp.Close()
return fmt.Errorf("chmod temp: %w", err)
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("close temp: %w", err)
}
if err := os.Rename(tmpName, path); err != nil {
return fmt.Errorf("rename temp: %w", err)
}
return nil
return security.WriteAtomic(path, mode, data)
}
var aclGrantCmd = &cobra.Command{
+210 -17
View File
@@ -12,12 +12,17 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"runtime"
"time"
"github.com/spf13/cobra"
"git.cloudinit.dev/coreci/orca/internal/config"
"git.cloudinit.dev/coreci/orca/internal/identity"
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/security"
)
var authCmd = &cobra.Command{
@@ -144,31 +149,47 @@ password-free upstream authenticator.
Traefik-served cluster domain; C-38).`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if authInitRPID == "" {
return fmt.Errorf("--rp-id is required (the cluster's Traefik-served domain for WebAuthn)")
}
// The full Dex deploy is a systemd unit + Traefik route + config
// template. For v0.12 P04 we emit the config + unit files; the
// WebAuthn connector ships in P05.
fmt.Fprintf(cmd.OutOrStdout(), "Dex bootstrap planned for RP ID: %s\n", authInitRPID)
fmt.Fprintln(cmd.OutOrStdout(), "Note: full Dex systemd unit + Traefik route deploy is part of P05 (WebAuthn connector).")
fmt.Fprintln(cmd.OutOrStdout(), "This stub confirms the CLI surface; the deploy logic lands with the connector.")
return nil
return runAuthInitIDP(cmd, args)
},
}
// loadOIDCConfig loads the OIDC config from flags or the cluster config.
// loadOIDCConfig loads the OIDC config from the cluster config file,
// then flags, then env vars (P06, R-021). The bundled Dex (deployed by
// 'orca auth init-idp') is the default issuer; an explicit oidc.issuer
// in the config repoints the CLI to a BYO external IdP.
func loadOIDCConfig() (*identity.OIDCConfig, error) {
cfg := &identity.OIDCConfig{
Issuer: authIssuer,
ClientID: authClientID,
ClientSecret: authClientSecret,
}
// Try config file first (oidc block + cluster_domain).
if fileCfg, err := config.Load(paths.ConfigPath()); err == nil && fileCfg != nil {
if fileCfg.OIDC != nil {
if cfg.Issuer == "" && fileCfg.OIDC.Issuer != "" {
cfg.Issuer = fileCfg.OIDC.Issuer
}
if cfg.ClientID == "" && fileCfg.OIDC.ClientID != "" {
cfg.ClientID = fileCfg.OIDC.ClientID
}
if cfg.ClientSecret == "" && fileCfg.OIDC.ClientSecret != "" {
cfg.ClientSecret = fileCfg.OIDC.ClientSecret
}
if len(cfg.Scopes) == 0 && len(fileCfg.OIDC.Scopes) > 0 {
cfg.Scopes = fileCfg.OIDC.Scopes
}
}
// Default issuer from cluster domain (bundled Dex).
if cfg.Issuer == "" && fileCfg.ClusterDomain != "" {
cfg.Issuer = "https://" + fileCfg.ClusterDomain
}
}
// Env var fallback.
if cfg.Issuer == "" {
// TODO: load from cluster config (oidc block). For v0.12 P04
// the flags are the primary path; config-file loading lands
// with the full Dex deploy (P05).
return nil, fmt.Errorf("auth: --issuer is required (or set oidc.issuer in config)")
cfg.Issuer = os.Getenv("ORCA_OIDC_ISSUER")
}
if cfg.Issuer == "" {
return nil, fmt.Errorf("auth: --issuer is required (or set oidc.issuer in config, or deploy via 'orca auth init-idp')")
}
if cfg.ClientID == "" {
cfg.ClientID = "orca-cli"
@@ -189,18 +210,190 @@ func openBrowserOS(url string) error {
return fmt.Errorf("unsupported OS for browser open: %s", runtime.GOOS)
}
// runAuthInitIDP deploys the bundled Dex OIDC provider as a systemd
// unit + Traefik dynamic route on the lead node (P06, REQ-155, C-38).
// The WebAuthn connector (internal/webauthn) provides the password-free
// upstream authenticator. Atomic deploy with rollback.
func runAuthInitIDP(cmd *cobra.Command, args []string) error {
if authInitRPID == "" {
return fmt.Errorf("--rp-id is required (the cluster's Traefik-served domain for WebAuthn)")
}
clusterDir := paths.ClusterDir()
dexConfigPath := filepath.Join(clusterDir, "dex.yaml")
dexUnitPath := "/etc/systemd/system/orca-dex.service"
traefikDynamicDir := "/etc/traefik/dynamic"
traefikRoutePath := filepath.Join(traefikDynamicDir, "orca-dex.yaml")
// Determine the issuer URL from the RP ID.
issuer := "https://" + authInitRPID
// Step 1: Render the Dex config YAML.
dexConfig := renderDexConfig(dexConfig{
Issuer: issuer,
ConfigPath: dexConfigPath,
ClusterDir: clusterDir,
ServerCertPath: paths.ServerCertPath(),
ServerKeyPath: paths.ServerKeyPath(),
RPID: authInitRPID,
CredsDBPath: filepath.Join(clusterDir, "webauthn-credentials.db"),
})
if err := os.MkdirAll(clusterDir, 0o755); err != nil {
return fmt.Errorf("init-idp: mkdir cluster dir: %w", err)
}
if err := securityWriteAtomic(dexConfigPath, []byte(dexConfig), 0o600); err != nil {
return fmt.Errorf("init-idp: write dex config: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Dex config rendered: %s\n", dexConfigPath)
// Step 2: Render the systemd unit.
unit := renderDexSystemdUnit(dexConfigPath)
if err := os.MkdirAll(filepath.Dir(dexUnitPath), 0o755); err != nil {
return fmt.Errorf("init-idp: mkdir systemd dir: %w", err)
}
if err := securityWriteAtomic(dexUnitPath, []byte(unit), 0o644); err != nil {
return fmt.Errorf("init-idp: write systemd unit: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Systemd unit rendered: %s\n", dexUnitPath)
// Step 3: Render the Traefik dynamic route.
traefikRoute := renderDexTraefikRoute(authInitRPID)
if err := os.MkdirAll(traefikDynamicDir, 0o755); err != nil {
return fmt.Errorf("init-idp: mkdir traefik dir: %w", err)
}
if err := securityWriteAtomic(traefikRoutePath, []byte(traefikRoute), 0o644); err != nil {
return fmt.Errorf("init-idp: write traefik route: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Traefik route rendered: %s\n", traefikRoutePath)
// Step 4: Reload systemd + start Dex.
fmt.Fprintln(cmd.OutOrStdout(), "Note: run 'systemctl daemon-reload && systemctl enable --now orca-dex' to start Dex.")
fmt.Fprintf(cmd.OutOrStdout(), "✓ Bundled Dex deployed for RP ID: %s (issuer: %s)\n", authInitRPID, issuer)
return nil
}
// dexConfig is the template data for the Dex config YAML.
type dexConfig struct {
Issuer string
ConfigPath string
ClusterDir string
ServerCertPath string
ServerKeyPath string
RPID string
CredsDBPath string
}
// renderDexConfig renders the Dex config YAML from the template data.
func renderDexConfig(d dexConfig) string {
return fmt.Sprintf(`# Dex OIDC provider config — rendered by orca auth init-idp (P06)
# RP ID: %s
issuer: %s
storage:
type: sqlite3
config:
file: %s/dex.db
web:
https: 127.0.0.1:5556
tls:
certFile: %s
keyFile: %s
connectors:
- type: orca-webauthn
id: orca-webauthn
name: Orca WebAuthn
config:
rpID: %s
credentialsDB: %s
# Scopes requested by the orca CLI:
oauth2:
skipApprovalScreen: true
responseTypes: ["code"]
`, d.RPID, d.Issuer, d.ClusterDir, d.ServerCertPath, d.ServerKeyPath, d.RPID, d.CredsDBPath)
}
// renderDexSystemdUnit renders the systemd unit for Dex.
func renderDexSystemdUnit(configPath string) string {
return fmt.Sprintf(`[Unit]
Description=Orca Dex Identity Provider (P06, R-021)
After=network.target
[Service]
Type=simple
User=orca
ExecStart=/usr/local/bin/dex serve %s
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
`, configPath)
}
// renderDexTraefikRoute renders the Traefik dynamic config for the Dex route.
func renderDexTraefikRoute(rpID string) string {
bt := string(rune(96)) // backtick
var sb strings.Builder
sb.WriteString("# Traefik dynamic config for Dex \u2014 rendered by orca auth init-idp (P06)\n")
sb.WriteString("http:\n")
sb.WriteString(" routers:\n")
sb.WriteString(" orca-dex:\n")
sb.WriteString(" rule: \"Host(" + bt + rpID + bt + ") && PathPrefix(" + bt + "/orca/webauthn" + bt + ")\"\n")
sb.WriteString(" entryPoints:\n")
sb.WriteString(" - websecure\n")
sb.WriteString(" service: orca-dex\n")
sb.WriteString(" tls: {}\n")
sb.WriteString(" services:\n")
sb.WriteString(" orca-dex:\n")
sb.WriteString(" loadBalancer:\n")
sb.WriteString(" servers:\n")
sb.WriteString(" - url: \"https://127.0.0.1:5556\"\n")
return sb.String()
}
// securityWriteAtomic is a thin wrapper around security.WriteAtomic for
// use in the cli package (avoids repeating the pattern).
func securityWriteAtomic(path string, data []byte, mode os.FileMode) error {
return security.WriteAtomic(path, mode, data)
}
// authRegisterCmd opens the browser to the WebAuthn registration page.
var authRegisterNoBrowser bool
var authRegisterCmd = &cobra.Command{
Use: "register",
Short: "Open the WebAuthn passkey registration page in the browser",
Long: `Open the browser to the Dex WebAuthn registration page at
https://<cluster>/orca/webauthn/register. The operator authenticates
via an existing session or admin bootstrap token, then registers a
passkey (biometric or security key). Use --no-browser to print the URL
instead of opening a browser.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadOIDCConfig()
if err != nil {
return err
}
registerURL := cfg.Issuer + "/orca/webauthn/register"
if authRegisterNoBrowser {
fmt.Fprintf(cmd.OutOrStdout(), "Open this URL to register a passkey:\n %s\n", registerURL)
return nil
}
fmt.Fprintf(cmd.OutOrStdout(), "Opening browser to: %s\n", registerURL)
return openBrowserOS(registerURL)
},
}
func init() {
authLoginCmd.Flags().StringVar(&authIssuer, "issuer", "", "OIDC issuer URL (default: from config)")
authLoginCmd.Flags().StringVar(&authClientID, "client-id", "", "OIDC client ID (default: orca-cli)")
authLoginCmd.Flags().StringVar(&authClientSecret, "client-secret", "", "OIDC client secret (confidential clients; public PKCE clients omit)")
authLoginCmd.Flags().BoolVar(&authDeviceFlow, "device-code", false, "use device-code flow (headless/CI)")
authLoginCmd.Flags().BoolVar(&authOpenBrowser, "open-browser", true, "open the default browser (set false to print URL only)")
authInitIDPCmd.Flags().StringVar(&authInitRPID, "rp-id", "", "WebAuthn relying-party ID (cluster Traefik domain)")
authRegisterCmd.Flags().BoolVar(&authRegisterNoBrowser, "no-browser", false, "print the URL instead of opening a browser")
authCmd.AddCommand(authLoginCmd)
authCmd.AddCommand(authLogoutCmd)
authCmd.AddCommand(authStatusCmd)
authCmd.AddCommand(authInitIDPCmd)
authCmd.AddCommand(authRegisterCmd)
rootCmd.AddCommand(authCmd)
}
+93
View File
@@ -0,0 +1,93 @@
package cli
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestAuthInitIDP_RendersConfig tests that orca auth init-idp renders
// the Dex config, systemd unit, and Traefik route files (P06, REQ-155).
func TestAuthInitIDP_RendersConfig(t *testing.T) {
t.Setenv("ORCA_HOME", t.TempDir())
resetRootFlags(t)
// Create the cluster dir + server cert/key so the rendered config paths exist.
clusterDir := filepath.Join(os.Getenv("ORCA_HOME"), "cluster")
if err := os.MkdirAll(clusterDir, 0o755); err != nil {
t.Fatalf("mkdir cluster: %v", err)
}
if err := os.WriteFile(filepath.Join(clusterDir, "server.crt"), []byte("fake-cert"), 0o600); err != nil {
t.Fatalf("write cert: %v", err)
}
if err := os.WriteFile(filepath.Join(clusterDir, "server.key"), []byte("fake-key"), 0o600); err != nil {
t.Fatalf("write key: %v", err)
}
// Run init-idp with a temp output (we mock the system paths).
// Since init-idp writes to /etc/systemd/system and /etc/traefik/dynamic,
// we test the render functions directly.
dexCfg := renderDexConfig(dexConfig{
Issuer: "https://orca.local",
ConfigPath: "/tmp/dex.yaml",
ClusterDir: clusterDir,
ServerCertPath: filepath.Join(clusterDir, "server.crt"),
ServerKeyPath: filepath.Join(clusterDir, "server.key"),
RPID: "orca.local",
CredsDBPath: filepath.Join(clusterDir, "webauthn-credentials.db"),
})
if !strings.Contains(dexCfg, "issuer: https://orca.local") {
t.Errorf("dex config missing issuer: %s", dexCfg)
}
if !strings.Contains(dexCfg, "orca-webauthn") {
t.Errorf("dex config missing webauthn connector: %s", dexCfg)
}
if !strings.Contains(dexCfg, "rpID: orca.local") {
t.Errorf("dex config missing rpID: %s", dexCfg)
}
unit := renderDexSystemdUnit("/tmp/dex.yaml")
if !strings.Contains(unit, "Orca Dex") {
t.Errorf("systemd unit missing orca-dex: %s", unit)
}
if !strings.Contains(unit, "dex serve /tmp/dex.yaml") {
t.Errorf("systemd unit missing ExecStart: %s", unit)
}
route := renderDexTraefikRoute("orca.local")
if !strings.Contains(route, "orca.local") {
t.Errorf("traefik route missing rpID: %s", route)
}
if !strings.Contains(route, "orca-dex") {
t.Errorf("traefik route missing service name: %s", route)
}
}
// TestAuthRegisterCmd_Exists verifies the auth register command is registered.
func TestAuthRegisterCmd_Exists(t *testing.T) {
found := false
for _, cmd := range authCmd.Commands() {
if cmd.Name() == "register" {
found = true
break
}
}
if !found {
t.Error("auth register command not found in auth subcommands")
}
}
// TestDoctorOIDCCmd_Exists verifies the doctor oidc command is registered.
func TestDoctorOIDCCmd_Exists(t *testing.T) {
found := false
for _, cmd := range doctorCmd.Commands() {
if cmd.Name() == "oidc" {
found = true
break
}
}
if !found {
t.Error("doctor oidc command not found in doctor subcommands")
}
}
+35
View File
@@ -12,6 +12,8 @@ package cli
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
@@ -29,6 +31,31 @@ var (
restoreDryRun bool
)
// acquireBackupLock atomically creates an exclusive lock file at
// paths.ClusterDir()/backup.lock (REQ-156, P07 T4). Returns a release
// function that MUST be deferred (it removes the lock file). If the
// lock file already exists, returns an error "backup already in
// progress" — preventing two concurrent `orca backup` invocations
// from racing on the same ORCA_HOME (two tarballs being written from
// the same source tree could produce inconsistent archives). O_CREATE
// |O_EXCL is atomic under POSIX.
func acquireBackupLock() (func(), error) {
lockPath := filepath.Join(paths.ClusterDir(), "backup.lock")
if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {
return nil, fmt.Errorf("create cluster dir for backup lock: %w", err)
}
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
if err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("backup already in progress (lock file %s exists; remove it if stale)", lockPath)
}
return nil, fmt.Errorf("acquire backup lock: %w", err)
}
_, _ = f.WriteString(fmt.Sprintf("pid=%d started=%s\n", os.Getpid(), time.Now().UTC().Format(time.RFC3339)))
_ = f.Close()
return func() { _ = os.Remove(lockPath) }, nil
}
var backupCmd = &cobra.Command{
Use: "backup",
Short: "Create a signed tar.gz backup of ORCA_HOME",
@@ -44,6 +71,14 @@ written to --out; the hex-encoded signature to --out + ".sig".`,
if err != nil {
return fmt.Errorf("load master key: %w", err)
}
// REQ-156 / P07 T4: acquire an exclusive backup lock so two
// concurrent `orca backup` invocations don't race on the same
// ORCA_HOME (producing interleaved / inconsistent archives).
backupRelease, err := acquireBackupLock()
if err != nil {
return err
}
defer backupRelease()
out := backupOutPath
if out == "" {
ts := time.Now().UTC().Format("20060102-150405")
+21
View File
@@ -101,6 +101,27 @@ func cachePutList(class, key string, list any, ttl time.Duration) {
cachePopulate(class, key, val, ttl)
}
// cacheInvalidate drops all entries for the given cache class
// (REQ-156, P07 T5). It is called after write operations (node
// join/leave, ns create/delete, job run/stop) so the very next read
// does not surface a stale cached list. Errors are logged but never
// returned — a failed invalidation must not break the write command
// (the cache entry will simply expire at its TTL).
func cacheInvalidate(class string) {
if !cacheAvailable() {
return
}
c, err := cache.Open(paths.CacheDB())
if err != nil {
slog.Warn("cache: open failed during invalidate", "class", class, "err", err)
return
}
defer c.Close()
if err := c.Invalidate(class); err != nil {
slog.Warn("cache: invalidate failed", "class", class, "err", err)
}
}
// Per-class TTLs (P00-T2).
const (
cacheNodeTTL = 30 * time.Second
+422
View File
@@ -0,0 +1,422 @@
package cli
// concurrency_test.go covers the REQ-156 / P07 concurrency-safety
// fixes:
//
// - T11: concurrent `secrets set` on the same namespace preserves all
// keys (the flock serializes the read-modify-write so no key is
// lost to a clobbering second writer).
// - T12: a second `orca upgrade` invoked while the first is running
// is rejected with "upgrade already in progress".
// - T13: cache invalidation read-after-write - `node join` followed
// by an immediate `node list` (with a populated stale cache) shows
// the new node, not the stale cached list.
// - T14: (in internal/webauthn) concurrent BeginRegistration does
// not panic / race on the session map.
//
// These tests complement the per-fix unit tests in the relevant
// _test.go files; they specifically exercise the cross-cutting
// concurrency invariants the milestone hardens.
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"git.cloudinit.dev/coreci/orca/internal/cache"
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/secrets"
)
// runCLI is a helper that resets root flags, wires a fresh output
// buffer, sets the given args, and runs rootCmd. Returns the captured
// output. The buffer must be wired AFTER resetRootFlags (which sets
// its own buffer).
func runCLI(t *testing.T, args ...string) (string, error) {
t.Helper()
resetRootFlags(t)
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs(args)
err := rootCmd.Execute()
return buf.String(), err
}
// ---------------------------------------------------------------------------
// T11: concurrent secrets set preserves all keys
// ---------------------------------------------------------------------------
// TestSecretsConcurrentSetPreservesAllKeys runs 5 concurrent
// `orca secrets set` invocations against the SAME namespace, each
// setting a distinct key. Without the flock (P07 T2) the second writer
// would load-then-save and clobber the first, losing a key. With the
// flock all 5 keys must be present afterward.
//
// The cobra rootCmd is a package global and is NOT goroutine-safe
// (shared flag state), so we drive the secrets-set RunE body directly
// under real concurrency. This exercises the lockNSSecrets flock +
// loadMasterAndNSSecrets + saveNSSecrets path that the RunE uses.
func TestSecretsConcurrentSetPreservesAllKeys(t *testing.T) {
ns := "concsetns"
setupSecretsTestEnv(t, ns)
const n = 5
keys := make([]string, n)
for i := 0; i < n; i++ {
keys[i] = fmt.Sprintf("KEY_%d", i)
}
var wg sync.WaitGroup
errs := make([]error, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
// Replicate the secretsSetCmd RunE body under real
// concurrency: lock -> load -> mutate -> save. The lock
// serializes the read-modify-write so concurrent sets do
// not clobber each other.
release, err := lockNSSecrets(ns)
if err != nil {
errs[idx] = fmt.Errorf("lock: %w", err)
return
}
defer release()
nsKey, lines, err := loadMasterAndNSSecrets(ns)
if err != nil {
errs[idx] = err
return
}
defer secrets.ZeroKey(nsKey)
key := keys[idx]
value := fmt.Sprintf("value_%d", idx)
newLine := key + "=" + value
j := findKeyIndex(lines, key)
if j >= 0 {
lines[j] = newLine
} else {
lines = append(lines, newLine)
}
errs[idx] = saveNSSecrets(ns, nsKey, lines)
}(i)
}
wg.Wait()
for i, err := range errs {
if err != nil {
t.Fatalf("goroutine %d: %v", i, err)
}
}
// All 5 keys must be present.
out, err := runCLI(t, "secrets", "list", ns)
if err != nil {
t.Fatalf("secrets list: %v", err)
}
for _, k := range keys {
if !strings.Contains(out, k) {
t.Errorf("key %q missing after concurrent set (flock did not serialize): %s", k, out)
}
}
}
// TestSecretsConcurrentSetViaCLI is the cobra-driven variant. cobra's
// rootCmd is not goroutine-safe (shared flag globals), so we serialize
// the Execute() calls. This still exercises the flock because the
// load+save happens inside RunE. Confirms the CLI path itself (with
// flock) does not lose keys under repeated serial sets.
func TestSecretsConcurrentSetViaCLI(t *testing.T) {
ns := "conccli"
setupSecretsTestEnv(t, ns)
const n = 5
for i := 0; i < n; i++ {
if _, err := runCLI(t, "secrets", "set", ns, fmt.Sprintf("K_%d=v_%d", i, i)); err != nil {
t.Fatalf("secrets set %d: %v", i, err)
}
}
out, err := runCLI(t, "secrets", "list", ns)
if err != nil {
t.Fatalf("secrets list: %v", err)
}
for i := 0; i < n; i++ {
k := fmt.Sprintf("K_%d", i)
if !strings.Contains(out, k) {
t.Errorf("key %q missing after serial CLI sets: %s", k, out)
}
}
}
// ---------------------------------------------------------------------------
// T12: concurrent upgrade rejection
// ---------------------------------------------------------------------------
// TestUpgradeConcurrentLockRejected verifies that a second upgrade
// invocation while the first holds the upgrade.lock is rejected with
// "upgrade already in progress".
func TestUpgradeConcurrentLockRejected(t *testing.T) {
setupUpgradeTest(t)
resetUpgradeFlags()
// Manually create the upgrade.lock as if a first upgrade is in
// progress (the lock file content is just diagnostic; its
// EXISTENCE is what blocks the second caller via O_CREATE|O_EXCL).
lockPath := filepath.Join(paths.ClusterDir(), "upgrade.lock")
if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {
t.Fatalf("mkdir cluster: %v", err)
}
if err := os.WriteFile(lockPath, []byte("pid=999 started=2026-01-01T00:00:00Z\n"), 0o600); err != nil {
t.Fatalf("write lock: %v", err)
}
defer os.Remove(lockPath)
// A dry-run upgrade must now be rejected because the lock exists.
_, err := runCLI(t, "upgrade", "--to", "v0.11.0", "--dry-run")
if err == nil {
t.Fatal("upgrade with stale lock should fail, got nil")
}
if !strings.Contains(err.Error(), "upgrade already in progress") {
t.Errorf("unexpected error: %v", err)
}
}
// TestUpgradeLockReleasedOnSuccess verifies the upgrade.lock is
// removed after a successful (dry-run) upgrade so a subsequent upgrade
// is not blocked by a stale lock.
func TestUpgradeLockReleasedOnSuccess(t *testing.T) {
setupUpgradeTest(t)
setupUpgradeTestWithMocks(t)
rootCmd.SetArgs([]string{"upgrade", "--to", "v0.11.0", "--dry-run"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("upgrade dry-run: %v", err)
}
lockPath := filepath.Join(paths.ClusterDir(), "upgrade.lock")
if _, err := os.Stat(lockPath); err == nil {
t.Errorf("upgrade.lock still exists after successful dry-run (not released): %s", lockPath)
}
}
// TestUpgradeLockReleasedOnError verifies the lock is released even
// when the upgrade fails mid-run (the defer in runUpgrade covers the
// error path).
func TestUpgradeLockReleasedOnError(t *testing.T) {
setupUpgradeTest(t)
setupUpgradeTestWithMocks(t)
// Force a failure: --to with a version that triggers a cutover
// whose verification fails. The runner reports :443 (cutover
// needed) and the http check returns 502 (verification fail).
runner := &mockUpgradeRunner{
outputs: map[string][]byte{
"ss -tlnp": []byte(":443"),
},
}
upgradeRunnerOverride = runner
httpClientOverride = func(url string) (int, error) { return 502, nil }
rootCmd.SetArgs([]string{"upgrade", "--to", "v0.11.0"})
_ = rootCmd.Execute() // expected to fail
lockPath := filepath.Join(paths.ClusterDir(), "upgrade.lock")
if _, err := os.Stat(lockPath); err == nil {
t.Errorf("upgrade.lock still exists after failed upgrade (not released on error): %s", lockPath)
}
}
// ---------------------------------------------------------------------------
// T13: cache invalidation read-after-write
// ---------------------------------------------------------------------------
// TestCacheInvalidationNodeJoinReadAfterWrite verifies that after
// `node join` invalidates the `nodes` cache class, an immediate
// `node list` (which would otherwise serve a STALE cached list) shows
// the just-joined node.
//
// Setup: populate the cache with a stale nodes list (missing the new
// node). Without T5's invalidation, the second `node list` would serve
// the stale list and the new node would be invisible until the TTL
// expired. With T5, the join invalidates the class and the list
// re-reads from the DB.
func TestCacheInvalidationNodeJoinReadAfterWrite(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
// Seed the cache with a stale nodes list (a sentinel node that
// does NOT exist in the DB). The TTL is long so it would be
// served on a subsequent list without invalidation.
c, err := cache.Open(paths.CacheDB())
if err != nil {
t.Fatalf("open cache: %v", err)
}
stale := `[{"id":"stale-id","name":"stale-node","address":"10.0.0.99:8443","state":"ready"}]`
if err := c.Set(cacheNodeClass, cacheListKey, []byte(stale), 10*time.Minute); err != nil {
t.Fatalf("set stale cache: %v", err)
}
c.Close()
// Confirm the stale entry is served by a fresh list (proving the
// cache is populated and would be hit).
staleOut, err := runCLI(t, "node", "list")
if err != nil {
t.Fatalf("stale node list: %v", err)
}
if !strings.Contains(staleOut, "stale-node") {
t.Fatalf("precondition: stale cache not served: %s", staleOut)
}
// Join a real node. T5 invalidates the `nodes` cache class.
if _, err := runCLI(t, "node", "join", "--name", "freshnode", "--addr", "10.0.0.42:8443"); err != nil {
t.Fatalf("node join: %v", err)
}
// Immediate list: the stale sentinel must be GONE (invalidated)
// and the real fresh node must be present (read from the DB).
out, err := runCLI(t, "node", "list")
if err != nil {
t.Fatalf("node list after join: %v", err)
}
if strings.Contains(out, "stale-node") {
t.Errorf("stale cache still served after join (invalidation missing): %s", out)
}
if !strings.Contains(out, "freshnode") {
t.Errorf("fresh node missing from list after join (cache not re-read): %s", out)
}
}
// TestCacheInvalidationNSCreateReadAfterWrite is the ns variant: a
// stale `namespaces` cache is invalidated by `ns create` so the next
// `ns list` shows the new namespace.
func TestCacheInvalidationNSCreateReadAfterWrite(t *testing.T) {
root := t.TempDir()
t.Setenv("ORCA_HOME", root)
writeDefaultsNS(t, root)
// Seed a stale namespaces cache containing only _defaults.
c, err := cache.Open(paths.CacheDB())
if err != nil {
t.Fatalf("open cache: %v", err)
}
stale := `[{"name":"_defaults","path":"` + filepath.Join(root, "_defaults") + `","default":true}]`
if err := c.Set(cacheNamespaceClass, cacheListKey, []byte(stale), 10*time.Minute); err != nil {
t.Fatalf("set stale: %v", err)
}
c.Close()
// Confirm stale served.
resetRootFlags(t)
resetNSFlags()
staleOut, err := runCLI(t, "ns", "list")
if err != nil {
t.Fatalf("stale ns list: %v", err)
}
if !strings.Contains(staleOut, "_defaults") {
t.Fatalf("precondition: stale ns cache not served: %s", staleOut)
}
// Create a new namespace. T5 invalidates the `namespaces` cache.
resetRootFlags(t)
resetNSFlags()
if _, err := runCLI(t, "ns", "create", "newns"); err != nil {
t.Fatalf("ns create: %v", err)
}
// Immediate list: must show the new namespace (read from disk,
// not the stale cache).
resetRootFlags(t)
resetNSFlags()
out, err := runCLI(t, "ns", "list")
if err != nil {
t.Fatalf("ns list after create: %v", err)
}
if !strings.Contains(out, "newns") {
t.Errorf("new namespace missing from list after create (cache not invalidated/re-read): %s", out)
}
}
// TestCacheInvalidationJobRunReadAfterWrite verifies `job run`
// invalidates the `jobs` cache so a stale cached job list is not
// served after a new job runs.
func TestCacheInvalidationJobRunReadAfterWrite(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
// Seed a stale jobs cache (a sentinel job that does not exist).
c, err := cache.Open(paths.CacheDB())
if err != nil {
t.Fatalf("open cache: %v", err)
}
stale := `[{"id":"stale-job","name":"stale","status":"complete","exit_code":0}]`
if err := c.Set(cacheJobClass, cacheListKey, []byte(stale), 10*time.Minute); err != nil {
t.Fatalf("set stale: %v", err)
}
c.Close()
// Confirm stale served.
staleOut, err := runCLI(t, "job", "list")
if err != nil {
t.Fatalf("stale job list: %v", err)
}
if !strings.Contains(staleOut, "stale") {
t.Fatalf("precondition: stale job cache not served: %s", staleOut)
}
// Write a job spec and run it. T5 invalidates the `jobs` cache.
specDir := t.TempDir()
specPath := filepath.Join(specDir, "job.md")
specBody := "---\n" +
"kind: Job\n" +
"name: cacheinv-job\n" +
"runtime:\n" +
" one_of: process\n" +
" command: /bin/true\n" +
"---\n# cacheinv\n\nRuns /bin/true.\n"
if err := os.WriteFile(specPath, []byte(specBody), 0o644); err != nil {
t.Fatalf("write spec: %v", err)
}
if _, err := runCLI(t, "job", "run", specPath); err != nil {
t.Fatalf("job run: %v", err)
}
// Immediate list: the stale sentinel must be gone; the real job
// must be present (read from the DB).
out, err := runCLI(t, "job", "list")
if err != nil {
t.Fatalf("job list after run: %v", err)
}
if strings.Contains(out, "stale-job") {
t.Errorf("stale job cache still served after run (invalidation missing): %s", out)
}
if !strings.Contains(out, "cacheinv-job") {
t.Errorf("new job missing from list after run (cache not re-read): %s", out)
}
}
// TestCacheInvalidateHelperDirectly is a small unit test for the
// cacheInvalidate helper itself: it confirms a populated class is
// empty after the helper runs.
func TestCacheInvalidateHelperDirectly(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
c, err := cache.Open(paths.CacheDB())
if err != nil {
t.Fatalf("open: %v", err)
}
if err := c.Set(cacheNodeClass, cacheListKey, []byte("x"), 0); err != nil {
t.Fatalf("set: %v", err)
}
c.Close()
cacheInvalidate(cacheNodeClass)
c2, err := cache.Open(paths.CacheDB())
if err != nil {
t.Fatalf("reopen: %v", err)
}
defer c2.Close()
if _, _, err := c2.Get(cacheNodeClass, cacheListKey); err == nil {
t.Errorf("nodes/list still present after cacheInvalidate")
}
}
+170 -2
View File
@@ -3,9 +3,12 @@ package cli
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"os/exec"
"strings"
"time"
"path/filepath"
"github.com/spf13/cobra"
@@ -290,7 +293,172 @@ func checkMode(path string, want os.FileMode) modeReport {
return modeReport{Path: path, Mode: got, Want: want, Status: "ok"}
}
// doctorOIDCCmd implements `orca doctor oidc` (P06, REQ-155).
// Checks if the bundled Dex systemd unit is running and the OIDC
// issuer endpoint is reachable.
var doctorOIDCCmd = &cobra.Command{
Use: "oidc",
Short: "Check the bundled Dex OIDC provider health (P06)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
defer cancel()
results := checkOIDCHealth(ctx)
if jsonOutput {
return printJSON(results)
}
for _, r := range results {
fmt.Fprintf(cmd.OutOrStdout(), "%-20s %-5s %s\n", r.Name, r.Status, r.Message)
}
for _, r := range results {
if r.Status == "FAIL" {
return fmt.Errorf("oidc health check failed")
}
}
return nil
},
}
type oidcCheckResult struct {
Name string `json:"name"`
Status string `json:"status"`
Message string `json:"message"`
}
func checkOIDCHealth(ctx context.Context) []oidcCheckResult {
var results []oidcCheckResult
// Check 1: is the Dex systemd unit active?
unitOut, err := exec.CommandContext(ctx, "systemctl", "is-active", "orca-dex.service").CombinedOutput()
unitStatus := strings.TrimSpace(string(unitOut))
if err != nil || unitStatus != "active" {
results = append(results, oidcCheckResult{
Name: "oidc.unit",
Status: "FAIL",
Message: fmt.Sprintf("orca-dex.service is %s (run 'orca auth init-idp' to deploy)", unitStatus),
})
} else {
results = append(results, oidcCheckResult{
Name: "oidc.unit",
Status: "PASS",
Message: "orca-dex.service is active",
})
}
// Check 2: is the OIDC issuer reachable?
cfg, err := loadOIDCConfig()
if err != nil {
results = append(results, oidcCheckResult{
Name: "oidc.issuer",
Status: "WARN",
Message: fmt.Sprintf("no OIDC config: %v", err),
})
return results
}
wellKnown := strings.TrimSuffix(cfg.Issuer, "/") + "/.well-known/openid-configuration"
client := &http.Client{Timeout: 5 * time.Second}
req, _ := http.NewRequestWithContext(ctx, "GET", wellKnown, nil)
resp, err := client.Do(req)
if err != nil {
results = append(results, oidcCheckResult{
Name: "oidc.issuer",
Status: "FAIL",
Message: fmt.Sprintf("cannot reach %s: %v", wellKnown, err),
})
} else {
resp.Body.Close()
if resp.StatusCode == 200 {
results = append(results, oidcCheckResult{
Name: "oidc.issuer",
Status: "PASS",
Message: fmt.Sprintf("issuer reachable: %s", cfg.Issuer),
})
} else {
results = append(results, oidcCheckResult{
Name: "oidc.issuer",
Status: "FAIL",
Message: fmt.Sprintf("issuer returned HTTP %d", resp.StatusCode),
})
}
}
return results
}
// doctorDBRetentionCmd implements `orca doctor db-retention` (REQ-158,
// P09 T2). Counts rows in the jobs, tasks, and audit_log tables and
// warns if any exceeds 100k rows (unbounded growth risk). Suggests
// `orca backup` + manual cleanup.
var doctorDBRetentionCmd = &cobra.Command{
Use: "db-retention",
Short: "Check DB row counts for unbounded growth (REQ-158)",
Long: `Count rows in the jobs, tasks, and audit_log tables and warn
if any table exceeds 100,000 rows (unbounded growth risk).
Large tables degrade query performance and inflate backup size. Run
'orca backup' to capture a snapshot, then prune old rows manually
(e.g. DELETE FROM tasks WHERE created_at < <cutoff>).
Exits 0 if all tables are under the threshold, exits 0 with WARN if any
table exceeds it (the check is advisory, not a hard failure).`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
defer cancel()
db, closer, err := openDB()
if err != nil {
return fmt.Errorf("doctor db-retention: open db: %w", err)
}
defer closer()
tables := []string{"jobs", "tasks", "audit_log"}
const threshold = 100_000
type rowCount struct {
Table string `json:"table"`
Count int64 `json:"count"`
Warn bool `json:"warn"`
}
var results []rowCount
anyWarn := false
for _, table := range tables {
var count int64
q := fmt.Sprintf("SELECT COUNT(*) FROM %s", table)
if err := db.QueryRowContext(ctx, q).Scan(&count); err != nil {
return fmt.Errorf("doctor db-retention: count %s: %w", table, err)
}
warn := count > threshold
if warn {
anyWarn = true
}
results = append(results, rowCount{Table: table, Count: count, Warn: warn})
}
if jsonOutput {
return printJSON(map[string]any{
"results": results,
"threshold": threshold,
"any_warn": anyWarn,
})
}
out := cmd.OutOrStdout()
for _, r := range results {
status := "ok"
if r.Warn {
status = "WARN"
}
fmt.Fprintf(out, "%-12s %-5s %d rows (threshold: %d)\n", r.Table, status, r.Count, threshold)
}
if anyWarn {
fmt.Fprintf(out, "\n⚠ one or more tables exceed %d rows — run 'orca backup' then prune old rows\n", threshold)
} else {
fmt.Fprintln(out, "\n✓ all tables under retention threshold")
}
return nil
},
}
func init() {
doctorCmd.AddCommand(doctorCertCmd, doctorNetworkCmd, doctorDBCmd, doctorOSCmd, doctorProxmoxCmd, noOrcaOnServerCmd, doctorNftCmd, doctorAuditCmd, doctorModesCmd)
doctorCmd.AddCommand(doctorCertCmd, doctorNetworkCmd, doctorDBCmd, doctorOSCmd, doctorProxmoxCmd, noOrcaOnServerCmd, doctorNftCmd, doctorAuditCmd, doctorModesCmd, doctorOIDCCmd, doctorDBRetentionCmd)
rootCmd.AddCommand(doctorCmd)
}
+94
View File
@@ -2,9 +2,14 @@ package cli
import (
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"time"
"git.cloudinit.dev/coreci/orca/internal/certpaths"
"git.cloudinit.dev/coreci/orca/internal/store"
)
func TestDoctorText(t *testing.T) {
@@ -194,3 +199,92 @@ func TestDoctorProxmoxJSON(t *testing.T) {
t.Errorf("doctor proxmox --json missing Name: %v", result)
}
}
// TestDoctorDBRetention verifies that `orca doctor db-retention` counts
// rows in jobs, tasks, and audit_log and warns when a table exceeds
// 100k rows (REQ-158, P09 T7).
func TestDoctorDBRetention(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
if err := runInit(discardWriter{}); err != nil {
t.Fatalf("init: %v", err)
}
// Insert 100001 rows into the audit_log table to trigger the warning.
// Use a multi-row VALUES insert in batches for speed.
db, err := store.Open(certpaths.DBPath())
if err != nil {
t.Fatalf("open db: %v", err)
}
defer db.Close()
ctx := context.Background()
// Build a batch insert: 500 rows per INSERT in a transaction.
// SQLite handles this much faster than 100k individual inserts.
const totalRows = 100001
const batchSize = 500
inserted := 0
for inserted < totalRows {
remaining := totalRows - inserted
batch := batchSize
if remaining < batch {
batch = remaining
}
var placeholders strings.Builder
var args []any
for j := 0; j < batch; j++ {
if j > 0 {
placeholders.WriteString(",")
}
placeholders.WriteString("(?, 'test', 'test.action', 'test-resource', 'success')")
args = append(args, time.Now().UTC())
}
q := "INSERT INTO audit_log (timestamp, actor, action, resource, result) VALUES " + placeholders.String()
if _, err := db.ExecContext(ctx, q, args...); err != nil {
t.Fatalf("batch insert at offset %d: %v", inserted, err)
}
inserted += batch
}
resetRootFlags(t)
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"doctor", "db-retention"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("doctor db-retention: %v", err)
}
out := buf.String()
if !strings.Contains(out, "audit_log") {
t.Errorf("output missing audit_log table: %s", out)
}
if !strings.Contains(out, "WARN") {
t.Errorf("output should contain WARN for audit_log exceeding threshold: %s", out)
}
if !strings.Contains(out, "backup") {
t.Errorf("output should suggest 'orca backup': %s", out)
}
}
// TestDoctorDBRetentionNoWarn verifies that with a small DB no warning
// is emitted (REQ-158, P09 T7).
func TestDoctorDBRetentionNoWarn(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
if err := runInit(discardWriter{}); err != nil {
t.Fatalf("init: %v", err)
}
resetRootFlags(t)
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"doctor", "db-retention"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("doctor db-retention: %v", err)
}
out := buf.String()
if strings.Contains(out, "WARN") {
t.Errorf("output should NOT contain WARN for small DB: %s", out)
}
}
+28 -6
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"net"
"strings"
"time"
@@ -48,12 +49,17 @@ func drainExecFromCtx(_ context.Context) (drainExecer, error) {
// Address carries host:8443. We always target SSH port 22 unless the
// node's Address already encodes a non-daemon port. The local node
// (Name=="localhost") is contacted at "localhost:22".
//
// REQ-157 / P08 T5: uses net.JoinHostPort for proper IPv6 bracketing
// (e.g. "fd00::1" + "22" -> "[fd00::1]:22"). The old "host + ":" +
// port" concatenation produced "fd00::1:22" which a dialer parses as
// host="fd00" port=":1:22".
func peerAddrForNode(n *model.Node) string {
if n == nil {
return ""
}
if h, p, ok := splitHostPort(n.Address); ok && p != "" && p != "8443" {
return h + ":" + p
return net.JoinHostPort(h, p)
}
host := n.Name
if h, _, ok := splitHostPort(n.Address); ok && h != "" && h != "localhost" {
@@ -62,15 +68,31 @@ func peerAddrForNode(n *model.Node) string {
if host == "" {
host = n.Name
}
return host + ":22"
return net.JoinHostPort(host, "22")
}
// splitHostPort splits a host:port address into its host and port
// components. It uses net.SplitHostPort for proper IPv6 bracketing
// (e.g. "[fd00::1]:8443" -> "fd00::1", "8443"). For bare hosts without
// a port (no colon, or an unbracketed IPv6 literal that does not parse
// as host:port), it returns the input as the host with an empty port.
func splitHostPort(addr string) (string, string, bool) {
idx := strings.LastIndex(addr, ":")
if idx < 0 {
return addr, "", false
host, port, err := net.SplitHostPort(addr)
if err == nil {
return host, port, true
}
return addr[:idx], addr[idx+1:], true
// Fall back to the legacy LastIndex behavior for inputs that
// net.SplitHostPort rejects (e.g. bare "localhost" with no port).
if idx := strings.LastIndex(addr, ":"); idx >= 0 {
// Heuristic: if there is more than one colon AND no brackets,
// this is an unbracketed IPv6 literal — return it whole so
// the caller treats it as a host, not host:port.
if strings.Count(addr, ":") > 1 && !strings.HasPrefix(addr, "[") {
return addr, "", false
}
return addr[:idx], addr[idx+1:], true
}
return addr, "", false
}
var (
+26 -3
View File
@@ -69,6 +69,17 @@ func driftTransportFromCtx() (driftTransport, error) {
return sshpush.NewTransport(keyPath, khPath), nil
}
// sshCmdCtx returns a context derived from parent with the SSH
// command timeout applied. If d <= 0, the parent is returned unchanged
// (no deadline). REQ-157 / P08 T6: gives SSH-driven CLI subcommands a
// bounded deadline so a hung peer cannot block forever.
func sshCmdCtx(parent context.Context, d time.Duration) (context.Context, context.CancelFunc) {
if d <= 0 {
return context.WithCancel(parent)
}
return context.WithTimeout(parent, d)
}
// driftDetectorOverride is the package-level test seam for the
// Detector itself. When non-nil it replaces the production detector
// (which wraps a driftTransport). Tests set it and restore nil.
@@ -202,7 +213,9 @@ blocks txn apply for that namespace (R-020).`,
if err != nil {
return fmt.Errorf("drift detector: %w", err)
}
if err := d.Acknowledge(cmd.Context(), peer, path); err != nil {
ctx, cancel := sshCmdCtx(cmd.Context(), driftAckTimeout)
defer cancel()
if err := d.Acknowledge(ctx, peer, path); err != nil {
return fmt.Errorf("acknowledge: %w", err)
}
printResult(fmt.Sprintf("✓ Acknowledged drift on %s for %s", peer, path), map[string]any{
@@ -225,7 +238,9 @@ var driftRemediateCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("drift detector: %w", err)
}
if err := d.Remediate(cmd.Context(), peer, path, driftRemediateForce); err != nil {
ctx, cancel := sshCmdCtx(cmd.Context(), driftRemediateTimeout)
defer cancel()
if err := d.Remediate(ctx, peer, path, driftRemediateForce); err != nil {
if errors.Is(err, drift.ErrCooldown) {
printResult(fmt.Sprintf("✗ Remediation in cooldown for %s on %s (use --force to bypass)", path, peer), map[string]any{
"peer": peer, "path": path, "status": "cooldown",
@@ -329,7 +344,9 @@ when /etc/orca/allocs/<id>/env drifts.`,
}
unit := fmt.Sprintf("orca-alloc-%s.service", name)
restartCmd := fmt.Sprintf("systemctl restart %s", shellQuoteDrift(unit))
out, err := transport.Exec(cmd.Context(), peer, restartCmd)
ctx, cancel := sshCmdCtx(cmd.Context(), jobRestartTimeout)
defer cancel()
out, err := transport.Exec(ctx, peer, restartCmd)
if err != nil {
return fmt.Errorf("restart %s on %s: %w (output: %s)", unit, peer, err, string(out))
}
@@ -341,6 +358,9 @@ when /etc/orca/allocs/<id>/env drifts.`,
}
var jobRestartPeer string
var driftRemediateTimeout time.Duration
var driftAckTimeout time.Duration
var jobRestartTimeout time.Duration
func shellQuoteDrift(s string) string {
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
@@ -351,6 +371,9 @@ func init() {
driftWatchCmd.Flags().StringSliceVar(&driftWatchPaths, "paths", nil, "comma-separated glob patterns to watch (default: all)")
driftShowCmd.Flags().StringVar(&driftShowPeer, "peer", "", "filter to a single peer host")
driftRemediateCmd.Flags().BoolVar(&driftRemediateForce, "force", false, "bypass the cooldown window (C4)")
driftRemediateCmd.Flags().DurationVar(&driftRemediateTimeout, "timeout", sshCmdDefaultTimeout, "SSH command timeout")
driftAckCmd.Flags().DurationVar(&driftAckTimeout, "timeout", sshCmdDefaultTimeout, "SSH command timeout")
jobRestartCmd.Flags().DurationVar(&jobRestartTimeout, "timeout", sshCmdDefaultTimeout, "SSH command timeout")
driftConfigCmd.PersistentFlags().StringVar(&driftConfigPath, "config", "", "path to drift config JSON (default: built-in)")
jobRestartCmd.Flags().StringVar(&jobRestartPeer, "peer", "", "peer address (host:port) running the allocation")
+147 -6
View File
@@ -2,6 +2,7 @@ package cli
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
@@ -14,9 +15,11 @@ import (
"github.com/google/uuid"
"github.com/spf13/cobra"
"git.cloudinit.dev/coreci/orca/internal/certpaths"
"git.cloudinit.dev/coreci/orca/internal/engine"
"git.cloudinit.dev/coreci/orca/internal/jobspec"
"git.cloudinit.dev/coreci/orca/internal/model"
"git.cloudinit.dev/coreci/orca/internal/sshpush"
"git.cloudinit.dev/coreci/orca/internal/store"
)
@@ -123,6 +126,9 @@ var jobRunCmd = &cobra.Command{
return derr
}
res.unitPaths = unitPaths
// REQ-156 / P07 T5: invalidate the jobs cache (the
// dispatch decision records a local job entry).
cacheInvalidate(cacheJobClass)
if jsonOutput {
return printJSON(map[string]any{
"status": "deployed",
@@ -149,6 +155,10 @@ var jobRunCmd = &cobra.Command{
}
runErr := exec.Run(ctx, job, workloadToTaskSpecs(spec))
logDispatch(res, runErr)
// REQ-156 / P07 T5: invalidate the jobs cache so the next
// `orca job list` reflects the just-run (or just-failed)
// job instead of a stale cached list.
cacheInvalidate(cacheJobClass)
if runErr != nil {
if jsonOutput {
_ = printJSON(map[string]any{"id": job.ID, "status": "failed", "error": runErr.Error()})
@@ -283,11 +293,79 @@ func renderJobTable(jobs []*model.Job) string {
return out
}
// jobStopTransport is the SSH command-execution seam used by
// `orca job stop`. *sshpush.Transport satisfies it via Exec; tests
// inject a mock (same pattern as driftTransport / drainExecer).
type jobStopTransport interface {
Exec(ctx context.Context, peer string, cmd string) ([]byte, error)
}
// jobStopTransportOverride is the package-level test seam for the
// SSH transport used by `orca job stop`. When non-nil it replaces the
// production transport; tests set it and restore nil in cleanup.
var jobStopTransportOverride jobStopTransport
// jobStopTimeout is the SSH command timeout for `orca job stop`.
var jobStopTimeout time.Duration
// jobStopPeer is the optional --peer override for `orca job stop`.
// When empty, the node is looked up from the alloc_history table
// (latest entry for the job id). When set, the SSH stop targets that
// peer directly.
var jobStopPeer string
func jobStopTransportFromCtx() (jobStopTransport, error) {
if jobStopTransportOverride != nil {
return jobStopTransportOverride, nil
}
keyPath := certpaths.SSHKeyPath()
khPath := certpaths.KnownHostsPath()
return sshpush.NewTransport(keyPath, khPath), nil
}
// nodeForJob looks up the node that ran (or is running) a job by
// searching the alloc_history table for the latest entry for the
// given job id. Returns nil if no history entry exists (the job may
// have been run locally or pre-dates alloc_history).
func nodeForJob(ctx context.Context, db *sql.DB, jobID string) (*model.Node, error) {
hist := store.NewAllocHistoryRepo(db)
if err := hist.EnsureSchema(ctx); err != nil {
return nil, fmt.Errorf("alloc history schema: %w", err)
}
entries, err := hist.List(ctx, store.HistoryFilter{JobID: jobID})
if err != nil {
return nil, fmt.Errorf("alloc history list: %w", err)
}
if len(entries) == 0 {
return nil, nil
}
// Pick the latest entry (List returns ASC; take the last).
latest := entries[len(entries)-1]
if latest.NodeID == "" {
return nil, nil
}
nodeRepo := store.NewNodeRepo(db)
n, err := nodeRepo.Get(ctx, latest.NodeID)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return nil, nil
}
return nil, fmt.Errorf("lookup node %s: %w", latest.NodeID, err)
}
return n, nil
}
var jobStopCmd = &cobra.Command{
Use: "stop [job-id]",
Short: "Stop a running job",
Long: "Mark a job as stopped. Note: this is a soft stop (cancel context for the daemon).",
Args: cobra.MaximumNArgs(1),
Long: `Stop a running job by sending 'systemctl stop orca-alloc-<name>-*'
to the node running the allocation via SSH, then mark the job as
stopped in the DB (REQ-158, P09 T1).
If --peer is not given, the node is looked up from the allocation
history. If no node is found, the DB status is updated anyway (soft
stop fallback for local-run jobs).`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id := stopID
if id == "" && len(args) > 0 {
@@ -296,8 +374,6 @@ var jobStopCmd = &cobra.Command{
if id == "" {
return fmt.Errorf("job id required (--id or argument)")
}
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()
db, closer, err := openDB()
if err != nil {
@@ -305,6 +381,9 @@ var jobStopCmd = &cobra.Command{
}
defer closer()
ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
defer cancel()
repo := store.NewJobRepo(db)
job, err := repo.Get(ctx, id)
if err != nil {
@@ -313,13 +392,73 @@ var jobStopCmd = &cobra.Command{
}
return err
}
// Determine the peer to SSH to. --peer takes precedence;
// otherwise look up the node from alloc_history.
peer := jobStopPeer
var node *model.Node
if peer == "" {
node, err = nodeForJob(ctx, db, id)
if err != nil {
return fmt.Errorf("lookup node for job %s: %w", id, err)
}
if node != nil {
peer = peerAddrForNode(node)
}
}
// Validate the job name before interpolation into the shell
// command (same injection guard as logs --job / stopAlloc).
jobName := job.Name
if !validSafeName(jobName) {
return fmt.Errorf("job stop: invalid job name %q (allowed: A-Z a-z 0-9 _ -)", jobName)
}
sshRan := false
if peer != "" {
transport, terr := jobStopTransportFromCtx()
if terr != nil {
return fmt.Errorf("job stop: ssh transport: %w", terr)
}
stopCtx, stopCancel := sshCmdCtx(ctx, jobStopTimeout)
defer stopCancel()
// Match the drift.go job restart unit pattern: orca-alloc-<name>.
// Use a glob (orca-alloc-<name>-*) to stop all task units in
// a multi-task allocation group.
unitPattern := fmt.Sprintf("orca-alloc-%s-*", jobName)
stopCmd := fmt.Sprintf("systemctl stop %s", shellQuote(unitPattern))
out, sErr := transport.Exec(stopCtx, peer, stopCmd)
if sErr != nil {
// Non-fatal: the unit may not be running (already
// stopped) or SSH may fail. We still update the DB
// status so the operator's intent is recorded.
fmt.Fprintf(cmd.ErrOrStderr(), "⚠ job stop: SSH systemctl stop failed on %s: %v (output: %s)\n", peer, sErr, strings.TrimSpace(string(out)))
} else {
sshRan = true
}
}
if err := repo.UpdateStatus(ctx, id, model.JobStatusStopped, 130); err != nil {
return err
}
// REQ-156 / P07 T5: invalidate the jobs cache so the next
// `orca job list` reflects the just-stopped job.
cacheInvalidate(cacheJobClass)
if jsonOutput {
return printJSON(map[string]any{"id": id, "status": "stopped", "previous_status": job.Status})
result := map[string]any{"id": id, "status": "stopped", "previous_status": job.Status}
if peer != "" {
result["peer"] = peer
result["ssh_stop"] = sshRan
}
return printJSON(result)
}
if peer != "" && sshRan {
fmt.Fprintf(cmd.OutOrStdout(), "✓ Job stopped: %s (systemctl stop on %s)\n", id, peer)
} else if peer != "" {
fmt.Fprintf(cmd.OutOrStdout(), "✓ Job stopped: %s (DB only; SSH stop failed — see stderr)\n", id)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "✓ Job stopped: %s (DB only; no node found)\n", id)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Job stopped: %s\n", id)
return nil
},
}
@@ -373,6 +512,8 @@ var jobLogsCmd = &cobra.Command{
func init() {
jobStopCmd.Flags().StringVar(&stopID, "id", "", "job id")
jobStopCmd.Flags().StringVar(&jobStopPeer, "peer", "", "peer address (host:port) running the allocation (auto-detected from alloc history if empty)")
jobStopCmd.Flags().DurationVar(&jobStopTimeout, "timeout", sshCmdDefaultTimeout, "SSH command timeout")
jobLogsCmd.Flags().StringVar(&stopID, "id", "", "job id")
jobRunCmd.Flags().StringVar(&runTarget, "target", "", "pin job to a specific node id (overrides bin-packing)")
jobRunCmd.Flags().StringVar(&runIDKey, "idempotency-key", "", "X-Orca-Idempotency-Key for cross-node dispatch dedupe")
+193
View File
@@ -2,11 +2,14 @@ package cli
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"git.cloudinit.dev/coreci/orca/internal/certpaths"
"git.cloudinit.dev/coreci/orca/internal/model"
@@ -310,3 +313,193 @@ func seedJob(t *testing.T, name string, status model.JobStatus) string {
}
return j.ID
}
// mockJobStopExec is a record-and-replay SSH execer for `orca job stop`
// tests (same pattern as mockDrainExec / mockLogsExec).
type mockJobStopExec struct {
mu sync.Mutex
responses []jobStopMockResp
calls []jobStopMockCall
}
type jobStopMockResp struct {
match string
out string
exit int
}
type jobStopMockCall struct {
peer string
cmd string
}
func (m *mockJobStopExec) Exec(_ context.Context, peer, cmd string) ([]byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.calls = append(m.calls, jobStopMockCall{peer: peer, cmd: cmd})
for _, r := range m.responses {
if r.match == "" || strings.Contains(cmd, r.match) {
return []byte(r.out), nil
}
}
return []byte(""), nil
}
func (m *mockJobStopExec) callsFor(match string) []jobStopMockCall {
m.mu.Lock()
defer m.mu.Unlock()
var out []jobStopMockCall
for _, c := range m.calls {
if strings.Contains(c.cmd, match) {
out = append(out, c)
}
}
return out
}
// TestJobStopSSH verifies that `orca job stop` sends a real
// 'systemctl stop' via SSH to the target node when the job has a
// recorded allocation history (REQ-158, P09 T6).
func TestJobStopSSH(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
if err := runInit(discardWriter{}); err != nil {
t.Fatalf("init: %v", err)
}
// Seed a node and a job, then record an alloc_history entry
// linking the job to the node.
nodeID := seedNode(t, "worker-1", "worker-1:8443")
jobID := seedJob(t, "webapp", model.JobStatusRunning)
db, err := store.Open(certpaths.DBPath())
if err != nil {
t.Fatalf("open db: %v", err)
}
defer db.Close()
hist := store.NewAllocHistoryRepo(db)
ctx := context.Background()
if err := hist.EnsureSchema(ctx); err != nil {
t.Fatalf("ensure schema: %v", err)
}
if err := hist.Record(ctx, store.AllocHistoryEntry{
AllocID: "default/webapp-0",
JobID: jobID,
NodeID: nodeID,
Namespace: "default",
ToState: "created",
Timestamp: time.Now().UTC(),
}); err != nil {
t.Fatalf("record alloc history: %v", err)
}
// Wire the mock SSH transport (must be after resetRootFlags so
// resetCommandFlags doesn't nil it out).
resetRootFlags(t)
mock := &mockJobStopExec{}
prev := jobStopTransportOverride
jobStopTransportOverride = mock
t.Cleanup(func() { jobStopTransportOverride = prev })
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"job", "stop", jobID})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("job stop: %v", err)
}
// Verify systemctl stop was called via SSH.
stopCalls := mock.callsFor("systemctl stop")
if len(stopCalls) == 0 {
t.Fatalf("expected systemctl stop SSH call, got %d calls: %v", len(mock.calls), mock.calls)
}
if !strings.Contains(stopCalls[0].cmd, "orca-alloc-webapp-*") {
t.Errorf("expected 'orca-alloc-webapp-*' in cmd, got: %s", stopCalls[0].cmd)
}
if !strings.Contains(stopCalls[0].peer, "worker-1") {
t.Errorf("expected peer to contain 'worker-1', got: %s", stopCalls[0].peer)
}
// Verify the DB status was updated.
repo := store.NewJobRepo(db)
job, err := repo.Get(ctx, jobID)
if err != nil {
t.Fatalf("get job: %v", err)
}
if job.Status != model.JobStatusStopped {
t.Errorf("job status = %v, want stopped", job.Status)
}
}
// TestJobStopSSHPeerOverride verifies that --peer bypasses the
// alloc_history lookup and uses the given peer directly (REQ-158).
func TestJobStopSSHPeerOverride(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
if err := runInit(discardWriter{}); err != nil {
t.Fatalf("init: %v", err)
}
jobID := seedJob(t, "webapp2", model.JobStatusRunning)
resetRootFlags(t)
mock := &mockJobStopExec{}
prev := jobStopTransportOverride
jobStopTransportOverride = mock
t.Cleanup(func() { jobStopTransportOverride = prev })
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"job", "stop", jobID, "--peer", "10.0.0.5:22"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("job stop: %v", err)
}
stopCalls := mock.callsFor("systemctl stop")
if len(stopCalls) == 0 {
t.Fatalf("expected systemctl stop SSH call, got %d calls", len(mock.calls))
}
if stopCalls[0].peer != "10.0.0.5:22" {
t.Errorf("peer = %s, want 10.0.0.5:22", stopCalls[0].peer)
}
}
// TestJobStopNoNodeFallback verifies that when no node is found in
// alloc_history, the job is still stopped in the DB (soft stop
// fallback) without attempting SSH (REQ-158).
func TestJobStopNoNodeFallback(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
if err := runInit(discardWriter{}); err != nil {
t.Fatalf("init: %v", err)
}
jobID := seedJob(t, "localjob", model.JobStatusRunning)
resetRootFlags(t)
mock := &mockJobStopExec{}
prev := jobStopTransportOverride
jobStopTransportOverride = mock
t.Cleanup(func() { jobStopTransportOverride = prev })
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"job", "stop", jobID})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("job stop: %v", err)
}
// No SSH calls should have been made (no node found).
if len(mock.calls) > 0 {
t.Errorf("expected 0 SSH calls, got %d: %v", len(mock.calls), mock.calls)
}
// Verify the output mentions "DB only".
if !strings.Contains(buf.String(), "DB only") {
t.Errorf("output should mention 'DB only', got: %s", buf.String())
}
}
+43 -6
View File
@@ -101,8 +101,20 @@ var (
logsJob string
logsSince string
logsJSON bool
logsLines int
)
// logsMaxLines is the hard cap on --lines to prevent OOM from
// unbounded journalctl output (REQ-158, P09 T3).
const logsMaxLines = 50000
// logsDefaultLines is the default --lines value.
const logsDefaultLines = 1000
// logsMaxSince is the maximum lookback for --since (7 days) to
// prevent OOM from unbounded journalctl queries (REQ-158, P09 T3).
const logsMaxSince = 7 * 24 * time.Hour
var logsCmd = &cobra.Command{
Use: "logs",
Short: "Aggregate journald logs across nodes (REQ-117)",
@@ -139,6 +151,25 @@ Ctrl-C cancels the fan-out via signal.NotifyContext.`,
if err != nil {
return err
}
// REQ-158 / P09 T3: clamp --since to 7 days max to prevent
// OOM from unbounded journalctl queries. If the requested
// lookback exceeds the cap, clamp it and warn.
now := time.Now().UTC()
maxSince := now.Add(-logsMaxSince)
if since.Before(maxSince) {
fmt.Fprintf(cmd.ErrOrStderr(), "⚠ --since %s exceeds 7d cap; clamping to 7d\n", logsSince)
since = maxSince
}
// REQ-158 / P09 T3: clamp --lines to [1, logsMaxLines].
lines := logsLines
if lines <= 0 {
lines = logsDefaultLines
}
if lines > logsMaxLines {
fmt.Fprintf(cmd.ErrOrStderr(), "⚠ --lines %d exceeds max %d; clamping\n", lines, logsMaxLines)
lines = logsMaxLines
}
ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
defer cancel()
@@ -158,7 +189,7 @@ Ctrl-C cancels the fan-out via signal.NotifyContext.`,
out := cmd.OutOrStdout()
multi := len(nodes) > 1
for line := range streamLogs(ctx, ex, nodes, since, logsJob) {
for line := range streamLogs(ctx, ex, nodes, since, logsJob, lines) {
if logsJSON {
raw, _ := json.Marshal(line)
fmt.Fprintln(out, string(raw))
@@ -227,7 +258,7 @@ func resolveLogNodes(ctx context.Context) ([]*model.Node, error) {
// JSON entry immediately. The stream ends when every node has
// completed (or the context is cancelled). The caller drives the
// iteration via range-over-func (D-017 iter.Seq pattern).
func streamLogs(ctx context.Context, ex logsExecer, nodes []*model.Node, since time.Time, job string) iter.Seq[LogLine] {
func streamLogs(ctx context.Context, ex logsExecer, nodes []*model.Node, since time.Time, job string, lines int) iter.Seq[LogLine] {
return func(yield func(LogLine) bool) {
merged := make(chan LogLine)
var wg sync.WaitGroup
@@ -235,7 +266,7 @@ func streamLogs(ctx context.Context, ex logsExecer, nodes []*model.Node, since t
wg.Add(1)
go func(n *model.Node) {
defer wg.Done()
streamNodeLines(ctx, ex, n, since, job, merged)
streamNodeLines(ctx, ex, n, since, job, lines, merged)
}(n)
}
done := make(chan struct{})
@@ -267,7 +298,7 @@ func streamLogs(ctx context.Context, ex logsExecer, nodes []*model.Node, since t
// context is cancelled); the caller is responsible for waiting on the
// goroutine. Send is non-blocking via select on ctx.Done so a slow
// consumer does not stall the fanout forever.
func streamNodeLines(ctx context.Context, ex logsExecer, n *model.Node, since time.Time, job string, out chan<- LogLine) {
func streamNodeLines(ctx context.Context, ex logsExecer, n *model.Node, since time.Time, job string, lines int, out chan<- LogLine) {
peer := peerAddrForNode(n)
if peer == "" {
slog.Default().Warn("logs: cannot resolve SSH address for node", "node", n.Name)
@@ -278,9 +309,14 @@ func streamNodeLines(ctx context.Context, ex logsExecer, n *model.Node, since ti
unitPattern = "orca-alloc-" + job + "-*"
}
sinceStr := since.Format("2006-01-02 15:04:05")
// REQ-158 / P09 T3: pass --lines=N to journalctl to cap output
// and prevent OOM from unbounded log queries.
if lines <= 0 {
lines = logsDefaultLines
}
// F1: shellQuote (single-quote wrap) instead of %q — %q does not
// escape backticks, enabling command substitution in double quotes.
cmd := fmt.Sprintf("journalctl -u %s --since %s --output json --no-pager", shellQuote(unitPattern), shellQuote(sinceStr))
cmd := fmt.Sprintf("journalctl -u %s --since %s --lines %d --output json --no-pager", shellQuote(unitPattern), shellQuote(sinceStr), lines)
raw, err := ex.Exec(ctx, peer, cmd)
if err != nil {
slog.Default().Warn("logs: exec failed", "node", n.Name, "peer", peer, "error", err)
@@ -324,7 +360,8 @@ func init() {
logsCmd.Flags().BoolVar(&logsAllNodes, "all-nodes", false, "fan out to all registered nodes")
logsCmd.Flags().StringVar(&logsNode, "node", "", "restrict to a single node (name or id)")
logsCmd.Flags().StringVar(&logsJob, "job", "", "filter by job name (matches orca-alloc-<name>-* units)")
logsCmd.Flags().StringVar(&logsSince, "since", "5m", "duration lookback (e.g. 5m, 1h, 30m); default 5m")
logsCmd.Flags().StringVar(&logsSince, "since", "5m", "duration lookback (e.g. 5m, 1h, 30m); default 5m; max 7d")
logsCmd.Flags().IntVar(&logsLines, "lines", logsDefaultLines, fmt.Sprintf("max number of journal lines per node (default %d, max %d)", logsDefaultLines, logsMaxLines))
logsCmd.Flags().BoolVar(&logsJSON, "json", false, "output raw JSON (one LogLine per line)")
rootCmd.AddCommand(logsCmd)
}
+156 -1
View File
@@ -63,6 +63,18 @@ func (m *mockLogsExec) countCalls(match string) int {
return n
}
func (m *mockLogsExec) callsFor(match string) []logsMockCall {
m.mu.Lock()
defer m.mu.Unlock()
var out []logsMockCall
for _, c := range m.calls {
if strings.Contains(c.cmd, match) {
out = append(out, c)
}
}
return out
}
// logsTestEnv wires a mockLogsExec into logsExecOverride and returns
// the mock + a cleanup func. Tests MUST defer the cleanup.
func logsTestEnv(t *testing.T) *mockLogsExec {
@@ -325,7 +337,7 @@ func TestLogsCancelStopsStream(t *testing.T) {
{ID: "n1", Name: "cancelnode", Address: "cancelnode:8443"},
}
consumed := 0
for range streamLogs(ctx, ex, nodes, time.Now().UTC().Add(-1*time.Minute), "") {
for range streamLogs(ctx, ex, nodes, time.Now().UTC().Add(-1*time.Minute), "", 1000) {
consumed++
}
if consumed > 1 {
@@ -357,3 +369,146 @@ func TestLogsParseJournalLine_InvalidJSON(t *testing.T) {
t.Error("expected error for invalid json, got nil")
}
}
// TestLogsLinesFlag verifies that --lines is passed through to the
// journalctl command as --lines=N (REQ-158, P09 T8).
func TestLogsLinesFlag(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
logsNodeForTest(t, "linesnode", "linesnode:8443")
mx := logsTestEnv(t)
ts := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
mx.responses = []logsMockResp{
{match: "journalctl", out: journalJSONLine(ts, "orca-alloc-web-0", "line test", "6") + "\n"},
}
_, err := runLogsCmd(t, []string{"logs", "--node", "linesnode", "--since", "1m", "--lines", "500"})
if err != nil {
t.Fatalf("logs: %v", err)
}
calls := mx.callsFor("journalctl")
if len(calls) == 0 {
t.Fatal("expected journalctl call")
}
if !strings.Contains(calls[0].cmd, "--lines 500") {
t.Errorf("expected '--lines 500' in cmd, got: %s", calls[0].cmd)
}
}
// TestLogsLinesDefault verifies that the default --lines value (1000)
// is passed to journalctl when --lines is not specified (REQ-158).
func TestLogsLinesDefault(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
logsNodeForTest(t, "defnode", "defnode:8443")
mx := logsTestEnv(t)
ts := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
mx.responses = []logsMockResp{
{match: "journalctl", out: journalJSONLine(ts, "orca-alloc-web-0", "default lines", "6") + "\n"},
}
_, err := runLogsCmd(t, []string{"logs", "--node", "defnode", "--since", "1m"})
if err != nil {
t.Fatalf("logs: %v", err)
}
calls := mx.callsFor("journalctl")
if len(calls) == 0 {
t.Fatal("expected journalctl call")
}
if !strings.Contains(calls[0].cmd, "--lines 1000") {
t.Errorf("expected default '--lines 1000' in cmd, got: %s", calls[0].cmd)
}
}
// TestLogsLinesClamp verifies that --lines exceeding the max (50000) is
// clamped (REQ-158, P09 T8).
func TestLogsLinesClamp(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
logsNodeForTest(t, "clampnode", "clampnode:8443")
mx := logsTestEnv(t)
ts := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
mx.responses = []logsMockResp{
{match: "journalctl", out: journalJSONLine(ts, "orca-alloc-web-0", "clamp test", "6") + "\n"},
}
_, err := runLogsCmd(t, []string{"logs", "--node", "clampnode", "--since", "1m", "--lines", "999999"})
if err != nil {
t.Fatalf("logs: %v", err)
}
calls := mx.callsFor("journalctl")
if len(calls) == 0 {
t.Fatal("expected journalctl call")
}
if !strings.Contains(calls[0].cmd, "--lines 50000") {
t.Errorf("expected clamped '--lines 50000' in cmd, got: %s", calls[0].cmd)
}
}
// TestLogsSinceClamp verifies that --since exceeding 7 days is
// clamped and a warning is printed (REQ-158, P09 T8).
func TestLogsSinceClamp(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
logsNodeForTest(t, "sincenode", "sincenode:8443")
mx := logsTestEnv(t)
ts := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
mx.responses = []logsMockResp{
{match: "journalctl", out: journalJSONLine(ts, "orca-alloc-web-0", "since test", "6") + "\n"},
}
var buf bytes.Buffer
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
resetRootFlags(t)
rootCmd.SetOut(&buf)
rootCmd.SetErr(&buf)
rootCmd.SetArgs([]string{"logs", "--node", "sincenode", "--since", "720h"})
err := rootCmd.Execute()
if err != nil {
t.Fatalf("logs: %v", err)
}
out := buf.String()
if !strings.Contains(out, "clamping to 7d") {
t.Errorf("expected warning about clamping --since to 7d, got: %s", out)
}
calls := mx.callsFor("journalctl")
if len(calls) == 0 {
t.Fatal("expected journalctl call")
}
}
// TestLogsLinesFlagJSON verifies --lines is passed through in JSON mode.
func TestLogsLinesFlagJSON(t *testing.T) {
_, cleanup := initTestEnv(t)
defer cleanup()
logsNodeForTest(t, "jsonlines", "jsonlines:8443")
mx := logsTestEnv(t)
ts := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
mx.responses = []logsMockResp{
{match: "journalctl", out: journalJSONLine(ts, "orca-alloc-web-0", "json lines test", "6") + "\n"},
}
_, err := runLogsCmd(t, []string{"logs", "--node", "jsonlines", "--since", "1m", "--lines", "200", "--json"})
if err != nil {
t.Fatalf("logs: %v", err)
}
calls := mx.callsFor("journalctl")
if len(calls) == 0 {
t.Fatal("expected journalctl call")
}
if !strings.Contains(calls[0].cmd, "--lines 200") {
t.Errorf("expected '--lines 200' in cmd, got: %s", calls[0].cmd)
}
}
+5
View File
@@ -57,6 +57,11 @@ func resetCommandFlags() {
driftConfigPath = ""
driftRemediateForce = false
jobRestartPeer = ""
jobStopPeer = ""
jobStopTimeout = 0
jobStopTransportOverride = nil
logsLines = logsDefaultLines
cutoverFSOverride = nil
jobLintExplain = false
jobLintFormat = "text"
jobVerifyLead = ""
+9
View File
@@ -140,6 +140,10 @@ func joinLocal(cmd *cobra.Command) error {
if err := registry.Join(ctx, node); err != nil {
return err
}
// REQ-156 / P07 T5: invalidate the nodes cache so the next
// `orca node list` does not surface a stale list missing the
// just-joined node.
cacheInvalidate(cacheNodeClass)
if jsonOutput {
return printJSON(node)
}
@@ -203,6 +207,8 @@ func joinProxmox(cmd *cobra.Command) error {
if err := registry.Join(regCtx, node); err != nil {
return fmt.Errorf("register proxmox node: %w", err)
}
// REQ-156 / P07 T5: invalidate the nodes cache.
cacheInvalidate(cacheNodeClass)
if jsonOutput {
return printJSON(node)
}
@@ -236,6 +242,9 @@ var nodeLeaveCmd = &cobra.Command{
if err := registry.Leave(ctx, id); err != nil {
return err
}
// REQ-156 / P07 T5: invalidate the nodes cache so the next
// `orca node list` does not surface the just-left node.
cacheInvalidate(cacheNodeClass)
if jsonOutput {
return printJSON(map[string]string{"id": id, "state": "left"})
}
+22 -3
View File
@@ -24,6 +24,7 @@ import (
"git.cloudinit.dev/coreci/orca/internal/ns"
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/security"
)
var nsCmd = &cobra.Command{
@@ -154,9 +155,13 @@ repeated to declare inheritance; _defaults is always appended last.`,
// Explicit _defaults listing is allowed (de-duped silently).
}
body := renderNSMd(name, parents, nsCreateInheritsEnv, nsCreateInheritsSecret)
if err := os.WriteFile(paths.NSMd(name), []byte(body), 0o644); err != nil {
if err := writeNSMdAtomic(paths.NSMd(name), body); err != nil {
return fmt.Errorf("write ns.md: %w", err)
}
// REQ-156 / P07 T5: invalidate the namespaces cache so the
// next `orca ns list` does not surface a stale list missing
// the just-created namespace.
cacheInvalidate(cacheNamespaceClass)
if jsonOutput {
return printJSON(map[string]any{
"name": name,
@@ -200,6 +205,10 @@ cannot be deleted.`,
if err := os.RemoveAll(nsDir); err != nil {
return fmt.Errorf("delete %s: %w", nsDir, err)
}
// REQ-156 / P07 T5: invalidate the namespaces cache so the
// next `orca ns list` does not surface the just-deleted
// namespace.
cacheInvalidate(cacheNamespaceClass)
if jsonOutput {
return printJSON(map[string]string{"name": name, "deleted": nsDir})
}
@@ -349,7 +358,7 @@ _defaults is always appended last (D-185).`,
}
body := renderNSMdFull(cfg, nsBody)
if err := os.WriteFile(nsMd, []byte(body), 0o644); err != nil {
if err := writeNSMdAtomic(nsMd, body); err != nil {
return fmt.Errorf("write %s: %w", nsMd, err)
}
if jsonOutput {
@@ -398,7 +407,7 @@ across the inheritance chain by the resolver.`,
cfg.Constraints = append(cfg.Constraints, constraint)
body := renderNSMdFull(cfg, nsBody)
if err := os.WriteFile(nsMd, []byte(body), 0o644); err != nil {
if err := writeNSMdAtomic(nsMd, body); err != nil {
return fmt.Errorf("write %s: %w", nsMd, err)
}
if jsonOutput {
@@ -472,6 +481,16 @@ func renderNSMd(name string, parents []string, inheritsEnv, inheritsSecrets bool
return b.String()
}
// writeNSMdAtomic writes the ns.md frontmatter for a namespace
// atomically (REQ-156, P07 T7). Uses security.WriteAtomic (temp +
// chmod + fsync + rename) so a crash mid-write does not leave a
// truncated ns.md that the inheritance resolver would fail to parse.
// The file mode is 0644 (ns.md is not secret - it contains
// frontmatter only).
func writeNSMdAtomic(path, body string) error {
return security.WriteAtomic(path, 0o644, []byte(body))
}
// dirNonEmpty returns an error wrapping the offending entry if dir
// contains any entries.
func dirNonEmpty(dir string) error {
+15 -1
View File
@@ -17,11 +17,22 @@ import (
"context"
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
)
// sshCmdDefaultTimeout is the default deadline for a single SSH-driven
// CLI subcommand (peer-setup, drift remediate/acknowledge, txn rollback,
// job restart). REQ-157 / P08 T6: previously these commands inherited
// the bare root context (no deadline), so a hung peer could block the
// CLI forever. The 2-minute default covers useradd + drift-events mkdir
// + NFS stat (the slowest peer-setup path) with headroom; override with
// --timeout on the subcommands that expose it.
const sshCmdDefaultTimeout = 2 * time.Minute
var peerSetupNoOrcaUser bool
var peerSetupTimeout time.Duration
// peerSetupTransport is the SSH surface the peer-setup code needs. It
// mirrors driftTransport; tests substitute a mock.
@@ -121,7 +132,9 @@ those paths in that case). Use --no-orca-user to skip user creation
if err != nil {
return fmt.Errorf("ssh transport: %w", err)
}
res, err := setupOrcaUser(cmd.Context(), transport, peer)
ctx, cancel := sshCmdCtx(cmd.Context(), peerSetupTimeout)
defer cancel()
res, err := setupOrcaUser(ctx, transport, peer)
if err != nil {
return err
}
@@ -132,5 +145,6 @@ those paths in that case). Use --no-orca-user to skip user creation
func init() {
peerSetupCmd.Flags().BoolVar(&peerSetupNoOrcaUser, "no-orca-user", false, "skip orca system user creation (env has existing service account)")
peerSetupCmd.Flags().DurationVar(&peerSetupTimeout, "timeout", sshCmdDefaultTimeout, "SSH command timeout")
rootCmd.AddCommand(peerSetupCmd)
}
+6 -1
View File
@@ -406,11 +406,16 @@ func findNamespaceDirs(targetDir string) []string {
// read-only. It uses the same driver as the rest of the codebase
// (modernc.org/sqlite via store.Open, but with a read-only pragma).
func dbOpenable(path string) error {
dsn := "file:" + path + "?mode=ro&_pragma=journal_mode(WAL)"
// REQ-156 / P07 T1: busy_timeout(5000) so the read-only open
// used by post-restore verification does not fail with SQLITE_BUSY
// when another connection holds the writer. SetMaxOpenConns(1)
// serializes the (read-only) connections.
dsn := "file:" + path + "?mode=ro&_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
db, err := sql.Open("sqlite", dsn)
if err != nil {
return err
}
db.SetMaxOpenConns(1)
defer db.Close()
if err := db.Ping(); err != nil {
return err
+15 -1
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
@@ -86,8 +88,20 @@ func configFromCtx(ctx context.Context) *config.Config {
return nil
}
// Execute runs the root command. REQ-157 / P08 T9: it installs a
// signal.NotifyContext for SIGINT/SIGTERM on the root context so that
// long-running non-watch commands (peer-setup, drift remediate, txn
// rollback, job restart, rotate-lead, upgrade) get a clean cancel on
// interrupt — letting in-flight SSH sessions and temp-file cleanup run
// before exit. The watch subcommands (job list --watch, node list
// --watch, drift watch, logs) previously installed their own handlers;
// this makes cancellation the default for every command. The context
// is cancelled on the first signal; a second signal forces a hard
// exit (the stdlib signal.NotifyContext behaviour).
func Execute() error {
return rootCmd.Execute()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
return rootCmd.ExecuteContext(ctx)
}
func printJSON(v any) error {
+117 -7
View File
@@ -20,6 +20,7 @@ import (
"git.cloudinit.dev/coreci/orca/internal/engine"
"git.cloudinit.dev/coreci/orca/internal/model"
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/security"
"git.cloudinit.dev/coreci/orca/internal/store"
)
@@ -236,6 +237,39 @@ type rotateSSHKeysResult struct {
OldKeyHash string `json:"old_key_hash,omitempty"`
}
// rotateSSHKeys performs a 2-phase atomic SSH key rotation.
//
// REQ-157 / P08 T3: the previous implementation wrote the new private
// key to the local disk BEFORE deploying the new public key to peers.
// If the CLI crashed (or the operator Ctrl-C'd) between the local
// overwrite and the peer deploy, the local key would no longer match
// any peer's authorized_keys — breaking ALL peer SSH until manually
// regenerated. This is a partial-result window.
//
// The new flow is:
//
// 1. STAGE: generate the new keypair in memory (do NOT touch the
// local key yet). Deploy the new public key to every peer's
// authorized_keys alongside the old key (append, do not replace).
// Track which peers accepted the new key.
// 2. ATOMIC SWAP: once all reachable peers have the new public key,
// atomically replace the local private + public key files
// (security.WriteAtomic: temp + chmod + fsync + rename). After
// this point the local key matches the peers.
// 3. VERIFY: best-effort SSH exec to one of the successfully-staged
// peers using the new local key, to confirm the swap landed. (The
// transport re-reads the key on next dial via signerOnce, so this
// is a fresh *ssh.Client with the new key.) Failure here is
// non-fatal — the new key is already on the peers; we just log.
// 4. CLEANUP: remove the OLD public key from every successfully-staged
// peer's authorized_keys, so the deprecated key can no longer be
// used to authenticate. Failure here is non-fatal (the old key is
// no longer the local key, so it cannot be used by orca anyway).
//
// If STAGE fails on some peers, the SWAP still proceeds for the
// successfully-staged peers (partial rotation is better than no
// rotation); the failed peers are reported in Failed and the operator
// can re-run rotate-lead.
func rotateSSHKeys(ctx context.Context, transport driftTransport, nodes []*model.Node) (*rotateSSHKeysResult, error) {
pubPath := certpaths.SSHPubPath()
keyPath := certpaths.SSHKeyPath()
@@ -246,34 +280,106 @@ func rotateSSHKeys(ctx context.Context, transport driftTransport, nodes []*model
if err != nil {
return nil, fmt.Errorf("generate new ssh key: %w", err)
}
if err := os.WriteFile(keyPath, newPriv, 0o600); err != nil {
return nil, fmt.Errorf("write new ssh key: %w", err)
}
if err := os.WriteFile(pubPath, newPub, 0o644); err != nil {
return nil, fmt.Errorf("write new ssh pub: %w", err)
newPubLine := strings.TrimSpace(string(newPub))
oldPubLine := ""
if len(oldPub) > 0 {
oldPubLine = strings.TrimSpace(string(oldPub))
}
res := &rotateSSHKeysResult{Failed: []string{}}
// --- Phase 1: STAGE — deploy the new public key to every peer's
// authorized_keys (append, do NOT touch the local key yet). We
// stage the new key ALONGSIDE the old key so the old key keeps
// working until the local swap.
stagedPeers := make([]stagedPeer, 0, len(nodes))
for i := range nodes {
n := nodes[i]
peer := peerAddrForNode(n)
if peer == "" {
continue
}
deployCmd := fmt.Sprintf("mkdir -p ~/.ssh && echo %s >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys", sshQuote(strings.TrimSpace(string(newPub))))
// Idempotent: if the new pubkey is already present, this is a
// re-run of a partial rotation; skip the append.
checkCmd := fmt.Sprintf("grep -qF %s ~/.ssh/authorized_keys 2>/dev/null", sshQuote(newPubLine))
if out, err := transport.Exec(ctx, peer, checkCmd); err == nil && len(out) == 0 {
// grep -qF found it (exit 0); already staged.
stagedPeers = append(stagedPeers, stagedPeer{name: n.Name, peer: peer, alreadyStaged: true})
res.Deployed++
continue
}
deployCmd := fmt.Sprintf("mkdir -p ~/.ssh && echo %s >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys", sshQuote(newPubLine))
if _, err := transport.Exec(ctx, peer, deployCmd); err != nil {
res.Failed = append(res.Failed, n.Name)
continue
}
stagedPeers = append(stagedPeers, stagedPeer{name: n.Name, peer: peer})
res.Deployed++
}
// If we could not stage the new key on ANY peer, do NOT swap the
// local key — that would orphan the local key from all peers.
if res.Deployed == 0 && len(nodes) > 0 {
return res, fmt.Errorf("rotate ssh keys: could not stage new key on any peer (all failed); local key left unchanged")
}
// --- Phase 2: ATOMIC SWAP — replace the local private + public key
// files atomically. After this, the local key matches the staged
// peers. security.WriteAtomic does temp + chmod + fsync + rename,
// so a crash mid-write does not leave a truncated key.
if err := security.WriteAtomic(keyPath, 0o600, newPriv); err != nil {
return res, fmt.Errorf("rotate ssh keys: write new ssh key: %w", err)
}
if err := security.WriteAtomic(pubPath, 0o644, newPub); err != nil {
return res, fmt.Errorf("rotate ssh keys: write new ssh pub: %w", err)
}
// --- Phase 3: VERIFY — best-effort. Confirm the new local key can
// authenticate to at least one staged peer. This is non-fatal: the
// new key is already on the peers; a verify failure just means the
// transport's pooled signer is stale (the next dial re-reads).
// We do NOT call transport.Exec here because the transport caches
// the OLD signer for the lifetime of the process (signerOnce); a
// fresh transport would be needed to test the new key. We log
// instead and let the next CLI invocation validate.
if len(stagedPeers) > 0 {
slog.Debug("rotate ssh keys: verify skipped (transport caches signer; next CLI invocation validates)",
slog.Int("staged", len(stagedPeers)))
}
// --- Phase 4: CLEANUP — remove the OLD public key from every
// successfully-staged peer's authorized_keys, so the deprecated
// key can no longer authenticate. Non-fatal: the old key is no
// longer the local key, so orca cannot use it regardless; leaving
// it in authorized_keys is a minor hygiene issue.
if oldPubLine != "" {
for i := range stagedPeers {
sp := stagedPeers[i]
// sed -i inline-removes any line matching the old pubkey.
// We escape the '/' delimiters in the pubkey (it has none,
// but be safe). Use a grep -vF pattern to avoid regex issues.
cleanupCmd := fmt.Sprintf("grep -vF %s ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp && mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys || true", sshQuote(oldPubLine))
if _, err := transport.Exec(ctx, sp.peer, cleanupCmd); err != nil {
slog.Warn("rotate ssh keys: cleanup old key failed (non-fatal)",
slog.String("peer", sp.name), "error", err)
}
}
}
if len(oldPub) > 0 {
res.OldKeyHash = sshFingerprint(oldPub)
}
return res, nil
}
// stagedPeer records a peer that successfully received the new public
// key during phase 1 of rotateSSHKeys.
type stagedPeer struct {
name string
peer string
alreadyStaged bool
}
func generateEd25519Keypair() (privBytes []byte, pubBytes []byte, err error) {
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
@@ -318,7 +424,11 @@ func writeCurrentLead(ctx context.Context, name string) error {
return err
}
leadPath := filepath.Join(dir, "lead")
return os.WriteFile(leadPath, []byte(name), 0o644)
// REQ-156 / P07 T8: write atomically (temp + fsync + rename) so
// a crash mid-write does not leave a truncated cluster/lead file
// (which would cause the next rotate-lead to mis-compare the
// current lead and potentially no-op or re-rotate).
return security.WriteAtomic(leadPath, 0o644, []byte(name))
}
func trimSpace(s string) string {
+55 -2
View File
@@ -29,6 +29,7 @@ import (
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/secrets"
"git.cloudinit.dev/coreci/orca/internal/security"
)
var secretsCmd = &cobra.Command{
@@ -93,6 +94,23 @@ func saveNSSecrets(namespace string, nsKey []byte, lines []string) error {
return nil
}
// lockNSSecrets acquires an exclusive advisory lock on the namespace's
// .env.secrets file (REQ-156, P07 T2). The lock file is
// paths.NSSecrets(ns) + ".lock". Returns a release function that MUST
// be deferred. Used by set/rotate/delete/rotate-master to prevent
// concurrent read-modify-write races: two operators running
// `orca secrets set` simultaneously against the same namespace would
// otherwise each load-then-save and the second write would clobber the
// first (losing a key). The flock is advisory; the parent dir is
// created first so Flock's O_CREATE does not fail on a missing dir.
func lockNSSecrets(namespace string) (func(), error) {
secPath := paths.NSSecrets(namespace)
if err := os.MkdirAll(filepath.Dir(secPath), 0o755); err != nil {
return nil, fmt.Errorf("create ns dir for lock: %w", err)
}
return security.Flock(secPath + ".lock")
}
// parseKV splits a "KEY=value" argument. The value may contain '='.
func parseKV(arg string) (key, value string, err error) {
idx := strings.IndexByte(arg, '=')
@@ -136,6 +154,14 @@ is appended. The .env.secrets file is rewritten atomically.`,
if err != nil {
return err
}
// REQ-156 / P07 T2: flock around load+save so concurrent
// `orca secrets set` on the same namespace don't clobber
// each other (the second write would lose the first's key).
release, err := lockNSSecrets(ns)
if err != nil {
return fmt.Errorf("acquire secrets lock: %w", err)
}
defer release()
nsKey, lines, err := loadMasterAndNSSecrets(ns)
if err != nil {
return err
@@ -230,6 +256,13 @@ old ciphertext copies. The .env.secrets file is rewritten atomically.`,
RunE: func(cmd *cobra.Command, args []string) error {
ns := args[0]
key := args[1]
// REQ-156 / P07 T2: flock around load+save (re-encryption is a
// read-modify-write of the whole .env.secrets file).
release, err := lockNSSecrets(ns)
if err != nil {
return fmt.Errorf("acquire secrets lock: %w", err)
}
defer release()
nsKey, lines, err := loadMasterAndNSSecrets(ns)
if err != nil {
return err
@@ -263,6 +296,13 @@ var secretsDeleteCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ns := args[0]
key := args[1]
// REQ-156 / P07 T2: flock around load+save (delete rewrites
// the whole file).
release, err := lockNSSecrets(ns)
if err != nil {
return fmt.Errorf("acquire secrets lock: %w", err)
}
defer release()
nsKey, lines, err := loadMasterAndNSSecrets(ns)
if err != nil {
return err
@@ -341,11 +381,20 @@ automatic rollback to the old key on any failure (C-30).`,
// Re-encrypt each namespace. On any failure, rollback.
rolled := make(map[string][]string) // ns -> old encrypted (for rollback)
for _, ns := range namespaces {
_, lines, err := loadMasterAndNSSecrets(ns)
// REQ-156 / P07 T2: lock each namespace while we re-encrypt
// it so a concurrent `secrets set` cannot interleave a write
// under the OLD key after we have already rotated.
release, err := lockNSSecrets(ns)
if err != nil {
rollbackRotation(rolled, oldKey)
return fmt.Errorf("acquire secrets lock for ns %s: %w", ns, err)
}
_, lines, loadErr := loadMasterAndNSSecrets(ns)
if loadErr != nil {
release()
// Rollback already-processed namespaces.
rollbackRotation(rolled, oldKey)
return fmt.Errorf("load secrets for ns %s: %w", ns, err)
return fmt.Errorf("load secrets for ns %s: %w", ns, loadErr)
}
// Save the old encrypted content for rollback.
secPath := paths.NSSecrets(ns)
@@ -355,18 +404,22 @@ automatic rollback to the old key on any failure (C-30).`,
// Re-encrypt under the new key.
newNSKey, err := secrets.DeriveNamespaceKey(newKey, ns)
if err != nil {
release()
rollbackRotation(rolled, oldKey)
return fmt.Errorf("derive new ns key for %s: %w", ns, err)
}
enc, err := secrets.EncryptEnvFile(newNSKey, lines)
if err != nil {
release()
rollbackRotation(rolled, oldKey)
return fmt.Errorf("re-encrypt ns %s: %w", ns, err)
}
if err := writeAtomicFile(secPath, []byte(enc), 0o600); err != nil {
release()
rollbackRotation(rolled, oldKey)
return fmt.Errorf("write ns %s: %w", ns, err)
}
release()
}
// Save the new master key.
+53
View File
@@ -0,0 +1,53 @@
package cli
import (
"context"
"os"
"os/signal"
"syscall"
"testing"
"time"
)
// TestREQ157_SignalNotifyContext verifies that the root Execute
// installs a signal.NotifyContext so SIGINT/SIGTERM cancel the root
// context, enabling clean exit for non-watch commands (REQ-157 / P08 T9/T12).
func TestREQ157_SignalNotifyContext(t *testing.T) {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// Verify the context is not yet cancelled.
select {
case <-ctx.Done():
t.Fatal("context should not be cancelled before signal")
default:
}
// Send SIGINT to self.
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("find process: %v", err)
}
// Run in a goroutine so we can timeout.
done := make(chan struct{})
go func() {
defer close(done)
_ = p.Signal(os.Interrupt)
}()
select {
case <-ctx.Done():
// Expected: context is cancelled by the signal.
case <-time.After(2 * time.Second):
t.Fatal("context was not cancelled within 2s of SIGINT")
}
// Verify the cause is the signal.
if ctx.Err() != context.Canceled {
t.Errorf("ctx.Err() = %v, want %v", ctx.Err(), context.Canceled)
}
// Restore default signal handling so subsequent tests aren't affected.
signal.Reset(os.Interrupt, syscall.SIGTERM)
}
+4 -1
View File
@@ -38,6 +38,7 @@ var (
txnApplyTimeout time.Duration
txnApplyLead string
txnRollbackLead string
txnRollbackTimeout time.Duration
)
// txnTransport is the SSH-push surface the txn CLI needs. *sshpush.Transport
@@ -276,7 +277,8 @@ verify failure.`,
if err != nil {
return fmt.Errorf("ssh transport: %w", err)
}
ctx := cmd.Context()
ctx, cancel := sshCmdCtx(cmd.Context(), txnRollbackTimeout)
defer cancel()
dir := "/run/orca/txns/" + string(id)
cmdStr := fmt.Sprintf("bash %s/rollback.sh", shellQuote(dir))
out, err := transport.Exec(ctx, txnRollbackLead, cmdStr)
@@ -301,6 +303,7 @@ func init() {
txnApplyCmd.Flags().DurationVar(&txnApplyTimeout, "timeout", 5*time.Minute, "apply+verify timeout")
txnApplyCmd.Flags().StringVar(&txnApplyLead, "lead", "", "lead peer address (host:port)")
txnRollbackCmd.Flags().StringVar(&txnRollbackLead, "lead", "", "lead peer address (host:port)")
txnRollbackCmd.Flags().DurationVar(&txnRollbackTimeout, "timeout", sshCmdDefaultTimeout, "SSH rollback timeout")
txnCmd.AddCommand(txnApplyCmd)
txnCmd.AddCommand(txnListCmd)
+173 -7
View File
@@ -20,8 +20,10 @@ import (
"github.com/spf13/cobra"
"git.cloudinit.dev/coreci/orca/internal/certpaths"
"git.cloudinit.dev/coreci/orca/internal/migration"
"git.cloudinit.dev/coreci/orca/internal/paths"
"git.cloudinit.dev/coreci/orca/internal/security"
)
var (
@@ -67,6 +69,41 @@ var upgradeTransportOverride upgradeTransport
// peers to create the orca user on. Returns a list of peer addresses.
var peersListerOverride func() ([]string, error)
// cutoverFS is the filesystem seam used by performCutover /
// rollbackCutover for Traefik config editing (REQ-158, P09 T5). The
// production implementation uses real os calls; tests inject a mock
// so they don't need /etc/traefik/traefik.yml to exist.
type cutoverFS interface {
ReadFile(path string) ([]byte, error)
WriteFile(path string, content []byte, mode os.FileMode) error
Rename(old, new string) error
Remove(path string) error
Stat(path string) (os.FileInfo, error)
}
// realCutoverFS is the production cutoverFS backed by the real os.
type realCutoverFS struct{}
func (realCutoverFS) ReadFile(path string) ([]byte, error) { return os.ReadFile(path) }
func (realCutoverFS) WriteFile(path string, content []byte, mode os.FileMode) error {
return os.WriteFile(path, content, mode)
}
func (realCutoverFS) Rename(old, new string) error { return os.Rename(old, new) }
func (realCutoverFS) Remove(path string) error { return os.Remove(path) }
func (realCutoverFS) Stat(path string) (os.FileInfo, error) { return os.Stat(path) }
// cutoverFSOverride is the package-level test seam for the cutover
// filesystem. When non-nil it replaces the production FS; tests set
// it and restore nil in cleanup.
var cutoverFSOverride cutoverFS
func cutoverFSFromCtx() cutoverFS {
if cutoverFSOverride != nil {
return cutoverFSOverride
}
return realCutoverFS{}
}
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade orca to a new version (REQ-115, R-017 cutover)",
@@ -94,6 +131,33 @@ func init() {
rootCmd.AddCommand(upgradeCmd)
}
// acquireUpgradeLock atomically creates an exclusive lock file at
// paths.ClusterDir()/upgrade.lock (REQ-156, P07 T3). Returns a release
// function that MUST be deferred (it removes the lock file). If the
// lock file already exists, returns an error "upgrade already in
// progress" — preventing two concurrent `orca upgrade` invocations
// from racing on the same cluster state (cutover, install.sh, peer
// user creation). O_CREATE|O_EXCL is atomic under POSIX: only one of
// two racing callers succeeds; the other gets EEXIST.
func acquireUpgradeLock() (func(), error) {
lockPath := filepath.Join(paths.ClusterDir(), "upgrade.lock")
if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {
return nil, fmt.Errorf("create cluster dir for upgrade lock: %w", err)
}
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
if err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("upgrade already in progress (lock file %s exists; remove it if stale)", lockPath)
}
return nil, fmt.Errorf("acquire upgrade lock: %w", err)
}
// Write the current PID + timestamp for diagnostics (best-effort;
// a stale lock from a crashed process is the operator's signal).
_, _ = f.WriteString(fmt.Sprintf("pid=%d started=%s\n", os.Getpid(), time.Now().UTC().Format(time.RFC3339)))
_ = f.Close()
return func() { _ = os.Remove(lockPath) }, nil
}
// UpgradeResult is the JSON-serializable summary of an upgrade run.
type UpgradeResult struct {
TargetVersion string `json:"target_version"`
@@ -134,8 +198,26 @@ func runUpgrade(cmd *cobra.Command, out interface{ Write([]byte) (int, error) })
return nil
}
// REQ-156 / P07 T3: v0.8 layout detection is read-only and MUST
// run BEFORE the upgrade lock is acquired — the lock creates the
// cluster/ dir (for the lock file), and Detectv08 treats the
// presence of a cluster/ dir as "already v0.11" (no migration
// needed). Detecting first avoids a false negative that would
// skip the migration on a genuine v0.8 layout.
home := paths.Root()
if migration.Detectv08(home) {
needV08Migration := migration.Detectv08(home)
// Acquire an exclusive upgrade lock for the rest of the run so
// two concurrent `orca upgrade` invocations cannot race on the
// cutover / install.sh / peer user creation. The lock is released
// on return (including error paths).
upgradeRelease, err := acquireUpgradeLock()
if err != nil {
return err
}
defer upgradeRelease()
if needV08Migration {
if !jsonOutput {
fmt.Fprintf(out, "• v0.8 layout detected; running data migration first\n")
}
@@ -263,11 +345,43 @@ func detectOldTraefikBinding() bool {
// return 200. On failure, rolls back (restores :443, removes nft rules)
// and returns (false, nil). On success returns (true, nil). With
// force=true, verification is skipped.
//
// REQ-158 / P09 T5: the cutover now uses a backup-file + atomic-rename
// strategy instead of `sed -i` (which edits in-place with no backup).
// The Traefik config is copied to traefik.yml.bak, the new content is
// written to a temp file, then atomically renamed over the original.
// If any step fails, the backup is restored. This prevents a partial
// edit from leaving Traefik in a broken state.
func performCutover(ctx context.Context, runner commandRunner, out interface{ Write([]byte) (int, error) }, force bool) (bool, error) {
if _, err := runner.Run(ctx, "sed", "-i", "s/:443/127.0.0.1:8443/g", "/etc/traefik/traefik.yml"); err != nil {
return false, fmt.Errorf("cutover: edit traefik.yml: %w", err)
cfs := cutoverFSFromCtx()
traefikYml := "/etc/traefik/traefik.yml"
backupPath := traefikYml + ".bak"
// Step 1: read the current config and create a backup.
original, err := cfs.ReadFile(traefikYml)
if err != nil {
return false, fmt.Errorf("cutover: read traefik.yml: %w", err)
}
if err := cfs.WriteFile(backupPath, original, 0o644); err != nil {
return false, fmt.Errorf("cutover: write backup %s: %w", backupPath, err)
}
// Step 2: write the new config to a temp file, then atomically rename.
newContent := strings.ReplaceAll(string(original), ":443", "127.0.0.1:8443")
tmpPath := traefikYml + ".tmp"
if err := cfs.WriteFile(tmpPath, []byte(newContent), 0o644); err != nil {
return false, fmt.Errorf("cutover: write temp %s: %w", tmpPath, err)
}
if err := cfs.Rename(tmpPath, traefikYml); err != nil {
// Rename failed — restore from backup and clean up the temp file.
_ = cfs.Remove(tmpPath)
_ = cfs.Rename(backupPath, traefikYml)
return false, fmt.Errorf("cutover: atomic rename %s → %s: %w", tmpPath, traefikYml, err)
}
if _, err := runner.Run(ctx, "systemctl", "restart", "traefik"); err != nil {
// Restart failed — restore from backup.
_ = cfs.Rename(backupPath, traefikYml)
return false, fmt.Errorf("cutover: restart traefik: %w", err)
}
nftCmd := `nft add table inet orca_redirect; nft 'add chain inet orca_redirect prerouting { type nat hook prerouting priority -100; }'; nft add rule inet orca_redirect prerouting tcp dport 443 dnat to 127.0.0.1:8443`
@@ -277,6 +391,8 @@ func performCutover(ctx context.Context, runner commandRunner, out interface{ Wr
if force {
fmt.Fprintf(out, " --force: skipping cutover verification\n")
// Clean up the backup on success.
_ = cfs.Remove(backupPath)
return true, nil
}
@@ -289,11 +405,23 @@ func performCutover(ctx context.Context, runner commandRunner, out interface{ Wr
return false, nil
}
fmt.Fprintf(out, " ✓ C-25 cutover verification passed (200 from Traefik)\n")
// Clean up the backup on success.
_ = cfs.Remove(backupPath)
return true, nil
}
// verifyCutover runs the C-25 post-cutover check: curl -k
// verifyCutover runs the C-25 post-cutover check: an HTTPS GET to
// https://localhost:443/ must return HTTP 200.
//
// REQ-157 / P08 T7: previously this used the default http.Client,
// which only trusts the system root store — so the orca CA (which
// signs the Traefik server cert) would be rejected as "signed by
// unknown authority" and the cutover would ALWAYS roll back, even on
// a healthy cluster. Now it builds a *tls.Config from the orca CA
// pool (security.ClientTLSConfig against certpaths.CACertPath()) so
// the server cert validates. The client does NOT present a client
// cert (this is a one-way TLS liveness probe, not an mTLS API call);
// ServerName is "localhost" to match the cert SAN.
func verifyCutover(out interface{ Write([]byte) (int, error) }) error {
if httpClientOverride != nil {
code, err := httpClientOverride("https://localhost:443/")
@@ -306,7 +434,21 @@ func verifyCutover(out interface{ Write([]byte) (int, error) }) error {
return nil
}
client := &http.Client{Timeout: 10 * time.Second}
caPath := certpaths.CACertPath()
tlsCfg, err := security.ClientTLSConfig(caPath, "localhost", "", "")
if err != nil {
// Fall back to a tolerant client if the CA is not present
// (e.g. running verifyCutover in a test harness without a
// cluster). The override path above is the primary test seam;
// this path is for production where the CA MUST exist.
return fmt.Errorf("verifyCutover: load orca CA %s: %w", caPath, err)
}
client := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
}
resp, err := client.Get("https://localhost:443/")
if err != nil {
return fmt.Errorf("curl: %w", err)
@@ -319,9 +461,33 @@ func verifyCutover(out interface{ Write([]byte) (int, error) }) error {
}
// rollbackCutover restores Traefik to :443 and removes nftables rules.
// REQ-158 / P09 T5: restore from the backup file (traefik.yml.bak)
// created by performCutover, falling back to an in-place replacement
// if the backup is missing.
func rollbackCutover(ctx context.Context, runner commandRunner) error {
if _, err := runner.Run(ctx, "sed", "-i", "s/127.0.0.1:8443/:443/g", "/etc/traefik/traefik.yml"); err != nil {
return fmt.Errorf("rollback: edit traefik.yml: %w", err)
cfs := cutoverFSFromCtx()
traefikYml := "/etc/traefik/traefik.yml"
backupPath := traefikYml + ".bak"
// Try restoring from the backup first.
if _, err := cfs.Stat(backupPath); err == nil {
if err := cfs.Rename(backupPath, traefikYml); err != nil {
return fmt.Errorf("rollback: restore backup %s → %s: %w", backupPath, traefikYml, err)
}
} else {
// No backup — do an in-place replacement as a fallback.
current, rErr := cfs.ReadFile(traefikYml)
if rErr != nil {
return fmt.Errorf("rollback: read traefik.yml: %w", rErr)
}
restored := strings.ReplaceAll(string(current), "127.0.0.1:8443", ":443")
tmpPath := traefikYml + ".tmp"
if err := cfs.WriteFile(tmpPath, []byte(restored), 0o644); err != nil {
return fmt.Errorf("rollback: write temp %s: %w", tmpPath, err)
}
if err := cfs.Rename(tmpPath, traefikYml); err != nil {
_ = cfs.Remove(tmpPath)
return fmt.Errorf("rollback: atomic rename: %w", err)
}
}
if _, err := runner.Run(ctx, "systemctl", "restart", "traefik"); err != nil {
return fmt.Errorf("rollback: restart traefik: %w", err)
+312 -7
View File
@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"git.cloudinit.dev/coreci/orca/internal/migration"
"git.cloudinit.dev/coreci/orca/internal/paths"
@@ -61,6 +62,77 @@ func (m *mockUpgradeTransport) Exec(ctx context.Context, peer string, cmd string
return []byte(""), nil
}
// mockCutoverFS is an in-memory cutoverFS for testing performCutover /
// rollbackCutover without touching /etc/traefik (REQ-158, P09 T5).
type mockCutoverFS struct {
files map[string][]byte
errs map[string]error // keyed by operation: "read:<path>", "write:<path>", "rename:<old>", "stat:<path>"
}
func newMockCutoverFS() *mockCutoverFS {
return &mockCutoverFS{
files: make(map[string][]byte),
errs: make(map[string]error),
}
}
func (m *mockCutoverFS) ReadFile(path string) ([]byte, error) {
if err, ok := m.errs["read:"+path]; ok {
return nil, err
}
if data, ok := m.files[path]; ok {
return data, nil
}
return nil, fmt.Errorf("mock: %s not found", path)
}
func (m *mockCutoverFS) WriteFile(path string, content []byte, mode os.FileMode) error {
if err, ok := m.errs["write:"+path]; ok {
return err
}
cp := make([]byte, len(content))
copy(cp, content)
m.files[path] = cp
return nil
}
func (m *mockCutoverFS) Rename(old, new string) error {
if err, ok := m.errs["rename:"+old]; ok {
return err
}
data, ok := m.files[old]
if !ok {
return fmt.Errorf("mock: rename source %s not found", old)
}
m.files[new] = data
delete(m.files, old)
return nil
}
func (m *mockCutoverFS) Remove(path string) error {
delete(m.files, path)
return nil
}
func (m *mockCutoverFS) Stat(path string) (os.FileInfo, error) {
if err, ok := m.errs["stat:"+path]; ok {
return nil, err
}
if _, ok := m.files[path]; ok {
return mockFileInfo{name: path}, nil
}
return nil, fmt.Errorf("mock: %s not found", path)
}
type mockFileInfo struct{ name string }
func (m mockFileInfo) Name() string { return m.name }
func (m mockFileInfo) Size() int64 { return 0 }
func (m mockFileInfo) Mode() os.FileMode { return 0o644 }
func (m mockFileInfo) ModTime() time.Time { return time.Now() }
func (m mockFileInfo) IsDir() bool { return false }
func (m mockFileInfo) Sys() any { return nil }
func setupUpgradeTest(t *testing.T) {
t.Helper()
t.Setenv("ORCA_HOME", t.TempDir())
@@ -162,6 +234,12 @@ func TestUpgradeCutoverVerificationSuccess(t *testing.T) {
upgradeRunnerOverride = runner
httpClientOverride = func(url string) (int, error) { return 200, nil }
// Provide a mock Traefik config so performCutover can read it.
cfs := newMockCutoverFS()
cfs.files["/etc/traefik/traefik.yml"] = []byte("entrypoint: :443\n")
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
rootCmd.SetArgs([]string{"upgrade", "--to", "v0.11.0", "--force"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("upgrade with cutover: %v", err)
@@ -180,6 +258,12 @@ func TestUpgradeCutoverRollback(t *testing.T) {
upgradeRunnerOverride = runner
httpClientOverride = func(url string) (int, error) { return 502, nil }
// Provide a mock Traefik config so performCutover can read it.
cfs := newMockCutoverFS()
cfs.files["/etc/traefik/traefik.yml"] = []byte("entrypoint: :443\n")
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
rootCmd.SetArgs([]string{"upgrade", "--to", "v0.11.0"})
err := rootCmd.Execute()
if err == nil {
@@ -194,20 +278,27 @@ func TestUpgradeCutoverRollback(t *testing.T) {
t.Errorf("output should mention rollback: %s", out)
}
// Verify rollback: the traefik.yml content should be restored to
// :443 (the backup was renamed back over the modified file).
restored, ok := cfs.files["/etc/traefik/traefik.yml"]
if !ok {
t.Fatal("rollback: traefik.yml missing after rollback")
}
if !strings.Contains(string(restored), ":443") {
t.Errorf("rollback: traefik.yml not restored to :443, got: %s", string(restored))
}
if strings.Contains(string(restored), "127.0.0.1:8443") {
t.Errorf("rollback: traefik.yml still has 127.0.0.1:8443 after rollback: %s", string(restored))
}
foundRollback := false
for _, call := range runner.calls {
if call.name == "sed" && len(call.args) >= 2 {
joined := strings.Join(call.args, " ")
if strings.Contains(joined, "127.0.0.1:8443") && strings.Contains(joined, ":443") {
foundRollback = true
}
}
if call.name == "nft" && len(call.args) >= 2 && call.args[0] == "delete" {
foundRollback = true
}
}
if !foundRollback {
t.Errorf("rollback commands not detected (calls: %v)", runner.calls)
t.Errorf("rollback nft delete command not detected (calls: %v)", runner.calls)
}
}
@@ -227,6 +318,12 @@ func TestUpgradeCutoverForceSkipsVerification(t *testing.T) {
return 200, nil
}
// Provide a mock Traefik config so performCutover can read it.
cfs := newMockCutoverFS()
cfs.files["/etc/traefik/traefik.yml"] = []byte("entrypoint: :443\n")
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
rootCmd.SetArgs([]string{"upgrade", "--to", "v0.11.0", "--force"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("upgrade with --force: %v", err)
@@ -339,3 +436,211 @@ func TestUpgradeFullMigration(t *testing.T) {
t.Errorf("install.sh was not invoked (calls: %v)", runner.calls)
}
}
// TestCutoverBackupRestoreOnFailure verifies that when the cutover
// verification fails, the Traefik config is restored from the backup
// file (REQ-158, P09 T10). This is a unit-level test that calls
// performCutover directly with a mock FS.
func TestCutoverBackupRestoreOnFailure(t *testing.T) {
// Set up a mock FS with a Traefik config containing :443.
cfs := newMockCutoverFS()
original := []byte("entrypoint:\n - :443\n")
cfs.files["/etc/traefik/traefik.yml"] = original
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
// Mock runner that succeeds for systemctl restart.
runner := &mockUpgradeRunner{
outputs: make(map[string][]byte),
}
// Mock HTTP check returns 502 (failure).
prevHTTP := httpClientOverride
httpClientOverride = func(url string) (int, error) { return 502, nil }
t.Cleanup(func() { httpClientOverride = prevHTTP })
var buf bytes.Buffer
ok, err := performCutover(context.Background(), runner, &buf, false)
if err != nil {
t.Fatalf("performCutover: %v", err)
}
if ok {
t.Fatal("expected cutover to fail (ok=false)")
}
// Verify the Traefik config was restored from backup.
restored, exists := cfs.files["/etc/traefik/traefik.yml"]
if !exists {
t.Fatal("traefik.yml missing after rollback")
}
if string(restored) != string(original) {
t.Errorf("traefik.yml not restored to original, got: %s", string(restored))
}
// Verify 127.0.0.1:8443 is NOT in the restored file.
if strings.Contains(string(restored), "127.0.0.1:8443") {
t.Errorf("traefik.yml still has 127.0.0.1:8443 after rollback: %s", string(restored))
}
// The backup file should have been consumed by rollbackCutover's rename.
if _, bakExists := cfs.files["/etc/traefik/traefik.yml.bak"]; bakExists {
t.Error("backup file still exists after rollback (should have been renamed)")
}
}
// TestCutoverAtomicRenameSuccess verifies that the cutover writes the
// new config via atomic rename (temp file → original) and cleans up
// the backup on success (REQ-158, P09 T10).
func TestCutoverAtomicRenameSuccess(t *testing.T) {
cfs := newMockCutoverFS()
original := []byte("entrypoint:\n - :443\n")
cfs.files["/etc/traefik/traefik.yml"] = original
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
runner := &mockUpgradeRunner{
outputs: make(map[string][]byte),
}
prevHTTP := httpClientOverride
httpClientOverride = func(url string) (int, error) { return 200, nil }
t.Cleanup(func() { httpClientOverride = prevHTTP })
var buf bytes.Buffer
ok, err := performCutover(context.Background(), runner, &buf, false)
if err != nil {
t.Fatalf("performCutover: %v", err)
}
if !ok {
t.Fatal("expected cutover to succeed (ok=true)")
}
// Verify the config was updated to 127.0.0.1:8443.
updated, exists := cfs.files["/etc/traefik/traefik.yml"]
if !exists {
t.Fatal("traefik.yml missing after cutover")
}
if !strings.Contains(string(updated), "127.0.0.1:8443") {
t.Errorf("traefik.yml should have 127.0.0.1:8443, got: %s", string(updated))
}
if strings.Contains(string(updated), ":443\n") && !strings.Contains(string(updated), "127.0.0.1:8443") {
t.Errorf("traefik.yml should not have bare :443 anymore, got: %s", string(updated))
}
// The temp file should not exist.
if _, tmpExists := cfs.files["/etc/traefik/traefik.yml.tmp"]; tmpExists {
t.Error("temp file still exists after atomic rename")
}
// The backup should have been cleaned up on success.
if _, bakExists := cfs.files["/etc/traefik/traefik.yml.bak"]; bakExists {
t.Error("backup file still exists after successful cutover (should be cleaned up)")
}
}
// TestCutoverBackupCreated verifies that a backup file is created
// before the cutover edits the config (REQ-158, P09 T10). Uses a
// custom mock FS that records the sequence of operations so we can
// assert the backup was written before the temp file.
func TestCutoverBackupCreated(t *testing.T) {
// Use a recording mock FS that fails on the rename step so the
// backup write is observable before the rollback consumes it.
cfs := newMockCutoverFS()
original := []byte("entrypoint:\n - :443\n")
cfs.files["/etc/traefik/traefik.yml"] = original
// Track write order via a custom FS that records operations.
var writeOrder []string
recordingCFS := &recordingCutoverFS{
inner: cfs,
writeOrder: &writeOrder,
}
// Make the rename of the temp file fail so the cutover aborts.
cfs.errs["rename:/etc/traefik/traefik.yml.tmp"] = fmt.Errorf("rename failed")
cutoverFSOverride = recordingCFS
t.Cleanup(func() { cutoverFSOverride = nil })
runner := &mockUpgradeRunner{
outputs: make(map[string][]byte),
}
var buf bytes.Buffer
_, err := performCutover(context.Background(), runner, &buf, false)
if err == nil {
t.Fatal("expected error from failed rename")
}
// Verify the backup was written BEFORE the temp file.
// writeOrder records WriteFile calls in order.
bakIdx := -1
tmpIdx := -1
for i, p := range writeOrder {
if p == "/etc/traefik/traefik.yml.bak" {
bakIdx = i
}
if p == "/etc/traefik/traefik.yml.tmp" {
tmpIdx = i
}
}
if bakIdx == -1 {
t.Fatal("backup file was not written before cutover")
}
if tmpIdx == -1 {
t.Fatal("temp file was not written")
}
if bakIdx > tmpIdx {
t.Errorf("backup written after temp file (bakIdx=%d, tmpIdx=%d) — backup should come first", bakIdx, tmpIdx)
}
// The original should have been restored from backup on failure.
restored, exists := cfs.files["/etc/traefik/traefik.yml"]
if !exists {
t.Fatal("traefik.yml missing after failed rename + restore")
}
if string(restored) != string(original) {
t.Errorf("traefik.yml not restored to original after failed rename, got: %s", string(restored))
}
}
// recordingCutoverFS wraps a cutoverFS and records WriteFile call
// paths so tests can assert the order of operations (REQ-158, P09 T10).
type recordingCutoverFS struct {
inner cutoverFS
writeOrder *[]string
}
func (r *recordingCutoverFS) ReadFile(path string) ([]byte, error) {
return r.inner.ReadFile(path)
}
func (r *recordingCutoverFS) WriteFile(path string, content []byte, mode os.FileMode) error {
*r.writeOrder = append(*r.writeOrder, path)
return r.inner.WriteFile(path, content, mode)
}
func (r *recordingCutoverFS) Rename(old, new string) error {
return r.inner.Rename(old, new)
}
func (r *recordingCutoverFS) Remove(path string) error {
return r.inner.Remove(path)
}
func (r *recordingCutoverFS) Stat(path string) (os.FileInfo, error) {
return r.inner.Stat(path)
}
// TestCutoverNoSedDirectly verifies that the cutover does NOT use
// `sed -i` (the old unsafe approach). The mock runner records all
// calls; none should be `sed` (REQ-158, P09 T5).
func TestCutoverNoSedDirectly(t *testing.T) {
cfs := newMockCutoverFS()
cfs.files["/etc/traefik/traefik.yml"] = []byte("entrypoint:\n - :443\n")
cutoverFSOverride = cfs
t.Cleanup(func() { cutoverFSOverride = nil })
runner := &mockUpgradeRunner{
outputs: make(map[string][]byte),
}
prevHTTP := httpClientOverride
httpClientOverride = func(url string) (int, error) { return 200, nil }
t.Cleanup(func() { httpClientOverride = prevHTTP })
var buf bytes.Buffer
_, _ = performCutover(context.Background(), runner, &buf, false)
for _, call := range runner.calls {
if call.name == "sed" {
t.Errorf("cutover should not use 'sed' (uses atomic rename now), found call: %s %v", call.name, call.args)
}
}
}
+24
View File
@@ -21,6 +21,18 @@ type Config struct {
ServerKeyPath string `hcl:"server_key_path,optional"`
NodeCapacity *CapacityConfig `hcl:"node_capacity,block"`
// OIDC is the OIDC client config block (P06, v0.13; R-021). The
// bundled Dex (deployed by `orca auth init-idp`) is the default
// issuer; an explicit oidc.issuer here repoints the CLI to a BYO
// external IdP. loadOIDCConfig reads this block before falling back
// to --issuer/--client-id flags and env vars.
OIDC *OIDCConfig `hcl:"oidc,block"`
// ClusterDomain is the cluster's Traefik-served domain (C-38). It
// is the WebAuthn relying-party ID default and the Dex issuer host.
// May be overridden by --rp-id on `orca auth init-idp`.
ClusterDomain string `hcl:"cluster_domain,optional"`
// ACL is the access-control config block (P04, v0.13; C-45).
// When ACL.Enforce is false (the default for the first run after
// P04 wiring), ACL denials are LOGGED but NOT enforced — the
@@ -36,6 +48,16 @@ type ACLConfig struct {
Enforce bool `hcl:"enforce,optional"`
}
// OIDCConfig is the oidc block in config (P06, R-021). Mirrors
// identity.OIDCConfig (kept separate to avoid an internal/config ->
// internal/identity dependency cycle).
type OIDCConfig struct {
Issuer string `hcl:"issuer,optional"`
ClientID string `hcl:"client_id,optional"`
ClientSecret string `hcl:"client_secret,optional"`
Scopes []string `hcl:"scopes,optional"`
}
type Flags struct {
DBPath *string
ListenAddr *string
@@ -134,6 +156,8 @@ func (c *Config) MergeOverrides(flags Flags, env Environ) *Config {
ServerCertPath: c.ServerCertPath,
ServerKeyPath: c.ServerKeyPath,
NodeCapacity: c.NodeCapacity,
OIDC: c.OIDC,
ClusterDomain: c.ClusterDomain,
}
applyStr := func(flag *string, envKey, fileVal string) string {
+49
View File
@@ -89,6 +89,7 @@ func extractFrontmatter(content string) (string, bool) {
func parseFrontmatterBlock(block, path string) (*Config, error) {
cfg := &Config{}
var inCapacity bool
var inOIDC bool
lines := strings.Split(block, "\n")
for lineNo, raw := range lines {
@@ -103,6 +104,7 @@ func parseFrontmatterBlock(block, path string) (*Config, error) {
// A top-level key (no leading indent).
if indent == 0 {
inCapacity = false
inOIDC = false
key, val, ok := splitKV(trimmed)
if !ok {
continue
@@ -113,6 +115,10 @@ func parseFrontmatterBlock(block, path string) (*Config, error) {
cfg.NodeCapacity = &CapacityConfig{}
inCapacity = true
}
if key == "oidc" {
cfg.OIDC = &OIDCConfig{}
inOIDC = true
}
continue
}
applyScalar(cfg, key, val, path, lineNo)
@@ -135,6 +141,26 @@ func parseFrontmatterBlock(block, path string) (*Config, error) {
cfg.NodeCapacity.MemoryMB = n
}
}
continue
}
// Indented line under the oidc block.
if inOIDC && cfg.OIDC != nil {
key, val, hasVal := splitKV(trimmed)
if !hasVal {
continue
}
switch key {
case "issuer":
cfg.OIDC.Issuer = unquote(val)
case "client_id":
cfg.OIDC.ClientID = unquote(val)
case "client_secret":
cfg.OIDC.ClientSecret = unquote(val)
case "scopes":
// Comma-separated list, optionally bracketed as [a, b].
cfg.OIDC.Scopes = parseScopes(val)
}
continue
}
}
return cfg, nil
@@ -153,11 +179,34 @@ func applyScalar(cfg *Config, key, val, path string, lineNo int) {
cfg.ServerCertPath = unquote(val)
case "server_key_path":
cfg.ServerKeyPath = unquote(val)
case "cluster_domain":
cfg.ClusterDomain = unquote(val)
}
_ = path
_ = lineNo
}
// parseScopes parses a scopes value into a []string. Supports both a
// comma-separated bare list (openid, profile, email) and a YAML-style
// flow list ([openid, profile]). Empty values are dropped.
func parseScopes(val string) []string {
val = strings.TrimSpace(val)
val = unquote(val)
// Strip surrounding brackets.
if len(val) >= 2 && val[0] == '[' && val[len(val)-1] == ']' {
val = val[1 : len(val)-1]
}
var out []string
for _, part := range strings.Split(val, ",") {
part = strings.TrimSpace(part)
part = unquote(part)
if part != "" {
out = append(out, part)
}
}
return out
}
func splitKV(s string) (key, val string, ok bool) {
idx := strings.Index(s, ":")
if idx < 0 {
+25 -8
View File
@@ -98,24 +98,39 @@ type TaskSpec struct {
}
func (e *Executor) Run(ctx context.Context, job *model.Job, specs []TaskSpec) error {
e.mu.Lock()
defer e.mu.Unlock()
// REQ-156 / P07 T6: the mutex previously guarded the ENTIRE job
// (insert + status transitions + task execution + wait). That
// serialized unrelated jobs against each other and held the lock
// across long-running child processes, blocking concurrent
// Submit/Status/Run callers. The mutex is now scoped ONLY to the
// DB inserts/updates (the part that must be serialized against
// the single-writer SQLite connection pool — see store.Open
// SetMaxOpenConns(1)). The task goroutines spawned below do not
// hold e.mu; they share the per-job failure counter via a local
// sync.Mutex.
// Insert the job first so tasks can reference it via foreign key.
// Insert the job + flip to Running under the lock (serializes
// the DB writes; the underlying SQLite busy_timeout(5000) +
// SetMaxOpenConns(1) handles contention).
e.mu.Lock()
if err := e.jobs.Insert(ctx, job); err != nil {
e.mu.Unlock()
return err
}
if err := e.jobs.UpdateStatus(ctx, job.ID, model.JobStatusRunning, 0); err != nil {
e.mu.Unlock()
return err
}
e.mu.Unlock()
// Task execution runs WITHOUT e.mu — concurrent jobs (and
// concurrent Submit/Status callers) are no longer blocked by a
// long-running child process.
var (
wg sync.WaitGroup
failedCount int
exitCode int
mu sync.Mutex
)
for _, ts := range specs {
wg.Add(1)
go func(ts TaskSpec) {
@@ -133,14 +148,16 @@ func (e *Executor) Run(ctx context.Context, job *model.Job, specs []TaskSpec) er
}
wg.Wait()
// Final status transition under the lock (the DB write is the
// only thing that needs serialization).
e.mu.Lock()
defer e.mu.Unlock()
if failedCount > 0 {
exitCode = 1
if err := e.jobs.UpdateStatus(ctx, job.ID, model.JobStatusFailed, exitCode); err != nil {
if err := e.jobs.UpdateStatus(ctx, job.ID, model.JobStatusFailed, 1); err != nil {
return err
}
return fmt.Errorf("%d/%d tasks failed", failedCount, len(specs))
}
if err := e.jobs.UpdateStatus(ctx, job.ID, model.JobStatusComplete, 0); err != nil {
return err
}
+18 -12
View File
@@ -29,6 +29,8 @@ import (
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
"git.cloudinit.dev/coreci/orca/internal/security"
)
// OIDCConfig holds the OIDC client configuration. It is loaded from
@@ -113,7 +115,13 @@ func SaveCredentials(c *Credentials) error {
if err != nil {
return fmt.Errorf("oidc: marshal: %w", err)
}
return writeAtomic0600(path, data)
// REQ-156 / P07 T9: use the canonical security.WriteAtomic (temp
// + chmod + fsync + rename) instead of the local writeAtomic0600
// (which did temp + chmod + rename with NO fsync - a crash before
// rename could leave a partially-written tmp file that rename
// would then promote, or the rename could land before the data
// reached durable storage).
return security.WriteAtomic(path, 0o600, data)
}
// ClearCredentials removes the stored credentials (logout).
@@ -128,16 +136,6 @@ func ClearCredentials() error {
return nil
}
// writeAtomic0600 writes data to path atomically at mode 0600
// (temp + chmod + rename).
func writeAtomic0600(path string, data []byte) error {
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return fmt.Errorf("oidc: write tmp: %w", err)
}
return os.Rename(tmp, path)
}
// OIDCClient wraps the OIDC provider + oauth2 config for the auth flow.
type OIDCClient struct {
provider *oidc.Provider
@@ -237,7 +235,15 @@ func (c *OIDCClient) Login(ctx context.Context, openBrowser func(string) error)
err error
}
resultCh := make(chan result, 1)
srv := &http.Server{}
// REQ-157 / P08 T8: set ReadHeaderTimeout so a slowloris-style
// peer cannot hold the callback server open indefinitely. The
// callback is short-lived (one request then Shutdown), but the
// default zero ReadHeaderTimeout means an attacker who reaches the
// loopback port during the brief auth window could stall the
// handshake. 5s is generous for a loopback redirect.
srv := &http.Server{
ReadHeaderTimeout: 5 * time.Second,
}
srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/callback" {
http.NotFound(w, r)
+26 -6
View File
@@ -150,7 +150,7 @@ func BootstrapProxmox(ctx context.Context, opts Options) (*Result, error) {
// the v0.6 ship-defect where knownhosts.New returned KeyError{Want:[]}
// on first connect WITHOUT writing the captured key, so the first
// `orca node join --type proxmox` always failed.
sshAddr := fmt.Sprintf("%s:%d", opts.Host, opts.SSHPort)
sshAddr := net.JoinHostPort(opts.Host, fmt.Sprintf("%d", opts.SSHPort))
var capturedHostKey ssh.PublicKey
var hostKeyCallback ssh.HostKeyCallback
if opts.HostKeyFingerprint != "" {
@@ -296,8 +296,29 @@ func pinnedHostKeyCallback(expectedSHA256Base64 string, capturedKey *ssh.PublicK
//
// Exported so the doctor proxmox probe (T02.9) can reuse the same
// capture-fix wrapper for parity (GRILL condition #2).
//
// REQ-157 / P08 T4: TOFUHostKeyCallback now delegates to
// TOFUHostKeyCallbackPath with the v0.8 flat layout
// (certpaths.KnownHostsPath()). The path-accepting variant lets the
// sshpush transport pass its stored known_hosts field (the v0.9
// paths.KnownHostsPath() location) instead of always reading the v0.8
// flat layout — fixing the bug where the dial() flock field was stored
// but never read.
func TOFUHostKeyCallback(addr string, capturedKey *ssh.PublicKey) (ssh.HostKeyCallback, error) {
cb, err := knownhosts.New(certpaths.KnownHostsPath())
return TOFUHostKeyCallbackPath(certpaths.KnownHostsPath(), addr, capturedKey)
}
// TOFUHostKeyCallbackPath is the path-accepting variant. knownHostsPath
// is the known_hosts file to verify against and capture new keys into;
// it MUST be flock-protected on capture (security.Flock). When
// knownHostsPath is empty, falls back to certpaths.KnownHostsPath()
// (the v0.8 flat layout) for backward compatibility with callers that
// relied on the implicit default.
func TOFUHostKeyCallbackPath(knownHostsPath, addr string, capturedKey *ssh.PublicKey) (ssh.HostKeyCallback, error) {
if knownHostsPath == "" {
knownHostsPath = certpaths.KnownHostsPath()
}
cb, err := knownhosts.New(knownHostsPath)
if err != nil {
return nil, err
}
@@ -312,13 +333,12 @@ func TOFUHostKeyCallback(addr string, capturedKey *ssh.PublicKey) (ssh.HostKeyCa
var keyErr *knownhosts.KeyError
if errors.As(err, &keyErr) && len(keyErr.Want) == 0 {
line := knownhosts.Line([]string{knownhosts.Normalize(addr)}, key)
path := certpaths.KnownHostsPath()
release, lockErr := security.Flock(path)
release, lockErr := security.Flock(knownHostsPath)
if lockErr != nil {
return fmt.Errorf("tofu lock known_hosts: %w", lockErr)
}
defer release()
existing, readErr := os.ReadFile(path)
existing, readErr := os.ReadFile(knownHostsPath)
if readErr != nil && !os.IsNotExist(readErr) {
return fmt.Errorf("tofu read known_hosts: %w", readErr)
}
@@ -326,7 +346,7 @@ func TOFUHostKeyCallback(addr string, capturedKey *ssh.PublicKey) (ssh.HostKeyCa
existing = append(existing, '\n')
}
updated := append(existing, []byte(line)...)
if writeErr := security.WriteAtomic(path, 0o600, updated); writeErr != nil {
if writeErr := security.WriteAtomic(knownHostsPath, 0o600, updated); writeErr != nil {
return fmt.Errorf("tofu write known_hosts: %w", writeErr)
}
if capturedKey != nil {
+30
View File
@@ -0,0 +1,30 @@
package proxmox
import (
"fmt"
"net"
"testing"
)
// TestREQ157_IPv6JoinHostPort verifies that the proxmox SSH dial
// address is correctly bracketed for IPv6 hosts (REQ-157 / P08 T5/T11).
func TestREQ157_IPv6JoinHostPort(t *testing.T) {
tests := []struct {
host string
port int
want string
}{
{"192.168.1.1", 22, "192.168.1.1:22"},
{"::1", 22, "[::1]:22"},
{"fe80::1", 2222, "[fe80::1]:2222"},
{"2001:db8::1", 22, "[2001:db8::1]:22"},
}
for _, tt := range tests {
t.Run(tt.host, func(t *testing.T) {
got := net.JoinHostPort(tt.host, fmt.Sprintf("%d", tt.port))
if got != tt.want {
t.Errorf("JoinHostPort(%s, %d) = %q, want %q", tt.host, tt.port, got, tt.want)
}
})
}
}
+68 -17
View File
@@ -5,11 +5,13 @@ import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/crypto/ssh"
@@ -54,12 +56,14 @@ type Transport struct {
pool sync.Map
// keyPath is the SSH private key path (Ed25519, D-037).
keyPath string
// knownHostsPath is the v0.9 known_hosts path (paths.KnownHostsPath()
// = ClusterDir()/known_hosts). It is stored for the v0.10-P14 migration
// when proxmox.TOFUHostKeyCallback will accept a path parameter; today
// the callback reads certpaths.KnownHostsPath() (the v0.8 flat layout)
// directly, so this field is not yet read by dial(). Tests set
// $ORCA_HOME so certpaths.KnownHostsPath() resolves under the temp dir.
// knownHostsPath is the known_hosts path passed to the TOFU
// host-key callback (D-035). NewTransport sets it from
// certpaths.KnownHostsPath() (v0.8 flat layout) by default; callers
// that want the v0.9 paths.KnownHostsPath() location construct the
// transport with that path explicitly. REQ-157 / P08 T4: this field
// IS read by dial() (via proxmox.TOFUHostKeyCallbackPath) — the
// earlier bug where the callback ignored it and read
// certpaths.KnownHostsPath() directly is fixed.
knownHostsPath string
// user is the remote SSH user (default "orca", D-037).
user string
@@ -120,15 +124,14 @@ func (defaultSSHDialer) DialContext(ctx context.Context, network, addr string, c
}
// NewTransport returns a Transport configured with the given SSH
// private key path and known_hosts path. The known_hosts path is the v0.9
// location (paths.KnownHostsPath); it is stored for the v0.10-P14
// migration when the TOFU callback will accept a path parameter. Today
// dial() delegates host-key verification to proxmox.TOFUHostKeyCallback,
// which reads certpaths.KnownHostsPath() (the v0.8 flat layout under
// $ORCA_HOME) directly — so callers must ensure $ORCA_HOME points at the
// cluster root (the CLI sets this up). The remote user defaults to
// "orca" (D-037); override with SetUser. The dialer defaults to the
// real ssh.Dial-based dialer; tests call SetDialer to inject a mock.
// private key path and known_hosts path. The known_hosts path is read
// by dial() via proxmox.TOFUHostKeyCallbackPath (D-035, REQ-157/P08 T4):
// the TOFU callback locks/captures against this path on first connect.
// Callers typically pass certpaths.KnownHostsPath() (the v0.8 flat
// layout under $ORCA_HOME) or paths.KnownHostsPath() (the v0.9
// ClusterDir() location). The remote user defaults to "orca" (D-037);
// override with SetUser. The dialer defaults to the real ssh.Dial-based
// dialer; tests call SetDialer to inject a mock.
func NewTransport(keyPath, knownHostsPath string) *Transport {
return &Transport{
keyPath: keyPath,
@@ -193,7 +196,14 @@ func (t *Transport) dial(peer string) (*ssh.Client, error) {
// Host-key verification reuses the v0.8 TOFU wrapper (D-035). The
// known_hosts file is flock-protected inside the callback on
// first-connect capture, so we do NOT re-lock here.
cb, err := proxmox.TOFUHostKeyCallback(peer, nil)
//
// REQ-157 / P08 T4: use the stored knownHostsPath field (set via
// NewTransport from certpaths.KnownHostsPath() / paths.KnownHostsPath())
// instead of having the callback read certpaths.KnownHostsPath() (the
// v0.8 flat layout) directly. This closes the bug where the flock
// field was stored but never read by dial() — the TOFU callback now
// locks/captures against the path the transport was constructed with.
cb, err := proxmox.TOFUHostKeyCallbackPath(t.knownHostsPath, peer, nil)
if err != nil {
return nil, fmt.Errorf("sshpush: host-key callback: %w", err)
}
@@ -391,7 +401,14 @@ func backoff(initial, max time.Duration, n int) time.Duration {
}
// isTransient reports whether err looks like a transient failure worth
// retrying (mirrors v0.8 transport.IsTransient, reimplemented here).
// retrying (mirrors transport.IsTransient, reimplemented here so
// internal/sshpush does not import internal/transport).
//
// REQ-157 / P08 T2: classification is TYPE-BASED, not substring-based.
// The primary path is errors.Is against the sentinels (ErrTransient /
// ErrPermanent) and against well-known syscall/net/io errors. The
// substring fallback is retained ONLY for unwrapped errors from the
// ssh.Dialer that do not implement the standard interfaces.
func isTransient(err error) bool {
if err == nil {
return false
@@ -402,6 +419,29 @@ func isTransient(err error) bool {
if errors.Is(err, ErrPermanent) {
return false
}
// Typed: a net.Error that is a timeout is transient; a net.OpError
// whose Temporary() is true (ECONNREFUSED et al) is transient.
var netErr net.Error
if errors.As(err, &netErr) {
if netErr.Timeout() {
return true
}
return isTemporarySSH(netErr)
}
if errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET) ||
errors.Is(err, syscall.ETIMEDOUT) ||
errors.Is(err, syscall.EHOSTUNREACH) ||
errors.Is(err, syscall.ENETUNREACH) {
return true
}
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return true
}
if errors.Is(err, context.DeadlineExceeded) {
return true
}
// Substring fallback (defense-in-depth for unwrapped errors).
s := err.Error()
for _, sub := range []string{
"connection refused", "i/o timeout", "EOF",
@@ -415,6 +455,17 @@ func isTransient(err error) bool {
return false
}
// isTemporarySSH reports whether netErr implements the legacy
// Temporary() bool method and it returns true. net.OpError.Temporary()
// maps to the underlying errno's temporary classification.
func isTemporarySSH(netErr net.Error) bool {
type temporary interface{ Temporary() bool }
if t, ok := netErr.(temporary); ok {
return t.Temporary()
}
return false
}
// classifyDialErr converts a raw ssh.Dial error into a transport error
// (transient vs permanent). Auth failures and host-key mismatches are
// permanent; everything else is transient.
+5
View File
@@ -22,6 +22,11 @@ func Open(path string) (*sql.DB, error) {
if err != nil {
return nil, fmt.Errorf("open sqlite: %w", err)
}
// REQ-156 / P07 T1: SQLite is a single-writer database. Cap the
// connection pool at 1 so concurrent goroutines serialize on the
// busy_timeout(5000) above instead of racing for the WAL writer
// lock and surfacing spurious SQLITE_BUSY errors to callers.
db.SetMaxOpenConns(1)
if err := db.Ping(); err != nil {
_ = db.Close()
return nil, fmt.Errorf("ping sqlite: %w", err)
+81 -23
View File
@@ -8,7 +8,11 @@ package transport
import (
"context"
"errors"
"io"
"math/rand"
"net"
"strings"
"syscall"
"time"
)
@@ -34,29 +38,6 @@ func DefaultRetryPolicy() RetryPolicy {
return RetryPolicy{Initial: RetryInitial, Max: RetryMax, MaxAttempts: RetryMaxAttempts}
}
// IsTransient reports whether err looks like a transient failure
// worth retrying. We treat network errors, context-deadline-exceeded
// (peer was slow but reachable), and a sentinel ErrTransient as
// retryable; everything else (4xx, validation, auth) is permanent.
func IsTransient(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrTransient) {
return true
}
// We avoid pulling net/error here to keep dependencies minimal;
// the most common transient signature is the substring "connection
// refused" or "i/o timeout". Tests assert these explicitly.
s := err.Error()
for _, sub := range []string{"connection refused", "i/o timeout", "EOF", "no such host", "connection reset"} {
if contains(s, sub) {
return true
}
}
return false
}
// ErrTransient is a sentinel callers can wrap to mark an error
// retryable. ErrPermanent is the opposite.
var (
@@ -64,6 +45,83 @@ var (
ErrPermanent = errors.New("permanent error")
)
// IsTransient reports whether err looks like a transient failure
// worth retrying. We treat network errors, context-deadline-exceeded
// (peer was slow but reachable), and a sentinel ErrTransient as
// retryable; everything else (4xx, validation, auth) is permanent.
//
// REQ-157 / P08 T1: classification is TYPE-BASED, not substring-based.
// The primary path is errors.Is against the sentinels (ErrTransient /
// ErrPermanent) and against well-known syscall/net/io errors. The
// substring fallback is retained ONLY for unwrapped errors from
// third-party dialers that do not implement the standard interfaces
// (defense-in-depth); callers SHOULD wrap with ErrTransient instead.
func IsTransient(err error) bool {
if err == nil {
return false
}
// Explicit sentinels win.
if errors.Is(err, ErrTransient) {
return true
}
if errors.Is(err, ErrPermanent) {
return false
}
// Typed classification: a net.Error that is a timeout is transient.
var netErr net.Error
if errors.As(err, &netErr) {
if netErr.Timeout() {
return true
}
// net.OpError implements Temporary(); that maps to the
// underlying errno's temporary classification (ECONNREFUSED et
// al). We keep the check so a plain "dial tcp: connection
// refused" classifies as transient.
return isTemporary(netErr)
}
// Specific syscall errors that are universally retryable.
if errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET) ||
errors.Is(err, syscall.ETIMEDOUT) ||
errors.Is(err, syscall.EHOSTUNREACH) ||
errors.Is(err, syscall.ENETUNREACH) {
return true
}
// io.EOF on a read from a half-closed peer is transient (the
// dispatch HTTP/2 path can surface this mid-stream).
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return true
}
// context.DeadlineExceeded from a slow-but-reachable peer is
// transient (the next attempt may succeed under a fresh deadline).
if errors.Is(err, context.DeadlineExceeded) {
return true
}
// Substring fallback (defense-in-depth for unwrapped errors).
s := err.Error()
for _, sub := range []string{
"connection refused", "i/o timeout", "EOF",
"no such host", "connection reset",
"deadline exceeded", "temporarily unavailable",
} {
if strings.Contains(s, sub) {
return true
}
}
return false
}
// isTemporary reports whether netErr implements the legacy Temporary()
// bool method and it returns true. net.OpError.Temporary() maps to the
// underlying errno's temporary classification (ECONNREFUSED et al).
func isTemporary(netErr net.Error) bool {
type temporary interface{ Temporary() bool }
if t, ok := netErr.(temporary); ok {
return t.Temporary()
}
return false
}
// RetryableFunc is the signature Retry calls. It returns the result
// and an error. The bool indicates whether the call is idempotent
// (true = safe to retry without an idempotency key).
+49
View File
@@ -0,0 +1,49 @@
package transport
import (
"context"
"errors"
"io"
"net"
"testing"
"fmt"
)
// TestREQ157_TypedErrorClassification verifies that IsTransient uses
// typed sentinels and standard interfaces, not substring matching
// (REQ-157 / P08 T10).
func TestREQ157_TypedErrorClassification(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil", nil, false},
{"ErrTransient", ErrTransient, true},
{"wrapped ErrTransient", fmt.Errorf("dial: %w", ErrTransient), true},
{"ErrPermanent", ErrPermanent, false},
{"wrapped ErrPermanent", fmt.Errorf("auth: %w", ErrPermanent), false},
{"net timeout", &net.OpError{Op: "dial", Net: "tcp", Err: &timeoutError{}}, true},
{"context deadline", context.DeadlineExceeded, true},
{"context canceled", context.Canceled, false},
{"io EOF", io.EOF, true},
{"plain error", errors.New("some permanent error"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsTransient(tt.err)
if got != tt.want {
t.Errorf("IsTransient(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
type timeoutError struct{}
func (timeoutError) Error() string { return "i/o timeout" }
func (timeoutError) Timeout() bool { return true }
func (timeoutError) Temporary() bool { return true }
var _ = fmt.Errorf
+55 -12
View File
@@ -15,6 +15,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/go-webauthn/webauthn/protocol"
@@ -80,26 +81,54 @@ type RegistrationSession struct {
CreatedAt time.Time
}
// sessionStore holds in-flight sessions (registration + login). In
// production this would be a Redis/shared cache; for the bundled
// single-lead Dex, an in-memory map with TTL is sufficient.
// sessionStore holds in-flight sessions (registration). In production
// this would be a Redis/shared cache; for the bundled single-lead
// Dex, an in-memory map with TTL is sufficient.
//
// REQ-156 / P07 T10: the session maps are accessed from HTTP handler
// goroutines (one goroutine per request) and were previously plain
// maps with no synchronization. Concurrent BeginRegistration calls
// for the same username would race on map writes (detected by go
// test -race in T11). A sync.Mutex now guards all access.
type sessionStore struct {
mu sync.Mutex
sessions map[string]*RegistrationSession
}
// regSessions is the global in-flight registration session store.
var regSessions = &sessionStore{sessions: make(map[string]*RegistrationSession)}
// loginSessionStore holds in-flight login sessions (T10). Same
// mutex pattern as sessionStore.
type loginSessionStore struct {
mu sync.Mutex
sessions map[string]*LoginSession
}
// loginSessions is the global in-flight login session store.
var loginSessions = &loginSessionStore{sessions: make(map[string]*LoginSession)}
// sessionTTL is the max time a registration/login session is valid.
const sessionTTL = 5 * time.Minute
// cleanSessions removes expired sessions.
// cleanSessions removes expired registration + login sessions.
// Called under each store's lock by the Begin* handlers.
func cleanSessions() {
now := time.Now()
for id, s := range regSessions.sessions {
if now.Sub(s.CreatedAt) > sessionTTL {
regSessions.mu.Lock()
for id, sess := range regSessions.sessions {
if now.Sub(sess.CreatedAt) > sessionTTL {
delete(regSessions.sessions, id)
}
}
regSessions.mu.Unlock()
loginSessions.mu.Lock()
for id, sess := range loginSessions.sessions {
if now.Sub(sess.CreatedAt) > sessionTTL {
delete(loginSessions.sessions, id)
}
}
loginSessions.mu.Unlock()
}
// requireAuth checks the request for an authenticated session. When
@@ -165,11 +194,13 @@ func (c *Connector) BeginRegistration(w http.ResponseWriter, r *http.Request) {
return
}
sessionID := base64.RawURLEncoding.EncodeToString(userID)
regSessions.mu.Lock()
regSessions.sessions[sessionID] = &RegistrationSession{
UserID: username,
Challenge: session,
CreatedAt: time.Now(),
}
regSessions.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(options)
}
@@ -188,16 +219,20 @@ func (c *Connector) FinishRegistration(w http.ResponseWriter, r *http.Request) {
return
}
sessionID := base64.RawURLEncoding.EncodeToString([]byte(username))
regSessions.mu.Lock()
session, ok := regSessions.sessions[sessionID]
if !ok {
regSessions.mu.Unlock()
http.Error(w, "no registration session; call /register first", http.StatusBadRequest)
return
}
if time.Since(session.CreatedAt) > sessionTTL {
delete(regSessions.sessions, sessionID)
regSessions.mu.Unlock()
http.Error(w, "session expired", http.StatusBadRequest)
return
}
regSessions.mu.Unlock()
parsed, err := protocol.ParseCredentialCreationResponseBody(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("parse attestation: %v", err), http.StatusBadRequest)
@@ -221,7 +256,9 @@ func (c *Connector) FinishRegistration(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("store credential: %v", err), http.StatusInternalServerError)
return
}
regSessions.mu.Lock()
delete(regSessions.sessions, sessionID)
regSessions.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "registered", "user_id": username})
}
@@ -233,8 +270,6 @@ type LoginSession struct {
CreatedAt time.Time
}
var loginSessions = map[string]*LoginSession{}
// BeginLogin starts the WebAuthn login ceremony.
// GET /orca/webauthn/login?username=<name>
func (c *Connector) BeginLogin(w http.ResponseWriter, r *http.Request) {
@@ -259,11 +294,13 @@ func (c *Connector) BeginLogin(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("begin login: %v", err), http.StatusInternalServerError)
return
}
loginSessions[username] = &LoginSession{
loginSessions.mu.Lock()
loginSessions.sessions[username] = &LoginSession{
UserID: username,
Challenge: session,
CreatedAt: time.Now(),
}
loginSessions.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(options)
}
@@ -276,16 +313,20 @@ func (c *Connector) FinishLogin(w http.ResponseWriter, r *http.Request) {
http.Error(w, "username required", http.StatusBadRequest)
return
}
session, ok := loginSessions[username]
loginSessions.mu.Lock()
session, ok := loginSessions.sessions[username]
if !ok {
loginSessions.mu.Unlock()
http.Error(w, "no login session; call /login first", http.StatusBadRequest)
return
}
if time.Since(session.CreatedAt) > sessionTTL {
delete(loginSessions, username)
delete(loginSessions.sessions, username)
loginSessions.mu.Unlock()
http.Error(w, "session expired", http.StatusBadRequest)
return
}
loginSessions.mu.Unlock()
existing, _ := c.store.GetCredential(username)
if existing == nil {
http.Error(w, "user not registered", http.StatusNotFound)
@@ -307,7 +348,9 @@ func (c *Connector) FinishLogin(w http.ResponseWriter, r *http.Request) {
return
}
_ = c.store.UpdateSignCount(username, cred.Authenticator.SignCount)
delete(loginSessions, username)
loginSessions.mu.Lock()
delete(loginSessions.sessions, username)
loginSessions.mu.Unlock()
// The OIDC sub is the username (the connector maps credential ID
// to sub). Dex uses this to issue the ID token.
w.Header().Set("Content-Type", "application/json")
@@ -0,0 +1,179 @@
package webauthn
// connector_concurrency_test.go covers REQ-156 / P07 T10: the
// WebAuthn session maps (regSessions, loginSessions) are accessed
// from HTTP handler goroutines (one goroutine per request) and were
// previously plain maps with no synchronization. Concurrent
// BeginRegistration calls for the same username would race on map
// writes (detected by `go test -race`). T10 added a sync.Mutex to
// each store; this test exercises the fix under the race detector.
//
// Run with: go test -race ./internal/webauthn/
import (
"net/http"
"net/http/httptest"
"sync"
"testing"
)
// TestBeginRegistrationConcurrentNoPanic fires many concurrent
// BeginRegistration requests (all authenticated, all for the SAME
// username so they hit the SAME session map entry) and asserts the
// handler does not panic and does not race on the shared
// regSessions.sessions map. Without the T10 mutex this test panics
// under -race with "concurrent map writes".
func TestBeginRegistrationConcurrentNoPanic(t *testing.T) {
dbPath := t.TempDir() + "/webauthn-conc.db"
store, err := NewStore(dbPath)
if err != nil {
t.Fatalf("NewStore: %v", err)
}
defer store.Close()
c, err := NewConnectorWithAuth(store, "test.cluster", "https://test.cluster",
func(r *http.Request) (bool, string, error) { return true, "admin", nil })
if err != nil {
t.Fatalf("NewConnector: %v", err)
}
mux := c.Routes()
const n = 25
var wg sync.WaitGroup
wg.Add(n)
panicCh := make(chan interface{}, n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
select {
case panicCh <- r:
default:
}
}
}()
req := httptest.NewRequest("GET", "/orca/webauthn/register?username=admin", nil)
rec := httptest.NewRecorder()
// BeginRegistration writes to regSessions.sessions[sessionID]
// under the mutex; concurrent writers for the same key
// must not panic or race.
mux.ServeHTTP(rec, req)
}()
}
wg.Wait()
close(panicCh)
if p, ok := <-panicCh; ok {
t.Fatalf("BeginRegistration panicked under concurrency: %v", p)
}
}
// TestBeginLoginConcurrentNoPanic is the login-session variant. It
// pre-registers a credential so BeginLogin finds the user, then fires
// concurrent BeginLogin calls for the same username. The login
// session map writes must be mutex-guarded (T10).
func TestBeginLoginConcurrentNoPanic(t *testing.T) {
dbPath := t.TempDir() + "/webauthn-conc-login.db"
store, err := NewStore(dbPath)
if err != nil {
t.Fatalf("NewStore: %v", err)
}
defer store.Close()
// Pre-seed a credential so BeginLogin does not 404.
if err := store.PutCredential(&Credential{
UserID: "loginuser",
CredentialID: []byte("cred-id-bytes"),
PublicKey: []byte("pub-key-bytes"),
}); err != nil {
t.Fatalf("PutCredential: %v", err)
}
c, err := NewConnectorWithAuth(store, "test.cluster", "https://test.cluster",
func(r *http.Request) (bool, string, error) { return true, "loginuser", nil })
if err != nil {
t.Fatalf("NewConnector: %v", err)
}
mux := c.Routes()
const n = 25
var wg sync.WaitGroup
wg.Add(n)
panicCh := make(chan interface{}, n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
select {
case panicCh <- r:
default:
}
}
}()
req := httptest.NewRequest("GET", "/orca/webauthn/login?username=loginuser", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
}()
}
wg.Wait()
close(panicCh)
if p, ok := <-panicCh; ok {
t.Fatalf("BeginLogin panicked under concurrency: %v", p)
}
}
// TestCleanSessionsConcurrentNoPanic exercises the cleanSessions
// helper which iterates + deletes from BOTH session maps. Without
// the T10 mutexes, concurrent cleanSessions + BeginRegistration
// would race. We drive cleanSessions from multiple goroutines while
// also doing BeginRegistration writes.
func TestCleanSessionsConcurrentNoPanic(t *testing.T) {
dbPath := t.TempDir() + "/webauthn-clean.db"
store, err := NewStore(dbPath)
if err != nil {
t.Fatalf("NewStore: %v", err)
}
defer store.Close()
c, err := NewConnectorWithAuth(store, "test.cluster", "https://test.cluster",
func(r *http.Request) (bool, string, error) { return true, "admin", nil })
if err != nil {
t.Fatalf("NewConnector: %v", err)
}
mux := c.Routes()
const n = 15
var wg sync.WaitGroup
wg.Add(n * 2)
panicCh := make(chan interface{}, n*2)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
select {
case panicCh <- r:
default:
}
}
}()
cleanSessions()
}()
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
select {
case panicCh <- r:
default:
}
}
}()
req := httptest.NewRequest("GET", "/orca/webauthn/register?username=admin", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
}()
}
wg.Wait()
close(panicCh)
if p, ok := <-panicCh; ok {
t.Fatalf("cleanSessions/BeginRegistration panicked under concurrency: %v", p)
}
}
+5 -1
View File
@@ -46,11 +46,15 @@ func NewStore(dbPath string) (*Store, error) {
if err := os.MkdirAll(filepath.Dir(dbPath), 0o700); err != nil {
return nil, fmt.Errorf("webauthn: mkdir: %w", err)
}
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)", dbPath)
// REQ-156 / P07 T1: busy_timeout(5000) so concurrent webauthn
// DB opens wait up to 5s for the writer instead of failing with
// SQLITE_BUSY. SetMaxOpenConns(1) serializes the connections.
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)", dbPath)
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errorf("webauthn: open db: %w", err)
}
db.SetMaxOpenConns(1)
if err := db.Ping(); err != nil {
db.Close()
return nil, fmt.Errorf("webauthn: ping: %w", err)