Skip to content

Commit 2a9800d

Browse files
henrybarretootavio
authored andcommitted
fix(api): skip openapi validation for internal routes and metrics endpoint
1 parent bed1803 commit 2a9800d

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

api/pkg/openapi/openapi.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10-
"strings"
1110
"sync"
1211

1312
"github.com/getkin/kin-openapi/openapi3"
@@ -120,10 +119,6 @@ func (v *OpenAPIValidator) ValidateResponse(r *http.Request, response *http.Resp
120119
return result
121120
}
122121

123-
if v.shouldSkipPath(r.URL.Path) {
124-
return result
125-
}
126-
127122
route, pathParams, err := v.router.FindRoute(r)
128123
if err != nil {
129124
v.logger.WithFields(logrus.Fields{
@@ -206,25 +201,3 @@ func GetDefaultSchemaPath() *url.URL {
206201

207202
return u
208203
}
209-
210-
// shouldSkipPath determines if a path should be skipped from validation
211-
func (v *OpenAPIValidator) shouldSkipPath(path string) bool {
212-
// Skip internal endpoints
213-
if strings.HasPrefix(path, "/internal") {
214-
return true
215-
}
216-
217-
skipPaths := []string{
218-
"/api/healthcheck",
219-
"/metrics",
220-
"/openapi",
221-
}
222-
223-
for _, skipPath := range skipPaths {
224-
if strings.HasPrefix(path, skipPath) {
225-
return true
226-
}
227-
}
228-
229-
return false
230-
}

api/routes/middleware/openapi.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type OpenAPIValidatorConfig struct {
4545
FailOnMismatch bool
4646
// SchemaPath overrides the default schema path
4747
SchemaPath *url.URL
48+
// Skipper defines a function to skip middleware. If Skipper returns true, middleware is skipped.
49+
Skipper func(echo.Context) bool
4850
}
4951

5052
type OpenAPIValidationMessage struct {
@@ -60,6 +62,10 @@ func OpenAPIValidator(cfg *OpenAPIValidatorConfig) echo.MiddlewareFunc {
6062

6163
return func(next echo.HandlerFunc) echo.HandlerFunc {
6264
return func(c echo.Context) error {
65+
if cfg.Skipper != nil && cfg.Skipper(c) {
66+
return next(c)
67+
}
68+
6369
validator := getOrCreateValidator(*cfg)
6470
if validator == nil {
6571
return next(c)

api/server.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"context"
55
"os"
6+
"strings"
67

78
"github.com/getsentry/sentry-go"
89
"github.com/labstack/echo/v4"
910
"github.com/shellhub-io/shellhub/api/routes"
11+
"github.com/shellhub-io/shellhub/api/routes/middleware"
1012
"github.com/shellhub-io/shellhub/api/services"
1113
"github.com/shellhub-io/shellhub/api/store/mongo"
1214
"github.com/shellhub-io/shellhub/api/store/mongo/options"
@@ -204,7 +206,20 @@ func (s *Server) routerOptions() ([]routes.Option, error) {
204206
if envs.IsDevelopment() {
205207
log.Info("Enabling OpenAPI validation in development mode")
206208

207-
opts = append(opts, routes.WithOpenAPIValidator(nil))
209+
opts = append(opts, routes.WithOpenAPIValidator(&middleware.OpenAPIValidatorConfig{
210+
// NOTE: By default, metrics and internal endpoints are skipped from validation for now.
211+
Skipper: func(ctx echo.Context) bool {
212+
routes := []string{"/metrics", "/internal"}
213+
214+
for _, path := range routes {
215+
if strings.HasPrefix(ctx.Request().URL.Path, path) {
216+
return true
217+
}
218+
}
219+
220+
return false
221+
},
222+
}))
208223
}
209224

210225
return opts, nil

0 commit comments

Comments
 (0)