Add web auth, infinite-scroll, agent admission and review fixes across agent/server/web

This commit is contained in:
Stanislav Rossovskii
2026-05-28 23:45:12 +04:00
parent 37b1a1d6d6
commit 1026e9ebbe
75 changed files with 3021 additions and 916 deletions

View File

@@ -8,6 +8,7 @@ import (
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/monlet/agent/internal/agentkey"
"github.com/monlet/agent/internal/app"
@@ -20,6 +21,13 @@ import (
var version = "0.1.0"
const shutdownTimeout = 30 * time.Second
type reloadTarget interface {
RecordReloadFailure()
Reload(*config.Config, string) error
}
func main() {
cfgPath := flag.String("config", "config.toml", "path to TOML config")
flag.Parse()
@@ -65,6 +73,12 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
startShutdownWatchdog(ctx, shutdownTimeout, log, os.Exit)
hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP)
defer signal.Stop(hup)
go runReloadLoop(ctx, *cfgPath, hup, a, log)
log.Info("monlet-agent starting", "agent_id", agentID, "version", version, "checks", len(cfg.Checks))
if err := a.Run(ctx); err != nil {
@@ -73,3 +87,45 @@ func main() {
}
log.Info("monlet-agent stopped")
}
func startShutdownWatchdog(ctx context.Context, timeout time.Duration, log *slog.Logger, exit func(int)) {
go func() {
<-ctx.Done()
t := time.NewTimer(timeout)
defer t.Stop()
<-t.C
log.Error("shutdown timeout exceeded", "timeout", timeout.String())
exit(1)
}()
}
func runReloadLoop(ctx context.Context, cfgPath string, hup <-chan os.Signal, target reloadTarget, log *slog.Logger) {
for {
select {
case <-ctx.Done():
return
case <-hup:
reloadOnce(cfgPath, target, log)
}
}
}
func reloadOnce(cfgPath string, target reloadTarget, log *slog.Logger) {
nextCfg, err := config.Load(cfgPath)
if err != nil {
target.RecordReloadFailure()
log.Warn("reload config failed", "err", err)
return
}
nextAgentID, err := ids.ResolveAgentID(nextCfg.AgentID, nextCfg.Hostname)
if err != nil {
target.RecordReloadFailure()
log.Warn("reload agent_id failed", "err", err)
return
}
if err := target.Reload(nextCfg, nextAgentID); err != nil {
log.Warn("reload rejected", "err", err)
return
}
log.Info("reload complete", "checks", len(nextCfg.Checks))
}

View File

@@ -0,0 +1,118 @@
package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/monlet/agent/internal/config"
)
type reloadCall struct {
cfg *config.Config
agentID string
}
type fakeReloadTarget struct {
reloads chan reloadCall
failures chan struct{}
}
func (f *fakeReloadTarget) RecordReloadFailure() {
f.failures <- struct{}{}
}
func (f *fakeReloadTarget) Reload(cfg *config.Config, agentID string) error {
f.reloads <- reloadCall{cfg: cfg, agentID: agentID}
return nil
}
func TestRunReloadLoopHandlesSIGHUP(t *testing.T) {
cfgPath := writeTestConfig(t)
target := &fakeReloadTarget{
reloads: make(chan reloadCall, 1),
failures: make(chan struct{}, 1),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hup := make(chan os.Signal, 1)
go runReloadLoop(ctx, cfgPath, hup, target, discardLogger())
hup <- syscall.SIGHUP
select {
case call := <-target.reloads:
if call.agentID != "agent-test" || len(call.cfg.Checks) != 1 {
t.Fatalf("unexpected reload call: agent_id=%q cfg=%+v", call.agentID, call.cfg)
}
case <-time.After(time.Second):
t.Fatal("reload was not called")
}
}
func TestRunReloadLoopRecordsLoadFailure(t *testing.T) {
target := &fakeReloadTarget{
reloads: make(chan reloadCall, 1),
failures: make(chan struct{}, 1),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hup := make(chan os.Signal, 1)
go runReloadLoop(ctx, filepath.Join(t.TempDir(), "missing.toml"), hup, target, discardLogger())
hup <- syscall.SIGHUP
select {
case <-target.failures:
case <-time.After(time.Second):
t.Fatal("reload failure was not recorded")
}
}
func TestShutdownWatchdogExitsAfterTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
exits := make(chan int, 1)
startShutdownWatchdog(ctx, time.Millisecond, discardLogger(), func(code int) {
exits <- code
})
cancel()
select {
case code := <-exits:
if code != 1 {
t.Fatalf("exit code: %d", code)
}
case <-time.After(time.Second):
t.Fatal("watchdog did not exit")
}
}
func writeTestConfig(t *testing.T) string {
t.Helper()
path := filepath.Join(t.TempDir(), "agent.toml")
body := fmt.Sprintf(`
agent_id = "agent-test"
state_dir = %q
[server]
enabled = true
url = "http://localhost"
[[checks]]
id = "c1"
command = "true"
interval = "10s"
timeout = "5s"
`, t.TempDir())
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
return path
}
func discardLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(io.Discard, nil))
}