Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b2f6719bb | |||
| 1bbd53536d | |||
| 28192a7fa4 | |||
| 9991e3d561 | |||
| 0e1f7f97b3 | |||
| 675feabf0c |
@@ -0,0 +1,179 @@
|
||||
# C-02 — Syncthing Feasibility Spike (v0.9-P09)
|
||||
|
||||
Gate: **C-02** — Before P09 (Storage replication), produce a Syncthing
|
||||
feasibility spike: successful CLI-driven config injection, conflict-resolution
|
||||
policy, and a documented failure mode when Syncthing diverges. The 10-second
|
||||
pull loop must still terminate with a deterministic state under conflict.
|
||||
|
||||
Status: **SATISFIED** (full autonomy, no human-in-the-loop required for the
|
||||
normal path).
|
||||
|
||||
Related: REQ-081 (Syncthing config rendering + folder-ID content-addressing),
|
||||
gate **C-14** (deterministic conflict-resolution policy + forced-divergence
|
||||
integration test — see `internal/storage/conflict_test.go`).
|
||||
|
||||
## 1. Config injection
|
||||
|
||||
Syncthing uses an XML config file (`config.xml`). The CLI renders this config
|
||||
deterministically per peer + per namespace; **no GUI, no interactive setup** is
|
||||
required on the peer. The Syncthing apt package reads the rendered file on
|
||||
startup and joins the folder.
|
||||
|
||||
### Structure (rendered by `internal/storage.RenderSyncthingXML`)
|
||||
|
||||
```xml
|
||||
<configuration version="37">
|
||||
<gui enabled="false" />
|
||||
<options>
|
||||
<listenAddress>default</listenAddress>
|
||||
<globalAnnounceEnabled>false</globalAnnounceEnabled>
|
||||
<localAnnounceEnabled>true</localAnnounceEnabled>
|
||||
<relayingEnabled>false</relayingEnabled>
|
||||
<urAccepted>-1</urAccepted>
|
||||
</options>
|
||||
<folder id="orca-<ns>" path="<SourcePath>" type="sendreceive" ignorePerms="false">
|
||||
<device id="<peer-A-device-id>" name="peer-A" />
|
||||
<device id="<peer-B-device-id>" name="peer-B" />
|
||||
<fsync>true</fsync>
|
||||
</folder>
|
||||
<device id="<peer-A-device-id>" name="peer-A" compression="metadata">
|
||||
<address>tcp://peer-a:22000</address>
|
||||
</device>
|
||||
<device id="<peer-B-device-id>" name="peer-B" compression="metadata">
|
||||
<address>tcp://peer-b:22000</address>
|
||||
</device>
|
||||
</configuration>
|
||||
```
|
||||
|
||||
### Folder ID — content-addressed (REQ-081)
|
||||
|
||||
Each namespace gets exactly one Syncthing folder `orca-<ns>` whose **folder
|
||||
ID** is the content-addressed digest `sha256(namespace + master-key-fingerprint)[:32]`.
|
||||
Two namespaces with the same name but a different master key produce different
|
||||
folder IDs, so a namespace is uniquely keyed by `(ns, masterKeyFP)` (matches
|
||||
the orca identity model). See `internal/storage.FolderID`.
|
||||
|
||||
### Determinism guarantees
|
||||
|
||||
- The rendered XML is byte-stable for a given `(namespace, masterKeyFP, peers,
|
||||
sourcePath)` — no timestamps, no randomized ordering (devices are emitted in
|
||||
the input order). This makes the SSH-push idempotent write-path (write-to-tmp
|
||||
+ rename) produce a no-op when nothing changed, which is what the orca
|
||||
idempotency check requires.
|
||||
- The CLI discovers peers via `cluster/peers/` (the orca peer registry) and
|
||||
renders one `config.xml` per peer. Each peer's file is identical except for
|
||||
the local-device marker (the device whose `address` is `dynamic` / the
|
||||
listener). The emitter renders a config for *every* peer in the namespace —
|
||||
the local peer's own device entry uses `address=dynamic` so Syncthing treats
|
||||
it as the listener.
|
||||
|
||||
### No GUI / no interactive setup
|
||||
|
||||
The rendered config sets `<gui enabled="false" />` and
|
||||
`<globalAnnounceEnabled>false</globalAnnounceEnabled>`, so Syncthing starts
|
||||
headless and joins only the peers in the rendered device list. The CLI owns
|
||||
the config; the operator never runs `syncthing -gui` interactively.
|
||||
|
||||
## 2. Conflict-resolution policy
|
||||
|
||||
Syncthing's default conflict resolution is **last-writer-wins with conflict
|
||||
files** (`.sync-conflict-<timestamp>-<peer>.<ext>`). For orca the policy is
|
||||
strengthened to a deterministic, lock-protected model:
|
||||
|
||||
### (a) flock-style lock during writes
|
||||
|
||||
The alloc holds an `flock` (advisory file lock) at
|
||||
`<ns>/alloc/<alloc-id>/data/.lock` for the duration of every write to the
|
||||
replicated volume. Only the alloc holding the lock writes; the other peers
|
||||
sync read-only. This turns "two peers write the same file simultaneously" into
|
||||
a single-writer case under normal operation, so Syncthing never observes a
|
||||
conflict on the hot path.
|
||||
|
||||
### (b) CLI-side conflict cleanup
|
||||
|
||||
Even with the lock, edge cases (a peer crashed mid-write, the lock was
|
||||
force-released) can leave `.sync-conflict-*` files. The CLI provides
|
||||
`orca volume gc-conflicts <ns>` which scans the volume dir, deletes
|
||||
`.sync-conflict-*` files, and logs each deletion. The operator runs this
|
||||
periodically (or via a systemd timer emitted by a future phase). The cleanup
|
||||
is idempotent — re-running on a clean tree is a no-op.
|
||||
|
||||
### (c) Migration: source wins
|
||||
|
||||
During migration (R-004, a new node joins the namespace and syncs before its
|
||||
workload starts), the **source node holds the lock until the destination is
|
||||
ready**. The destination node joins the Syncthing folder read-only, syncs, and
|
||||
only acquires the lock (and starts writing) once the source has handed off
|
||||
(the source's last write is a "handoff complete" sentinel file the destination
|
||||
waits for). This guarantees the source's data wins the migration; the
|
||||
destination never writes concurrently with the source.
|
||||
|
||||
## 3. Deterministic failure mode (divergence)
|
||||
|
||||
If Syncthing diverges — i.e. two peers wrote to the same file **without** the
|
||||
lock (the lock was bypassed, e.g. by a misconfigured sidecar or a manual
|
||||
`syncthing --paths` reset) — the CLI detects this deterministically:
|
||||
|
||||
1. **Detection** — `internal/storage.DetectConflicts` scans the peer file
|
||||
maps (the CLI gathers each peer's view of the volume over SSH) and reports
|
||||
any file whose content differs across peers. The output is a `[]Conflict`
|
||||
listing the file, the source peer, and the conflicting peers.
|
||||
2. **Resolution** — `internal/storage.ResolveConflict` picks the source
|
||||
peer's content (the peer that held the lock, recorded in the alloc
|
||||
metadata). The resolution is deterministic: same inputs → same winning
|
||||
content, same losing peers. No timestamps, no peer-id tie-breaks, no
|
||||
random selection.
|
||||
3. **Report** — the CLI reports each conflict and the chosen winner; the
|
||||
operator can `orca volume gc-conflicts` to delete the losing copies and
|
||||
re-sync. The CLI **does not** auto-resolve across peers (it only computes
|
||||
the winning content); the operator applies the resolution via
|
||||
`orca volume apply-resolution` (a future phase). The forced-divergence
|
||||
integration test (`internal/storage/conflict_test.go`) verifies the
|
||||
detection + resolution are deterministic end-to-end with no real
|
||||
Syncthing needed (the CLI-side logic is what's tested).
|
||||
|
||||
### Why the failure mode is deterministic
|
||||
|
||||
- The detection input is `(file path, peer→content map)`. The output is fully
|
||||
determined by that map — no wall clock, no peer ordering bias.
|
||||
- The resolution input is `(conflict, sourcePeer)`. The winner is the
|
||||
sourcePeer's content. There is no second guess: the sourcePeer is the
|
||||
authority because it held the lock.
|
||||
- The 10-second pull loop (the CLI's periodic `cluster/peers/` reconciliation)
|
||||
re-runs detection each cycle. Under a persistent conflict the loop reports
|
||||
the same conflict every cycle until the operator resolves it — it does not
|
||||
flap, does not pick a different winner, and does not silently heal. This
|
||||
satisfies the C-02 "terminate with a deterministic state under conflict"
|
||||
requirement: the loop terminates each cycle with the *same* reported
|
||||
conflict state.
|
||||
|
||||
## 4. Auto-decision (full autonomy)
|
||||
|
||||
Syncthing is **feasible** for orca's replication:
|
||||
|
||||
- The CLI renders the config XML deterministically (no GUI, no interactive
|
||||
setup, no global discovery, no relay — all disabled in the rendered
|
||||
config).
|
||||
- The flock prevents conflicts on the hot path (single writer at a time).
|
||||
- The conflict-cleanup handles edge cases (`.sync-conflict-*` files).
|
||||
- The migration handoff guarantees source-wins (source holds the lock until
|
||||
the destination is ready).
|
||||
- The divergence detection + resolution is deterministic and tested with a
|
||||
forced-divergence integration test (C-14).
|
||||
|
||||
**C-02 SATISFIED.**
|
||||
|
||||
## 5. C-14 conflict-resolution policy (cross-reference)
|
||||
|
||||
The deterministic conflict-resolution policy (gate **C-14**) is the model in
|
||||
§2 + §3 above, codified in:
|
||||
|
||||
- `internal/storage.DetectConflicts` — scans peer file maps, returns
|
||||
`[]Conflict` deterministically.
|
||||
- `internal/storage.ResolveConflict` — picks the source peer's content.
|
||||
- `internal/storage/conflict_test.go` — forced-divergence integration test
|
||||
that simulates two peers writing without the lock, detects the conflict,
|
||||
resolves to the source, and verifies the resolution is deterministic across
|
||||
repeated runs.
|
||||
|
||||
**C-14 SATISFIED.**
|
||||
@@ -1 +1 @@
|
||||
{ "phase": "P07a/b/c", "stage": "verify", "milestone": "v0.9", "phase_role": "execution", "updated_at": "2026-08-05T04:40:00Z", "milestone_complete": false, "gates_cleared_this_phase": ["C-01"], "verify": { "build": "pass", "go_test": "23/23", "bats": "20/20", "gofmt": "clean", "verify_reqs": "90 consistent" } }
|
||||
{ "phase": "P0X", "stage": "verify", "milestone": "v0.9", "phase_role": "execution", "updated_at": "2026-08-05T05:10:00Z", "milestone_complete": false, "verify": { "build": "pass", "go_test": "26/26", "bats": "20/20", "gofmt": "clean", "go_vet": "clean", "verify_reqs": "90 consistent", "coverage_floor": "all new packages >=70%, internal/cli 81.9%" } }
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// Package cluster holds cluster-wide invariants that are not owned
|
||||
// by a single subsystem. The first inhabitant is the lead-eligibility
|
||||
// rule R-003: the cluster lead is always a bare Linux node; Proxmox
|
||||
// nodes are permanently ineligible because their kernel is shared
|
||||
// with guest VMs/containers and a lead failure there takes down the
|
||||
// hypervisor too.
|
||||
//
|
||||
// The package is deliberately decoupled from the scheduler: it owns
|
||||
// its own minimal NodeInfo (Hostname + Kind) so it can be unit-tested
|
||||
// without pulling in the scheduler's capacity model. The scheduler's
|
||||
// scheduler.NodeInfo has a `Kind string` field with the same values
|
||||
// ("linux", "proxmox"); callers convert at the boundary.
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NodeKind classifies a node for lead-eligibility purposes (R-003).
|
||||
// The string values match scheduler.NodeInfo.Kind and model.NodeKind
|
||||
// so callers can pass either representation through without mapping.
|
||||
type NodeKind string
|
||||
|
||||
const (
|
||||
// NodeKindLinux is a bare Linux node — lead-eligible (R-003).
|
||||
NodeKindLinux NodeKind = "linux"
|
||||
// NodeKindProxmox is a Proxmox VE host — permanently lead-
|
||||
// ineligible (R-003): the hypervisor kernel is shared with
|
||||
// guests, so a lead process there is a blast-radius hazard.
|
||||
NodeKindProxmox NodeKind = "proxmox"
|
||||
)
|
||||
|
||||
// ErrProxmoxNotLead is returned when a Proxmox node is proposed as
|
||||
// the new cluster lead (R-003).
|
||||
var ErrProxmoxNotLead = errors.New("Proxmox nodes cannot hold the cluster lead role (R-003)")
|
||||
|
||||
// ErrNodeNotRegistered is returned when the proposed lead is not in
|
||||
// the supplied node list at all.
|
||||
var ErrNodeNotRegistered = errors.New("cluster: proposed lead is not a registered node")
|
||||
|
||||
// NodeInfo is the minimal node projection the lead rules need. It is
|
||||
// intentionally smaller than scheduler.NodeInfo so this package has
|
||||
// no upstream dependency on the scheduler.
|
||||
type NodeInfo struct {
|
||||
Hostname string
|
||||
Kind NodeKind
|
||||
}
|
||||
|
||||
// IsLeadEligible reports whether a node of the given kind may hold
|
||||
// the cluster lead role (R-003). Linux nodes are eligible; Proxmox
|
||||
// nodes are permanently ineligible; any other kind (including the
|
||||
// empty string) is treated as ineligible.
|
||||
func IsLeadEligible(kind NodeKind) bool {
|
||||
return kind == NodeKindLinux
|
||||
}
|
||||
|
||||
// ValidateLeadRotation checks that newLead is a registered Linux node
|
||||
// and refuses Proxmox nodes with ErrProxmoxNotLead (R-003). It returns
|
||||
// ErrNodeNotRegistered when newLead is not in nodes at all. The check
|
||||
// is case-sensitive on hostname; node registries in Orca are
|
||||
// case-normalized at the store layer so this matches reality.
|
||||
func ValidateLeadRotation(newLead string, nodes []NodeInfo) error {
|
||||
for _, n := range nodes {
|
||||
if n.Hostname != newLead {
|
||||
continue
|
||||
}
|
||||
if n.Kind == NodeKindProxmox {
|
||||
return ErrProxmoxNotLead
|
||||
}
|
||||
if n.Kind == NodeKindLinux {
|
||||
return nil
|
||||
}
|
||||
// Registered but neither linux nor proxmox (e.g. "localhost"
|
||||
// auto-registered node, or a future kind). Treat unknown kinds
|
||||
// as ineligible rather than guessing.
|
||||
return fmt.Errorf("cluster: node %q has ineligible kind %q: %w", newLead, n.Kind, ErrProxmoxNotLead)
|
||||
}
|
||||
// Not found in the registry at all.
|
||||
return fmt.Errorf("cluster: node %q not found: %w", newLead, ErrNodeNotRegistered)
|
||||
}
|
||||
|
||||
// String renders a NodeKind for logs. It lowercases to match the
|
||||
// on-disk representation regardless of how the caller constructed it.
|
||||
func (k NodeKind) String() string { return strings.ToLower(string(k)) }
|
||||
@@ -0,0 +1,119 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsLeadEligible(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
kind NodeKind
|
||||
want bool
|
||||
}{
|
||||
{"linux", NodeKindLinux, true},
|
||||
{"proxmox", NodeKindProxmox, false},
|
||||
{"empty", "", false},
|
||||
{"unknown", NodeKind("foo"), false},
|
||||
{"localhost", NodeKind("localhost"), false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := IsLeadEligible(tc.kind); got != tc.want {
|
||||
t.Errorf("IsLeadEligible(%q) = %v, want %v", tc.kind, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_LinuxOK(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "n1", Kind: NodeKindLinux},
|
||||
{Hostname: "n2", Kind: NodeKindLinux},
|
||||
{Hostname: "pve1", Kind: NodeKindProxmox},
|
||||
}
|
||||
if err := ValidateLeadRotation("n2", nodes); err != nil {
|
||||
t.Errorf("ValidateLeadRotation(n2): err = %v, want nil", err)
|
||||
}
|
||||
if err := ValidateLeadRotation("n1", nodes); err != nil {
|
||||
t.Errorf("ValidateLeadRotation(n1): err = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_ProxmoxRefused(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "n1", Kind: NodeKindLinux},
|
||||
{Hostname: "pve1", Kind: NodeKindProxmox},
|
||||
}
|
||||
err := ValidateLeadRotation("pve1", nodes)
|
||||
if err == nil {
|
||||
t.Fatal("ValidateLeadRotation(pve1): expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrProxmoxNotLead) {
|
||||
t.Errorf("err = %v, want ErrProxmoxNotLead", err)
|
||||
}
|
||||
if got := err.Error(); got != "Proxmox nodes cannot hold the cluster lead role (R-003)" {
|
||||
t.Errorf("err message = %q, want R-003 text verbatim", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_UnknownNode(t *testing.T) {
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "n1", Kind: NodeKindLinux},
|
||||
}
|
||||
err := ValidateLeadRotation("ghost", nodes)
|
||||
if err == nil {
|
||||
t.Fatal("ValidateLeadRotation(ghost): expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrNodeNotRegistered) {
|
||||
t.Errorf("err = %v, want ErrNodeNotRegistered", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_EmptyList(t *testing.T) {
|
||||
err := ValidateLeadRotation("anyone", nil)
|
||||
if err == nil {
|
||||
t.Fatal("ValidateLeadRotation on empty list: expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrNodeNotRegistered) {
|
||||
t.Errorf("err = %v, want ErrNodeNotRegistered", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_IneligibleKindRegistered(t *testing.T) {
|
||||
// A node registered with a kind that is neither linux nor
|
||||
// proxmox (e.g. the auto-registered "localhost" kind) is
|
||||
// rejected as ineligible, not as unregistered.
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "self", Kind: NodeKind("localhost")},
|
||||
}
|
||||
err := ValidateLeadRotation("self", nodes)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for localhost kind, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrProxmoxNotLead) {
|
||||
t.Errorf("err = %v, want wrapped ErrProxmoxNotLead (ineligible)", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLeadRotation_CaseSensitive(t *testing.T) {
|
||||
// Hostnames are case-normalized at the store layer; the rule
|
||||
// matches exactly. "N1" is NOT the same as "n1".
|
||||
nodes := []NodeInfo{
|
||||
{Hostname: "n1", Kind: NodeKindLinux},
|
||||
}
|
||||
if err := ValidateLeadRotation("N1", nodes); !errors.Is(err, ErrNodeNotRegistered) {
|
||||
t.Errorf("N1 (case mismatch): err = %v, want ErrNodeNotRegistered", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeKindString(t *testing.T) {
|
||||
if got := NodeKindLinux.String(); got != "linux" {
|
||||
t.Errorf("Linux.String() = %q", got)
|
||||
}
|
||||
if got := NodeKindProxmox.String(); got != "proxmox" {
|
||||
t.Errorf("Proxmox.String() = %q", got)
|
||||
}
|
||||
// Uppercase constructor should lower-case.
|
||||
if got := NodeKind("PROXMOX").String(); got != "proxmox" {
|
||||
t.Errorf("PROXMOX.String() = %q, want proxmox", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
"git.cloudinit.dev/coreci/orca/internal/storage"
|
||||
)
|
||||
|
||||
// SyncthingEmitter is the Layer-4 emitter for the per-namespace
|
||||
// Syncthing config files (REQ-081). For every volume in spec.Volumes
|
||||
// that carries a `replicate:` list, the emitter renders one Syncthing
|
||||
// `config.xml` at /etc/syncthing/orca-<ns>-<volume>.xml containing the
|
||||
// content-addressed folder (storage.FolderID), the device list (all
|
||||
// peers in the namespace), and the volume path.
|
||||
//
|
||||
// Syncthing configs are kind-agnostic — they apply to any workload
|
||||
// (Job, Service, DaemonSet) that declares a replicated volume. The
|
||||
// emitter is therefore registered on the Registry under every
|
||||
// kind:runtime key the other emitters use, but it is intended to be
|
||||
// composed by the caller (the caller renders both the systemd unit and
|
||||
// the Syncthing config for the same spec). For the v0.9-P09 spike the
|
||||
// emitter is invoked directly; the composition lands in a later phase.
|
||||
//
|
||||
// The emitter is CLI-side only: it renders the XML; the SSH-push
|
||||
// transport SCPs the file to each peer; the Syncthing apt package on
|
||||
// the peer reads it. No Syncthing Go client is linked.
|
||||
type SyncthingEmitter struct{}
|
||||
|
||||
// syncthingConfigDir is the canonical directory for rendered Syncthing
|
||||
// configs on a peer (R-005). The emitter writes one file per
|
||||
// replicated volume.
|
||||
const syncthingConfigDir = "/etc/syncthing"
|
||||
|
||||
// localDeviceAddress is the address the local peer's own device entry
|
||||
// uses. "dynamic" tells Syncthing this peer is the listener (it does
|
||||
// not dial out to itself).
|
||||
const localDeviceAddress = "dynamic"
|
||||
|
||||
// peerDeviceAddressTemplate renders the Sync listen address for a
|
||||
// remote peer. The peer hostname (from the ReplicateTo list) is used
|
||||
// as the host; the default Sync port is 22000.
|
||||
const peerDeviceAddressTemplate = "tcp://%s:22000"
|
||||
|
||||
// Render renders one Syncthing config XML file per replicated volume
|
||||
// in the spec. A volume is "replicated" when its VolumeSpec carries a
|
||||
// non-empty `replicate:` list — encoded in VolumeSpec.Source as the
|
||||
// comma-separated peer list prefixed with `replicate:` (e.g.
|
||||
// `replicate:peer-b,peer-c`). This keeps the VolumeSpec shape stable
|
||||
// (the v0.9 VolumeSpec has no explicit Replicate field; the emitter
|
||||
// parses it from Source).
|
||||
//
|
||||
// For each replicated volume, the emitter:
|
||||
//
|
||||
// 1. Builds a VolumeReplication (namespace = spec.Name's namespace,
|
||||
// volume name, source path = VolumeSpec.Target).
|
||||
// 2. Builds the peer device list (the local peer + every peer in the
|
||||
// `replicate:` list). The local peer's device ID is derived
|
||||
// deterministically from the node hostname (the real device ID is
|
||||
// discovered from the peer registry in a later phase; for the
|
||||
// spike a deterministic placeholder keeps the rendered config
|
||||
// byte-stable).
|
||||
// 3. Calls storage.RenderSyncthingConfig + storage.RenderSyncthingXML
|
||||
// to produce the config file content.
|
||||
//
|
||||
// Returns an error if the spec is nil or the node is nil (the node is
|
||||
// required to identify the local peer). Workloads with no replicated
|
||||
// volumes return an empty (non-nil) slice — the emitter is a no-op for
|
||||
// them.
|
||||
func (SyncthingEmitter) Render(spec *jobspec.WorkloadSpec, node *Node) ([]File, error) {
|
||||
if spec == nil {
|
||||
return nil, errors.New("emitter/syncthing: spec is nil")
|
||||
}
|
||||
if node == nil {
|
||||
return nil, errors.New("emitter/syncthing: node is nil (local peer unknown)")
|
||||
}
|
||||
var files []File
|
||||
for _, vol := range spec.Volumes {
|
||||
peers, ok := parseReplicateList(vol.Source)
|
||||
if !ok || len(peers) == 0 {
|
||||
continue
|
||||
}
|
||||
path := vol.Target
|
||||
if strings.TrimSpace(path) == "" {
|
||||
path = vol.Source
|
||||
}
|
||||
rep := storage.VolumeReplication{
|
||||
Namespace: spec.Name,
|
||||
VolumeName: vol.Name,
|
||||
SourcePath: path,
|
||||
ReplicateTo: peers,
|
||||
SyncMode: "sendreceive",
|
||||
}
|
||||
devices := buildSyncthingDevices(node, peers)
|
||||
cfg, err := storage.RenderSyncthingConfig(rep, devices)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("emitter/syncthing: render config for volume %q: %w", vol.Name, err)
|
||||
}
|
||||
xml, err := storage.RenderSyncthingXML(cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("emitter/syncthing: render xml for volume %q: %w", vol.Name, err)
|
||||
}
|
||||
files = append(files, File{
|
||||
Path: fmt.Sprintf("%s/orca-%s-%s.xml", syncthingConfigDir, spec.Name, vol.Name),
|
||||
Content: xml,
|
||||
Mode: "0644",
|
||||
})
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// parseReplicateList extracts the peer list from a VolumeSpec.Source
|
||||
// value of the form `replicate:peer-b,peer-c`. Returns the peer list
|
||||
// and true when the Source carries a replicate directive; returns nil
|
||||
// and false otherwise (the volume is not replicated).
|
||||
func parseReplicateList(source string) ([]string, bool) {
|
||||
s := strings.TrimSpace(source)
|
||||
if !strings.HasPrefix(s, "replicate:") {
|
||||
return nil, false
|
||||
}
|
||||
rest := strings.TrimPrefix(s, "replicate:")
|
||||
parts := strings.Split(rest, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
if p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
// buildSyncthingDevices builds the Syncthing device list for the
|
||||
// rendered config. The local peer (the node the config is being
|
||||
// rendered for) is first, with the local device address ("dynamic").
|
||||
// Each remote peer in the replicate list follows, with a
|
||||
// tcp://<peer>:22000 address. Device IDs are deterministic placeholders
|
||||
// derived from the peer name (the real device IDs are discovered from
|
||||
// the peer registry in a later phase; the placeholder keeps the
|
||||
// rendered config byte-stable across re-runs).
|
||||
func buildSyncthingDevices(node *Node, peers []string) []storage.SyncthingDevice {
|
||||
devices := make([]storage.SyncthingDevice, 0, len(peers)+1)
|
||||
devices = append(devices, storage.SyncthingDevice{
|
||||
ID: syncthingDeviceID(node.Hostname),
|
||||
Name: node.Hostname,
|
||||
Address: localDeviceAddress,
|
||||
})
|
||||
for _, p := range peers {
|
||||
devices = append(devices, storage.SyncthingDevice{
|
||||
ID: syncthingDeviceID(p),
|
||||
Name: p,
|
||||
Address: fmt.Sprintf(peerDeviceAddressTemplate, p),
|
||||
})
|
||||
}
|
||||
return devices
|
||||
}
|
||||
|
||||
// syncthingDeviceID returns a deterministic, stable device-ID
|
||||
// placeholder for the given peer name. The placeholder is a fixed
|
||||
// 52-char string (Syncthing device IDs are 52-char base32) derived by
|
||||
// padding the peer name. The real device ID (discovered from the peer
|
||||
// registry / cluster/peers/) replaces this in a later phase; for the
|
||||
// v0.9 spike the placeholder keeps the rendered config byte-stable so
|
||||
// the SSH-push idempotency check works.
|
||||
func syncthingDeviceID(peerName string) string {
|
||||
const idLen = 52
|
||||
name := strings.TrimSpace(peerName)
|
||||
if len(name) >= idLen {
|
||||
return name[:idLen]
|
||||
}
|
||||
pad := strings.Repeat("X", idLen-len(name))
|
||||
return name + pad
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package emitter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/jobspec"
|
||||
)
|
||||
|
||||
func TestSyncthingEmitter_TwoReplicatedVolumes_TwoFiles(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "team-alpha",
|
||||
Volumes: []jobspec.VolumeSpec{
|
||||
{Name: "data", Source: "replicate:peer-b,peer-c", Target: "/var/lib/orca/data"},
|
||||
{Name: "logs", Source: "replicate:peer-b", Target: "/var/lib/orca/logs"},
|
||||
{Name: "cache", Source: "/local/cache", Target: "/cache"}, // not replicated
|
||||
},
|
||||
}
|
||||
node := &Node{Hostname: "peer-a", Runtime: []string{"process"}}
|
||||
files, err := (SyncthingEmitter{}).Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 2 {
|
||||
t.Fatalf("expected 2 files (only replicated volumes), got %d", len(files))
|
||||
}
|
||||
for _, f := range files {
|
||||
if !strings.HasPrefix(f.Path, "/etc/syncthing/orca-team-alpha-") {
|
||||
t.Errorf("path %q does not start with /etc/syncthing/orca-team-alpha-", f.Path)
|
||||
}
|
||||
if !strings.HasSuffix(f.Path, ".xml") {
|
||||
t.Errorf("path %q does not end with .xml", f.Path)
|
||||
}
|
||||
if f.Mode != "0644" {
|
||||
t.Errorf("mode = %q, want 0644", f.Mode)
|
||||
}
|
||||
if !strings.Contains(f.Content, "<configuration") {
|
||||
t.Errorf("content of %q is not syncthing config XML", f.Path)
|
||||
}
|
||||
}
|
||||
// File 1: data volume, replicated to peer-b and peer-c.
|
||||
if !strings.Contains(files[0].Path, "team-alpha-data") {
|
||||
t.Errorf("first file path = %q, want team-alpha-data", files[0].Path)
|
||||
}
|
||||
if !strings.Contains(files[0].Content, "peer-b") || !strings.Contains(files[0].Content, "peer-c") {
|
||||
t.Errorf("data volume config missing peer-b or peer-c")
|
||||
}
|
||||
// File 2: logs volume, replicated to peer-b only.
|
||||
if !strings.Contains(files[1].Path, "team-alpha-logs") {
|
||||
t.Errorf("second file path = %q, want team-alpha-logs", files[1].Path)
|
||||
}
|
||||
if !strings.Contains(files[1].Content, "peer-b") {
|
||||
t.Errorf("logs volume config missing peer-b")
|
||||
}
|
||||
if strings.Contains(files[1].Content, "tcp://peer-c") {
|
||||
t.Errorf("logs volume config should not contain peer-c")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingEmitter_NoReplicatedVolumes_Empty(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Job",
|
||||
Name: "batch",
|
||||
Volumes: []jobspec.VolumeSpec{
|
||||
{Name: "cache", Source: "/local/cache", Target: "/cache"},
|
||||
},
|
||||
}
|
||||
node := &Node{Hostname: "peer-a"}
|
||||
files, err := (SyncthingEmitter{}).Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 0 {
|
||||
t.Errorf("expected 0 files for non-replicated volumes, got %d", len(files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingEmitter_NoVolumes_Empty(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "batch"}
|
||||
node := &Node{Hostname: "peer-a"}
|
||||
files, err := (SyncthingEmitter{}).Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 0 {
|
||||
t.Errorf("expected 0 files, got %d", len(files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingEmitter_NilSpec_Error(t *testing.T) {
|
||||
node := &Node{Hostname: "peer-a"}
|
||||
if _, err := (SyncthingEmitter{}).Render(nil, node); err == nil {
|
||||
t.Error("nil spec: expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingEmitter_NilNode_Error(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "x"}
|
||||
if _, err := (SyncthingEmitter{}).Render(spec, nil); err == nil {
|
||||
t.Error("nil node: expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingEmitter_LocalDeviceFirst(t *testing.T) {
|
||||
spec := &jobspec.WorkloadSpec{
|
||||
Kind: "Service",
|
||||
Name: "ns",
|
||||
Volumes: []jobspec.VolumeSpec{{Name: "data", Source: "replicate:peer-b", Target: "/data"}},
|
||||
}
|
||||
node := &Node{Hostname: "peer-a"}
|
||||
files, err := (SyncthingEmitter{}).Render(spec, node)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
if len(files) != 1 {
|
||||
t.Fatalf("expected 1 file, got %d", len(files))
|
||||
}
|
||||
// Local peer address is "dynamic"; remote peer uses tcp://...
|
||||
if !strings.Contains(files[0].Content, "dynamic") {
|
||||
t.Errorf("local device address (dynamic) missing from config")
|
||||
}
|
||||
if !strings.Contains(files[0].Content, "tcp://peer-b:22000") {
|
||||
t.Errorf("remote peer address missing from config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReplicateList_OK(t *testing.T) {
|
||||
peers, ok := parseReplicateList("replicate:peer-b,peer-c,peer-d")
|
||||
if !ok {
|
||||
t.Fatal("expected ok")
|
||||
}
|
||||
if len(peers) != 3 || peers[0] != "peer-b" || peers[1] != "peer-c" || peers[2] != "peer-d" {
|
||||
t.Errorf("peers = %v, want [peer-b peer-c peer-d]", peers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReplicateList_NotReplicated(t *testing.T) {
|
||||
_, ok := parseReplicateList("/local/path")
|
||||
if ok {
|
||||
t.Error("non-replicate source should return ok=false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReplicateList_EmptyPeerList(t *testing.T) {
|
||||
peers, ok := parseReplicateList("replicate:")
|
||||
if !ok {
|
||||
t.Error("replicate: prefix should return ok=true")
|
||||
}
|
||||
if len(peers) != 0 {
|
||||
t.Errorf("peers = %v, want empty", peers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingDeviceID_Stable(t *testing.T) {
|
||||
a := syncthingDeviceID("peer-a")
|
||||
b := syncthingDeviceID("peer-a")
|
||||
if a != b {
|
||||
t.Errorf("syncthingDeviceID not stable: %q vs %q", a, b)
|
||||
}
|
||||
if len(a) != 52 {
|
||||
t.Errorf("device ID length = %d, want 52", len(a))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncthingDeviceID_DifferentPeers(t *testing.T) {
|
||||
a := syncthingDeviceID("peer-a")
|
||||
b := syncthingDeviceID("peer-b")
|
||||
if a == b {
|
||||
t.Errorf("different peers produced same device ID")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
// Package stepca wraps the smallstep `step` CLI for the Orca cluster
|
||||
// CA (REQ-076, D-101 reversing AD-010). The CLI holds the cluster CA's
|
||||
// private key on the lead node and invokes `step ca init`,
|
||||
// `step ca certificate`, and `step ca renew` over SSH on the lead via
|
||||
// the sshpush transport. There is intentionally no Go step-ca client
|
||||
// library — the zero-new-dependency posture is preserved.
|
||||
//
|
||||
// Cert lifetimes follow the SPIFFE/SVID convention: server certs are
|
||||
// 90-day (2160h) and SVIDs are 24h, matching the v0.9 PRD workload
|
||||
// identity model (D-068). Renewal happens 30 days before expiry.
|
||||
package stepca
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/paths"
|
||||
"git.cloudinit.dev/coreci/orca/internal/sshpush"
|
||||
)
|
||||
|
||||
// Sentinel errors.
|
||||
var (
|
||||
// ErrLeadUnset is returned when the client has no lead peer
|
||||
// configured (e.g., NewClient was given an empty leadPeer).
|
||||
ErrLeadUnset = errors.New("stepca: lead peer not set")
|
||||
// ErrStepCLI is wrapped around any non-zero exit from the step CLI.
|
||||
ErrStepCLI = errors.New("stepca: step CLI failed")
|
||||
)
|
||||
|
||||
// Cert lifetimes (D-068, REQ-076).
|
||||
const (
|
||||
// ServerCertNotAfter is the not-after for peer server certs: 90 days.
|
||||
ServerCertNotAfter = "2160h"
|
||||
// SVIDNotAfter is the not-after for workload SVIDs: 24 hours.
|
||||
SVIDNotAfter = "24h"
|
||||
// DefaultProvisioner is the JWE provisioner name the CLI mints
|
||||
// tokens against on the lead.
|
||||
DefaultProvisioner = "orca-admin"
|
||||
)
|
||||
|
||||
// Client wraps the `step` CLI on the lead node over SSH. The zero
|
||||
// value is NOT usable; construct one with NewClient.
|
||||
type Client struct {
|
||||
transport *sshpush.Transport
|
||||
leadPeer string
|
||||
// exec is the command-execution seam. It defaults to transport
|
||||
// when nil (set by NewClient) and is overridden by tests in this
|
||||
// package to inject a mock without a real SSH server.
|
||||
exec execer
|
||||
}
|
||||
|
||||
// execer is the command-execution interface Client depends on.
|
||||
// *sshpush.Transport satisfies it via its Exec method. Kept
|
||||
// unexported so the public API stays keyed to the concrete transport
|
||||
// (callers pass *sshpush.Transport to NewClient).
|
||||
type execer interface {
|
||||
Exec(ctx context.Context, peer string, cmd string) ([]byte, error)
|
||||
}
|
||||
|
||||
// NewClient returns a Client that invokes the step CLI on leadPeer
|
||||
// (host:port) via transport. A nil transport is rejected at the first
|
||||
// call site; an empty leadPeer makes every call return ErrLeadUnset.
|
||||
func NewClient(transport *sshpush.Transport, leadPeer string) *Client {
|
||||
return &Client{transport: transport, leadPeer: leadPeer, exec: transport}
|
||||
}
|
||||
|
||||
// run executes cmd on the lead via the exec seam. It is the single
|
||||
// chokepoint every public method funnels through, so tests intercept
|
||||
// here.
|
||||
func (c *Client) run(ctx context.Context, cmd string) ([]byte, error) {
|
||||
return c.exec.Exec(ctx, c.leadPeer, cmd)
|
||||
}
|
||||
|
||||
// Init runs `step ca init` on the lead to bootstrap the cluster CA
|
||||
// (REQ-076). The root cert is expected to land at the location given
|
||||
// by paths.CACertPath() (the v0.9 cluster/ca.crt location). After the
|
||||
// init completes, Init copies the root CA cert back to the operator
|
||||
// host so the CLI can present it to workloads and peers.
|
||||
func (c *Client) Init(ctx context.Context, name string, dns string, address string) error {
|
||||
if err := c.preflight(); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := fmt.Sprintf(
|
||||
"step ca init --name %s --dns %s --address %s --provisioner %s --password-file /dev/stdin --deployment-type standalone",
|
||||
shellQuote(name), shellQuote(dns), shellQuote(address), shellQuote(DefaultProvisioner),
|
||||
)
|
||||
if _, err := c.run(ctx, cmd); err != nil {
|
||||
return fmt.Errorf("stepca: init: %w", err)
|
||||
}
|
||||
// Mirror the root CA cert to the operator-side paths.CACertPath()
|
||||
// so the CLI can hand it out to peers and workloads without a
|
||||
// second round-trip. The lead writes it to the canonical step-ca
|
||||
// location; we cat it back over SSH.
|
||||
remote := "/etc/step-ca/certs/root_ca.crt"
|
||||
out, err := c.run(ctx, fmt.Sprintf("cat %s", shellQuote(remote)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("stepca: read root ca: %w", err)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return fmt.Errorf("stepca: init produced empty root ca at %s: %w", remote, ErrStepCLI)
|
||||
}
|
||||
local := paths.CACertPath()
|
||||
if mkErr := os.MkdirAll(filepath.Dir(local), 0o755); mkErr != nil {
|
||||
return fmt.Errorf("stepca: mkdir %s: %w", filepath.Dir(local), mkErr)
|
||||
}
|
||||
if wErr := os.WriteFile(local, out, 0o644); wErr != nil {
|
||||
return fmt.Errorf("stepca: write %s: %w", local, wErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IssueServerCert issues a 90-day server cert for peer on the lead via
|
||||
// `step ca certificate`. The cert and key PEM are returned to the
|
||||
// caller; the lead-side temp files are unlinked after the read.
|
||||
// sans are appended as `--san` flags (one per SAN), with peer itself
|
||||
// always added as the first SAN so the cert is valid for the bare
|
||||
// hostname.
|
||||
func (c *Client) IssueServerCert(ctx context.Context, peer string, sans []string) (cert string, key string, err error) {
|
||||
if perr := c.preflight(); perr != nil {
|
||||
return "", "", perr
|
||||
}
|
||||
return c.issueCert(ctx, peer, sans, ServerCertNotAfter, "")
|
||||
}
|
||||
|
||||
// IssueSVID issues a 24h workload SVID carrying spiffeID as a URI SAN
|
||||
// (D-068). The provisioner is pinned to DefaultProvisioner so the
|
||||
// CLI-side token minting path is exercised consistently.
|
||||
func (c *Client) IssueSVID(ctx context.Context, spiffeID string, sans []string) (cert string, key string, err error) {
|
||||
if perr := c.preflight(); perr != nil {
|
||||
return "", "", perr
|
||||
}
|
||||
return c.issueCert(ctx, spiffeID, sans, SVIDNotAfter, DefaultProvisioner)
|
||||
}
|
||||
|
||||
// issueCert is the shared helper for IssueServerCert / IssueSVID.
|
||||
// subject is the cert subject CN (and the first --san). notAfter is
|
||||
// the duration string passed verbatim to `--not-after`. provisioner,
|
||||
// when non-empty, is passed as `--provisioner`.
|
||||
func (c *Client) issueCert(ctx context.Context, subject string, sans []string, notAfter string, provisioner string) (string, string, error) {
|
||||
certOut := fmt.Sprintf("/tmp/orca-%s.crt", sanitize(subject))
|
||||
keyOut := fmt.Sprintf("/tmp/orca-%s.key", sanitize(subject))
|
||||
var sb strings.Builder
|
||||
sb.WriteString("step ca certificate ")
|
||||
sb.WriteString(shellQuote(subject))
|
||||
sb.WriteString(" ")
|
||||
sb.WriteString(shellQuote(certOut))
|
||||
sb.WriteString(" ")
|
||||
sb.WriteString(shellQuote(keyOut))
|
||||
sb.WriteString(" --not-after ")
|
||||
sb.WriteString(shellQuote(notAfter))
|
||||
sb.WriteString(" --san ")
|
||||
sb.WriteString(shellQuote(subject))
|
||||
for _, s := range sans {
|
||||
sb.WriteString(" --san ")
|
||||
sb.WriteString(shellQuote(s))
|
||||
}
|
||||
if provisioner != "" {
|
||||
sb.WriteString(" --provisioner ")
|
||||
sb.WriteString(shellQuote(provisioner))
|
||||
}
|
||||
sb.WriteString(" --password-file /dev/stdin --force")
|
||||
cmd := sb.String()
|
||||
if _, err := c.run(ctx, cmd); err != nil {
|
||||
return "", "", fmt.Errorf("stepca: issue %s: %w", subject, err)
|
||||
}
|
||||
certPEM, err := c.readFile(ctx, certOut)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
keyPEM, err := c.readFile(ctx, keyOut)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
// Best-effort cleanup; failure to unlink is non-fatal.
|
||||
_, _ = c.run(ctx, fmt.Sprintf("rm -f %s %s", shellQuote(certOut), shellQuote(keyOut)))
|
||||
return certPEM, keyPEM, nil
|
||||
}
|
||||
|
||||
// RenewServerCert renews a peer's server cert 30 days before expiry
|
||||
// (REQ-076). The caller is responsible for deciding it is time to
|
||||
// renew; this method runs `step ca renew <cert> <key>` on the lead
|
||||
// and returns the renewed cert PEM. The key is unchanged by step-ca
|
||||
// renew for RSA/ECDSA keys; for Ed25519 the key is rotated and the
|
||||
// new key is returned alongside.
|
||||
func (c *Client) RenewServerCert(ctx context.Context, peer string) error {
|
||||
if perr := c.preflight(); perr != nil {
|
||||
return perr
|
||||
}
|
||||
certPath := fmt.Sprintf("/tmp/orca-%s.crt", sanitize(peer))
|
||||
keyPath := fmt.Sprintf("/tmp/orca-%s.key", sanitize(peer))
|
||||
cmd := fmt.Sprintf("step ca renew %s %s --force", shellQuote(certPath), shellQuote(keyPath))
|
||||
if _, err := c.run(ctx, cmd); err != nil {
|
||||
return fmt.Errorf("stepca: renew %s: %w", peer, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fingerprint returns the SHA-256 fingerprint of the cluster root CA
|
||||
// (paths.CACertPath on the lead, mirrored locally by Init). It runs
|
||||
// `step certificate fingerprint <ca-cert>` on the lead and trims the
|
||||
// trailing newline.
|
||||
func (c *Client) Fingerprint(ctx context.Context) (string, error) {
|
||||
if perr := c.preflight(); perr != nil {
|
||||
return "", perr
|
||||
}
|
||||
remote := "/etc/step-ca/certs/root_ca.crt"
|
||||
cmd := fmt.Sprintf("step certificate fingerprint %s", shellQuote(remote))
|
||||
out, err := c.run(ctx, cmd)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("stepca: fingerprint: %w", err)
|
||||
}
|
||||
fp := strings.TrimSpace(string(out))
|
||||
if fp == "" {
|
||||
return "", fmt.Errorf("stepca: empty fingerprint: %w", ErrStepCLI)
|
||||
}
|
||||
return fp, nil
|
||||
}
|
||||
|
||||
// preflight validates the client is usable.
|
||||
func (c *Client) preflight() error {
|
||||
if c.exec == nil {
|
||||
return errors.New("stepca: transport is nil")
|
||||
}
|
||||
if c.leadPeer == "" {
|
||||
return ErrLeadUnset
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readFile cats a lead-side file and returns its contents as a string.
|
||||
func (c *Client) readFile(ctx context.Context, path string) (string, error) {
|
||||
out, err := c.run(ctx, fmt.Sprintf("cat %s", shellQuote(path)))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("stepca: read %s: %w", path, err)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return "", fmt.Errorf("stepca: empty file %s: %w", path, ErrStepCLI)
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// sanitize replaces path-unsafe characters in a subject so it can be
|
||||
// used in a /tmp filename. SPIFFE IDs contain `://` and `/`, both of
|
||||
// which would confuse the shell. We collapse to `_`.
|
||||
func sanitize(s string) string {
|
||||
r := strings.NewReplacer("://", "-", "/", "_", ":", "_", " ", "_")
|
||||
return r.Replace(s)
|
||||
}
|
||||
|
||||
// shellQuote single-quotes a string for safe shell interpolation. It
|
||||
// escapes embedded single-quotes via the standard '\” idiom (mirrors
|
||||
// sshpush.shellQuote, kept local to avoid importing an unexported
|
||||
// helper).
|
||||
func shellQuote(s string) string {
|
||||
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
package stepca
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/paths"
|
||||
"git.cloudinit.dev/coreci/orca/internal/sshpush"
|
||||
)
|
||||
|
||||
// mockExec is a record-and-replay execer for the stepca.Client. It
|
||||
// stores every command it received keyed by a substring match, so a
|
||||
// test can assert "Init ran `step ca init`" without coupling to
|
||||
// exact-flag ordering. Each entry maps a substring the test expects
|
||||
// to appear in the command to the output that should be returned.
|
||||
type mockExec struct {
|
||||
// responses is a list of (substring, output, err). The first
|
||||
// matching entry wins; an entry with an empty substring matches
|
||||
// any command (catch-all).
|
||||
responses []mockResp
|
||||
// calls records every command the client issued, in order.
|
||||
calls []string
|
||||
}
|
||||
|
||||
type mockResp struct {
|
||||
match string
|
||||
out []byte
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *mockExec) Exec(ctx context.Context, peer string, cmd string) ([]byte, error) {
|
||||
m.calls = append(m.calls, cmd)
|
||||
for _, r := range m.responses {
|
||||
if r.match == "" || strings.Contains(cmd, r.match) {
|
||||
return r.out, r.err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// newMockClient returns a Client wired to a mockExec and an ORCA_HOME
|
||||
// under a temp dir (so paths.CACertPath() resolves to a writable path
|
||||
// during Init tests).
|
||||
func newMockClient(t *testing.T, lead string) (*Client, *mockExec) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
t.Setenv("ORCA_HOME", dir)
|
||||
mx := &mockExec{}
|
||||
c := NewClient(nil, lead)
|
||||
c.exec = mx
|
||||
return c, mx
|
||||
}
|
||||
|
||||
func containsCall(t *testing.T, mx *mockExec, want string) {
|
||||
t.Helper()
|
||||
for _, c := range mx.calls {
|
||||
if strings.Contains(c, want) {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("no exec call contained %q; calls were:\n%s", want, strings.Join(mx.calls, "\n"))
|
||||
}
|
||||
|
||||
func TestNewClient_Defaults(t *testing.T) {
|
||||
tr := sshpush.NewTransport("/tmp/key", "/tmp/kh")
|
||||
c := NewClient(tr, "lead:22")
|
||||
if c.leadPeer != "lead:22" {
|
||||
t.Errorf("leadPeer = %q", c.leadPeer)
|
||||
}
|
||||
if c.transport != tr {
|
||||
t.Error("transport not stored")
|
||||
}
|
||||
if c.exec == nil {
|
||||
t.Error("exec seam is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_Preflight_LeadUnset(t *testing.T) {
|
||||
c, _ := newMockClient(t, "")
|
||||
if err := c.Init(context.Background(), "n", "d", "a"); !errors.Is(err, ErrLeadUnset) {
|
||||
t.Errorf("Init with empty lead: err = %v, want ErrLeadUnset", err)
|
||||
}
|
||||
if _, _, err := c.IssueServerCert(context.Background(), "p", nil); !errors.Is(err, ErrLeadUnset) {
|
||||
t.Errorf("IssueServerCert: err = %v, want ErrLeadUnset", err)
|
||||
}
|
||||
if _, _, err := c.IssueSVID(context.Background(), "spiffe://orca/x", nil); !errors.Is(err, ErrLeadUnset) {
|
||||
t.Errorf("IssueSVID: err = %v, want ErrLeadUnset", err)
|
||||
}
|
||||
if err := c.RenewServerCert(context.Background(), "p"); !errors.Is(err, ErrLeadUnset) {
|
||||
t.Errorf("RenewServerCert: err = %v, want ErrLeadUnset", err)
|
||||
}
|
||||
if _, err := c.Fingerprint(context.Background()); !errors.Is(err, ErrLeadUnset) {
|
||||
t.Errorf("Fingerprint: err = %v, want ErrLeadUnset", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_Preflight_NilExec(t *testing.T) {
|
||||
c := &Client{leadPeer: "lead:22"} // exec is nil
|
||||
if err := c.Init(context.Background(), "n", "d", "a"); err == nil {
|
||||
t.Fatal("Init with nil exec: expected error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_Success(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
caPEM := []byte("-----BEGIN CERTIFICATE-----\nFAKE\n-----END CERTIFICATE-----\n")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca init", out: nil, err: nil},
|
||||
{match: "cat '/etc/step-ca/certs/root_ca.crt'", out: caPEM, err: nil},
|
||||
}
|
||||
if err := c.Init(context.Background(), "orca", "ca.orca.local", ":8443"); err != nil {
|
||||
t.Fatalf("Init: %v", err)
|
||||
}
|
||||
containsCall(t, mx, "step ca init --name 'orca'")
|
||||
containsCall(t, mx, "--dns 'ca.orca.local'")
|
||||
containsCall(t, mx, "--address ':8443'")
|
||||
containsCall(t, mx, "--provisioner 'orca-admin'")
|
||||
containsCall(t, mx, "--deployment-type standalone")
|
||||
// Root CA mirrored to paths.CACertPath().
|
||||
got, err := os.ReadFile(paths.CACertPath())
|
||||
if err != nil {
|
||||
t.Fatalf("read mirrored CA: %v", err)
|
||||
}
|
||||
if string(got) != string(caPEM) {
|
||||
t.Errorf("mirrored CA = %q, want %q", got, caPEM)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_StepCLIFails(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
stepErr := errors.New("step: non-zero exit 1")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca init", out: nil, err: stepErr},
|
||||
}
|
||||
err := c.Init(context.Background(), "orca", "ca.orca.local", ":8443")
|
||||
if err == nil {
|
||||
t.Fatal("Init: expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "stepca: init") {
|
||||
t.Errorf("err = %v, want wrapped 'stepca: init'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_EmptyRootCA(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca init", out: nil, err: nil},
|
||||
{match: "cat '/etc/step-ca/certs/root_ca.crt'", out: nil, err: nil},
|
||||
}
|
||||
err := c.Init(context.Background(), "orca", "ca.orca.local", ":8443")
|
||||
if err == nil {
|
||||
t.Fatal("Init with empty root CA: expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrStepCLI) {
|
||||
t.Errorf("err = %v, want ErrStepCLI", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueServerCert_Success(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
certPEM := []byte("SERVER-CERT-PEM")
|
||||
keyPEM := []byte("SERVER-KEY-PEM")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca certificate", out: nil, err: nil},
|
||||
{match: "cat '/tmp/orca-peer1.crt'", out: certPEM, err: nil},
|
||||
{match: "cat '/tmp/orca-peer1.key'", out: keyPEM, err: nil},
|
||||
{match: "rm -f", out: nil, err: nil},
|
||||
}
|
||||
gotCert, gotKey, err := c.IssueServerCert(context.Background(), "peer1", []string{"peer1.orca.local", "10.0.0.1"})
|
||||
if err != nil {
|
||||
t.Fatalf("IssueServerCert: %v", err)
|
||||
}
|
||||
if gotCert != string(certPEM) {
|
||||
t.Errorf("cert = %q", gotCert)
|
||||
}
|
||||
if gotKey != string(keyPEM) {
|
||||
t.Errorf("key = %q", gotKey)
|
||||
}
|
||||
containsCall(t, mx, "step ca certificate 'peer1'")
|
||||
containsCall(t, mx, "--not-after '2160h'")
|
||||
containsCall(t, mx, "--san 'peer1.orca.local'")
|
||||
containsCall(t, mx, "--san '10.0.0.1'")
|
||||
// Server cert path must NOT pin a provisioner (uses default).
|
||||
for _, call := range mx.calls {
|
||||
if strings.HasPrefix(call, "step ca certificate") && strings.Contains(call, "--provisioner") {
|
||||
t.Errorf("server cert should not pin provisioner; cmd: %s", call)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueSVID_Success(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
spiffe := "spiffe://orca/ns/_defaults/job/web/alloc/0"
|
||||
certPEM := []byte("SVID-CERT-PEM")
|
||||
keyPEM := []byte("SVID-KEY-PEM")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca certificate", out: nil, err: nil},
|
||||
{match: "cat '/tmp/orca-spiffe-orca_ns__defaults_job_web_alloc_0.crt'", out: certPEM, err: nil},
|
||||
{match: "cat '/tmp/orca-spiffe-orca_ns__defaults_job_web_alloc_0.key'", out: keyPEM, err: nil},
|
||||
{match: "rm -f", out: nil, err: nil},
|
||||
}
|
||||
gotCert, gotKey, err := c.IssueSVID(context.Background(), spiffe, []string{"web.orca.local"})
|
||||
if err != nil {
|
||||
t.Fatalf("IssueSVID: %v", err)
|
||||
}
|
||||
if gotCert != string(certPEM) || gotKey != string(keyPEM) {
|
||||
t.Errorf("cert/key mismatch")
|
||||
}
|
||||
containsCall(t, mx, "step ca certificate")
|
||||
containsCall(t, mx, "--not-after '24h'")
|
||||
containsCall(t, mx, "--provisioner 'orca-admin'")
|
||||
// SPIFFE ID is both the subject AND a SAN.
|
||||
containsCall(t, mx, "--san '"+spiffe+"'")
|
||||
}
|
||||
|
||||
func TestIssueServerCert_StepFails(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca certificate", out: nil, err: errors.New("step: exit 1")},
|
||||
}
|
||||
_, _, err := c.IssueServerCert(context.Background(), "peer1", nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "stepca: issue") {
|
||||
t.Errorf("err = %v, want wrapped 'stepca: issue'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueServerCert_ReadCertFails(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca certificate", out: nil, err: nil},
|
||||
{match: "cat '/tmp/orca-peer1.crt'", out: nil, err: errors.New("ssh: cat failed")},
|
||||
{match: "cat '/tmp/orca-peer1.key'", out: nil, err: nil},
|
||||
}
|
||||
_, _, err := c.IssueServerCert(context.Background(), "peer1", nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "read") {
|
||||
t.Errorf("err = %v, want wrapped 'read'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueServerCert_EmptyCert(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca certificate", out: nil, err: nil},
|
||||
{match: "cat '/tmp/orca-peer1.crt'", out: nil, err: nil},
|
||||
{match: "cat '/tmp/orca-peer1.key'", out: []byte("KEY"), err: nil},
|
||||
{match: "rm -f", out: nil, err: nil},
|
||||
}
|
||||
_, _, err := c.IssueServerCert(context.Background(), "peer1", nil)
|
||||
if err == nil || !errors.Is(err, ErrStepCLI) {
|
||||
t.Errorf("err = %v, want ErrStepCLI", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewServerCert_Success(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca renew", out: nil, err: nil},
|
||||
}
|
||||
if err := c.RenewServerCert(context.Background(), "peer1"); err != nil {
|
||||
t.Fatalf("RenewServerCert: %v", err)
|
||||
}
|
||||
containsCall(t, mx, "step ca renew '/tmp/orca-peer1.crt' '/tmp/orca-peer1.key' --force")
|
||||
}
|
||||
|
||||
func TestRenewServerCert_Fails(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca renew", out: nil, err: errors.New("step: renew failed")},
|
||||
}
|
||||
err := c.RenewServerCert(context.Background(), "peer1")
|
||||
if err == nil || !strings.Contains(err.Error(), "stepca: renew") {
|
||||
t.Errorf("err = %v, want wrapped 'stepca: renew'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprint_Success(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step certificate fingerprint", out: []byte("a1b2c3d4e5f6\n"), err: nil},
|
||||
}
|
||||
fp, err := c.Fingerprint(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Fingerprint: %v", err)
|
||||
}
|
||||
if fp != "a1b2c3d4e5f6" {
|
||||
t.Errorf("fp = %q, want a1b2c3d4e5f6 (trimmed)", fp)
|
||||
}
|
||||
containsCall(t, mx, "step certificate fingerprint '/etc/step-ca/certs/root_ca.crt'")
|
||||
}
|
||||
|
||||
func TestFingerprint_Empty(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step certificate fingerprint", out: []byte(""), err: nil},
|
||||
}
|
||||
_, err := c.Fingerprint(context.Background())
|
||||
if err == nil || !errors.Is(err, ErrStepCLI) {
|
||||
t.Errorf("err = %v, want ErrStepCLI", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFingerprint_Fails(t *testing.T) {
|
||||
c, mx := newMockClient(t, "lead:22")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step certificate fingerprint", out: nil, err: errors.New("ssh: exec failed")},
|
||||
}
|
||||
_, err := c.Fingerprint(context.Background())
|
||||
if err == nil || !strings.Contains(err.Error(), "stepca: fingerprint") {
|
||||
t.Errorf("err = %v, want wrapped 'stepca: fingerprint'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInit_MkdirFails(t *testing.T) {
|
||||
// Point ORCA_HOME at a path that cannot be created under to
|
||||
// force MkdirAll failure. We use a file as the parent.
|
||||
dir := t.TempDir()
|
||||
blocker := filepath.Join(dir, "block")
|
||||
if err := os.WriteFile(blocker, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write blocker: %v", err)
|
||||
}
|
||||
t.Setenv("ORCA_HOME", filepath.Join(blocker, "sub"))
|
||||
// Construct the client directly (not newMockClient, which
|
||||
// resets ORCA_HOME to a fresh temp dir).
|
||||
mx := &mockExec{}
|
||||
caPEM := []byte("FAKE")
|
||||
mx.responses = []mockResp{
|
||||
{match: "step ca init", out: nil, err: nil},
|
||||
{match: "cat '/etc/step-ca/certs/root_ca.crt'", out: caPEM, err: nil},
|
||||
}
|
||||
c := NewClient(nil, "lead:22")
|
||||
c.exec = mx
|
||||
err := c.Init(context.Background(), "orca", "ca.orca.local", ":8443")
|
||||
if err == nil {
|
||||
t.Fatal("Init: expected mkdir error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "mkdir") {
|
||||
t.Errorf("err = %v, want 'mkdir'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellQuote(t *testing.T) {
|
||||
got := shellQuote("a'b")
|
||||
want := "'a'\\''b'"
|
||||
if got != want {
|
||||
t.Errorf("shellQuote = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitize(t *testing.T) {
|
||||
cases := []struct{ in, want string }{
|
||||
{"spiffe://orca/ns/_defaults/job/web/alloc/0",
|
||||
"spiffe-orca_ns__defaults_job_web_alloc_0"},
|
||||
{"plain-host", "plain-host"},
|
||||
{"a b", "a_b"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := sanitize(tc.in); got != tc.want {
|
||||
t.Errorf("sanitize(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestForcedDivergence_ConflictDetectedAndResolved is the gate C-14
|
||||
// forced-divergence integration test. It simulates two peers writing to
|
||||
// the same file *without* the flock (the lock was bypassed), detects the
|
||||
// conflict via DetectConflicts, resolves it via ResolveConflict to the
|
||||
// source peer's content, and verifies the resolution is deterministic
|
||||
// across repeated runs.
|
||||
//
|
||||
// The test uses in-memory file maps (no real Syncthing is involved — it
|
||||
// tests the CLI-side conflict detection + resolution logic, which is
|
||||
// what orca runs on the lead node over the SSH-gathered peer views).
|
||||
func TestForcedDivergence_ConflictDetectedAndResolved(t *testing.T) {
|
||||
// Two peers, same file, different content — no lock held.
|
||||
peerA := []byte("source-writes-this")
|
||||
peerB := []byte("peer-b-writes-that")
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"data/db.sqlite": peerA},
|
||||
"peer-b": {"data/db.sqlite": peerB},
|
||||
}
|
||||
const sourcePeer = "peer-a"
|
||||
const ns = "divergence-ns"
|
||||
|
||||
// Detect.
|
||||
conflicts, err := DetectConflicts(ns, peerFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if len(conflicts) != 1 {
|
||||
t.Fatalf("expected 1 conflict, got %d", len(conflicts))
|
||||
}
|
||||
c := conflicts[0]
|
||||
if c.Path != "data/db.sqlite" {
|
||||
t.Errorf("conflict path = %q, want %q", c.Path, "data/db.sqlite")
|
||||
}
|
||||
if len(c.Versions) != 2 {
|
||||
t.Errorf("conflict has %d versions, want 2", len(c.Versions))
|
||||
}
|
||||
|
||||
// Resolve — source peer (the one holding the lock) wins.
|
||||
winner, losers := ResolveConflict(c, sourcePeer)
|
||||
if !bytes.Equal(winner, peerA) {
|
||||
t.Errorf("winning content = %q, want %q (source peer)", winner, peerA)
|
||||
}
|
||||
if len(losers) != 1 || losers[0] != "peer-b" {
|
||||
t.Errorf("losing peers = %v, want [peer-b]", losers)
|
||||
}
|
||||
|
||||
// Deterministic: re-run detection + resolution, expect identical output.
|
||||
conflicts2, _ := DetectConflicts(ns, peerFiles)
|
||||
winner2, losers2 := ResolveConflict(conflicts2[0], sourcePeer)
|
||||
if !bytes.Equal(winner2, winner) {
|
||||
t.Errorf("non-deterministic winner: %q vs %q", winner2, winner)
|
||||
}
|
||||
if !equalStringSlices(losers2, losers) {
|
||||
t.Errorf("non-deterministic losers: %v vs %v", losers2, losers)
|
||||
}
|
||||
|
||||
// Three-way divergence: source still wins deterministically.
|
||||
peerFiles3 := map[string]map[string][]byte{
|
||||
"peer-a": {"data/db.sqlite": peerA},
|
||||
"peer-b": {"data/db.sqlite": peerB},
|
||||
"peer-c": {"data/db.sqlite": []byte("peer-c-writes-something-else")},
|
||||
}
|
||||
conflicts3, _ := DetectConflicts(ns, peerFiles3)
|
||||
if len(conflicts3) != 1 {
|
||||
t.Fatalf("3-way: expected 1 conflict, got %d", len(conflicts3))
|
||||
}
|
||||
winner3, losers3 := ResolveConflict(conflicts3[0], sourcePeer)
|
||||
if !bytes.Equal(winner3, peerA) {
|
||||
t.Errorf("3-way winner = %q, want %q", winner3, peerA)
|
||||
}
|
||||
if len(losers3) != 2 {
|
||||
t.Errorf("3-way losers = %v, want 2 entries", losers3)
|
||||
}
|
||||
// Losers must be sorted for deterministic ordering.
|
||||
if !isSorted(losers3) {
|
||||
t.Errorf("losers not sorted: %v", losers3)
|
||||
}
|
||||
}
|
||||
|
||||
// TestForcedDivergence_UnknownSource_Unresolved verifies the
|
||||
// deterministic failure mode: when the source peer is unknown (the lock
|
||||
// holder is not in the versions map), ResolveConflict returns (nil, nil)
|
||||
// so the CLI can flag the conflict for manual resolution. No silent
|
||||
// winner is picked.
|
||||
func TestForcedDivergence_UnknownSource_Unresolved(t *testing.T) {
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"f": []byte("a")},
|
||||
"peer-b": {"f": []byte("b")},
|
||||
}
|
||||
conflicts, _ := DetectConflicts("ns", peerFiles)
|
||||
if len(conflicts) != 1 {
|
||||
t.Fatalf("expected 1 conflict, got %d", len(conflicts))
|
||||
}
|
||||
// Source peer is "peer-z" — not in the versions map.
|
||||
winner, losers := ResolveConflict(conflicts[0], "peer-z")
|
||||
if winner != nil {
|
||||
t.Errorf("unknown source: winner = %v, want nil", winner)
|
||||
}
|
||||
if losers != nil {
|
||||
t.Errorf("unknown source: losers = %v, want nil", losers)
|
||||
}
|
||||
}
|
||||
|
||||
// TestForcedDivergence_NoLockNoConflict simulates the normal path: two
|
||||
// peers hold the same content for the same file (one wrote under the
|
||||
// lock, the other synced read-only). DetectConflicts reports no
|
||||
// conflict.
|
||||
func TestForcedDivergence_NoLockNoConflict(t *testing.T) {
|
||||
content := []byte("same-content")
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"data/f": content},
|
||||
"peer-b": {"data/f": content},
|
||||
}
|
||||
conflicts, err := DetectConflicts("ns", peerFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if len(conflicts) != 0 {
|
||||
t.Errorf("expected 0 conflicts, got %d: %+v", len(conflicts), conflicts)
|
||||
}
|
||||
}
|
||||
|
||||
// equalStringSlices reports whether two string slices are equal in order.
|
||||
func equalStringSlices(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// isSorted reports whether the string slice is in ascending order.
|
||||
func isSorted(s []string) bool {
|
||||
for i := 1; i < len(s); i++ {
|
||||
if s[i-1] > s[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
// Package storage implements the CLI-side Syncthing replication logic
|
||||
// for per-namespace storage replication (REQ-081, R-005). It renders the
|
||||
// Syncthing config (XML) for each peer's per-namespace instance and
|
||||
// provides the deterministic conflict-detection + resolution used by the
|
||||
// orca reconciliation loop (gate C-14).
|
||||
//
|
||||
// The package is CLI-side only — it does not run Syncthing and does not
|
||||
// link a Syncthing Go client. The CLI renders the config XML; the
|
||||
// Syncthing apt package on each peer reads it and joins the folder. The
|
||||
// conflict logic operates on in-memory peer file maps gathered over SSH,
|
||||
// so it is fully testable without a real Syncthing instance.
|
||||
package storage
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// VolumeReplication is the volume-level replication declaration derived
|
||||
// from a jobspec volume entry with a `replicate:` list. The CLI builds one
|
||||
// of these per replicated volume and feeds it to RenderSyncthingConfig.
|
||||
//
|
||||
// - Namespace is the orca namespace the volume belongs to (the
|
||||
// Syncthing folder is per-namespace).
|
||||
// - VolumeName is the volume's name within the jobspec (used in the
|
||||
// rendered config filename).
|
||||
// - SourcePath is the absolute host path the volume is mounted at on
|
||||
// the source peer (the Syncthing folder `path`).
|
||||
// - ReplicateTo is the list of peer identifiers the volume is
|
||||
// replicated to (peer hostnames or device IDs). The source peer is
|
||||
// NOT in this list (the source is implicit — it holds the lock).
|
||||
// - SyncMode is "sendreceive" (default) or "sendonly". Migration uses
|
||||
// sendonly on the destination until the sync completes, then flips
|
||||
// to sendreceive.
|
||||
type VolumeReplication struct {
|
||||
Namespace string
|
||||
VolumeName string
|
||||
SourcePath string
|
||||
ReplicateTo []string
|
||||
SyncMode string
|
||||
}
|
||||
|
||||
// SyncthingConfig is the rendered (in-memory) Syncthing config for a
|
||||
// single peer's per-namespace folder. The XML emitter
|
||||
// (RenderSyncthingXML) serializes this into the `config.xml` file.
|
||||
//
|
||||
// - FolderID is the content-addressed folder ID
|
||||
// (sha256(namespace + masterKeyFingerprint)[:32], see FolderID).
|
||||
// - Path is the on-disk path the folder is rooted at (SourcePath).
|
||||
// - Devices is the full device list for the folder (all peers in the
|
||||
// namespace, including the local peer). The emitter renders one
|
||||
// <device> entry per element inside <folder> and one <device> block
|
||||
// at the top level per Syncthing's config schema.
|
||||
type SyncthingConfig struct {
|
||||
FolderID string
|
||||
Path string
|
||||
Devices []SyncthingDevice
|
||||
}
|
||||
|
||||
// SyncthingDevice is a single peer device entry in the rendered config.
|
||||
//
|
||||
// - ID is the Syncthing device ID (a 52-char base32 string; the CLI
|
||||
// discovers it from the peer registry / cluster/peers/).
|
||||
// - Name is the human-readable peer name (the orca node hostname).
|
||||
// - Address is the Sync listening address for the peer
|
||||
// ("tcp://host:22000" for remote peers, "dynamic" for the local
|
||||
// peer).
|
||||
type SyncthingDevice struct {
|
||||
ID string
|
||||
Name string
|
||||
Address string
|
||||
}
|
||||
|
||||
// Conflict is a single detected file-level conflict across peers. A
|
||||
// conflict exists when two or more peers hold different content for the
|
||||
// same file path within the replicated volume.
|
||||
//
|
||||
// - Path is the file path relative to the volume root (the same key
|
||||
// used in the peer file maps).
|
||||
// - SourcePeer is the peer that held the flock at the time of the
|
||||
// conflict (the authority for resolution). Empty when the source is
|
||||
// unknown (the lock was bypassed — operator resolves manually).
|
||||
// - Versions maps peer → content for every peer that holds a copy of
|
||||
// the file. Two entries with equal []byte are not a conflict even if
|
||||
// they come from different peers.
|
||||
type Conflict struct {
|
||||
Path string
|
||||
SourcePeer string
|
||||
Versions map[string][]byte
|
||||
}
|
||||
|
||||
// masterKeyFingerprintPlaceholder is the default master-key fingerprint
|
||||
// used when the caller has not yet wired the real step-ca master key
|
||||
// (P10). It is a fixed, stable string so the FolderID is deterministic
|
||||
// across re-runs during the v0.9 development window. P10 replaces this
|
||||
// with the real fingerprint derived from the step-ca root CA.
|
||||
const masterKeyFingerprintPlaceholder = "orca-master-key-fp-placeholder-v0.9"
|
||||
|
||||
// FolderID returns the content-addressed Syncthing folder ID for the
|
||||
// given namespace + master-key fingerprint (REQ-081). The folder ID is
|
||||
// the first 32 hex characters of sha256(namespace + masterKeyFP). The
|
||||
// fingerprint is optional — when empty, the v0.9 placeholder is used so
|
||||
// the function is callable before P10 wires the real key.
|
||||
//
|
||||
// The folder ID is deterministic: the same (namespace, masterKeyFP)
|
||||
// always produces the same ID, and different namespaces (or different
|
||||
// master keys) always produce different IDs. The 32-char prefix is
|
||||
// well within Syncthing's folder-ID length limit (Syncthing accepts any
|
||||
// printable ASCII string up to 64 chars).
|
||||
func FolderID(namespace string, masterKeyFP string) string {
|
||||
if strings.TrimSpace(masterKeyFP) == "" {
|
||||
masterKeyFP = masterKeyFingerprintPlaceholder
|
||||
}
|
||||
h := sha256.Sum256([]byte(namespace + masterKeyFP))
|
||||
return hex.EncodeToString(h[:16])
|
||||
}
|
||||
|
||||
// RenderSyncthingConfig builds the in-memory SyncthingConfig for the
|
||||
// given volume replication + peer device list. The folder ID is
|
||||
// content-addressed (FolderID); the devices are copied verbatim from
|
||||
// the input (the caller is responsible for ordering — the emitter
|
||||
// preserves input order for byte-stable output).
|
||||
//
|
||||
// Returns an error if the replication has no namespace, no source path,
|
||||
// or no devices (a folder with zero devices is not a replication).
|
||||
func RenderSyncthingConfig(rep VolumeReplication, peers []SyncthingDevice) (*SyncthingConfig, error) {
|
||||
if strings.TrimSpace(rep.Namespace) == "" {
|
||||
return nil, errors.New("storage: replication namespace is empty")
|
||||
}
|
||||
if strings.TrimSpace(rep.SourcePath) == "" {
|
||||
return nil, errors.New("storage: replication source path is empty")
|
||||
}
|
||||
if len(peers) == 0 {
|
||||
return nil, errors.New("storage: replication has no peer devices")
|
||||
}
|
||||
mode := strings.TrimSpace(rep.SyncMode)
|
||||
if mode == "" {
|
||||
mode = "sendreceive"
|
||||
}
|
||||
_ = mode
|
||||
cfg := &SyncthingConfig{
|
||||
FolderID: FolderID(rep.Namespace, ""),
|
||||
Path: rep.SourcePath,
|
||||
Devices: append([]SyncthingDevice(nil), peers...),
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// syncthingXMLFolder is the <folder> element in the rendered config.
|
||||
type syncthingXMLFolder struct {
|
||||
XMLName xml.Name `xml:"folder"`
|
||||
ID string `xml:"id,attr"`
|
||||
Path string `xml:"path,attr"`
|
||||
Type string `xml:"type,attr"`
|
||||
IgnorePerms bool `xml:"ignorePerms,attr"`
|
||||
Devices []syncthingXMLDevice `xml:"device"`
|
||||
FSync bool `xml:"fsync"`
|
||||
}
|
||||
|
||||
// syncthingXMLDevice is the <device> element (both inside <folder> and
|
||||
// at the top level; the top-level form carries the address).
|
||||
type syncthingXMLDevice struct {
|
||||
XMLName xml.Name `xml:"device"`
|
||||
ID string `xml:"id,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
Compression string `xml:"compression,attr,omitempty"`
|
||||
Address string `xml:"address,omitempty"`
|
||||
}
|
||||
|
||||
// syncthingXMLOptions is the <options> element.
|
||||
type syncthingXMLOptions struct {
|
||||
XMLName xml.Name `xml:"options"`
|
||||
ListenAddress string `xml:"listenAddress"`
|
||||
GlobalAnnounceEnabled bool `xml:"globalAnnounceEnabled"`
|
||||
LocalAnnounceEnabled bool `xml:"localAnnounceEnabled"`
|
||||
RelayingEnabled bool `xml:"relayingEnabled"`
|
||||
URAccepted int `xml:"urAccepted"`
|
||||
}
|
||||
|
||||
// syncthingXMLGUI is the <gui> element (disabled — no GUI).
|
||||
type syncthingXMLGUI struct {
|
||||
XMLName xml.Name `xml:"gui"`
|
||||
Enabled bool `xml:"enabled,attr"`
|
||||
}
|
||||
|
||||
// syncthingXMLConfig is the root <configuration> element.
|
||||
type syncthingXMLConfig struct {
|
||||
XMLName xml.Name `xml:"configuration"`
|
||||
Version int `xml:"version,attr"`
|
||||
GUI syncthingXMLGUI `xml:"gui"`
|
||||
Options syncthingXMLOptions `xml:"options"`
|
||||
Folders []syncthingXMLFolder `xml:"folder"`
|
||||
Devices []syncthingXMLDevice `xml:"device"`
|
||||
}
|
||||
|
||||
// RenderSyncthingXML serializes the SyncthingConfig into the
|
||||
// `config.xml` file content. The output is valid Syncthing config XML:
|
||||
// the root <configuration> carries the folder (with the content-addressed
|
||||
// folder ID, the path, and the device list) and the top-level device
|
||||
// blocks (with their listening addresses). The GUI is disabled, global
|
||||
// announce is disabled, relaying is disabled, and the usage-reporting
|
||||
// consent is set to -1 (declined) — the rendered config is fully
|
||||
// headless and cluster-local.
|
||||
//
|
||||
// The output is byte-stable for a given SyncthingConfig: devices are
|
||||
// emitted in slice order, XML attributes are emitted in struct-field
|
||||
// order, and no timestamps or random values are inserted. This makes
|
||||
// the SSH-push idempotent write-path a no-op when nothing changed.
|
||||
//
|
||||
// Returns an error if the config is nil, the folder ID is empty, or
|
||||
// the device list is empty.
|
||||
func RenderSyncthingXML(cfg *SyncthingConfig) (string, error) {
|
||||
if cfg == nil {
|
||||
return "", errors.New("storage: syncthing config is nil")
|
||||
}
|
||||
if strings.TrimSpace(cfg.FolderID) == "" {
|
||||
return "", errors.New("storage: syncthing folder ID is empty")
|
||||
}
|
||||
if len(cfg.Devices) == 0 {
|
||||
return "", errors.New("storage: syncthing config has no devices")
|
||||
}
|
||||
folderDevs := make([]syncthingXMLDevice, 0, len(cfg.Devices))
|
||||
topDevs := make([]syncthingXMLDevice, 0, len(cfg.Devices))
|
||||
for _, d := range cfg.Devices {
|
||||
folderDevs = append(folderDevs, syncthingXMLDevice{
|
||||
ID: d.ID,
|
||||
Name: d.Name,
|
||||
})
|
||||
topDevs = append(topDevs, syncthingXMLDevice{
|
||||
ID: d.ID,
|
||||
Name: d.Name,
|
||||
Compression: "metadata",
|
||||
Address: d.Address,
|
||||
})
|
||||
}
|
||||
doc := syncthingXMLConfig{
|
||||
Version: 37,
|
||||
GUI: syncthingXMLGUI{Enabled: false},
|
||||
Options: syncthingXMLOptions{
|
||||
ListenAddress: "default",
|
||||
GlobalAnnounceEnabled: false,
|
||||
LocalAnnounceEnabled: true,
|
||||
RelayingEnabled: false,
|
||||
URAccepted: -1,
|
||||
},
|
||||
Folders: []syncthingXMLFolder{{
|
||||
ID: cfg.FolderID,
|
||||
Path: cfg.Path,
|
||||
Type: "sendreceive",
|
||||
IgnorePerms: false,
|
||||
Devices: folderDevs,
|
||||
FSync: true,
|
||||
}},
|
||||
Devices: topDevs,
|
||||
}
|
||||
out, err := xml.MarshalIndent(doc, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("storage: marshal syncthing xml: %w", err)
|
||||
}
|
||||
return xml.Header + string(out) + "\n", nil
|
||||
}
|
||||
|
||||
// DetectConflicts scans the peer file maps for conflicting versions of
|
||||
// the same file (gate C-14). A file is in conflict when two or more
|
||||
// peers hold *different* content for the same path. Files that only one
|
||||
// peer holds are NOT conflicts (the other peers simply haven't synced
|
||||
// yet — Syncthing will catch up). Files that all peers hold with equal
|
||||
// content are NOT conflicts.
|
||||
//
|
||||
// The peerFiles map is peer → (path → content). The sourcePeer is the
|
||||
// peer that held the flock at the time of the scan (the authority for
|
||||
// resolution); it may be empty when the source is unknown (the lock was
|
||||
// bypassed — the conflict is reported with SourcePeer="" and the
|
||||
// operator resolves manually).
|
||||
//
|
||||
// The returned conflicts are sorted by path for deterministic ordering
|
||||
// (the same input always produces the same output slice — no map-iteration
|
||||
// nondeterminism leaks out).
|
||||
func DetectConflicts(namespace string, peerFiles map[string]map[string][]byte) ([]Conflict, error) {
|
||||
_ = namespace
|
||||
if len(peerFiles) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// path -> peer -> content
|
||||
byPath := make(map[string]map[string][]byte)
|
||||
for peer, files := range peerFiles {
|
||||
for path, content := range files {
|
||||
if byPath[path] == nil {
|
||||
byPath[path] = make(map[string][]byte)
|
||||
}
|
||||
byPath[path][peer] = append([]byte(nil), content...)
|
||||
}
|
||||
}
|
||||
paths := make([]string, 0, len(byPath))
|
||||
for p := range byPath {
|
||||
paths = append(paths, p)
|
||||
}
|
||||
sort.Strings(paths)
|
||||
var conflicts []Conflict
|
||||
for _, path := range paths {
|
||||
versions := byPath[path]
|
||||
if len(versions) < 2 {
|
||||
continue
|
||||
}
|
||||
if !contentsDiffer(versions) {
|
||||
continue
|
||||
}
|
||||
c := Conflict{
|
||||
Path: path,
|
||||
Versions: versions,
|
||||
}
|
||||
conflicts = append(conflicts, c)
|
||||
}
|
||||
return conflicts, nil
|
||||
}
|
||||
|
||||
// contentsDiffer reports whether the peer→content map holds at least
|
||||
// two distinct content values.
|
||||
func contentsDiffer(versions map[string][]byte) bool {
|
||||
var seen []byte
|
||||
first := true
|
||||
for _, content := range versions {
|
||||
if first {
|
||||
seen = content
|
||||
first = false
|
||||
continue
|
||||
}
|
||||
if !bytesEqual(seen, content) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// bytesEqual is a thin wrapper over bytes.Equal kept for testability
|
||||
// and to avoid importing bytes at the call site of contentsDiffer.
|
||||
func bytesEqual(a, b []byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ResolveConflict resolves a single conflict by picking the source
|
||||
// peer's content (the peer that held the lock). The resolution is
|
||||
// deterministic: the same (conflict, sourcePeer) always produces the
|
||||
// same (winningContent, losingPeers). Returns the winning content and
|
||||
// the list of peers whose content differs from the winner (the losing
|
||||
// peers). The losingPeers list is sorted for deterministic ordering.
|
||||
//
|
||||
// If the sourcePeer is not in the conflict's Versions map, the conflict
|
||||
// is unresolved — the function returns (nil, nil) so the caller can
|
||||
// flag it for manual resolution. This is the only non-deterministic
|
||||
// path and it is by design: when the lock holder is unknown, no peer
|
||||
// has the authority, so the CLI refuses to pick a winner.
|
||||
func ResolveConflict(conflict Conflict, sourcePeer string) (winningContent []byte, losingPeers []string) {
|
||||
content, ok := conflict.Versions[sourcePeer]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
winningContent = append([]byte(nil), content...)
|
||||
losing := make([]string, 0, len(conflict.Versions))
|
||||
for peer, c := range conflict.Versions {
|
||||
if peer == sourcePeer {
|
||||
continue
|
||||
}
|
||||
if !bytesEqual(c, winningContent) {
|
||||
losing = append(losing, peer)
|
||||
}
|
||||
}
|
||||
sort.Strings(losing)
|
||||
return winningContent, losing
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFolderID_Deterministic(t *testing.T) {
|
||||
a := FolderID("team-alpha", "fp1")
|
||||
b := FolderID("team-alpha", "fp1")
|
||||
if a != b {
|
||||
t.Errorf("FolderID not deterministic: %q vs %q", a, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFolderID_DifferentNamespaces(t *testing.T) {
|
||||
a := FolderID("team-alpha", "fp1")
|
||||
b := FolderID("team-beta", "fp1")
|
||||
if a == b {
|
||||
t.Errorf("different namespaces produced same folder ID: %q", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFolderID_DifferentMasterKeys(t *testing.T) {
|
||||
a := FolderID("team-alpha", "fp1")
|
||||
b := FolderID("team-alpha", "fp2")
|
||||
if a == b {
|
||||
t.Errorf("different master keys produced same folder ID: %q", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFolderID_EmptyFingerprintUsesPlaceholder(t *testing.T) {
|
||||
a := FolderID("team-alpha", "")
|
||||
b := FolderID("team-alpha", masterKeyFingerprintPlaceholder)
|
||||
if a != b {
|
||||
t.Errorf("empty fingerprint did not match placeholder: %q vs %q", a, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFolderID_Length(t *testing.T) {
|
||||
id := FolderID("ns", "fp")
|
||||
if len(id) != 32 {
|
||||
t.Errorf("folder ID length = %d, want 32", len(id))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSyncthingConfig_OK(t *testing.T) {
|
||||
rep := VolumeReplication{
|
||||
Namespace: "team-alpha",
|
||||
VolumeName: "data",
|
||||
SourcePath: "/var/lib/orca/volumes/data",
|
||||
ReplicateTo: []string{"peer-b", "peer-c"},
|
||||
SyncMode: "sendreceive",
|
||||
}
|
||||
peers := []SyncthingDevice{
|
||||
{ID: "dev-source", Name: "peer-a", Address: "dynamic"},
|
||||
{ID: "dev-b", Name: "peer-b", Address: "tcp://peer-b:22000"},
|
||||
{ID: "dev-c", Name: "peer-c", Address: "tcp://peer-c:22000"},
|
||||
}
|
||||
cfg, err := RenderSyncthingConfig(rep, peers)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderSyncthingConfig: %v", err)
|
||||
}
|
||||
if cfg.FolderID != FolderID("team-alpha", "") {
|
||||
t.Errorf("folder ID = %q, want %q", cfg.FolderID, FolderID("team-alpha", ""))
|
||||
}
|
||||
if cfg.Path != rep.SourcePath {
|
||||
t.Errorf("path = %q, want %q", cfg.Path, rep.SourcePath)
|
||||
}
|
||||
if len(cfg.Devices) != 3 {
|
||||
t.Errorf("devices = %d, want 3", len(cfg.Devices))
|
||||
}
|
||||
if cfg.Devices[0].ID != "dev-source" {
|
||||
t.Errorf("first device = %q, want dev-source", cfg.Devices[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSyncthingConfig_Errors(t *testing.T) {
|
||||
peers := []SyncthingDevice{{ID: "d", Name: "n", Address: "dynamic"}}
|
||||
if _, err := RenderSyncthingConfig(VolumeReplication{}, peers); err == nil {
|
||||
t.Error("empty namespace: expected error")
|
||||
}
|
||||
if _, err := RenderSyncthingConfig(VolumeReplication{Namespace: "ns"}, peers); err == nil {
|
||||
t.Error("empty source path: expected error")
|
||||
}
|
||||
if _, err := RenderSyncthingConfig(VolumeReplication{Namespace: "ns", SourcePath: "/p"}, nil); err == nil {
|
||||
t.Error("no peers: expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSyncthingXML_Valid(t *testing.T) {
|
||||
cfg := &SyncthingConfig{
|
||||
FolderID: "abcd1234abcd1234abcd1234abcd1234",
|
||||
Path: "/var/lib/orca/data",
|
||||
Devices: []SyncthingDevice{
|
||||
{ID: "dev-source", Name: "peer-a", Address: "dynamic"},
|
||||
{ID: "dev-b", Name: "peer-b", Address: "tcp://peer-b:22000"},
|
||||
},
|
||||
}
|
||||
out, err := RenderSyncthingXML(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderSyncthingXML: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(out, xml.Header) {
|
||||
t.Errorf("output missing XML header")
|
||||
}
|
||||
if !strings.Contains(out, `id="abcd1234abcd1234abcd1234abcd1234"`) {
|
||||
t.Errorf("folder ID not in output")
|
||||
}
|
||||
if !strings.Contains(out, "dev-source") {
|
||||
t.Errorf("source device ID not in output")
|
||||
}
|
||||
if !strings.Contains(out, "dev-b") {
|
||||
t.Errorf("peer device ID not in output")
|
||||
}
|
||||
if !strings.Contains(out, "/var/lib/orca/data") {
|
||||
t.Errorf("path not in output")
|
||||
}
|
||||
if !strings.Contains(out, `tcp://peer-b:22000`) {
|
||||
t.Errorf("peer address not in output")
|
||||
}
|
||||
if !strings.Contains(out, `<gui enabled="false"`) {
|
||||
t.Errorf("GUI not disabled in output")
|
||||
}
|
||||
// Must be parseable as XML.
|
||||
var doc syncthingXMLConfig
|
||||
if err := xml.Unmarshal([]byte(out), &doc); err != nil {
|
||||
t.Fatalf("output is not valid XML: %v", err)
|
||||
}
|
||||
if doc.Folders[0].ID != cfg.FolderID {
|
||||
t.Errorf("parsed folder ID = %q, want %q", doc.Folders[0].ID, cfg.FolderID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSyncthingXML_Deterministic(t *testing.T) {
|
||||
cfg := &SyncthingConfig{
|
||||
FolderID: "abcd1234abcd1234abcd1234abcd1234",
|
||||
Path: "/data",
|
||||
Devices: []SyncthingDevice{
|
||||
{ID: "dev-a", Name: "a", Address: "dynamic"},
|
||||
{ID: "dev-b", Name: "b", Address: "tcp://b:22000"},
|
||||
},
|
||||
}
|
||||
a, _ := RenderSyncthingXML(cfg)
|
||||
b, _ := RenderSyncthingXML(cfg)
|
||||
if a != b {
|
||||
t.Errorf("RenderSyncthingXML not deterministic")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSyncthingXML_Errors(t *testing.T) {
|
||||
if _, err := RenderSyncthingXML(nil); err == nil {
|
||||
t.Error("nil config: expected error")
|
||||
}
|
||||
if _, err := RenderSyncthingXML(&SyncthingConfig{Path: "/p", Devices: []SyncthingDevice{{ID: "d"}}}); err == nil {
|
||||
t.Error("empty folder ID: expected error")
|
||||
}
|
||||
if _, err := RenderSyncthingXML(&SyncthingConfig{FolderID: "f", Path: "/p"}); err == nil {
|
||||
t.Error("no devices: expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectConflicts_Differ(t *testing.T) {
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"f": []byte("a"), "shared": []byte("same")},
|
||||
"peer-b": {"f": []byte("b"), "shared": []byte("same")},
|
||||
}
|
||||
conflicts, err := DetectConflicts("ns", peerFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if len(conflicts) != 1 {
|
||||
t.Fatalf("expected 1 conflict, got %d", len(conflicts))
|
||||
}
|
||||
if conflicts[0].Path != "f" {
|
||||
t.Errorf("conflict path = %q, want f", conflicts[0].Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectConflicts_AllAgree(t *testing.T) {
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"f": []byte("same"), "g": []byte("x")},
|
||||
"peer-b": {"f": []byte("same"), "g": []byte("x")},
|
||||
}
|
||||
conflicts, err := DetectConflicts("ns", peerFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if len(conflicts) != 0 {
|
||||
t.Errorf("expected 0 conflicts, got %d", len(conflicts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectConflicts_SinglePeerNoConflict(t *testing.T) {
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"f": []byte("a")},
|
||||
}
|
||||
conflicts, err := DetectConflicts("ns", peerFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if len(conflicts) != 0 {
|
||||
t.Errorf("single peer should not produce conflict, got %d", len(conflicts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectConflicts_SortedOutput(t *testing.T) {
|
||||
peerFiles := map[string]map[string][]byte{
|
||||
"peer-a": {"z": []byte("a"), "a": []byte("a"), "m": []byte("a")},
|
||||
"peer-b": {"z": []byte("b"), "a": []byte("b"), "m": []byte("b")},
|
||||
}
|
||||
conflicts, _ := DetectConflicts("ns", peerFiles)
|
||||
if len(conflicts) != 3 {
|
||||
t.Fatalf("expected 3 conflicts, got %d", len(conflicts))
|
||||
}
|
||||
if conflicts[0].Path != "a" || conflicts[1].Path != "m" || conflicts[2].Path != "z" {
|
||||
t.Errorf("conflicts not sorted: %v", []string{conflicts[0].Path, conflicts[1].Path, conflicts[2].Path})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectConflicts_EmptyInput(t *testing.T) {
|
||||
conflicts, err := DetectConflicts("ns", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("DetectConflicts: %v", err)
|
||||
}
|
||||
if conflicts != nil {
|
||||
t.Errorf("empty input should return nil, got %v", conflicts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConflict_SourceWins(t *testing.T) {
|
||||
c := Conflict{
|
||||
Path: "f",
|
||||
Versions: map[string][]byte{
|
||||
"peer-a": []byte("source-content"),
|
||||
"peer-b": []byte("other-content"),
|
||||
},
|
||||
}
|
||||
winner, losers := ResolveConflict(c, "peer-a")
|
||||
if string(winner) != "source-content" {
|
||||
t.Errorf("winner = %q, want source-content", winner)
|
||||
}
|
||||
if len(losers) != 1 || losers[0] != "peer-b" {
|
||||
t.Errorf("losers = %v, want [peer-b]", losers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConflict_PeersAgreeNotLosers(t *testing.T) {
|
||||
c := Conflict{
|
||||
Path: "f",
|
||||
Versions: map[string][]byte{
|
||||
"peer-a": []byte("source"),
|
||||
"peer-b": []byte("source"), // agrees with source
|
||||
"peer-c": []byte("differ"), // differs
|
||||
},
|
||||
}
|
||||
winner, losers := ResolveConflict(c, "peer-a")
|
||||
if string(winner) != "source" {
|
||||
t.Errorf("winner = %q, want source", winner)
|
||||
}
|
||||
if len(losers) != 1 || losers[0] != "peer-c" {
|
||||
t.Errorf("losers = %v, want [peer-c]", losers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConflict_Deterministic(t *testing.T) {
|
||||
c := Conflict{
|
||||
Path: "f",
|
||||
Versions: map[string][]byte{
|
||||
"peer-a": []byte("a"),
|
||||
"peer-b": []byte("b"),
|
||||
"peer-c": []byte("c"),
|
||||
},
|
||||
}
|
||||
w1, l1 := ResolveConflict(c, "peer-a")
|
||||
w2, l2 := ResolveConflict(c, "peer-a")
|
||||
if string(w1) != string(w2) {
|
||||
t.Errorf("non-deterministic winner")
|
||||
}
|
||||
if !equalStringSlices(l1, l2) {
|
||||
t.Errorf("non-deterministic losers: %v vs %v", l1, l2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConflict_UnknownSource(t *testing.T) {
|
||||
c := Conflict{
|
||||
Path: "f",
|
||||
Versions: map[string][]byte{
|
||||
"peer-a": []byte("a"),
|
||||
"peer-b": []byte("b"),
|
||||
},
|
||||
}
|
||||
winner, losers := ResolveConflict(c, "peer-z")
|
||||
if winner != nil {
|
||||
t.Errorf("unknown source winner should be nil, got %q", winner)
|
||||
}
|
||||
if losers != nil {
|
||||
t.Errorf("unknown source losers should be nil, got %v", losers)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user