This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package reverseproxyapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
"github.com/itworx/pulse/internal/reverseproxy"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Provider reverseproxy.Provider
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/reverse-proxy" {
|
||||
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 reverse-proxy routes.", nil)
|
||||
return
|
||||
}
|
||||
if err := r.Context().Err(); err != nil {
|
||||
return
|
||||
}
|
||||
snapshot, err := h.snapshot(r)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return
|
||||
}
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "REVERSE_PROXY_UNAVAILABLE", "Reverse proxy not available", "De reverse-proxygegevens konden niet worden gelezen.", nil)
|
||||
return
|
||||
}
|
||||
if len(snapshot.Routes) > 150 {
|
||||
snapshot.Routes = snapshot.Routes[:150]
|
||||
snapshot.Total = len(snapshot.Routes)
|
||||
}
|
||||
writeJSON(w, snapshot)
|
||||
}
|
||||
|
||||
func (h Handler) snapshot(r *http.Request) (reverseproxy.Snapshot, error) {
|
||||
if h.Provider == nil {
|
||||
return reverseproxy.DisabledSnapshot(time.Now().UTC(), "reverse-proxy", "connector", "connector_disabled"), nil
|
||||
}
|
||||
return h.Provider.Snapshot(r.Context())
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=5")
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package reverseproxyapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/reverseproxy"
|
||||
)
|
||||
|
||||
type staticProvider struct {
|
||||
snapshot reverseproxy.Snapshot
|
||||
err error
|
||||
}
|
||||
|
||||
func (p staticProvider) Snapshot(context.Context) (reverseproxy.Snapshot, error) {
|
||||
return p.snapshot, p.err
|
||||
}
|
||||
|
||||
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 TestHandlerRequiresAuthAndReturnsDisabledSnapshot(t *testing.T) {
|
||||
handler := Handler{}
|
||||
unauthorized := httptest.NewRecorder()
|
||||
handler.ServeHTTP(unauthorized, httptest.NewRequest(http.MethodGet, "/api/v1/reverse-proxy", nil))
|
||||
if unauthorized.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unauthorized status=%d", unauthorized.Code)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/reverse-proxy"))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), "\"state\":\"disabled\"") || !strings.Contains(response.Body.String(), "\"routes\":[]") {
|
||||
t.Fatalf("disabled response status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerPreservesRouteSourceAndMapsProviderError(t *testing.T) {
|
||||
handler := Handler{Provider: staticProvider{snapshot: reverseproxy.Snapshot{Source: reverseproxy.Source{ID: "npm", State: reverseproxy.StateEnabled}, Routes: []reverseproxy.Route{{ID: "route-1", SourceID: "npm", Hostname: "pulse.example.test", URL: "https://pulse.example.test", TargetServiceID: "svc", Enabled: true}}, Total: 1}}}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/reverse-proxy"))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), "\"sourceId\":\"npm\"") {
|
||||
t.Fatalf("route response status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
failed := Handler{Provider: staticProvider{err: errors.New("backend unavailable")}}
|
||||
failure := httptest.NewRecorder()
|
||||
failed.ServeHTTP(failure, authenticatedRequest(http.MethodGet, "/api/v1/reverse-proxy"))
|
||||
if failure.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("failure status=%d", failure.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user