mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 09:10:58 +01:00
Reject unknown fleet inventory in CLI reads
The canonical connections handler emits an explicit array, including [] for an empty scope. The CLI instead converted null or missing inventories into a successful empty fleet. Return a response error before writing JSON and preserve valid empty/populated output, request authority and existing HTTP error handling. Registered command tests cover fixed and chunked HTTP responses, response errors and forward-compatible row fields. Contract-Neutral: Preserve the existing connections array schema and valid CLI output; reject missing or null inventories instead of inventing an empty fleet. Change-source: pulse-maintainer
This commit is contained in:
@@ -91,7 +91,9 @@ func runFleetConnections(cmd *cobra.Command, deps *FleetDeps, opts fleetConnecti
|
||||
return fmt.Errorf("failed to decode fleet connections response: %w", err)
|
||||
}
|
||||
if connections.Connections == nil {
|
||||
connections.Connections = []json.RawMessage{}
|
||||
// Missing data is not an empty fleet. The server emits an explicit
|
||||
// array, including [] when there are no connections in the scope.
|
||||
return fmt.Errorf("invalid fleet connections response: expected a non-null connections array (use [] for an empty fleet)")
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(cmd.OutOrStdout())
|
||||
|
||||
@@ -2,11 +2,14 @@ package pulsecli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -131,3 +134,98 @@ func newTestFleetRootCommand(env map[string]string) *cobra.Command {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func runFleetInventoryFixture(t *testing.T, body string, chunked bool) (string, string, error) {
|
||||
t.Helper()
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests.Add(1)
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/connections" || r.Header.Get("Authorization") != "Bearer synthetic-fleet-token" {
|
||||
t.Errorf("unexpected fleet request: method=%s path=%s bearer=%t", r.Method, r.URL.Path, r.Header.Get("Authorization") == "Bearer synthetic-fleet-token")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if chunked {
|
||||
w.(http.Flusher).Flush()
|
||||
}
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
defer server.Close()
|
||||
cmd := newTestFleetRootCommand(map[string]string{"PULSE_API_TOKEN": "synthetic-fleet-token", "PULSE_API_URL": server.URL})
|
||||
cmd.SetArgs([]string{"fleet", "connections"})
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.SetOut(&stdout)
|
||||
cmd.SetErr(&stderr)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
err := cmd.ExecuteContext(ctx)
|
||||
if got := requests.Load(); got != 1 {
|
||||
t.Fatalf("fleet read made %d requests, want one without retry", got)
|
||||
}
|
||||
return stdout.String(), stderr.String(), err
|
||||
}
|
||||
|
||||
func TestFleetConnectionsCommandRequiresExplicitInventory(t *testing.T) {
|
||||
for _, framing := range []string{"fixed", "chunked"} {
|
||||
t.Run(framing, func(t *testing.T) {
|
||||
for _, tc := range []struct{ name, body string }{
|
||||
{"null-envelope", `null`},
|
||||
{"empty-object", `{}`},
|
||||
{"systems-only", `{"systems":[{"id":"agent:saved-host"}]}`},
|
||||
{"error-envelope", `{"error":"synthetic-private-response"}`},
|
||||
{"null-inventory", `{"connections":null}`},
|
||||
{"object-inventory", `{"connections":{}}`},
|
||||
{"string-inventory", `{"connections":"not an inventory"}`},
|
||||
{"boolean-inventory", `{"connections":false}`},
|
||||
{"number-inventory", `{"connections":0}`},
|
||||
{"array-envelope", `[]`},
|
||||
{"empty-body", ``},
|
||||
{"truncated", `{"connections":[`},
|
||||
{"trailing-json", `{"connections":[]} null`},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
stdout, stderr, err := runFleetInventoryFixture(t, tc.body, framing == "chunked")
|
||||
if err == nil || !strings.Contains(err.Error(), "fleet connections response") {
|
||||
t.Errorf("incomplete inventory must return a response error, got %v", err)
|
||||
}
|
||||
if stdout != "" {
|
||||
t.Errorf("incomplete inventory emitted successful fleet JSON: %s", stdout)
|
||||
}
|
||||
if strings.Contains(stderr, "synthetic-private-response") || (err != nil && strings.Contains(err.Error(), "synthetic-private-response")) {
|
||||
t.Error("invalid envelope diagnostic echoed unrelated response content")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFleetConnectionsCommandPreservesExplicitInventory(t *testing.T) {
|
||||
for _, framing := range []string{"fixed", "chunked"} {
|
||||
t.Run(framing, func(t *testing.T) {
|
||||
for _, tc := range []struct{ name, body string }{
|
||||
{"empty", `{"connections":[]}`},
|
||||
{"empty-with-systems", `{"connections":[],"systems":[]}`},
|
||||
{"empty-with-null-systems", `{"connections":[],"systems":null}`},
|
||||
{"populated", `{"connections":[{"id":"agent:host-1","future":{"counter":9007199254740993},"state":"unknown"},{"id":"pbs:backup","state":"auth-failed"}],"systems":[{"id":"agent:host-1","components":[{"connectionId":"agent:host-1"}]}]}`},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
stdout, stderr, err := runFleetInventoryFixture(t, tc.body, framing == "chunked")
|
||||
if err != nil || stderr != "" {
|
||||
t.Fatalf("explicit inventory rejected: %v, stderr=%q", err, stderr)
|
||||
}
|
||||
var want fleetConnectionsResponse
|
||||
if err := json.Unmarshal([]byte(tc.body), &want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, err := json.MarshalIndent(want, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stdout != string(encoded)+"\n" {
|
||||
t.Fatalf("valid inventory output changed: got %s, want %s", stdout, encoded)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user