This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package processapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
"github.com/itworx/pulse/internal/process"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Provider interface {
|
||||
Snapshot(context.Context) (process.Snapshot, error)
|
||||
}
|
||||
Limits process.Limits
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/processes" {
|
||||
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 processes.", nil)
|
||||
return
|
||||
}
|
||||
limit := 50
|
||||
if value := r.URL.Query().Get("limit"); value != "" {
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
problem.Write(w, r, http.StatusBadRequest, "PROCESS_LIMIT_INVALID", "Invalid process limit", "The process page limit is invalid.", nil)
|
||||
return
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
mode := process.SortMode(r.URL.Query().Get("sort"))
|
||||
if mode == "" {
|
||||
mode = process.SortCPU
|
||||
}
|
||||
var snapshot process.Snapshot
|
||||
var err error
|
||||
if h.Provider == nil {
|
||||
snapshot = process.UnknownSnapshot(time.Now().UTC(), "process", "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) {
|
||||
return
|
||||
}
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "PROCESS_UNAVAILABLE", "Processgegevens niet beschikbaar", "De procesgegevens konden niet worden gelezen.", nil)
|
||||
return
|
||||
}
|
||||
page, err := process.FilteredPage(snapshot, mode, limit, r.URL.Query().Get("after"), h.Limits, r.URL.Query().Get("q"), r.URL.Query().Get("container"))
|
||||
if err != nil {
|
||||
problem.Write(w, r, http.StatusBadRequest, "PROCESS_QUERY_INVALID", "Invalid process query", "The process sort, cursor or limit is invalid.", nil)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=5")
|
||||
_ = json.NewEncoder(w).Encode(page)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package processapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/process"
|
||||
)
|
||||
|
||||
type provider struct{ value process.Snapshot }
|
||||
|
||||
func (p provider) Snapshot(context.Context) (process.Snapshot, error) { return p.value, nil }
|
||||
|
||||
func request(path string) *http.Request {
|
||||
r := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
}
|
||||
|
||||
func TestHandlerIsReadOnlyAndBounded(t *testing.T) {
|
||||
response := httptest.NewRecorder()
|
||||
Handler{Provider: provider{value: process.Snapshot{ContractVersion: process.ContractVersion, Source: process.Source{ID: "agent-1", State: "healthy"}, Processes: []process.Process{{PID: 1, Name: "init", State: "running"}}, Total: 1}}}.ServeHTTP(response, request("/api/v1/processes?limit=1&sort=cpu"))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"pid":1`) {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
mutating := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(mutating, httptest.NewRequest(http.MethodPost, "/api/v1/processes/1/kill", nil))
|
||||
if mutating.Code != http.StatusNotFound {
|
||||
t.Fatalf("mutation route status=%d", mutating.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRequiresAuthentication(t *testing.T) {
|
||||
response := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/processes", nil))
|
||||
if response.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status=%d", response.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerAppliesNameAndContainerFilters(t *testing.T) {
|
||||
snapshot := process.Snapshot{Source: process.Source{ID: "agent", State: "healthy"}, Processes: []process.Process{{PID: 1, Name: "api", ContainerName: "pulse"}, {PID: 2, Name: "postgres", ContainerName: "database"}}, Total: 2}
|
||||
response := httptest.NewRecorder()
|
||||
Handler{Provider: provider{value: snapshot}}.ServeHTTP(response, request("/api/v1/processes?limit=25&sort=cpu&q=api&container=pulse"))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"name":"api"`) || !strings.Contains(response.Body.String(), `"total":1`) {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user