TykTechnologies / tyk

Tyk Open Source API Gateway written in Go, supporting REST, GraphQL, TCP and gRPC protocols
Other
9.62k stars 1.08k forks source link

[TT-9410] `track_404_logs` does not work when the control port is different than the listener port #5131

Open ls-samer-abuthaher opened 1 year ago

ls-samer-abuthaher commented 1 year ago

Branch/Environment/Version

Describe the bug

When the gateway is started with a different control port than the listener port (which is recommended), the track_404_logs flag does not work. The gateway only logs 404 loglines for requests on the control port, but not the listener port.

Reproduction steps

This can be reproduced by trying the following locally:

> TYK_GW_TRACK404LOGS=true TYK_GW_LISTENPORT=8080 TYK_GW_CONTROLAPIPORT=9090 go run ./main.go
# ... in another terminal
> curl localhost:8080/notfound
404 page not found
# Does not generate a logline, and has a different status body
# Even though it does load the default API definitions in the repo, for example, this works
> curl localhost:8080/quickstart/get
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/X.X.X", 
    "X-Amzn-Trace-Id": "XXXX"
  }, 
  "origin": "XXXX", 
  "url": "http://httpbin.org/get"
}
# While this works
> curl localhost:9090/notfound-control
Not Found%
# As a log line on the other terminal:
time="Jun 13 12:41:20" level=error msg="Not Found" origin="127.0.0.1:52124" request="GET /notfound-control HTTP/1.1"

While if both ports are the same

> TYK_GW_TRACK404LOGS=true TYK_GW_LISTENPORT=8080 TYK_GW_CONTROLAPIPORT=8080 go run ./main.go
# ... in another terminal
> curl localhost:8080/notfound
Not Found%
# And as a logline in the original terminal
time="Jun 13 12:39:49" level=error msg="Not Found" origin="127.0.0.1:52091" request="GET /notfound HTTP/1.1"

Actual behavior Log line for track_404_logs only shows on control port requests, not on listener requests.

Expected behavior Log line should show for both requests on the control port and the listener port.

Screenshots/Video If applicable, add screenshots or video to help explain your problem.

Logs (debug mode or log file):

time="Jun 13 12:41:08" level=info msg="Tyk API Gateway v4.3.0" prefix=main
time="Jun 13 12:41:08" level=warning msg="Insecure configuration allowed" config.allow_insecure_configs=true prefix=checkup
time="Jun 13 12:41:08" level=error msg="cannot set key in pollerCacheKey" error="storage: Redis is either down or was not configured"
time="Jun 13 12:41:08" level=error msg="Redis health check failed" error="storage: Redis is either down or was not configured" liveness-check=true prefix=main
time="Jun 13 12:41:08" level=error msg="Could not set version in versionStore" error="storage: Redis is either down or was not configured" prefix=main
time="Jun 13 12:41:08" level=error msg="Connection to Redis failed, reconnect in 10s" error="storage: Redis is either down or was not configured" prefix=pub-sub
time="Jun 13 12:41:08" level=warning msg="Starting HTTP server on:[::]:9090" prefix=main
time="Jun 13 12:41:08" level=warning msg="Starting HTTP server on:[::]:8080" prefix=main
time="Jun 13 12:41:08" level=error msg="Failed to init event handler: no plugin driver is available"
time="Jun 13 12:41:08" level=error msg="No bundle base URL set, skipping bundle" api_id=7 org_id=default path=- prefix=main server_name="http://httpbin.org" user_id=- user_ip=-
time="Jun 13 12:41:08" level=error msg="Couldn't load bundle" error="No bundle base URL set, skipping bundle"
time="Jun 13 12:41:08" level=error msg="Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" prefix=coprocess
time="Jun 13 12:41:08" level=error msg="Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" prefix=coprocess
time="Jun 13 12:41:08" level=error msg="Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" prefix=coprocess
time="Jun 13 12:41:08" level=error msg="Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" prefix=coprocess
time="Jun 13 12:41:08" level=error msg="Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" prefix=coprocess
time="Jun 13 12:41:08" level=warning msg="JQ transform not supported."
time="Jun 13 12:41:20" level=error msg="Not Found" origin="127.0.0.1:52124" request="GET /notfound-control HTTP/1.1"

I don't know why it claims the version is 4.3.0 - but in any case, I tested this against head of master, a dockerised instance of the gateway, as well as against the release-5.0.1 branch.

Configuration (tyk config file): Using the default config file in the repo, the config was overridden with env vars.

Additional context I actually found out the cause of the issue - when we create a new router for the listen port, which we need if it's a different port, we do not attach the 404 handler in the same way we do for the control port router. I have a branch that should fix it. But before creating the PR I noticed you guys prefer if I create an issue first discussing it rather than on the PR itself.

(I have more questions about testing, since I'm not super familiar with golang, but those are probably better placed in the PR once that is to be created)

Thanks!

andyo-tyk commented 1 year ago

Thanks for pointing this out @ls-samer-abuthaher. We'll check this out - and also your proposed fix (thanks for that too!).