mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-18 17:55:26 +01:00
admin: manage filer webhook targets from the UI with live reload (#12690)
* webhook: share the default target settings Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * webhook: managed webhook targets with a drainable queue Targets kept as JSON (name, endpoint, token, filters, retry and worker settings) build their own queue; Close waits for queued and in-flight deliveries before stopping it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: run webhook targets stored in /etc/seaweedfs when notification.webhook.managed is set The targets in /etc/seaweedfs/notification_webhooks.json start with the filer and are rebuilt from the store whenever the file changes on any filer, alongside notification.toml. The previous queue drains before it stops. Events for the file itself, which holds bearer tokens, are not sent to notification queues. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin: manage filer webhook targets A Webhooks page under Management lists, adds, edits and deletes the targets kept in /etc/seaweedfs/notification_webhooks.json. Bearer tokens are write-only: the API reports whether one is set, and an empty token on edit keeps the stored one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep the webhook queue when the stored targets did not change Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: keep a replaced webhook queue open until its senders hand over Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * admin: read chunk-backed webhook targets before editing them Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: keep the stored webhook targets out of fs.meta.notify Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * webhook: reject negative managed target settings Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * webhook: no signal handler on queues that reloads replace Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: swap the webhook queue without waiting behind blocked senders The read lock held across SendMessage made a reload wait for any sender blocked on a saturated target, and the waiting writer stalled every other filer write with it, which is when an operator disables that target. Close now refuses new sends and waits for the senders already inside SendMessage, and a send that hits the closing queue retries on its replacement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * webhook: release blocked test handlers before the server shuts down Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: always run the webhook targets managed in the admin UI Drop the notification.webhook.managed switch: every filer loads and live reloads the stored targets. Without stored targets no queue is created. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: place the admin-managed webhook note above enabled Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: separate the admin-managed webhook note Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * scaffold: note that admin-managed webhooks follow the notification queue Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: retry loading webhook targets after a failed read Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: retire the admin-managed webhook queue on shutdown Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
386385101d
commit
95f46c8fff
19 files changed
+1844
-250
No files matched your search
@@ -0,0 +1,192 @@
|
||||
package dash
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/notification/webhook"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
)
|
||||
|
||||
// NotificationWebhook is a stored webhook target as shown to the browser:
|
||||
// the bearer token never leaves the server.
|
||||
type NotificationWebhook struct {
|
||||
webhook.ManagedTarget
|
||||
HasBearerToken bool `json:"has_bearer_token"`
|
||||
}
|
||||
|
||||
type NotificationWebhooksData struct {
|
||||
Webhooks []NotificationWebhook `json:"webhooks"`
|
||||
ConfigPath string `json:"config_path"`
|
||||
}
|
||||
|
||||
// NotificationWebhookRequest creates or replaces a target. An empty
|
||||
// bearer_token keeps the stored one unless clear_bearer_token is set.
|
||||
type NotificationWebhookRequest struct {
|
||||
webhook.ManagedTarget
|
||||
ClearBearerToken bool `json:"clear_bearer_token"`
|
||||
}
|
||||
|
||||
var notificationWebhooksMu sync.Mutex
|
||||
|
||||
func (s *AdminServer) readNotificationWebhooks(ctx context.Context) (*webhook.ManagedConfig, error) {
|
||||
var conf *webhook.ManagedConfig
|
||||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
||||
var buf bytes.Buffer
|
||||
err := filer.ReadEntry(s.masterClient, client, filer.DirectoryEtcSeaweedFS, filer.NotificationWebhooksConfName, &buf)
|
||||
if err != nil && !errors.Is(err, filer_pb.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
conf, err = webhook.ParseManagedConfig(buf.Bytes())
|
||||
return err
|
||||
})
|
||||
return conf, err
|
||||
}
|
||||
|
||||
func (s *AdminServer) saveNotificationWebhooks(ctx context.Context, conf *webhook.ManagedConfig) error {
|
||||
if err := conf.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
content, err := json.MarshalIndent(conf, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
||||
return filer.SaveInsideFiler(ctx, client, filer.DirectoryEtcSeaweedFS, filer.NotificationWebhooksConfName, content)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *AdminServer) GetNotificationWebhooks(ctx context.Context) (*NotificationWebhooksData, error) {
|
||||
conf, err := s.readNotificationWebhooks(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := &NotificationWebhooksData{
|
||||
Webhooks: make([]NotificationWebhook, 0, len(conf.Targets)),
|
||||
ConfigPath: filer.NotificationWebhooksConfPath,
|
||||
}
|
||||
for _, t := range conf.Targets {
|
||||
hasToken := t.BearerToken != ""
|
||||
t.BearerToken = ""
|
||||
data.Webhooks = append(data.Webhooks, NotificationWebhook{ManagedTarget: t, HasBearerToken: hasToken})
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *AdminServer) GetNotificationWebhooksAPI(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := s.GetNotificationWebhooks(r.Context())
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to read webhook targets: "+err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (s *AdminServer) CreateNotificationWebhookAPI(w http.ResponseWriter, r *http.Request) {
|
||||
s.putNotificationWebhook(w, r, "", true)
|
||||
}
|
||||
|
||||
func (s *AdminServer) UpdateNotificationWebhookAPI(w http.ResponseWriter, r *http.Request) {
|
||||
s.putNotificationWebhook(w, r, mux.Vars(r)["name"], false)
|
||||
}
|
||||
|
||||
func (s *AdminServer) putNotificationWebhook(w http.ResponseWriter, r *http.Request, name string, create bool) {
|
||||
if !requireSessionCSRFToken(w, r) {
|
||||
return
|
||||
}
|
||||
var req NotificationWebhookRequest
|
||||
if err := decodeJSONBody(newJSONMaxReader(w, r), &req); err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
if create {
|
||||
name = req.Name
|
||||
} else if req.Name != "" && req.Name != name {
|
||||
writeJSONError(w, http.StatusBadRequest, "Webhook target name cannot be changed")
|
||||
return
|
||||
}
|
||||
target := req.ManagedTarget
|
||||
target.Name = name
|
||||
|
||||
notificationWebhooksMu.Lock()
|
||||
defer notificationWebhooksMu.Unlock()
|
||||
conf, err := s.readNotificationWebhooks(r.Context())
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to read webhook targets: "+err.Error())
|
||||
return
|
||||
}
|
||||
index := -1
|
||||
for i := range conf.Targets {
|
||||
if conf.Targets[i].Name == name {
|
||||
index = i
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case create && index >= 0:
|
||||
writeJSONError(w, http.StatusConflict, "Webhook target already exists")
|
||||
return
|
||||
case !create && index < 0:
|
||||
writeJSONError(w, http.StatusNotFound, "Webhook target not found")
|
||||
return
|
||||
}
|
||||
if index >= 0 && target.BearerToken == "" && !req.ClearBearerToken {
|
||||
target.BearerToken = conf.Targets[index].BearerToken
|
||||
}
|
||||
if err := target.Validate(); err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if index >= 0 {
|
||||
conf.Targets[index] = target
|
||||
} else {
|
||||
conf.Targets = append(conf.Targets, target)
|
||||
}
|
||||
if err := s.saveNotificationWebhooks(r.Context(), conf); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to save webhook targets: "+err.Error())
|
||||
return
|
||||
}
|
||||
status := http.StatusOK
|
||||
if create {
|
||||
status = http.StatusCreated
|
||||
}
|
||||
hasToken := target.BearerToken != ""
|
||||
target.BearerToken = ""
|
||||
writeJSON(w, status, NotificationWebhook{ManagedTarget: target, HasBearerToken: hasToken})
|
||||
}
|
||||
|
||||
func (s *AdminServer) DeleteNotificationWebhookAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !requireSessionCSRFToken(w, r) {
|
||||
return
|
||||
}
|
||||
name := mux.Vars(r)["name"]
|
||||
|
||||
notificationWebhooksMu.Lock()
|
||||
defer notificationWebhooksMu.Unlock()
|
||||
conf, err := s.readNotificationWebhooks(r.Context())
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to read webhook targets: "+err.Error())
|
||||
return
|
||||
}
|
||||
kept := conf.Targets[:0]
|
||||
for _, t := range conf.Targets {
|
||||
if t.Name != name {
|
||||
kept = append(kept, t)
|
||||
}
|
||||
}
|
||||
if len(kept) == len(conf.Targets) {
|
||||
writeJSONError(w, http.StatusNotFound, "Webhook target not found")
|
||||
return
|
||||
}
|
||||
conf.Targets = kept
|
||||
if err := s.saveNotificationWebhooks(r.Context(), conf); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to save webhook targets: "+err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"message": "Webhook target deleted"})
|
||||
}
|
||||
Reference in new issue
Block a user