Middleware enforces access control for management endpoints. All requests (local and remote) require a valid management key. Additionally, remote access requires allow-remote-management=true.
()
| 263 | // All requests (local and remote) require a valid management key. |
| 264 | // Additionally, remote access requires allow-remote-management=true. |
| 265 | func (h *Handler) Middleware() gin.HandlerFunc { |
| 266 | return func(c *gin.Context) { |
| 267 | c.Header("X-CPA-VERSION", buildinfo.Version) |
| 268 | c.Header("X-CPA-COMMIT", buildinfo.Commit) |
| 269 | c.Header("X-CPA-BUILD-DATE", buildinfo.BuildDate) |
| 270 | c.Header("X-CPA-SUPPORT-PLUGIN", pluginhost.SupportPluginHeaderValue()) |
| 271 | |
| 272 | clientIP := c.ClientIP() |
| 273 | localClient := clientIP == "127.0.0.1" || clientIP == "::1" |
| 274 | |
| 275 | // Accept either Authorization: Bearer <key> or X-Management-Key |
| 276 | var provided string |
| 277 | if ah := c.GetHeader("Authorization"); ah != "" { |
| 278 | parts := strings.SplitN(ah, " ", 2) |
| 279 | if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" { |
| 280 | provided = parts[1] |
| 281 | } else { |
| 282 | provided = ah |
| 283 | } |
| 284 | } |
| 285 | if provided == "" { |
| 286 | provided = c.GetHeader("X-Management-Key") |
| 287 | } |
| 288 | |
| 289 | allowed, statusCode, errMsg := h.AuthenticateManagementKey(clientIP, localClient, provided) |
| 290 | if !allowed { |
| 291 | c.AbortWithStatusJSON(statusCode, gin.H{"error": errMsg}) |
| 292 | return |
| 293 | } |
| 294 | c.Next() |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // AuthenticateManagementKey verifies the provided management key for the given client. |
| 299 | // It mirrors the behaviour of Middleware() so non-HTTP callers can reuse the same logic. |