Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-16438-boot-safety-gates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Warn before boot recovery drops safety gates from prior runtime or dashboard config, retain the warning in status and settings, and allow operators to refuse startup with `config.refuse_gate_regression: true`; `hive config diff` shows the same gate deltas ([#16438](https://github.com/hivecommons/hive/issues/16438)).
51 changes: 51 additions & 0 deletions src/cmd/hive/config_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"os"

"github.com/hivecommons/hive/pkg/config"
)

// runConfigDiff starts nothing: the entrypoint calls it before snapshot writes.
// Positional paths override the conventional prior layers for operator use.
func runConfigDiff(args []string, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("config diff", flag.ContinueOnError)
fs.SetOutput(stderr)
defaultPath := os.Getenv("HIVE_CONFIG")
if defaultPath == "" {
defaultPath = "/etc/hive/hive.yaml"
}
path := fs.String("config", defaultPath, "effective config path")
asJSON := fs.Bool("json", false, "emit boot warnings as JSON")
if err := fs.Parse(args); err != nil {
return 1
}
previous := fs.Args()
if len(previous) == 0 {
previous = []string{config.RuntimeConfigFile, config.RuntimeConfigFile + ".bak", config.RuntimeConfigFileLegacy, config.DashboardOverlayFile}
}
warnings, strict, err := config.SafetyGateDiff(*path, previous)
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
for _, warning := range warnings {
fmt.Fprintln(stderr, "WARN", warning)
}
if *asJSON {
if err := json.NewEncoder(stdout).Encode(warnings); err != nil {
return 1
}
} else if len(warnings) == 0 {
fmt.Fprintln(stdout, "No safety gate regressions")
}
if strict && len(warnings) > 0 {
fmt.Fprintln(stderr, "refusing to start: config.refuse_gate_regression is enabled; restore the safety gates or explicitly disable strict mode")
return 1
}
return 0
}
44 changes: 44 additions & 0 deletions src/cmd/hive/config_diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)

