|
4 | 4 | package server |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "crypto/subtle" |
7 | 8 | "encoding/json" |
8 | 9 | "fmt" |
9 | 10 | "net/http" |
@@ -129,6 +130,8 @@ func NewTCPHandler(logger *types.Logger, config *types.ServerConfig, server *Ser |
129 | 130 | router.Mount(types.INTERNAL_URL_PREFIX, http.NotFoundHandler()) // reserve the path |
130 | 131 | } |
131 | 132 |
|
| 133 | + router.Mount(types.WEBHOOK_URL_PREFIX, handler.serveWebhooks()) |
| 134 | + |
132 | 135 | server.ssoAuth.RegisterRoutes(router) // register SSO routes |
133 | 136 |
|
134 | 137 | router.HandleFunc("/*", handler.callApp) |
@@ -210,6 +213,87 @@ func (h *Handler) apiHandler(w http.ResponseWriter, r *http.Request, enableBasic |
210 | 213 | } |
211 | 214 | } |
212 | 215 |
|
| 216 | +func (h *Handler) webhookHandler(w http.ResponseWriter, r *http.Request, webhookType types.WebhookType) { |
| 217 | + appPath := r.URL.Query().Get("appPath") |
| 218 | + if appPath == "" { |
| 219 | + http.Error(w, "appPath is required for webhook call", http.StatusBadRequest) |
| 220 | + return |
| 221 | + } |
| 222 | + appPathDomain, err := parseAppPath(appPath) |
| 223 | + if err != nil { |
| 224 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 225 | + return |
| 226 | + } |
| 227 | + |
| 228 | + app, err := h.server.GetApp(appPathDomain, false) |
| 229 | + if err != nil { |
| 230 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 231 | + } |
| 232 | + |
| 233 | + authHeader := r.Header.Get("Authorization") |
| 234 | + if !strings.HasPrefix(authHeader, "Bearer ") { |
| 235 | + http.Error(w, "Authorization header with bearer token is required", http.StatusUnauthorized) |
| 236 | + return |
| 237 | + } |
| 238 | + authToken := strings.TrimSpace(strings.TrimPrefix(authHeader, "Bearer ")) |
| 239 | + if authToken == "" { |
| 240 | + http.Error(w, "Bearer token is required", http.StatusUnauthorized) |
| 241 | + return |
| 242 | + } |
| 243 | + |
| 244 | + appToken := "" |
| 245 | + promote := false |
| 246 | + switch webhookType { |
| 247 | + case types.WebhookReload: |
| 248 | + appToken = app.Settings.WebhookTokens.Reload |
| 249 | + case types.WebhookReloadPromote: |
| 250 | + appToken = app.Settings.WebhookTokens.ReloadPromote |
| 251 | + promote = true |
| 252 | + default: |
| 253 | + http.Error(w, "Invalid webhook type", http.StatusInternalServerError) |
| 254 | + return |
| 255 | + } |
| 256 | + |
| 257 | + if appToken == "" { |
| 258 | + http.Error(w, "Webhook is not enabled for app", http.StatusBadRequest) |
| 259 | + return |
| 260 | + } |
| 261 | + |
| 262 | + if subtle.ConstantTimeCompare([]byte(appToken), []byte(authToken)) != 1 { |
| 263 | + http.Error(w, "Invalid bearer token", http.StatusUnauthorized) |
| 264 | + return |
| 265 | + } |
| 266 | + |
| 267 | + payload := map[string]any{} |
| 268 | + err = json.NewDecoder(r.Body).Decode(&payload) |
| 269 | + if err != nil { |
| 270 | + http.Error(w, "Error parsing request, expected JSON", http.StatusBadRequest) |
| 271 | + return |
| 272 | + } |
| 273 | + |
| 274 | + h.Trace().Str("method", r.Method).Str("url", r.URL.String()).Msg("API Received request") |
| 275 | + resp, err := h.server.ReloadApps(r.Context(), appPath, false, promote, false, "", "", "") |
| 276 | + if err != nil { |
| 277 | + if reqError, ok := err.(types.RequestError); ok { |
| 278 | + w.Header().Add("Content-Type", "application/json") |
| 279 | + errStr, _ := json.Marshal(reqError) |
| 280 | + http.Error(w, string(errStr), reqError.Code) |
| 281 | + return |
| 282 | + } |
| 283 | + h.Error().Err(err).Msg("error in api func call") |
| 284 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 285 | + return |
| 286 | + } |
| 287 | + |
| 288 | + w.Header().Add("Content-Type", "application/json") |
| 289 | + err = json.NewEncoder(w).Encode(resp) |
| 290 | + if err != nil { |
| 291 | + h.Error().Err(err).Msg("error encoding response") |
| 292 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 293 | + return |
| 294 | + } |
| 295 | +} |
| 296 | + |
213 | 297 | func parseBoolArg(arg string, defaultValue bool) (bool, error) { |
214 | 298 | if arg != "" { |
215 | 299 | ret, err := strconv.ParseBool(arg) |
@@ -551,6 +635,7 @@ func (h *Handler) versionSwitch(r *http.Request) (any, error) { |
551 | 635 | return ret, nil |
552 | 636 | } |
553 | 637 |
|
| 638 | +// serveInternal returns a handler for the internal APIs for app admin and management |
554 | 639 | func (h *Handler) serveInternal(enableBasicAuth bool) http.Handler { |
555 | 640 | // These API's are mounted at /_clace |
556 | 641 | r := chi.NewRouter() |
@@ -632,3 +717,23 @@ func (h *Handler) serveInternal(enableBasicAuth bool) http.Handler { |
632 | 717 |
|
633 | 718 | return r |
634 | 719 | } |
| 720 | + |
| 721 | +// serveWebhooks returns a handler for the app webhooks for reload and other events |
| 722 | +// webhooks are always mounted, even if admin over TCP is not enabled. At the app |
| 723 | +// level, webhooks are disabled by default and need to be enabled by the user |
| 724 | +func (h *Handler) serveWebhooks() http.Handler { |
| 725 | + // These API's are mounted at /_clace_webhook |
| 726 | + r := chi.NewRouter() |
| 727 | + |
| 728 | + // Reload app |
| 729 | + r.Post("/reload", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 730 | + h.webhookHandler(w, r, types.WebhookReload) |
| 731 | + })) |
| 732 | + |
| 733 | + // Reload and Promote app |
| 734 | + r.Post("/reload_promote", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 735 | + h.webhookHandler(w, r, types.WebhookReloadPromote) |
| 736 | + })) |
| 737 | + |
| 738 | + return r |
| 739 | +} |
0 commit comments