Publish ITWorx Pulse source
Public source validation / validate (push) Failing after 3m8s

This commit is contained in:
ITWorx Pulse release export
2026-09-03 02:09:19 +02:00
commit bd774932d5
614 changed files with 77116 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
package hostapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"time"
"github.com/itworx/pulse/internal/auth"
"github.com/itworx/pulse/internal/host"
"github.com/itworx/pulse/internal/problem"
)
type Handler struct{ Provider host.Provider }
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/host" {
http.NotFound(w, r)
return
}
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read host telemetry.", nil)
return
}
if err := r.Context().Err(); err != nil {
return
}
var snapshot host.Snapshot
var err error
if h.Provider == nil {
snapshot = host.UnknownSnapshot(time.Now().UTC(), "host", "agent", "source_unavailable")
} else {
snapshot, err = h.Provider.Snapshot(r.Context())
}
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(r.Context().Err(), context.Canceled) {
return
}
problem.Write(w, r, http.StatusServiceUnavailable, "HOST_UNAVAILABLE", "Hosttelemetrie niet beschikbaar", "De hosttelemetrie kon niet worden gelezen.", nil)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "private, max-age=5")
_ = json.NewEncoder(w).Encode(snapshot)
}
+51
View File
@@ -0,0 +1,51 @@
package hostapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/itworx/pulse/internal/auth"
"github.com/itworx/pulse/internal/host"
)
type provider struct{ snapshot host.Snapshot }
func (p provider) Snapshot(context.Context) (host.Snapshot, error) { return p.snapshot, nil }
func authenticatedRequest(method, path string) *http.Request {
request := httptest.NewRequest(method, path, nil)
return request.WithContext(auth.WithPrincipal(request.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
}
func TestHandlerRequiresAuthenticationAndExposesUnknownFallback(t *testing.T) {
unauthenticated := httptest.NewRecorder()
Handler{}.ServeHTTP(unauthenticated, httptest.NewRequest(http.MethodGet, "/api/v1/host", nil))
if unauthenticated.Code != http.StatusUnauthorized {
t.Fatalf("status=%d body=%s", unauthenticated.Code, unauthenticated.Body.String())
}
response := httptest.NewRecorder()
Handler{}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/host"))
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"state":"unknown"`) {
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
}
}
func TestHandlerReturnsProviderSnapshotAndRejectsOtherRoutes(t *testing.T) {
snapshot := host.UnknownSnapshot(time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC), "agent-1", "agent", "fixture")
response := httptest.NewRecorder()
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/host"))
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"id":"agent-1"`) {
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
}
other := httptest.NewRecorder()
Handler{}.ServeHTTP(other, authenticatedRequest(http.MethodPost, "/api/v1/host"))
if other.Code != http.StatusNotFound {
t.Fatalf("status=%d", other.Code)
}
}