func TestConfigDiffDispatchAndStrictMode(t *testing.T) {
for _, strict := range []bool{false, true} {
t.Run(map[bool]string{false: "warn", true: "refuse"}[strict], func(t *testing.T) {
dir := t.TempDir()
seed, prior := filepath.Join(dir, "seed"), filepath.Join(dir, "prior")
body := "project: {org: example}\n"
if strict {
body += "config: {refuse_gate_regression: true}\n"
}
if err := os.WriteFile(seed, []byte(body), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(prior, []byte("review: {require_labels: [hive-review]}\n"), 0600); err != nil {
t.Fatal(err)
}
var stdout, stderr bytes.Buffer
handled, code := dispatchSubcommand([]string{"config", "diff", "--config", seed, "--json", prior}, &stdout, &stderr)
if !handled || (code != 0) != strict {
t.Fatalf("handled=%v code=%d: %s", handled, code, stderr.String())
}
var warnings []string
if err := json.Unmarshal(stdout.Bytes(), &warnings); err != nil || len(warnings) != 1 {
t.Fatalf("warnings=%v err=%v stdout=%s", warnings, err, stdout.String())
}
if !strings.Contains(stderr.String(), "WARN safety gate dropped: review.require_labels") {
t.Fatal(stderr.String())
}
if strings.Contains(stderr.String(), "refusing to start") != strict {
t.Fatal(stderr.String())
}
})
}
}
6 changes: 6 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7113,6 +7113,12 @@ func dispatchSubcommand(args []string, stdout, stderr io.Writer) (bool, int) {
case "--version", "version":
fmt.Fprintf(stdout, "hive %s (commit %s, branch %s)\n", reportedVersion(), gitShort, gitBranch)
return true, 0
case "config":
if len(args) > 1 && args[1] == "diff" {
return true, runConfigDiff(args[2:], stdout, stderr)
}
fmt.Fprintln(stderr, "usage: hive config diff [-config path] [-json] [previous paths...]")
return true, 1
case "validate", "--config-check":
return true, runConfigCheck(args[1:], stdout, stderr)
case "agy-turn":
Expand Down
13 changes: 13 additions & 0 deletions src/deploy/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,16 @@ hive_write_system_gitconfig() {
echo "[entrypoint] agents commit as ${_hwsg_name} <${_hwsg_email}> (system gitconfig)"
}

# Capture safety deltas BEFORE snapshot writes erase the prior runtime config.
# The exported JSON survives the root -> dev re-exec and config reloads, keeping
# recovery warnings visible in /api/status for the entire boot.
hive_check_safety_gates() {
if [ -z "${HIVE_BOOT_SAFETY_WARNINGS:-}" ]; then
HIVE_BOOT_SAFETY_WARNINGS="$(hive config diff --config "${HIVE_CONFIG:-$HIVE_CONFIG_PATH}" --json)" || return 1
export HIVE_BOOT_SAFETY_WARNINGS
fi
}

# Detect Kubernetes vs Docker environment
IS_KUBERNETES=false
if [ -n "${KUBERNETES_SERVICE_HOST:-}" ] || [ -f /var/run/secrets/kubernetes.io/serviceaccount/token ]; then
Expand Down Expand Up @@ -1141,6 +1151,7 @@ PYEOF
echo "[entrypoint] K8s mode — dashboard overlay invalid, using ConfigMap seed as-is"
fi
fi
hive_check_safety_gates
# Write the (merged) config as the disaster-recovery snapshot. Always
# under the new name; the legacy file is left untouched on the PVC.
cp "$HIVE_CONFIG_PATH" "$HIVE_CONFIG_RUNTIME" 2>/dev/null || true
Expand Down Expand Up @@ -1203,6 +1214,8 @@ else
fi
fi

hive_check_safety_gates

# ── Cleanup stale stderr logs from previous agent launches ────────────
STDERR_LOG_MAX_AGE_MINUTES=60
find /tmp -maxdepth 1 -name '.hive-launch-stderr-*.log' -mmin +"$STDERR_LOG_MAX_AGE_MINUTES" -delete 2>/dev/null || true
Expand Down
45 changes: 45 additions & 0 deletions src/deploy/test_entrypoint_runtime_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,49 @@ else
trap - EXIT
fi

# #16438: execute the actual recovery branch, including its snapshot write.
# A fake diff command controls the verdict; Go tests exercise the comparator.
SAFETY_HELPER="$(sed -n '/^hive_check_safety_gates() {/,/^}/p' "$ENTRYPOINT")"
BOOT_BLOCK="$(sed -n '/^# Detect Kubernetes vs Docker environment/,/^# ── Cleanup stale stderr logs/{ /^# ── Cleanup stale stderr logs/d; p; }' "$ENTRYPOINT")"
exercise_safety_boot() (
set -e
local verdict="$1" dir
dir="$(mktemp -d)"
trap 'rm -rf "$dir"' EXIT
HIVE_CONFIG_PATH="$dir/seed"
HIVE_CONFIG_RUNTIME="$dir/runtime"
HIVE_CONFIG_RUNTIME_LEGACY="$dir/legacy"
HIVE_CONFIG_BOOT=""
HIVE_CONFIG=""
HIVE_BOOT_SAFETY_WARNINGS=""
KUBERNETES_SERVICE_HOST=probe
printf 'project: {org: probe}\n' > "$HIVE_CONFIG_PATH"
# Resolve no boot input, simulating an operator moving runtime aside.
hive_runtime_config_read() { :; }
hive_harden_runtime_config() { :; }
hive() {
if [ "$verdict" = refuse ]; then return 1; fi
printf '["safety gate dropped: review.require_labels"]\n'
}
# Use a separate shell so set -e remains active even when this harness
# captures its exit status in an if statement.
export HIVE_CONFIG_PATH HIVE_CONFIG_RUNTIME HIVE_CONFIG_RUNTIME_LEGACY
export KUBERNETES_SERVICE_HOST HIVE_CONFIG HIVE_BOOT_SAFETY_WARNINGS verdict
export -f hive_runtime_config_read hive_harden_runtime_config hive
local status=0
BOOT_BLOCK="${BOOT_BLOCK//\/data\/hive.yaml.dashboard/$dir/dashboard}"
bash -ec "$SAFETY_HELPER
$BOOT_BLOCK
[ -n \"\$HIVE_BOOT_SAFETY_WARNINGS\" ]" >"$dir/log" 2>&1 || status=$?
if [ "$verdict" = refuse ]; then
[ "$status" != 0 ] && [ ! -e "$HIVE_CONFIG_RUNTIME" ] && echo refused-before-snapshot
else
[ "$status" = 0 ] && cmp -s "$HIVE_CONFIG_PATH" "$HIVE_CONFIG_RUNTIME" && echo warned-and-snapshotted
fi
)
check "strict gate regression refuses before runtime snapshot" \
"refused-before-snapshot" "$(exercise_safety_boot refuse)"
check "normal recovery keeps boot warning across second guard call" \
"warned-and-snapshotted" "$(exercise_safety_boot warn)"

hive_test_report
6 changes: 6 additions & 0 deletions src/hive.yaml.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Compare boot safety gates with surviving runtime/backup/dashboard config.
# Warn by default; true refuses startup before replacing the runtime snapshot.
# Inspect deltas manually with: hive config diff --config /path/to/hive.yaml
config:
refuse_gate_regression: false

# Hive v2 Configuration
# All values support ${ENV_VAR} interpolation for secrets.
# Copy this file to hive.yaml and customize for your setup.
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func notifySaveObserver() {
var dashboardAuthTokenFile = "/secrets/dashboard-token"

type Config struct {
Project ProjectConfig `yaml:"project"`
Policies PoliciesConfig `yaml:"policies"`
Config *ConfigLoadPolicy `yaml:"config,omitempty" json:"config,omitempty"`
Project ProjectConfig `yaml:"project"`
Policies PoliciesConfig `yaml:"policies"`
// AgentRuntimeConfig groups the keys the agent runtime (Crux,
// hivecommons/hive#13536) owns. It is inlined so the YAML shape is
// unchanged: `agents:`, `backends:`, ... stay top-level.
Expand Down
132 changes: 132 additions & 0 deletions src/pkg/config/safety_gates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package config

import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"

"gopkg.in/yaml.v3"
)

// BootSafetyWarningsEnv carries the pre-snapshot comparison across the
// entrypoint's privilege-drop re-exec. It contains only gate diagnostics,
// never config contents or credentials, and lasts for this process's boot.
const BootSafetyWarningsEnv = "HIVE_BOOT_SAFETY_WARNINGS"

// ConfigLoadPolicy controls startup diagnostics; it does not change gate enforcement.
type ConfigLoadPolicy struct {
RefuseGateRegression bool `yaml:"refuse_gate_regression,omitempty" json:"refuse_gate_regression,omitempty"`
}

// BootSafetyWarnings returns warnings captured before the entrypoint replaced
// the runtime snapshot. Reading them here keeps reloads from erasing boot evidence.
func BootSafetyWarnings() []string {
var warnings []string
_ = json.Unmarshal([]byte(os.Getenv(BootSafetyWarningsEnv)), &warnings)
return warnings
}

// gateSnapshot deliberately reads only safety settings. Recovery diagnostics
// must work even when an unrelated agent field makes a full config invalid.
type gateSnapshot struct {
Config ConfigLoadPolicy `yaml:"config"`
Review struct {
RequireLabels []string `yaml:"require_labels"`
} `yaml:"review"`
Project struct {
Org string `yaml:"org"`
PausedRepos []struct {
Repo string `yaml:"repo"`
} `yaml:"paused_repos"`
IssueFilter struct {
RequireLabels []string `yaml:"require_labels"`
} `yaml:"issue_filter"`
} `yaml:"project"`
Writeback struct {
PerRepo map[string]struct {
Mode string `yaml:"mode"`
} `yaml:"per_repo"`
} `yaml:"writeback"`
}

func readGateSnapshot(path string) (gateSnapshot, error) {
var cfg gateSnapshot
data, err := os.ReadFile(path)
if err == nil {
err = yaml.Unmarshal([]byte(expandEnvVars(string(data))), &cfg)
}
return cfg, err
}

// SafetyGateDiff compares an effective boot config against all surviving prior
// layers, before any of them are overwritten. Missing layers mean first boot;
// unreadable or malformed layers are explicit diagnostics rather than silence.
// The bool is the effective config's strict-mode setting.
func SafetyGateDiff(path string, previous []string) ([]string, bool, error) {
effective, err := readGateSnapshot(path)
if err != nil {
return nil, false, fmt.Errorf("reading effective safety gates: %w", err)
}
warnings := []string{}
for _, priorPath := range previous {
prior, err := readGateSnapshot(priorPath)
if os.IsNotExist(err) {
continue
}
if err != nil {
// Do not print parser errors: they can contain unrelated secret values.
warnings = append(warnings, fmt.Sprintf("safety gate comparison unavailable: %s (unreadable or invalid YAML)", priorPath))
continue
}
dropped := func(key string, was any) {
value, _ := json.Marshal(was)
warnings = append(warnings, fmt.Sprintf("safety gate dropped: %s (was %s) — effective config has none or a weaker restriction (previous: %s)", key, value, priorPath))
}
if len(nonemptyLabels(prior.Review.RequireLabels)) > 0 && len(nonemptyLabels(effective.Review.RequireLabels)) == 0 {
dropped("review.require_labels", prior.Review.RequireLabels)
}
if len(nonemptyLabels(prior.Project.IssueFilter.RequireLabels)) > 0 && len(nonemptyLabels(effective.Project.IssueFilter.RequireLabels)) == 0 {
dropped("project.issue_filter.require_labels", prior.Project.IssueFilter.RequireLabels)
}
paused := map[string]bool{}
for _, p := range effective.Project.PausedRepos {
paused[repoPauseKey(effective.Project.Org, p.Repo)] = true
}
for _, p := range prior.Project.PausedRepos {
if p.Repo != "" && !paused[repoPauseKey(prior.Project.Org, p.Repo)] {
dropped("project.paused_repos", p.Repo)
}
}
modes := map[string]string{}
for repo, override := range effective.Writeback.PerRepo {
modes[strings.ToLower(strings.TrimSpace(repo))] = override.Mode
}
repos := make([]string, 0, len(prior.Writeback.PerRepo))
for repo := range prior.Writeback.PerRepo {
repos = append(repos, repo)
}
sort.Strings(repos)
for _, repo := range repos {
mode := prior.Writeback.PerRepo[repo].Mode
current := modes[strings.ToLower(strings.TrimSpace(repo))]
// off is strictly stronger than either partial-write mode. The partial
// modes allow different kinds, so switching between them is a delta too.
if (mode == "off" || mode == "issues-only" || mode == "labels-only") && current != mode && current != "off" {
dropped("writeback.per_repo."+repo+".mode", mode)
}
}
}
return warnings, effective.Config.RefuseGateRegression, nil
}

func nonemptyLabels(labels []string) []string {
var out []string
for _, label := range labels {
if strings.TrimSpace(label) != "" {
out = append(out, label)
}
}
return out
}
Loading
Loading