Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions server/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,27 @@ paths:
get:
tags: [Setup]
summary: Health check
description: Returns the health status of the database and OIDC subsystem. Used by the container health check.
description: |
Returns a bounded local operator health summary. It reads the database,
persisted configuration, node freshness, and in-process MCP state; it
does not make network calls to OIDC, AI, webhook, notification, or MCP
endpoints. The response does not contain secrets. Used by the
container health check.
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
required: [database, oidc, oidc_enabled]
required: [version, commit, database, oidc, oidc_enabled, mcp, ai, nodes, notifications, webhooks]
Comment on lines 450 to +457

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Document the 503 response.

The responses block lists only "200". The handler returns 503 when the database probe fails. See server/internal/handlers/setup.go line 105. Generated clients and the container health check both depend on that path.

The 503 body uses the same schema, with database set to error and the remaining sections at zero values.

📝 Proposed addition
                       enabled:
                         type: integer
+        "503":
+          description: Database unavailable. The body uses the 200 schema with `database` set to `error`; the remaining sections contain zero values.
+          content:
+            application/json:
+              schema:
+                type: object

Place it after line 532, aligned with the "200" key.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/docs/openapi.yaml` around lines 450 - 457, Add a "503" response entry
to the responses block for the documented endpoint, aligned with the existing
"200" response and using the same application/json schema. Document that this
response represents a failed database probe, with database set to error and the
remaining sections at their zero values.

properties:
version:
type: string
description: Server version set at build time.
commit:
type: string
description: Server commit identifier set at build time.
database:
type: string
enum: [ok, error]
Expand All @@ -459,6 +470,66 @@ paths:
enum: [ok, disabled, unavailable, error]
oidc_enabled:
type: boolean
mcp:
type: object
required: [enabled, token_configured, running]
properties:
enabled:
type: boolean
token_configured:
type: boolean
description: Whether an MCP token exists; the token is never returned.
running:
type: boolean
ai:
type: object
required: [provider, configured, testable, api_key_configured, mode]
properties:
provider:
type: string
configured:
type: boolean
description: Whether provider, URL, and model are present in local settings.
testable:
type: boolean
description: Whether the stored provider settings can be sent to the separate AI test endpoint; no probe is made here.
api_key_configured:
type: boolean
mode:
type: string
nodes:
type: object
required: [total, online, offline, stale]
properties:
total:
type: integer
online:
type: integer
offline:
type: integer
stale:
type: integer
description: Nodes whose last heartbeat is older than the server offline threshold.
notifications:
type: object
required: [configured, total, enabled]
properties:
configured:
type: boolean
total:
type: integer
enabled:
type: integer
webhooks:
type: object
required: [configured, total, enabled]
properties:
configured:
type: boolean
total:
type: integer
enabled:
type: integer

# ── Auth ──────────────────────────────────────────────────────────────────

Expand Down
96 changes: 96 additions & 0 deletions server/internal/handlers/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"blackbox/server/internal/auth"
"blackbox/server/internal/db"
"blackbox/server/internal/handlers"
bbmcp "blackbox/server/internal/mcp"
"blackbox/server/internal/models"
"github.com/oklog/ulid/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -97,6 +101,98 @@ func TestHealthCheck_DBError_Returns503(t *testing.T) {
assert.Equal(t, "error", resp["database"])
}

func TestHealthCheck_RichLocalSummaryDoesNotExposeSecretsOrProbeProviders(t *testing.T) {
database := newTestDB(t)
registry := auth.NewOIDCRegistry(database)
mcpManager := bbmcp.NewMCPManager(database)
require.NoError(t, mcpManager.ApplySettings(true, "mcp-health-secret"))

now := time.Now().UTC()
require.NoError(t, database.Create(&models.AppSetting{Key: "ai_provider", Value: "openai_compat"}).Error)
require.NoError(t, database.Create(&models.AppSetting{Key: "ai_url", Value: "http://127.0.0.1:1"}).Error)
require.NoError(t, database.Create(&models.AppSetting{Key: "ai_model", Value: "local-model"}).Error)
require.NoError(t, database.Create(&models.AppSetting{Key: "ai_api_key", Value: "ai-health-secret"}).Error)
require.NoError(t, database.Create(&models.AppSetting{Key: "mcp_enabled", Value: "true"}).Error)
require.NoError(t, database.Create(&models.AppSetting{Key: "mcp_auth_token", Value: "mcp-health-secret"}).Error)
require.NoError(t, database.Create(&models.NotificationDest{
ID: "notification-1", Name: "Ops", Type: "slack", URL: "https://hooks.example/notification-secret", Enabled: true,
}).Error)
require.NoError(t, database.Create(&models.DataSourceInstance{
ID: ulid.Make().String(), Type: "webhook_uptime_kuma", Scope: models.ScopeServer,
Name: "Uptime Kuma", Config: `{"secret":"webhook-health-secret"}`, Enabled: true,
}).Error)
require.NoError(t, database.Create(&models.DataSourceInstance{
ID: ulid.Make().String(), Type: "webhook_watchtower", Scope: models.ScopeServer,
Name: "Watchtower", Config: `{"secret":"disabled-webhook-secret"}`, Enabled: false,
}).Error)
require.NoError(t, database.Create(&models.Node{ID: "node-online", Name: "online", LastSeen: now, Status: "online"}).Error)
require.NoError(t, database.Create(&models.Node{ID: "node-stale", Name: "stale", LastSeen: now.Add(-10 * time.Minute), Status: "online"}).Error)
require.NoError(t, database.Create(&models.Node{ID: "node-offline", Name: "offline", LastSeen: now, Status: "offline"}).Error)

req := httptest.NewRequest(http.MethodGet, "/api/setup/health", nil)
w := httptest.NewRecorder()
handlers.HealthCheckWithBuildInfo(database, registry, mcpManager, "1.2.3", "abc123")(w, req)

assert.Equal(t, http.StatusOK, w.Code)
body := w.Body.String()
for _, secret := range []string{"mcp-health-secret", "ai-health-secret", "webhook-health-secret", "disabled-webhook-secret", "notification-secret"} {
assert.NotContains(t, body, secret)
}

var resp struct {
Version string `json:"version"`
Commit string `json:"commit"`
MCP struct {
Enabled bool `json:"enabled"`
TokenConfigured bool `json:"token_configured"`
Running bool `json:"running"`
} `json:"mcp"`
AI struct {
Provider string `json:"provider"`
Configured bool `json:"configured"`
Testable bool `json:"testable"`
APIKeyConfigured bool `json:"api_key_configured"`
} `json:"ai"`
Nodes struct {
Total int64 `json:"total"`
Online int64 `json:"online"`
Offline int64 `json:"offline"`
Stale int64 `json:"stale"`
} `json:"nodes"`
Notifications struct {
Configured bool `json:"configured"`
Total int64 `json:"total"`
Enabled int64 `json:"enabled"`
} `json:"notifications"`
Webhooks struct {
Configured bool `json:"configured"`
Total int64 `json:"total"`
Enabled int64 `json:"enabled"`
} `json:"webhooks"`
}
require.NoError(t, json.NewDecoder(w.Body).Decode(&resp))
assert.Equal(t, "1.2.3", resp.Version)
assert.Equal(t, "abc123", resp.Commit)
assert.True(t, resp.MCP.Enabled)
assert.True(t, resp.MCP.TokenConfigured)
assert.True(t, resp.MCP.Running)
assert.Equal(t, "openai_compat", resp.AI.Provider)
assert.True(t, resp.AI.Configured)
assert.True(t, resp.AI.Testable)
assert.True(t, resp.AI.APIKeyConfigured)
assert.Equal(t, int64(3), resp.Nodes.Total)
assert.Equal(t, int64(1), resp.Nodes.Online)
assert.Equal(t, int64(2), resp.Nodes.Offline)
assert.Equal(t, int64(1), resp.Nodes.Stale)
assert.True(t, resp.Notifications.Configured)
assert.Equal(t, int64(1), resp.Notifications.Total)
assert.Equal(t, int64(1), resp.Notifications.Enabled)
assert.True(t, resp.Webhooks.Configured)
assert.Equal(t, int64(2), resp.Webhooks.Total)
assert.Equal(t, int64(1), resp.Webhooks.Enabled)
assert.False(t, strings.Contains(body, "127.0.0.1:1"))
Comment on lines +179 to +193

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Confirm the default value of mode returned by getAISettings.
rg -nP --type=go -C12 'func getAISettings'

# Confirm nodeOfflineAfter, which the stale-node assertion at line 186 depends on.
rg -nP --type=go -C2 '\bnodeOfflineAfter\b\s*='

Repository: maxjb-xyz/blackbox

Length of output: 156


🏁 Script executed:

#!/bin/bash
set -u

printf 'Tracked files matching health_test.go:\n'
git ls-files | rg '(^|/)health_test\.go$' || true

printf '\nSearch for getAISettings:\n'
rg -n --type=go 'getAISettings|nodeOfflineAfter|ai_mode|mode' . || true

printf '\nLocate files around likely paths:\n'
fd -a 'setup\.go|handlers|health' . | sed 's#^\./##' | head -80

Repository: maxjb-xyz/blackbox

Length of output: 50375


🏁 Script executed:

#!/bin/bash
set -u

printf 'health_test.go relevant lines:\n'
sed -n '1,220p' server/internal/handlers/health_test.go

printf '\nsetup.go relevant lines:\n'
sed -n '130,165p' server/internal/handlers/setup.go

printf '\nadmin_config.go relevant lines:\n'
sed -n '420,545p' server/internal/handlers/admin_config.go

printf '\nimports in health_test.go:\n'
sed -n '1,20p' server/internal/handlers/health_test.go

Repository: maxjb-xyz/blackbox

Length of output: 12787


Assert ai.mode and use assert.NotContains for the probe URI check.

AI.Mode comes from getAISettings, which defaults to "analysis", so include ai_mode in the seed and one assertion. Use assert.NotContains(t, body, "127.0.0.1:1") instead of assert.False(t, strings.Contains(...)); then strings is no longer needed.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/internal/handlers/health_test.go` around lines 179 - 193, Update the
health test seed data used by getAISettings to include ai_mode, and assert the
response AI.Mode equals the expected value. Replace the strings.Contains-based
probe URI assertion with assert.NotContains, then remove the now-unused strings
import.

}

func setRegistryProvider(t *testing.T, registry *auth.OIDCRegistry, id string, provider *auth.OIDCProvider) {
t.Helper()
registry.SetProvider(id, provider)
Expand Down
Loading
Loading