2026-08-26 13:47:36 -07:00
|
|
|
package serviceadapter
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-27 11:16:15 -07:00
|
|
|
"context"
|
2026-08-26 13:47:36 -07:00
|
|
|
"fmt"
|
2026-08-27 08:55:41 -07:00
|
|
|
"net"
|
2026-08-26 13:47:36 -07:00
|
|
|
"net/http"
|
2026-08-27 08:55:41 -07:00
|
|
|
"net/http/httputil"
|
|
|
|
|
"net/url"
|
2026-08-27 11:16:15 -07:00
|
|
|
"strings"
|
2026-08-27 08:55:41 -07:00
|
|
|
"time"
|
2026-08-26 13:47:36 -07:00
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
|
2026-08-26 13:47:36 -07:00
|
|
|
"forgejo.riotpiao.com/rock/homelab-frontend/internal/problem"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// Dispatcher routes X-Service requests to upstreams.
|
2026-08-27 11:33:04 -07:00
|
|
|
// Auth per service:
|
|
|
|
|
// SQS: Gateway validates JWT (kmsvc code unverified)
|
|
|
|
|
// MinIO, Temporal: Native JWT support (dumb pipe pass-through)
|
|
|
|
|
// Memory, IAM: Services validate JWTs themselves
|
2026-08-26 13:47:36 -07:00
|
|
|
type Dispatcher struct {
|
2026-08-27 11:16:15 -07:00
|
|
|
registry *Registry
|
2026-08-26 13:47:36 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// NewDispatcher creates a new service adapter dispatcher.
|
2026-08-26 13:47:36 -07:00
|
|
|
func NewDispatcher(registry *Registry) *Dispatcher {
|
2026-08-27 11:16:15 -07:00
|
|
|
return &Dispatcher{
|
|
|
|
|
registry: registry,
|
2026-08-26 13:47:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Matches returns true if the request should be dispatched based on X-Service header.
|
|
|
|
|
func (d *Dispatcher) Matches(r *http.Request) bool {
|
|
|
|
|
return r.Header.Get("X-Service") != ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dispatch routes a request to the appropriate adapter.
|
|
|
|
|
// Returns a problem document if the adapter or resource is not found.
|
|
|
|
|
func (d *Dispatcher) Dispatch(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
serviceName := r.Header.Get("X-Service")
|
|
|
|
|
if serviceName == "" {
|
|
|
|
|
d.writeError(w, problem.BadRequest("X-Service header required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look up service adapter
|
|
|
|
|
adapter := d.registry.Get(serviceName)
|
|
|
|
|
if adapter == nil {
|
|
|
|
|
p := problem.NotFound(fmt.Sprintf("service '%s' not found", serviceName))
|
|
|
|
|
_ = p.Write(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get resource and method from request
|
|
|
|
|
resourceName := r.Header.Get("X-Resource")
|
|
|
|
|
if resourceName == "" {
|
|
|
|
|
d.writeError(w, problem.BadRequest("X-Resource header required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find resource
|
|
|
|
|
var resource *Resource
|
|
|
|
|
for i := range adapter.Spec.Resources {
|
|
|
|
|
if adapter.Spec.Resources[i].Name == resourceName {
|
|
|
|
|
resource = &adapter.Spec.Resources[i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resource == nil {
|
|
|
|
|
p := problem.NotFound(fmt.Sprintf("resource '%s' not found in service '%s'", resourceName, serviceName))
|
|
|
|
|
_ = p.Write(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find method matching HTTP verb
|
|
|
|
|
var method *Method
|
|
|
|
|
for i := range resource.Methods {
|
|
|
|
|
if resource.Methods[i].Verb == r.Method {
|
|
|
|
|
method = &resource.Methods[i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if method == nil {
|
|
|
|
|
p := problem.NotFound(fmt.Sprintf("method %s not defined for resource '%s'", r.Method, resourceName))
|
|
|
|
|
_ = p.Write(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:33:04 -07:00
|
|
|
// Gateway-level JWT validation for SQS (code unverified in kmsvc)
|
|
|
|
|
// MinIO, Temporal, Memory, IAM have native JWT support - pass through
|
|
|
|
|
if adapter.Spec.Auth.Required && serviceName == "sqs" {
|
|
|
|
|
if r.Header.Get("Authorization") == "" {
|
|
|
|
|
p := problem.NewProblem(http.StatusForbidden, "about:blank#forbidden",
|
|
|
|
|
"Forbidden", "SQS requires Authorization header")
|
|
|
|
|
_ = p.Write(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// TODO: Phase 3 - validate JWT signature against Authentik JWKS for SQS
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// Detect protocol from upstream URL scheme
|
|
|
|
|
upstreamURL := adapter.Spec.Upstream.URL
|
|
|
|
|
if strings.HasPrefix(upstreamURL, "grpc://") {
|
|
|
|
|
// gRPC upstream (Temporal, etc.)
|
|
|
|
|
d.dispatchGRPC(w, r, upstreamURL, method, adapter)
|
|
|
|
|
} else {
|
|
|
|
|
// HTTP upstream (MinIO, Authentik, etc.)
|
|
|
|
|
d.dispatchHTTP(w, r, upstreamURL, method, adapter)
|
2026-08-26 13:47:36 -07:00
|
|
|
}
|
2026-08-27 11:16:15 -07:00
|
|
|
}
|
2026-08-26 13:47:36 -07:00
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// dispatchHTTP forwards HTTP requests to upstream, passing Authorization header through.
|
|
|
|
|
func (d *Dispatcher) dispatchHTTP(w http.ResponseWriter, r *http.Request, upstreamURL string, method *Method, adapter *ServiceAdapter) {
|
|
|
|
|
parsedURL, err := url.Parse(upstreamURL)
|
2026-08-27 08:55:41 -07:00
|
|
|
if err != nil {
|
|
|
|
|
d.writeError(w, problem.NewProblem(http.StatusInternalServerError, "about:blank#server-error",
|
|
|
|
|
"Internal Server Error", fmt.Sprintf("invalid upstream URL: %v", err)))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// Create reverse proxy
|
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(parsedURL)
|
2026-08-27 08:55:41 -07:00
|
|
|
proxy.Director = func(req *http.Request) {
|
2026-08-27 11:16:15 -07:00
|
|
|
req.URL.Scheme = parsedURL.Scheme
|
|
|
|
|
req.URL.Host = parsedURL.Host
|
2026-08-27 08:55:41 -07:00
|
|
|
req.URL.Path = method.UpstreamPath
|
|
|
|
|
req.RequestURI = ""
|
2026-08-27 11:16:15 -07:00
|
|
|
req.Host = parsedURL.Host
|
|
|
|
|
// Authorization header passes through unchanged
|
2026-08-27 08:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set timeout
|
|
|
|
|
timeout := adapter.Spec.Upstream.TimeoutSeconds
|
|
|
|
|
if timeout <= 0 {
|
|
|
|
|
timeout = 30
|
|
|
|
|
}
|
|
|
|
|
proxy.Transport = &http.Transport{
|
|
|
|
|
DialContext: (&net.Dialer{Timeout: time.Duration(timeout) * time.Second}).DialContext,
|
|
|
|
|
TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Forward the request
|
|
|
|
|
proxy.ServeHTTP(w, r)
|
2026-08-26 13:47:36 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// dispatchGRPC forwards gRPC requests to upstream.
|
|
|
|
|
// gRPC URL format: grpc://host:port
|
|
|
|
|
func (d *Dispatcher) dispatchGRPC(w http.ResponseWriter, r *http.Request, upstreamURL string, method *Method, adapter *ServiceAdapter) {
|
|
|
|
|
// Extract host:port from grpc://host:port
|
|
|
|
|
host := strings.TrimPrefix(upstreamURL, "grpc://")
|
|
|
|
|
if host == upstreamURL {
|
|
|
|
|
d.writeError(w, problem.NewProblem(http.StatusInternalServerError, "about:blank#server-error",
|
|
|
|
|
"Internal Server Error", "invalid gRPC URL format"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-27 11:07:20 -07:00
|
|
|
|
2026-08-27 11:16:15 -07:00
|
|
|
// Validate that this is a gRPC request
|
|
|
|
|
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
|
|
|
|
|
d.writeError(w, problem.NewProblem(http.StatusBadRequest, "about:blank#bad-request",
|
|
|
|
|
"Bad Request", "gRPC service requires application/grpc content-type"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set timeout
|
|
|
|
|
timeout := adapter.Spec.Upstream.TimeoutSeconds
|
|
|
|
|
if timeout <= 0 {
|
|
|
|
|
timeout = 30
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(timeout)*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
// Dial gRPC upstream
|
|
|
|
|
conn, err := grpc.DialContext(ctx, host,
|
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
|
grpc.MaxCallRecvMsgSize(100 * 1024 * 1024), // 100MB
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.writeError(w, problem.NewProblem(http.StatusBadGateway, "about:blank#bad-gateway",
|
|
|
|
|
"Bad Gateway", fmt.Sprintf("failed to dial gRPC upstream: %v", err)))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
// Forward gRPC request
|
|
|
|
|
// Note: Full gRPC forwarding requires grpcproxy or custom middleware.
|
|
|
|
|
// For now, return unimplemented (Temporal support coming in Phase 9)
|
|
|
|
|
d.writeError(w, problem.NewProblem(http.StatusNotImplemented, "about:blank#not-implemented",
|
|
|
|
|
"Not Implemented", "gRPC forwarding not yet implemented - use in-cluster gRPC clients directly"))
|
|
|
|
|
}
|
2026-08-26 13:47:36 -07:00
|
|
|
|
|
|
|
|
func (d *Dispatcher) writeError(w http.ResponseWriter, p *problem.Problem) {
|
|
|
|
|
_ = p.Write(w)
|
|
|
|
|
}
|