Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect request url of backend doesn't work #940

Open
wolframite opened this issue Nov 4, 2024 · 0 comments
Open

Redirect request url of backend doesn't work #940

wolframite opened this issue Nov 4, 2024 · 0 comments
Labels

Comments

@wolframite
Copy link

wolframite commented Nov 4, 2024

Environment info:

  • KrakenD version: Version: 2.7.2
  • System info: docker
  • Hardware specs: 8 CPUs / 12GB RAM
  • Backend technology: Java
  • Additional environment information:
    I'm running krakend in docker-compose based on the community playground:
services:
  krakend:
    image: devopsfaith/krakend:2.7
    restart: unless-stopped
    volumes:
      - ./config/krakend:/etc/krakend
    ports:
      - "8080:8080"
    command: ["run", "-d", "-c", "/etc/krakend/krakend.json"]

Describe what are you trying to do:
I'm trying to implement the following behind the /seq endpoint

Call /info if X-Auth exists, fetch customerId and insert into /cart/{resp0_customerId}.json

  1. Happy path: If an X-Auth header is present, call the /info backend, extract the customerId and insert it into the /cart/{resp0_customerId}.json backend using the sequential proxy. This part works
  2. Invalid header: In case the /info backend returns a 401 because the X-Auth header is invalid, I pass-through the status code and interrupt the sequence. This part also works
  3. In case the X-Auth header is not set at all, I want to skip both the /info and the /cart/{resp0_customerId}.json backend and forward the request to /cart instead. I'm simulating the /cart backend without the parameter with a benchmark which also returns json. This is the part where I need help. I tried to use CEL to suppress the execution of the /info backend, I tried Lua to redirect, but I can't figure out how to make it work.

Is this mix of conditions and sequence possible in KrakenD?

Your configuration file:

{
  "$schema": "https://www.krakend.io/schema/v3.json",
  "version": 3,
  "name": "KrakenD Enterprise API Gateway",
  "port": 8080,
  "host": ["http://fake_api"],
  "timeout": "3000ms",
  "debug_endpoint": true,
  "endpoints": [
    {
      "@comment": "Call /info if X-Auth exists, fetch customerId and insert into /cart/{resp0_customerId}.json :: Redirect to /cart if X-Auth is missing. Pass through 401 from /info if X-Auth is invalid",
      "endpoint": "/seq",
      "input_headers": ["X-Auth"],
      "backend": [
        {
          "host": ["http://benchmark:8080"],
          "url_pattern": "/info",
          "allow": ["customerId"],
          "extra_config": {
            "backend/http": {
              "return_error_code": true
            },
            "modifier/lua-backend": {
              "allow_open_libs": true,
              "pre": "local req = request.load(); if string.len(req:headers('X-Auth')) == 0 then req:url('http://benchmark:8080/cart') end;",
              "post": "print('post'); local r = response.load(); print(r:body());"
            }
          }
        },
        {
          "url_pattern": "/cart/{resp0_customerId}.json",
          "extra_config": {
            "validation/cel": [
              {
                "check_expr": "req_headers['X-Auth'] != []"
              }
            ]
          }
        }
      ],
      "extra_config": {
        "proxy": {
          "sequential": true
        }
      }
    }
  ],
  "extra_config": {
    "telemetry/logging": {
      "level": "DEBUG",
      "syslog": false,
      "stdout": true
    },
    "security/cors": {
      "allow_origins": ["*"],
      "allow_methods": ["POST", "GET"],
      "allow_headers": ["Origin", "Authorization", "Content-Type"],
      "expose_headers": ["Content-Length"],
      "max_age": "12h"
    }
  }
}

Configuration check output:
Result of krakend check -dtc krakend.json --lint command

/etc/krakend # krakend check -dtc krakend.json --lint
Parsing configuration file: krakend.json
Global settings
	Name: KrakenD Enterprise API Gateway
	Version: 3
	Address:
	Port: 8080
	Default backend hosts: [http://fake_api]
3 global component configuration(s):
- security/cors
- telemetry/logging
- telemetry/metrics
1 API endpoint(s):
	- GET /seq
	Timeout: 3s
	1 endpoint component configuration(s):
	- proxy
	Connecting to 2 backend(s):
		[+] GET /info
		Timeout: 3s
		Hosts: [http://benchmark:8080]
		2 backend component configuration(s):
		- backend/http
		- modifier/lua-backend

		[+] GET /cart/{{.Resp0_customerId}}.json
		Timeout: 3s
		Hosts: [http://fake_api]
		1 backend component configuration(s):
		- validation/cel

0 async agent(s):
Syntax OK!

Commands used:
How did you start the software?

via docker-compose

Logs:
That's the logs for the happy path under 1.

krakend-1  | post
krakend-1  |
krakend-1  |  2024/11/04 - 19:37:41.096 ▶ DEBUG [BACKEND: /cart/{{.Resp0_customerId}}.json][CEL][pre] Evaluator #0 result: true
krakend-1  | [GIN] 2024/11/04 - 19:37:41 | 200 |   24.532445ms |      172.18.0.1 | GET      "/seq"

That's the logs for 2. with the invalid header:

krakend-1  |  2024/11/04 - 19:39:06.659 ▶ ERROR [ENDPOINT: /seq]
krakend-1  | [GIN] 2024/11/04 - 19:39:06 | 401 |    1.890536ms |      172.18.0.1 | GET      "/seq"
krakend-1  | Error #01:

That's the logs for 3. when the X-Auth header is missing:

krakend-1  | post
krakend-1  |
krakend-1  |  2024/11/04 - 19:39:57.966 ▶ INFO [BACKEND: /cart/{{.Resp0_customerId}}.json][CEL][pre] Evaluator #0 failed: no such key: X-Auth
krakend-1  |  2024/11/04 - 19:39:57.966 ▶ ERROR [ENDPOINT: /seq] Error #0: request aborted by evaluator #0
krakend-1  | [GIN] 2024/11/04 - 19:39:57 | 200 |   21.706592ms |      172.18.0.1 | GET      "/seq"
krakend-1  | Error #01: request aborted by evaluator #0

Additional comments:
Even when I set the encoding of the 1st backend to no-op, I don't get any response, only an empty object:

HTTP/1.1 200 OK
Content-Length: 2
Content-Type: application/json; charset=utf-8
Date: Mon, 04 Nov 2024 19:39:57 GMT
Vary: Origin
X-Krakend: Version 2.7.2
X-Krakend-Completed: false

{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant