mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Two follow-ups caught by independent re-review: - #29 (8855b78c0): GetConnectionStatuses keys a name-less instance by host, but the diagnostics lookup used 'pve-'/'pbs-'+Name only, so the monitor-state merge was a silent no-op for unnamed instances. Fall back to host to match. - #22 (d9da9b18f, #1254): add a full-router test asserting an UNBOUND agent:report token gets 404 (not 200) on config fetch — continuously verifying the token-binding security boundary that the scope change relies on.
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
|
|
)
|
|
|
|
func TestUnifiedAgentConfigUsesTokenBindingInRouter(t *testing.T) {
|
|
rawToken := "host-config-binding-token-123.12345678"
|
|
record := newTokenRecord(t, rawToken, []string{config.ScopeAgentConfigRead}, nil)
|
|
record.ID = "token-1"
|
|
|
|
cfg := newTestConfigWithTokens(t, record)
|
|
|
|
state := models.NewState()
|
|
state.UpsertHost(models.Host{ID: "host-1", TokenID: "token-1"})
|
|
monitor := &monitoring.Monitor{}
|
|
setUnexportedField(t, monitor, "state", state)
|
|
|
|
router := NewRouter(cfg, monitor, nil, nil, nil, "1.0.0")
|
|
|
|
for _, path := range []string{"/api/agents/agent/host-2/config", "/api/agents/host/host-2/config"} {
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
req.Header.Set("X-API-Token", rawToken)
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 for bound unified-agent config on %s, got %d", path, rec.Code)
|
|
}
|
|
|
|
var resp struct {
|
|
AgentID string `json:"agentId"`
|
|
}
|
|
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode response on %s: %v", path, err)
|
|
}
|
|
if resp.AgentID != "host-1" {
|
|
t.Fatalf("expected agent id %q on %s, got %q", "host-1", path, resp.AgentID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Regression for #1254: canReadConfig accepts agent:report, but a report-only
|
|
// token that is NOT bound to any host must still be rejected (404 from token
|
|
// binding, not 200) — verified at the full router level.
|
|
func TestUnifiedAgentConfigReportScopeUnboundReturnsNotFound(t *testing.T) {
|
|
rawToken := "host-report-unbound-token-123.12345678"
|
|
record := newTokenRecord(t, rawToken, []string{config.ScopeAgentReport}, nil)
|
|
record.ID = "token-unbound"
|
|
|
|
cfg := newTestConfigWithTokens(t, record)
|
|
|
|
state := models.NewState()
|
|
state.UpsertHost(models.Host{ID: "host-1", TokenID: "token-other"})
|
|
monitor := &monitoring.Monitor{}
|
|
setUnexportedField(t, monitor, "state", state)
|
|
|
|
router := NewRouter(cfg, monitor, nil, nil, nil, "1.0.0")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/agents/agent/host-1/config", nil)
|
|
req.Header.Set("X-API-Token", rawToken)
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404 for an unbound report-only token, got %d", rec.Code)
|
|
}
|
|
}
|