mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 15:51:26 +00:00
fix: e2e QA hardening — honest runtime URLs, health, and read-only anon UI
Backend: - managed DB provisioning: attach DB containers to the platform network and probe the container IP, fixing the 127.0.0.1 reachability failure inside the backend container; log provisioning failures - stop fabricating connection URLs when provisioning produced none - template deploys now carry port + health_check into service runtime fields - service health probed over the project network (backend joins it) instead of an in-container wget that fails on shell-less images - runtime/public/connection URLs rewrite loopback to the request Host so links are usable for LAN/remote clients Frontend: - service Networking panel shows real runtime URLs/ports/health instead of hardcoded containr.dev + 8080->443 - anonymous views hide all mutation controls (new project, add service, group, context menu) - project cards: remove decorative svcN labels; badge says DOWN only when zero services run, IDLE when empty, DEGRADED for partial - mobile projects header no longer clips (fluid search width) - unknown routes render a not-found page instead of silently redirecting Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
d968eb2315
commit
9f5dcc7296
12 files changed
+404
-137
No files matched your search
@@ -23,6 +23,7 @@ import (
|
||||
|
||||
dockercontainer "github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -196,7 +197,7 @@ func (h *DatabaseHandler) GetDatabases(c *gin.Context) {
|
||||
db.Backups = DatabaseBackupConfig{Backups: []DatabaseBackup{}}
|
||||
}
|
||||
db.Settings = h.generateMockSettings()
|
||||
db.ConnectionURL = h.resolveConnectionURL(db)
|
||||
db.ConnectionURL = rewriteLoopbackURL(h.resolveConnectionURL(db), requestHostname(c))
|
||||
} else {
|
||||
db.Backups = DatabaseBackupConfig{Backups: []DatabaseBackup{}}
|
||||
db.ConnectionURL = ""
|
||||
@@ -235,7 +236,7 @@ func (h *DatabaseHandler) GetDatabase(c *gin.Context) {
|
||||
db.Backups = DatabaseBackupConfig{Backups: []DatabaseBackup{}}
|
||||
}
|
||||
db.Settings = h.generateMockSettings()
|
||||
db.ConnectionURL = h.resolveConnectionURL(db)
|
||||
db.ConnectionURL = rewriteLoopbackURL(h.resolveConnectionURL(db), requestHostname(c))
|
||||
} else {
|
||||
db.Backups = DatabaseBackupConfig{Backups: []DatabaseBackup{}}
|
||||
db.ConnectionURL = ""
|
||||
@@ -942,33 +943,47 @@ func (h *DatabaseHandler) generateMockSettings() DatabaseSettings {
|
||||
}
|
||||
}
|
||||
|
||||
// resolveConnectionURL returns the stored connection URL for privileged
|
||||
// viewers, or "" when the runtime has not produced one (not provisioned yet).
|
||||
func (h *DatabaseHandler) resolveConnectionURL(db DatabaseService) string {
|
||||
if strings.TrimSpace(db.ConnectionURL) != "" {
|
||||
return db.ConnectionURL
|
||||
}
|
||||
return h.generateConnectionURL(db)
|
||||
return strings.TrimSpace(db.ConnectionURL)
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) generateConnectionURL(db DatabaseService) string {
|
||||
safeName := sanitizeDBIdentifier(db.Name, "app")
|
||||
switch db.Type {
|
||||
case "postgresql":
|
||||
return fmt.Sprintf("postgresql://user:password@%s.containr.local:5432/%s", safeName, safeName)
|
||||
case "redis":
|
||||
return fmt.Sprintf("redis://%s.containr.local:6379", safeName)
|
||||
case "dragonfly":
|
||||
return fmt.Sprintf("redis://%s.containr.local:6379", safeName)
|
||||
case "mysql":
|
||||
return fmt.Sprintf("mysql://user:password@%s.containr.local:3306/%s", safeName, safeName)
|
||||
case "mariadb":
|
||||
return fmt.Sprintf("mysql://user:password@%s.containr.local:3306/%s", safeName, safeName)
|
||||
case "mongodb":
|
||||
return fmt.Sprintf("mongodb://user:password@%s.containr.local:27017/%s", safeName, safeName)
|
||||
case "clickhouse":
|
||||
return fmt.Sprintf("http://%s.containr.local:8123", safeName)
|
||||
default:
|
||||
// requestHostname returns the host the client used to reach the API, without
|
||||
// the port. Empty when it cannot be determined.
|
||||
func requestHostname(c *gin.Context) string {
|
||||
host := strings.TrimSpace(c.Request.Host)
|
||||
if host == "" {
|
||||
return ""
|
||||
}
|
||||
if h, _, err := net.SplitHostPort(host); err == nil {
|
||||
return strings.Trim(h, "[]")
|
||||
}
|
||||
return strings.Trim(host, "[]")
|
||||
}
|
||||
|
||||
// rewriteLoopbackURL swaps loopback/unspecified hosts in rawURL for the host
|
||||
// the caller actually used, so URLs handed to remote browsers are usable.
|
||||
func rewriteLoopbackURL(rawURL, host string) string {
|
||||
if rawURL == "" || host == "" {
|
||||
return rawURL
|
||||
}
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return rawURL
|
||||
}
|
||||
switch strings.ToLower(u.Hostname()) {
|
||||
case "localhost", "127.0.0.1", "0.0.0.0", "::1", "":
|
||||
default:
|
||||
return rawURL
|
||||
}
|
||||
port := u.Port()
|
||||
if port == "" {
|
||||
u.Host = host
|
||||
} else {
|
||||
u.Host = net.JoinHostPort(host, port)
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) reconcileManagedDatabaseState(ctx context.Context, db DatabaseService) DatabaseService {
|
||||
@@ -1223,13 +1238,13 @@ func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseNam
|
||||
defer cancel()
|
||||
|
||||
if h.dockerClient == nil {
|
||||
fallbackURL := h.generateConnectionURL(DatabaseService{Name: databaseName, Type: dbType})
|
||||
_ = h.setDatabaseStatusAndConnection(databaseID, "running", sql.NullString{String: fallbackURL, Valid: fallbackURL != ""})
|
||||
_ = h.setDatabaseStatusAndConnection(databaseID, "running", sql.NullString{})
|
||||
return
|
||||
}
|
||||
|
||||
connectionURL, err := h.provisionDatabaseRuntime(ctx, databaseID, databaseName, dbType, runtimeVariables)
|
||||
if err != nil {
|
||||
log.Printf("containr: managed database %s (%s) provisioning failed: %v", databaseID, dbType, err)
|
||||
_ = h.setDatabaseStatusAndConnection(databaseID, "error", sql.NullString{})
|
||||
return
|
||||
}
|
||||
@@ -1337,7 +1352,7 @@ func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, database
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := waitForRuntimePortReadiness(ctx, hostPort); err != nil {
|
||||
if err := h.waitForDatabaseReadiness(ctx, containerID, hostPort, plan.Port.Port()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -1371,12 +1386,24 @@ func (h *DatabaseHandler) resolvePublishedHostPort(ctx context.Context, containe
|
||||
return "", fmt.Errorf("published host port not available")
|
||||
}
|
||||
|
||||
func waitForRuntimePortReadiness(ctx context.Context, hostPort string) error {
|
||||
addr := net.JoinHostPort("127.0.0.1", strings.TrimSpace(hostPort))
|
||||
deadline := time.Now().Add(30 * time.Second)
|
||||
// waitForDatabaseReadiness probes the database's TCP port until it accepts a
|
||||
// connection. The published port lives on host loopback, which a containerized
|
||||
// backend cannot reach; in that case the database is additionally attached to
|
||||
// the backend's own network and probed by container IP.
|
||||
func (h *DatabaseHandler) waitForDatabaseReadiness(ctx context.Context, containerID, hostPort, containerPort string) error {
|
||||
probeAddr := net.JoinHostPort("127.0.0.1", strings.TrimSpace(hostPort))
|
||||
if backendNet := h.dockerClient.OwnNetworkName(ctx); backendNet != "" {
|
||||
if err := h.dockerClient.ConnectNetwork(ctx, backendNet, containerID, network.EndpointSettings{}); err != nil {
|
||||
log.Printf("containr: attach database %s to %s failed: %v", containerID, backendNet, err)
|
||||
}
|
||||
if ip := h.containerIPOnNetwork(ctx, containerID, backendNet); ip != "" {
|
||||
probeAddr = net.JoinHostPort(ip, containerPort)
|
||||
}
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(30 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err := net.DialTimeout("tcp", addr, 1500*time.Millisecond)
|
||||
conn, err := net.DialTimeout("tcp", probeAddr, 1500*time.Millisecond)
|
||||
if err == nil {
|
||||
_ = conn.Close()
|
||||
return nil
|
||||
@@ -1389,7 +1416,18 @@ func waitForRuntimePortReadiness(ctx context.Context, hostPort string) error {
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("database runtime did not become reachable on %s", addr)
|
||||
return fmt.Errorf("database runtime did not become reachable on %s", probeAddr)
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) containerIPOnNetwork(ctx context.Context, containerID, networkName string) string {
|
||||
inspect, err := h.dockerClient.GetContainer(ctx, containerID)
|
||||
if err != nil || inspect.NetworkSettings == nil {
|
||||
return ""
|
||||
}
|
||||
if ep, ok := inspect.NetworkSettings.Networks[networkName]; ok && ep != nil {
|
||||
return strings.TrimSpace(ep.IPAddress)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) ensureImage(ctx context.Context, imageRef string) error {
|
||||
|
||||
@@ -51,15 +51,16 @@ func TestDatabaseConnectionURLAndDefaultVersion(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
url := handler.generateConnectionURL(DatabaseService{
|
||||
Type: tc.dbType,
|
||||
Name: "example",
|
||||
})
|
||||
if url == "" {
|
||||
t.Fatalf("generateConnectionURL returned empty for type %q", tc.dbType)
|
||||
plan, err := buildDatabaseRuntimePlan(tc.dbType, "example", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("buildDatabaseRuntimePlan(%q) failed: %v", tc.dbType, err)
|
||||
}
|
||||
url := plan.ConnectionURL("15432")
|
||||
if len(tc.urlContains) > 0 && !strings.HasPrefix(url, tc.urlContains) {
|
||||
t.Fatalf("generateConnectionURL(%q) = %q, expected prefix %q", tc.dbType, url, tc.urlContains)
|
||||
t.Fatalf("ConnectionURL(%q) = %q, expected prefix %q", tc.dbType, url, tc.urlContains)
|
||||
}
|
||||
if !strings.Contains(url, ":15432") {
|
||||
t.Fatalf("ConnectionURL(%q) = %q, expected published port", tc.dbType, url)
|
||||
}
|
||||
|
||||
gotVersion := handler.getDefaultVersion(tc.dbType)
|
||||
@@ -69,6 +70,17 @@ func TestDatabaseConnectionURLAndDefaultVersion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveConnectionURLNeverFabricates(t *testing.T) {
|
||||
handler := &DatabaseHandler{}
|
||||
|
||||
if got := handler.resolveConnectionURL(DatabaseService{Type: "postgresql", Name: "example"}); got != "" {
|
||||
t.Fatalf("resolveConnectionURL with no stored URL = %q, want empty", got)
|
||||
}
|
||||
if got := handler.resolveConnectionURL(DatabaseService{ConnectionURL: "postgresql://u:p@localhost:15432/db"}); got != "postgresql://u:p@localhost:15432/db" {
|
||||
t.Fatalf("resolveConnectionURL = %q, want stored URL", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDatabaseRuntimePlanSupportsAllTypes(t *testing.T) {
|
||||
cases := []struct {
|
||||
dbType string
|
||||
@@ -199,3 +211,23 @@ func TestHumanReadableBytes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteLoopbackURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, raw, host, want string
|
||||
}{
|
||||
{"service url", "http://localhost:35981", "10.0.0.182", "http://10.0.0.182:35981"},
|
||||
{"db url keeps creds", "postgresql://u:p@localhost:35432/db?sslmode=disable", "10.0.0.182", "postgresql://u:[email protected]:35432/db?sslmode=disable"},
|
||||
{"ipv4 loopback", "redis://:[email protected]:36379/0", "host.lan", "redis://:[email protected]:36379/0"},
|
||||
{"unspecified", "http://0.0.0.0:9000/x", "10.0.0.182", "http://10.0.0.182:9000/x"},
|
||||
{"external host untouched", "https://example.com:443/x", "10.0.0.182", "https://example.com:443/x"},
|
||||
{"empty raw", "", "10.0.0.182", ""},
|
||||
{"empty host", "http://localhost:35981", "", "http://localhost:35981"},
|
||||
{"no port", "http://localhost/path", "10.0.0.182", "http://10.0.0.182/path"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := rewriteLoopbackURL(tt.raw, tt.host); got != tt.want {
|
||||
t.Errorf("%s: rewriteLoopbackURL(%q,%q) = %q, want %q", tt.name, tt.raw, tt.host, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,6 +283,12 @@ func handleGetServiceRuntime(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
engine.ProbeServiceHealth(c.Request.Context(), state, service.ProjectID.String(), service.Name, service.Port, service.HealthCheckPath)
|
||||
if host := requestHostname(c); host != "" {
|
||||
for i, u := range state.URLs {
|
||||
state.URLs[i] = rewriteLoopbackURL(u, host)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"runtime": state})
|
||||
}
|
||||
|
||||
@@ -418,7 +424,7 @@ func liveServiceStatus(c *gin.Context, db *database.DB, service *Service) {
|
||||
if service.Domain != "" {
|
||||
service.PublicURL = "https://" + service.Domain
|
||||
} else if len(state.URLs) > 0 {
|
||||
service.PublicURL = state.URLs[0]
|
||||
service.PublicURL = rewriteLoopbackURL(state.URLs[0], requestHostname(c))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -336,6 +336,16 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// sqlc's schema predates the runtime columns; port and healthcheck_path are
|
||||
// written via raw SQL like every other service mutation path.
|
||||
if _, err = tx.ExecContext(ctx,
|
||||
`UPDATE services SET port = $1, healthcheck_path = $2 WHERE id = $3`,
|
||||
config.Port, strings.TrimSpace(config.HealthCheck), serviceID,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create service from template"})
|
||||
return
|
||||
}
|
||||
|
||||
for key, value := range envVars {
|
||||
if strings.TrimSpace(key) == "" {
|
||||
continue
|
||||
@@ -731,7 +741,7 @@ func SeedTemplates() []ServiceTemplate {
|
||||
Name: "Background Worker",
|
||||
Description: "Background job processing service",
|
||||
Category: "worker",
|
||||
Logo: "https://cdn.simpleicons.org/terminal",
|
||||
Logo: "https://cdn.simpleicons.org/gnubash",
|
||||
Config: `{"type":"worker","runtime":"node","build_command":"npm install","start_command":"npm run worker"}`,
|
||||
Variables: `[{"key":"WORKER_CONCURRENCY","label":"Concurrency","default":"4","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
@@ -741,7 +751,7 @@ func SeedTemplates() []ServiceTemplate {
|
||||
Name: "Cron Job",
|
||||
Description: "Scheduled task runner",
|
||||
Category: "cron",
|
||||
Logo: "https://cdn.simpleicons.org/clock",
|
||||
Logo: "https://cdn.simpleicons.org/clockify",
|
||||
Config: `{"type":"cron","runtime":"node","build_command":"npm install","start_command":"npm run cron"}`,
|
||||
Variables: `[{"key":"CRON_SCHEDULE","label":"Schedule","default":"0 * * * *","required":true,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
|
||||
Reference in new issue
Block a user