Skip to content

Commit

Permalink
configuration to be able to remove x-krakend errors
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Gogel <[email protected]>
  • Loading branch information
meshenka committed Jul 12, 2024
1 parent fb531bf commit 330592c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
5 changes: 4 additions & 1 deletion core/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

/*
Package core contains some basic constants and variables
Package core contains some basic constants and variables
*/
package core

Expand All @@ -24,3 +24,6 @@ var KrakendHeaderValue = fmt.Sprintf("Version %s", KrakendVersion)

// KrakendUserAgent is the value of the user agent header sent to the backends
var KrakendUserAgent = fmt.Sprintf("KrakenD Version %s", KrakendVersion)

// KrakendHeaders is a flag telling router if it needs to include KrakendHeaderName in the response.
var KrakendHeaders bool = true
7 changes: 5 additions & 2 deletions router/chi/router.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

/*
Package chi provides some basic implementations for building routers based on go-chi/chi
Package chi provides some basic implementations for building routers based on go-chi/chi
*/
package chi

Expand All @@ -13,6 +13,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/core"
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
"github.com/luraproject/lura/v2/router"
Expand Down Expand Up @@ -97,7 +98,9 @@ func (r chiRouter) Run(cfg config.ServiceConfig) {
r.registerKrakendEndpoints(cfg.Endpoints)

r.cfg.Engine.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
if core.KrakendHeaders {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}
http.NotFound(w, r)
})

Expand Down
10 changes: 8 additions & 2 deletions router/gin/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ var ErrorResponseWriter = func(c *gin.Context, err error) {
// EndpointHandler implements the HandlerFactory interface using the default ToHTTPError function
var EndpointHandler = CustomErrorEndpointHandler(logging.NoOp, server.DefaultToHTTPError)

func krakenheader(c *gin.Context, header, value string) {
if core.KrakendHeaders {
c.Header(header, value)
}
}

// CustomErrorEndpointHandler returns a HandlerFactory using the injected ToHTTPError function and logger
func CustomErrorEndpointHandler(logger logging.Logger, errF server.ToHTTPError) HandlerFactory {
return func(configuration *config.EndpointConfig, prxy proxy.Proxy) gin.HandlerFunc {
Expand All @@ -45,7 +51,7 @@ func CustomErrorEndpointHandler(logger logging.Logger, errF server.ToHTTPError)
return func(c *gin.Context) {
requestCtx, cancel := context.WithTimeout(c, configuration.Timeout)

c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
krakenheader(c, core.KrakendHeaderName, core.KrakendHeaderValue)

response, err := prxy(requestCtx, requestGenerator(c, configuration.QueryString))

Expand Down Expand Up @@ -74,7 +80,7 @@ func CustomErrorEndpointHandler(logger logging.Logger, errF server.ToHTTPError)
}
}

c.Header(server.CompleteResponseHeaderName, complete)
krakenheader(c, server.CompleteResponseHeaderName, complete)

for _, err := range c.Errors {
logger.Error(logPrefix, err.Error())
Expand Down
16 changes: 15 additions & 1 deletion router/gin/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ func NewEngine(cfg config.ServiceConfig, opt EngineOptions) *gin.Engine {
if ginOptions.ObfuscateVersionHeader {
core.KrakendHeaderValue = "Version undefined"
}

if ginOptions.RemoveKrakendHeaders {
core.KrakendHeaders = false
}
}
}
}

engine.NoRoute(func(c *gin.Context) {
c.Header(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
completeHeader(c, server.HeaderIncompleteResponseValue)
})

if !ginOptions.DisableAccessLog {
Expand Down Expand Up @@ -116,6 +120,12 @@ func NewEngine(cfg config.ServiceConfig, opt EngineOptions) *gin.Engine {
return engine
}

func completeHeader(c *gin.Context, value string) {
if core.KrakendHeaders {
c.Header(server.CompleteResponseHeaderName, value)
}
}

func healthEndpoint(health <-chan string) func(*gin.Context) {
mu := new(sync.RWMutex)
reports := map[string]string{}
Expand Down Expand Up @@ -240,6 +250,10 @@ type engineConfiguration struct {
// version with the value "undefined"
ObfuscateVersionHeader bool `json:"hide_version_header"`

// RemoveKrakendHeaders flags if the X-Krakend and X-Krakend-Completed headers should be included
// in the response/
RemoveKrakendHeaders bool `json:"remove_krakend_header"`

// UseH2C enable h2c support.
UseH2C bool `json:"use_h2c"`
}
Expand Down
4 changes: 3 additions & 1 deletion router/gin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ func (r ginRouter) registerOptionEndpoints(rg *gin.RouterGroup) {

rg.OPTIONS(path, func(c *gin.Context) {
c.Header("Allow", allowed)
c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
if core.KrakendHeaders {
c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
}
})
}
}
19 changes: 14 additions & 5 deletions router/mux/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF server.ToHTTPErr
method := strings.ToTitle(configuration.Method)

return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(core.KrakendHeaderName, core.KrakendHeaderValue)
if core.KrakendHeaders {
w.Header().Set(core.KrakendHeaderName, core.KrakendHeaderValue)
}
if r.Method != method {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
if core.KrakendHeaders {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
Expand All @@ -65,11 +69,13 @@ func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF server.ToHTTPErr

if response != nil && len(response.Data) > 0 {
if response.IsComplete {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderCompleteResponseValue)
if core.KrakendHeaders {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderCompleteResponseValue)
}
if isCacheEnabled {
w.Header().Set("Cache-Control", cacheControlHeaderValue)
}
} else {
} else if core.KrakendHeaders {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}

Expand All @@ -79,7 +85,10 @@ func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF server.ToHTTPErr
}
}
} else {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
if core.KrakendHeaders {
w.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}

if err != nil {
if t, ok := err.(responseError); ok {
http.Error(w, err.Error(), t.StatusCode())
Expand Down
9 changes: 7 additions & 2 deletions router/mux/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"sync"

"github.com/luraproject/lura/v2/core"
"github.com/luraproject/lura/v2/transport/http/server"
)

Expand Down Expand Up @@ -37,7 +38,9 @@ type HTTPErrorInterceptor struct {
func (i *HTTPErrorInterceptor) WriteHeader(code int) {
i.once.Do(func() {
if code != http.StatusOK {
i.ResponseWriter.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
if core.KrakendHeaders {
i.ResponseWriter.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}
}
})
i.ResponseWriter.WriteHeader(code)
Expand Down Expand Up @@ -73,7 +76,9 @@ func (e *BasicEngine) registrableHandler(pattern string) http.Handler {
return
}

rw.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
if core.KrakendHeaders {
rw.Header().Set(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}
http.Error(rw, "", http.StatusMethodNotAllowed)
})
}

0 comments on commit 330592c

Please sign in to comment.