8d6a527a2b
Completes the workload-first refactor's plugin layer:
- internal/workload/plugin/ — Source/Trigger plugin contract,
registry, types (Workload, DeploymentIntent, InboundEvent,
PublicFace). Self-registering init() pattern + blank-import
in cmd/server/main.go.
- Source plugins: image (blue-green with multi-face proxy routing),
compose, static. Trigger plugins: registry, git, manual.
- internal/deployer/dispatch.go — DispatchPlugin/Teardown/Reconcile
seam routing the legacy deployer through plugins.
- internal/api/workload_*.go — REST surface: workloads, env,
volumes, chain (parent/children), promote-from. hooks.go
serves /api/hooks/kinds/{kind}/schema for the wizard.
- internal/store: workload_env (encrypt-at-rest secrets) and
workload_volumes tables, keyed on workload_id.
- cmd/server/static_backend.go — phantom-row adapter delegating
the static source plugin to the legacy staticsite.Manager
(deleted at hard cutover once the static inline port lands).
- web/src/routes/apps/ — /apps list + /apps/new wizard +
/apps/[id] detail with kind-aware compose / image / static
forms (Advanced JSON toggle), env panel, volumes panel,
webhook panel, chain panel, manual deploy.
Volume scope generalization (v2 resolver):
- internal/volume.ResolveWorkloadPath (workload-keyed, sits
next to legacy ResolvePath). Honors all VolumeScope values:
absolute, ephemeral, instance, stage, project, project_named,
named. internal/workload/plugin/source/image/image.go
computeMounts wires settings + imageTag through. Coverage in
internal/volume/resolver_test.go (portable Linux/Windows via
t.TempDir).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
217 lines
6.4 KiB
Go
217 lines
6.4 KiB
Go
package api
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"runtime/debug"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// logging is an HTTP middleware that logs every request with method, path,
|
|
// status code, and duration. Webhook URLs are redacted before being logged
|
|
// because the secret is the only authenticator — leaking it to log
|
|
// aggregators is equivalent to leaking the credential.
|
|
func logging(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
wrapped := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
|
|
|
|
next.ServeHTTP(wrapped, r)
|
|
|
|
slog.Info("http request",
|
|
"method", r.Method,
|
|
"path", redactPath(r.URL.Path),
|
|
"status", wrapped.status,
|
|
"duration", time.Since(start).String(),
|
|
)
|
|
})
|
|
}
|
|
|
|
// redactPath strips secrets from URL paths that carry them in segments.
|
|
func redactPath(path string) string {
|
|
const projectPrefix = "/api/webhook/"
|
|
const sitePrefix = "/api/webhook/sites/"
|
|
switch {
|
|
case strings.HasPrefix(path, sitePrefix):
|
|
return sitePrefix + "***"
|
|
case strings.HasPrefix(path, projectPrefix):
|
|
return projectPrefix + "***"
|
|
}
|
|
return path
|
|
}
|
|
|
|
// recovery is an HTTP middleware that catches panics and returns a 500 response.
|
|
func recovery(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
slog.Error("panic recovered", "error", err, "stack", string(debug.Stack()))
|
|
respondError(w, http.StatusInternalServerError, "internal server error")
|
|
}
|
|
}()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// securityHeaders sets standard security headers on all responses.
|
|
func securityHeaders(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
|
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'")
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// cors is an HTTP middleware that handles CORS for same-origin requests.
|
|
// The frontend is served from the same origin, so cross-origin requests are not expected.
|
|
func cors(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// The frontend is served from the same origin, so cross-origin
|
|
// requests are not expected. We do NOT reflect the Origin header
|
|
// back, as that would allow any website to make credentialed requests.
|
|
// If cross-origin support is needed in the future, maintain an
|
|
// explicit allowlist of trusted origins here.
|
|
|
|
if r.Method == http.MethodOptions {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// maxBodySize limits request body sizes to prevent memory exhaustion.
|
|
const maxBodySize = 1 << 20 // 1 MB
|
|
|
|
func limitBody(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// rateLimiter provides per-IP rate limiting for login endpoints.
|
|
type rateLimiter struct {
|
|
mu sync.Mutex
|
|
attempts map[string][]time.Time
|
|
lastCleanup time.Time
|
|
}
|
|
|
|
func newRateLimiter() *rateLimiter {
|
|
return &rateLimiter{
|
|
attempts: make(map[string][]time.Time),
|
|
lastCleanup: time.Now(),
|
|
}
|
|
}
|
|
|
|
// allow checks if the IP is allowed to make another request.
|
|
// Returns false if the IP has exceeded the limit (10 requests per minute).
|
|
func (rl *rateLimiter) allow(ip string) bool {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
window := now.Add(-1 * time.Minute)
|
|
|
|
// Periodically clean all stale IPs to prevent memory leak.
|
|
if now.Sub(rl.lastCleanup) > 5*time.Minute {
|
|
for k, times := range rl.attempts {
|
|
filtered := times[:0]
|
|
for _, t := range times {
|
|
if t.After(window) {
|
|
filtered = append(filtered, t)
|
|
}
|
|
}
|
|
if len(filtered) == 0 {
|
|
delete(rl.attempts, k)
|
|
} else {
|
|
rl.attempts[k] = filtered
|
|
}
|
|
}
|
|
rl.lastCleanup = now
|
|
}
|
|
|
|
// Clean old entries for this IP.
|
|
filtered := rl.attempts[ip][:0]
|
|
for _, t := range rl.attempts[ip] {
|
|
if t.After(window) {
|
|
filtered = append(filtered, t)
|
|
}
|
|
}
|
|
rl.attempts[ip] = filtered
|
|
|
|
if len(filtered) >= 10 {
|
|
return false
|
|
}
|
|
|
|
rl.attempts[ip] = append(rl.attempts[ip], now)
|
|
return true
|
|
}
|
|
|
|
// jsonContentType is an HTTP middleware that sets the default Content-Type to JSON.
|
|
func jsonContentType(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// deprecated marks responses with RFC-8594-style headers so API consumers
|
|
// can detect that an endpoint is on its way out. The Workload-first
|
|
// refactor is migrating away from /api/projects, /api/stages,
|
|
// /api/static_sites, and /api/stacks toward /api/workloads; this signals
|
|
// it to integrators without breaking them. Date is the operator-facing
|
|
// sunset hint, not a hard switch.
|
|
func deprecated(replacement string) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Deprecation", "true")
|
|
if replacement != "" {
|
|
w.Header().Set("Link", `<`+replacement+`>; rel="successor-version"`)
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
// rateLimitMiddleware wraps a handler with per-IP rate limiting using the
|
|
// supplied limiter. Requests over the limit get 429.
|
|
func rateLimitMiddleware(rl *rateLimiter) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ip := r.RemoteAddr
|
|
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
|
|
ip = fwd
|
|
}
|
|
if !rl.allow(ip) {
|
|
respondError(w, http.StatusTooManyRequests, "rate limit exceeded")
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
// statusRecorder wraps http.ResponseWriter to capture the status code.
|
|
type statusRecorder struct {
|
|
http.ResponseWriter
|
|
status int
|
|
}
|
|
|
|
func (r *statusRecorder) WriteHeader(code int) {
|
|
r.status = code
|
|
r.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
// Flush delegates to the underlying ResponseWriter if it supports http.Flusher (needed for SSE).
|
|
func (r *statusRecorder) Flush() {
|
|
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
}
|