Просмотр исходного кода

Merge branch 'next' into webui-cache-control

James Read 19 часов назад
Родитель
Сommit
0552880d0f

+ 21 - 0
docs/modules/ROOT/pages/advanced_configuration/webui.adoc

@@ -160,6 +160,27 @@ When enabled, the header shows a search control that can jump to actions, dashbo
 
 Other `features.*` flags follow the same pattern: typed booleans under `features`, default false, exposed on Init for the web UI, and treated as alpha until graduated.
 
+[#replace-webui]
+== Replace the web UI (`webUIDir`)
+
+Themes and custom JavaScript change the default web UI. If you want a completely different interface, you can point OliveTin at another directory of static files with `webUIDir`. OliveTin serves `index.html` and everything next to it from that directory, while the API stays on the same origin, so authentication and ACLs apply unchanged.
+
+.`config.yaml`
+[source,yaml]
+----
+webUIDir: /etc/OliveTin/my-webui/
+----
+
+Restart OliveTin after changing `webUIDir`. To go back to the default web UI, remove the option.
+
+Some paths are handled by OliveTin itself and never reach `webUIDir`, so don't use them for your own files: `/api/`, `/oauth/`, `/readyz`, `/webhooks`, `/theme.css`, `/custom-webui/` and, when Prometheus is enabled, `/metrics`. Requests for paths without a file extension (for example `/logs`) are answered with `index.html`, so a single-page app can use client-side routing.
+
+A replacement web UI only talks to the OliveTin API, so it needs to be kept compatible with the version of OliveTin you run.
+
+=== Community web UIs
+
+* https://github.com/DabuSoft-pl/olivetin-lamp[Lamp] - a phone-friendly web UI, with "hold to run" confirmations, live output and an optional status screen built from entities.
+
 == Custom CSS (with a custom theme)
 
 You can customize OliveTin with themes, but it's also possible to write your on very simple theme that contains just a few CSS rules to change the look and feel of OliveTin. This is very useful if you just want to change the colours of OliveTin, or hide a few elements.

+ 1 - 1
docs/modules/ROOT/pages/config.adoc

@@ -131,7 +131,7 @@ All configuration options are covered in the solution sections
 |===
 | Option | Description | Default | Live Reloadable | Documentation
 
-| `webUIDir` | The directory to serve the web UI from. | Calculated at runtime. | Requires Restart | -
+| `webUIDir` | The directory to serve the web UI from. | Calculated at runtime. | Requires Restart | xref:advanced_configuration/webui.adoc#replace-webui[Replace the web UI]
 | `cronSupportForSeconds` | Whether or not to support seconds in cron expressions. | `false` | Requires Restart | xref:action_execution/oncron.adoc[Cron]
 | `saveLogs` | Whether or not to save logs to disk. | `[]` | Requires Restart | xref:logs/saving.adoc[Save Logs]
 | `serviceLogs` | Windows process log directory (`serviceLogs.directory`). | `%ProgramData%\OliveTin\logs\` on Windows | Requires Restart | xref:install/windows_service.adoc#windows-service-logs[Windows service logs]

+ 1 - 1
docs/modules/ROOT/pages/solutions/k8s-control-panel-hosted/index.adoc

@@ -82,7 +82,7 @@ Add `kubectl` job to OliveTin config with `kubectl edit cm/olivetin-config -n ol
 apiVersion: v1
 data:
   config.yaml: |
-    defaultOnClick: execution-dialog-output-only
+    defaultOnClick: execution-dialog
 
     actions:
       - title: get pods

+ 56 - 13
service/internal/config/sanitize.go

@@ -263,6 +263,7 @@ func (action *Action) sanitize(cfg *Config) {
 	action.Icon = lookupHTMLIcon(action.Icon, cfg.DefaultIconForActions)
 	migrateActionOnClick(action)
 	action.sanitizeJustification()
+	action.reportUnknownOnClick(cfg)
 	action.OnClick = sanitizeOnClick(action.OnClick, cfg)
 	action.PopupOnStart = action.OnClick
 
@@ -454,22 +455,63 @@ func getActionID(action *Action) string {
 	return action.ID
 }
 
-//gocyclo:ignore
+// onClickModes are the onclick values that change how an action behaves.
+var onClickModes = []string{
+	"execution-dialog",
+	"execution-dialog-output-html",
+	"execution-dialog-stdout-only",
+	"execution-button",
+	"history",
+}
+
+// onClickDefaultAliases are accepted values that mean "use the default behaviour".
+var onClickDefaultAliases = []string{
+	"",
+	"default",
+	"nothing",
+}
+
+func isKnownOnClick(value string) bool {
+	return slices.Contains(onClickModes, value) || slices.Contains(onClickDefaultAliases, value)
+}
+
 func sanitizeOnClick(raw string, cfg *Config) string {
-	switch raw {
-	case "execution-dialog":
+	if slices.Contains(onClickModes, raw) {
 		return raw
-	case "execution-dialog-output-html":
-		return raw
-	case "execution-dialog-stdout-only":
-		return raw
-	case "execution-button":
-		return raw
-	case "history":
-		return raw
-	default:
-		return cfg.DefaultOnClick
 	}
+
+	return cfg.DefaultOnClick
+}
+
+func (action *Action) reportUnknownOnClick(cfg *Config) {
+	// On a second sanitize pass, an action's onclick may already hold the
+	// (separately reported) defaultOnClick value.
+	if isKnownOnClick(action.OnClick) || action.OnClick == cfg.DefaultOnClick {
+		return
+	}
+
+	configissues.ReportSticky(configissues.Issue{
+		Severity:    configissues.SeverityWarning,
+		Code:        configissues.CodeOnClickUnknown,
+		Message:     fmt.Sprintf("Unknown onclick value %q, using defaultOnClick (%q) instead", action.OnClick, cfg.DefaultOnClick),
+		ActionID:    action.ID,
+		ActionTitle: action.Title,
+		Source:      action.OnClick,
+		ConfigFile:  action.SourceFile,
+	})
+}
+
+func (cfg *Config) reportUnknownDefaultOnClick() {
+	if isKnownOnClick(cfg.DefaultOnClick) {
+		return
+	}
+
+	configissues.ReportSticky(configissues.Issue{
+		Severity: configissues.SeverityWarning,
+		Code:     configissues.CodeOnClickUnknown,
+		Message:  fmt.Sprintf("Unknown defaultOnClick value %q, actions will behave as if it were \"nothing\"", cfg.DefaultOnClick),
+		Source:   cfg.DefaultOnClick,
+	})
 }
 
 func migrateActionOnClick(action *Action) {
@@ -511,6 +553,7 @@ func (cfg *Config) sanitizeOnClickDefaults() {
 		cfg.DefaultOnClick = "nothing"
 	}
 
+	cfg.reportUnknownDefaultOnClick()
 	cfg.DefaultPopupOnStart = cfg.DefaultOnClick
 }
 

+ 50 - 0
service/internal/config/sanitize_test.go

@@ -3,6 +3,7 @@ package config
 import (
 	"testing"
 
+	"github.com/OliveTin/OliveTin/internal/configissues"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 )
@@ -293,3 +294,52 @@ func TestValidateChecklistChoiceValuesAllowsCommas(t *testing.T) {
 	err := c.validateChecklistChoiceValues()
 	require.NoError(t, err)
 }
+
+func stickyIssuesWithCode(code string) []configissues.Issue {
+	out := make([]configissues.Issue, 0)
+	for _, issue := range configissues.CopySticky() {
+		if issue.Code == code {
+			out = append(out, issue)
+		}
+	}
+	return out
+}
+
+func TestSanitizeReportsUnknownOnClick(t *testing.T) {
+	configissues.BeginConfigLoad()
+	c := DefaultConfig()
+	c.Actions = append(c.Actions, &Action{Title: "Ping", OnClick: "myexample"})
+	c.Sanitize()
+
+	issues := stickyIssuesWithCode(configissues.CodeOnClickUnknown)
+	require.Len(t, issues, 1)
+	assert.Equal(t, "Ping", issues[0].ActionTitle)
+	assert.Equal(t, "myexample", issues[0].Source)
+	assert.Equal(t, "nothing", c.findAction("Ping").OnClick, "Unknown onclick still falls back to the default")
+}
+
+func TestSanitizeAcceptsKnownOnClickValues(t *testing.T) {
+	configissues.BeginConfigLoad()
+	c := DefaultConfig()
+	for _, value := range []string{"", "default", "nothing", "execution-dialog", "execution-dialog-output-html", "execution-dialog-stdout-only", "execution-button", "history"} {
+		c.Actions = append(c.Actions, &Action{Title: "Action " + value, OnClick: value})
+	}
+	c.Sanitize()
+
+	assert.Empty(t, stickyIssuesWithCode(configissues.CodeOnClickUnknown))
+}
+
+func TestSanitizeReportsUnknownDefaultOnClickOnce(t *testing.T) {
+	configissues.BeginConfigLoad()
+	c := DefaultConfig()
+	c.DefaultOnClick = "myexample"
+	c.Actions = append(c.Actions, &Action{Title: "Ping"})
+	c.Sanitize()
+	c.Sanitize()
+
+	issues := stickyIssuesWithCode(configissues.CodeOnClickUnknown)
+	require.NotEmpty(t, issues)
+	for _, issue := range issues {
+		assert.Empty(t, issue.ActionTitle, "Only defaultOnClick is reported, not each action inheriting it")
+	}
+}

+ 61 - 0
service/internal/configcheck/dashboard_types.go

@@ -0,0 +1,61 @@
+package configcheck
+
+import (
+	"fmt"
+	"slices"
+
+	"github.com/OliveTin/OliveTin/internal/config"
+	"github.com/OliveTin/OliveTin/internal/configissues"
+)
+
+// knownDashboardComponentTypes are the dashboard component types the web UI
+// can render. An empty type lets OliveTin pick one from the component's contents.
+var knownDashboardComponentTypes = []string{
+	"",
+	"fieldset",
+	"directory",
+	"display",
+	"link",
+	"stdout-most-recent-execution",
+}
+
+func collectDashboardTypeIssues(cfg *config.Config) []configissues.Issue {
+	out := make([]configissues.Issue, 0)
+	visited := make(map[*config.DashboardComponent]bool)
+
+	for _, dashboard := range cfg.Dashboards {
+		out = append(out, dashboardTypeIssues(dashboard, dashboard, visited)...)
+	}
+
+	return out
+}
+
+func dashboardTypeIssues(dashboard, component *config.DashboardComponent, visited map[*config.DashboardComponent]bool) []configissues.Issue {
+	if component == nil || visited[component] {
+		return nil
+	}
+
+	visited[component] = true
+
+	out := unknownDashboardTypeIssue(dashboard, component)
+
+	for _, child := range component.Contents {
+		out = append(out, dashboardTypeIssues(dashboard, child, visited)...)
+	}
+
+	return out
+}
+
+func unknownDashboardTypeIssue(dashboard, component *config.DashboardComponent) []configissues.Issue {
+	if slices.Contains(knownDashboardComponentTypes, component.Type) {
+		return nil
+	}
+
+	return []configissues.Issue{{
+		Severity: configissues.SeverityWarning,
+		Code:     configissues.CodeDashboardTypeUnknown,
+		Message: fmt.Sprintf("Dashboard %q component %q has unknown type %q; a default type will be used instead",
+			dashboard.Title, component.Title, component.Type),
+		Source: component.Type,
+	}}
+}

+ 70 - 0
service/internal/configcheck/dashboard_types_test.go

@@ -0,0 +1,70 @@
+package configcheck_test
+
+import (
+	"testing"
+
+	"github.com/OliveTin/OliveTin/internal/config"
+	"github.com/OliveTin/OliveTin/internal/configcheck"
+	"github.com/OliveTin/OliveTin/internal/configissues"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func TestRebuildUnknownDashboardComponentType(t *testing.T) {
+	configissues.BeginConfigLoad()
+	cfg := config.DefaultConfig()
+	cfg.Dashboards = []*config.DashboardComponent{
+		{
+			Title: "Containers",
+			Contents: []*config.DashboardComponent{
+				{
+					Title:  "Container {{ .CurrentEntity.Names }}",
+					Entity: "container",
+					Type:   "myexample",
+					Contents: []*config.DashboardComponent{
+						{Title: "Start", Type: "display"},
+					},
+				},
+			},
+		},
+	}
+
+	configcheck.Rebuild(cfg)
+
+	issues := issuesWithCode(configissues.List(), configissues.CodeDashboardTypeUnknown)
+	require.Len(t, issues, 1)
+	assert.Equal(t, "myexample", issues[0].Source)
+	assert.Contains(t, issues[0].Message, "Containers")
+}
+
+func TestRebuildKnownDashboardComponentTypes(t *testing.T) {
+	configissues.BeginConfigLoad()
+	cfg := config.DefaultConfig()
+	cfg.Dashboards = []*config.DashboardComponent{
+		{
+			Title: "Everything",
+			Contents: []*config.DashboardComponent{
+				{Title: "a"},
+				{Title: "b", Type: "fieldset"},
+				{Title: "c", Type: "directory"},
+				{Title: "d", Type: "display"},
+				{Title: "e", Type: "link"},
+				{Title: "f", Type: "stdout-most-recent-execution"},
+			},
+		},
+	}
+
+	configcheck.Rebuild(cfg)
+
+	assert.False(t, hasCode(configissues.List(), configissues.CodeDashboardTypeUnknown))
+}
+
+func issuesWithCode(issues []configissues.Issue, code string) []configissues.Issue {
+	out := make([]configissues.Issue, 0)
+	for _, issue := range issues {
+		if issue.Code == code {
+			out = append(out, issue)
+		}
+	}
+	return out
+}

+ 1 - 0
service/internal/configcheck/rebuild.go

@@ -38,6 +38,7 @@ func Rebuild(cfg *config.Config, extra ...configissues.Issue) {
 	collected = append(collected, collectEntityEmptyIssues(cfg)...)
 	collected = append(collected, collectCronIssues(cfg)...)
 	collected = append(collected, collectWatcherPathIssues(cfg)...)
+	collected = append(collected, collectDashboardTypeIssues(cfg)...)
 	collected = append(collected, extra...)
 	configissues.Replace(collected)
 }

+ 2 - 0
service/internal/configissues/issue.go

@@ -23,6 +23,8 @@ const (
 	CodeWatcherPath            = "watcher_path"
 	CodeAclUnknown             = "acl_unknown"
 	CodeDeprecatedAPICall      = "deprecated_api_call"
+	CodeOnClickUnknown         = "onclick_unknown"
+	CodeDashboardTypeUnknown   = "dashboard_type_unknown"
 )
 
 // Issue is a configuration warning or error surfaced on Diagnostics.

+ 29 - 19
service/internal/webhooks/handler.go

@@ -7,9 +7,14 @@ import (
 	"github.com/OliveTin/OliveTin/internal/auth"
 	"github.com/OliveTin/OliveTin/internal/config"
 	"github.com/OliveTin/OliveTin/internal/executor"
+	"github.com/google/uuid"
 	log "github.com/sirupsen/logrus"
 )
 
+// CorrelationIDHeader is the response header carrying the ID that appears in
+// OliveTin's log lines for the same webhook request.
+const CorrelationIDHeader = "X-OliveTin-Log-Correlation-ID"
+
 type ActionWebhookConfig struct {
 	Action *config.Action
 	Config config.WebhookConfig
@@ -33,7 +38,11 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	payload, err := h.readPayload(r)
+	correlationID := uuid.NewString()
+	w.Header().Set(CorrelationIDHeader, correlationID)
+	reqLog := log.WithFields(webhookRequestFields(r, correlationID))
+
+	payload, err := h.readPayload(r, reqLog)
 	if err != nil {
 		http.Error(w, "Failed to read payload", http.StatusBadRequest)
 		return
@@ -41,13 +50,13 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
 
 	matchingActions := h.findMatchingActions(r, payload)
 	if len(matchingActions) == 0 {
-		log.WithFields(webhookRequestFields(r)).Infof("Webhook: no matching actions")
+		reqLog.Infof("Webhook: no matching actions")
 		h.writeOKResponse(w, "no matching webhook actions")
 		return
 	}
 
-	processed := h.processMatchingActions(matchingActions, r, payload)
-	log.WithFields(webhookRequestFields(r)).WithFields(log.Fields{
+	processed := h.processMatchingActions(matchingActions, r, payload, reqLog)
+	reqLog.WithFields(log.Fields{
 		"matched":   len(matchingActions),
 		"processed": processed,
 	}).Infof("Webhook: matched")
@@ -55,19 +64,20 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
 	h.writeOKResponse(w, "webhook actions")
 }
 
-func webhookRequestFields(r *http.Request) log.Fields {
+func webhookRequestFields(r *http.Request, correlationID string) log.Fields {
 	return log.Fields{
-		"method": r.Method,
-		"path":   r.URL.Path,
-		"query":  r.URL.RawQuery,
+		"correlationId": correlationID,
+		"method":        r.Method,
+		"path":          r.URL.Path,
+		"query":         r.URL.RawQuery,
 	}
 }
 
-func (h *WebhookHandler) readPayload(r *http.Request) ([]byte, error) {
+func (h *WebhookHandler) readPayload(r *http.Request, reqLog *log.Entry) ([]byte, error) {
 	maxSize := int64(1024 * 1024)
 	payload, err := io.ReadAll(io.LimitReader(r.Body, maxSize))
 	if err != nil {
-		log.WithFields(log.Fields{
+		reqLog.WithFields(log.Fields{
 			"error": err,
 		}).Warnf("Failed to read webhook payload")
 		return nil, err
@@ -83,10 +93,10 @@ func (h *WebhookHandler) writeOKResponse(w http.ResponseWriter, context string)
 	}
 }
 
-func (h *WebhookHandler) processMatchingActions(matchingActions []ActionWebhookConfig, r *http.Request, payload []byte) int {
+func (h *WebhookHandler) processMatchingActions(matchingActions []ActionWebhookConfig, r *http.Request, payload []byte, reqLog *log.Entry) int {
 	processed := 0
 	for _, actionConfig := range matchingActions {
-		if h.processWebhook(actionConfig, r, payload) {
+		if h.processWebhook(actionConfig, r, payload, reqLog) {
 			processed++
 		}
 	}
@@ -125,10 +135,10 @@ func (h *WebhookHandler) findMatchingWebhooksForAction(action *config.Action, r
 	return matches
 }
 
-func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *http.Request, payload []byte) bool {
+func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *http.Request, payload []byte, reqLog *log.Entry) bool {
 	verifier := NewAuthVerifier(actionConfig.Config)
 	if !verifier.Verify(r, payload) {
-		log.WithFields(log.Fields{
+		reqLog.WithFields(log.Fields{
 			"actionTitle": actionConfig.Action.Title,
 			"authType":    actionConfig.Config.AuthType,
 		}).Warnf("Webhook authentication failed")
@@ -139,7 +149,7 @@ func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *htt
 
 	args, err := matcher.ExtractArguments()
 	if err != nil {
-		log.WithFields(log.Fields{
+		reqLog.WithFields(log.Fields{
 			"actionTitle": actionConfig.Action.Title,
 			"error":       err,
 		}).Warnf("Failed to extract webhook arguments")
@@ -148,21 +158,21 @@ func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *htt
 
 	justification, err := matcher.ExtractJustification()
 	if err != nil {
-		log.WithFields(log.Fields{
+		reqLog.WithFields(log.Fields{
 			"actionTitle": actionConfig.Action.Title,
 			"error":       err,
 		}).Warnf("Failed to extract webhook justification")
 		return false
 	}
 
-	h.executeAction(actionConfig.Action, args, justification)
+	h.executeAction(actionConfig.Action, args, justification, reqLog)
 	return true
 }
 
-func (h *WebhookHandler) executeAction(action *config.Action, args map[string]string, justification string) {
+func (h *WebhookHandler) executeAction(action *config.Action, args map[string]string, justification string, reqLog *log.Entry) {
 	binding := h.executor.FindBindingWithNoEntity(action)
 	if binding == nil {
-		log.WithFields(log.Fields{
+		reqLog.WithFields(log.Fields{
 			"actionTitle": action.Title,
 		}).Warnf("Action binding not found, skipping execution")
 		return

+ 85 - 0
service/internal/webhooks/handler_correlation_test.go

@@ -0,0 +1,85 @@
+package webhooks
+
+import (
+	"net/http"
+	"net/http/httptest"
+	"strings"
+	"testing"
+
+	"github.com/OliveTin/OliveTin/internal/config"
+	"github.com/google/uuid"
+	log "github.com/sirupsen/logrus"
+	"github.com/sirupsen/logrus/hooks/test"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func postWebhook(t *testing.T, cfg *config.Config) (*httptest.ResponseRecorder, *test.Hook) {
+	t.Helper()
+	hook := test.NewGlobal()
+	t.Cleanup(func() { log.StandardLogger().ReplaceHooks(make(log.LevelHooks)) })
+
+	req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks?source=ci", strings.NewReader(`{"ref":"main"}`))
+	rec := httptest.NewRecorder()
+	NewWebhookHandler(cfg, nil).HandleWebhook(rec, req)
+
+	return rec, hook
+}
+
+func requireCorrelationID(t *testing.T, rec *httptest.ResponseRecorder) string {
+	t.Helper()
+	id := rec.Header().Get(CorrelationIDHeader)
+	_, err := uuid.Parse(id)
+	require.NoError(t, err, "header should be a bare UUID, got %q", id)
+	return id
+}
+
+func findLogEntry(hook *test.Hook, message string) *log.Entry {
+	for _, entry := range hook.AllEntries() {
+		if entry.Message == message {
+			return entry
+		}
+	}
+	return nil
+}
+
+func TestWebhookNoMatchReturnsCorrelationIDInHeaderAndLog(t *testing.T) {
+	rec, hook := postWebhook(t, config.DefaultConfig())
+
+	assert.Equal(t, http.StatusOK, rec.Code)
+	id := requireCorrelationID(t, rec)
+
+	entry := findLogEntry(hook, "Webhook: no matching actions")
+	require.NotNil(t, entry)
+	assert.Equal(t, id, entry.Data["correlationId"])
+}
+
+func TestWebhookMatchUsesSameCorrelationIDForEveryLogLine(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.Actions = []*config.Action{{
+		Title: "Deploy",
+		ExecOnWebhook: []config.WebhookConfig{{
+			AuthType: "bearer",
+			Secret:   "not-sent-by-this-request",
+		}},
+	}}
+
+	rec, hook := postWebhook(t, cfg)
+
+	id := requireCorrelationID(t, rec)
+
+	matched := findLogEntry(hook, "Webhook: matched")
+	require.NotNil(t, matched)
+	assert.Equal(t, id, matched.Data["correlationId"])
+
+	authFailed := findLogEntry(hook, "Webhook authentication failed")
+	require.NotNil(t, authFailed)
+	assert.Equal(t, id, authFailed.Data["correlationId"])
+}
+
+func TestWebhookCorrelationIDIsUniquePerRequest(t *testing.T) {
+	first, _ := postWebhook(t, config.DefaultConfig())
+	second, _ := postWebhook(t, config.DefaultConfig())
+
+	assert.NotEqual(t, requireCorrelationID(t, first), requireCorrelationID(t, second))
+}