krakend / krakend-lua

a lua interpreter for the KrakenD framework
Apache License 2.0
7 stars 14 forks source link

Change and forward http status code of response with LUA #18

Closed Castronebe closed 3 years ago

Castronebe commented 3 years ago

hi, I am trying to return an error with lua when sequential backends are called.

for example when calling 2 sequential endpoints, if the first responds 401, return 401 and not a 500 as Krakend does today

"endpoints": [
    {
      "endpoint": "/rests-partners-gateway/cauth/CO/menu/{storeId}",
      "method": "GET",
      "output_encoding": "json",
      "timeout": "10s",
      "headers_to_pass": [ "*" ],
      "querystring_params": [ "*" ],
      "cache_ttl": "0s",
      "extra_config": {
              "github.com/devopsfaith/krakend/proxy": {
                 "sequential": true
              },
              "github.com/devopsfaith/krakend-jose/validator": {
                  "roles_key": "https://example.com/roles",
                  "roles": ["PORTAL_KAM"],
                  "jwk-url" : "https://example/.well-known/jwks.json",
                  "issuer": "https://example.auth0.com/",
                  "disable_jwk_security": true,
                  "alg" : "RS256"
              }
      },
      "backend": [
        {
          "url_pattern": "/rests-partners-accounts/auth/stores/validate",
          "timeout": "10s",
          "encoding": "json",
          "host": ["http://example.com"],
          "disable_host_sanitize": "false",
          "method": "POST",
          "extra_config": {
              "github.com/devopsfaith/krakend-martian": {
                  "fifo.Group": {
                      "scope": ["request"],
                      "aggregateErrors": false,
                      "modifiers": [
                          {
                              "body.Modifier": {
                                  "scope": ["request"],
                                  "body": "eyAic3RvcmVfaWRzIjogWyAxMTkgXSAsImNvdW50cnkiOiJDTyJ9"
                              }
                          },
                          {
                              "header.Modifier": {
                                "scope": ["request"],
                                "name": "Content-type",
                                "value": "application/json"
                              }
                          }
                      ]
                  }
              },
              "github.com/devopsfaith/krakend-lua/proxy/backend": {
                  "post": "local r = response.load(); print(\"Backend StatusCodeResponse: \"..r:statusCode());",
                                    "live": true,
                  "allow_open_libs": true
              }
          }
        },
        {
          "url_pattern": "/api/rests-availability/partner/store/{storeId}/menu",
          "timeout": "8s",
          "encoding": "json",
          "host": ["http://example.com"],
          "disable_host_sanitize": "false",
          "method": "GET",
          "extra_config": {}
        }
      ]
    }
  ]

I need to know the status code of the first response to use the custom_error ("", 403) function, but the status code is always "0"

note: I did the same with "github.com/devopsfaith/krakend-lua/proxy" and it didn't work.

can you help me?

kpacha commented 3 years ago

hi, @Castronebe !

the feature you're describing is not supported. Whenever more than one backend is linked to a single endpoint, the internal pipe ignores the actual value of the received status code and goes for an ok/ko aproach (thus, returning a 200 or a 500)

once there is a ko in the pipe, it is really hard for the outer layers to know what happened and why

for such complex workflows, I'd rather:

cheers!

github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.