Fix preflight response status in access logs

Co-authored-by: lbenguigui <lbenguigui@gmail.com>
This commit is contained in:
Romain 2023-09-29 12:18:06 +02:00 committed by GitHub
parent ab7993428d
commit e95fde5652
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 0 deletions

View file

@ -592,6 +592,53 @@ func (s *AccessLogSuite) TestAccessLogAuthFrontendSuccess(c *check.C) {
checkNoOtherTraefikProblems(c) checkNoOtherTraefikProblems(c)
} }
func (s *AccessLogSuite) TestAccessLogPreflightHeadersMiddleware(c *check.C) {
ensureWorkingDirectoryIsClean()
expected := []accessLogValue{
{
formatOnly: false,
code: "200",
user: "-",
routerName: "rt-preflightCORS",
serviceURL: "-",
},
}
// Start Traefik
cmd, display := s.traefikCmd(withConfigFile("fixtures/access_log_config.toml"))
defer display(c)
err := cmd.Start()
c.Assert(err, checker.IsNil)
defer s.killCmd(cmd)
checkStatsForLogFile(c)
waitForTraefik(c, "preflightCORS")
// Verify Traefik started OK
checkTraefikStarted(c)
// Test preflight response
req, err := http.NewRequest(http.MethodOptions, "http://127.0.0.1:8009/", nil)
c.Assert(err, checker.IsNil)
req.Host = "preflight.docker.local"
req.Header.Set("Origin", "whatever")
req.Header.Set("Access-Control-Request-Method", "GET")
err = try.Request(req, 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
c.Assert(err, checker.IsNil)
// Verify access.log output as expected
count := checkAccessLogExactValuesOutput(c, expected)
c.Assert(count, checker.GreaterOrEqualThan, len(expected))
// Verify no other Traefik problems
checkNoOtherTraefikProblems(c)
}
func checkNoOtherTraefikProblems(c *check.C) { func checkNoOtherTraefikProblems(c *check.C) {
traefikLog, err := os.ReadFile(traefikTestLogFile) traefikLog, err := os.ReadFile(traefikTestLogFile)
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)

View file

@ -20,6 +20,8 @@
address = ":8007" address = ":8007"
[entryPoints.digestAuth] [entryPoints.digestAuth]
address = ":8008" address = ":8008"
[entryPoints.preflight]
address = ":8009"
[api] [api]
insecure = true insecure = true

View file

@ -85,6 +85,16 @@ services:
traefik.http.middlewares.wl.ipwhitelist.sourcerange: 8.8.8.8/32 traefik.http.middlewares.wl.ipwhitelist.sourcerange: 8.8.8.8/32
traefik.http.services.service3.loadbalancer.server.port: 80 traefik.http.services.service3.loadbalancer.server.port: 80
preflightCORS:
image: traefik/whoami
labels:
traefik.enable: true
traefik.http.routers.rt-preflightCORS.entryPoints: preflight
traefik.http.routers.rt-preflightCORS.rule: Host(`preflight.docker.local`)
traefik.http.routers.rt-preflightCORS.middlewares: preflightCORS
traefik.http.middlewares.preflightCORS.headers.accessControlAllowMethods: OPTIONS, GET
traefik.http.services.preflightCORS.loadbalancer.server.port: 80
networks: networks:
default: default:
name: traefik-test-network name: traefik-test-network

View file

@ -53,6 +53,7 @@ func NewHeader(next http.Handler, cfg dynamic.Headers) (*Header, error) {
func (s *Header) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (s *Header) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Handle Cors headers and preflight if configured. // Handle Cors headers and preflight if configured.
if isPreflight := s.processCorsHeaders(rw, req); isPreflight { if isPreflight := s.processCorsHeaders(rw, req); isPreflight {
rw.WriteHeader(http.StatusOK)
return return
} }