Dynamically modify request headers

251 views
Skip to first unread message

Fuat Bozkurt

unread,
Sep 14, 2023, 7:53:21 AM9/14/23
to KrakenD Community
Hi,

I have user service which gives me some information such as userId, name etc. 
I want krakend gateway to do is before making request to backend, call userService, get userId and set userId to header of backend request. 

I know it has Martian plugin but it is a static component :/ 
Is there any idea to solve case?

Thanks.

Fuat Bozkurt

unread,
Sep 14, 2023, 8:16:54 AM9/14/23
to KrakenD Community, Fuat Bozkurt
If I need to explain step by step;

1- make http request to userService
2- get userId from userService response
3- set userId to backend request header

14 Eylül 2023 Perşembe tarihinde saat 14:53:21 UTC+3 itibarıyla Fuat Bozkurt şunları yazdı:

Albert Garcia

unread,
Sep 14, 2023, 8:34:33 AM9/14/23
to Fuat Bozkurt, KrakenD Community
Hey Fuat,

Thanks for reaching out! I see you're aiming to pull the userId from your userService and then slot it into the backend request header. While it might seem straightforward, there are some important considerations.

Performance Impact: Your method involves a sequential proxy, which can be a real drag on performance (multiple calls for EACH request). For more insights on why this isn't the best route, check out our sequential proxy documentation: https://www.krakend.io/docs/endpoints/sequential-proxy/. A more efficient alternative would be to use an Identity Provider like Keycloak, which offers built-in support for user federation.

Martian Limitations: You hit the nail on the head; Martian is more about static header manipulation and won't suit your dynamic needs.

Lua Scripts in Community Edition: If you're using the KrakenD Community Edition, Lua scripts can help you out with dynamic header manipulation and making those extra calls. However, keep in mind that this will still have the performance impact mentioned in point 1. More on Lua scripts: https://www.krakend.io/docs/endpoints/lua/#making-additional-requests-http_response

Dynamic Routing in Enterprise Edition: If you're on KrakenD Enterprise, you've got the option for dynamic routing. This feature allows you to add info from any part of the request when building the backend request. Learn more at: https://www.krakend.io/docs/enterprise/endpoints/dynamic-routing/

Feel free to write back if you have more questions or need further clarification.

Best,
Logo
Albert García
Business Director
LinkedIn icon   Twitter icon  

Confidentiality Notice: This email, including any attachments, may contain confidential and privileged information for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender immediately and destroy all copies of this email. Thank you. This email has been sent in accordance with the European Union General Data Protection Regulation (EU GDPR).



--
You received this message because you are subscribed to the Google Groups "KrakenD Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to community+...@krakend.io.
To view this discussion on the web visit https://groups.google.com/a/krakend.io/d/msgid/community/13c295c0-f2cc-42d6-af09-a98c7c7c01can%40krakend.io.

Fuat Bozkurt

unread,
Sep 14, 2023, 8:53:28 AM9/14/23
to KrakenD Community, Albert Garcia, KrakenD Community, Fuat Bozkurt
Hi Albert,

You wrote it in great detail, thank you very much. 
I cannot use keycloak or any identity provider because the application has been used for a very long time so there are millions of user. 
My aim is to put microservices (lots of) behind the krakend. Also you said it may be performance issue and already working on POC so we'll see the results. 

I'll consider to write custom plugin with go or Lua Script. 
Which one should I prefer in terms of performance "custom plugin with Golang" or "Lua Script"?

Thanks,

Fuat Bozkurt

14 Eylül 2023 Perşembe tarihinde saat 15:34:33 UTC+3 itibarıyla Albert Garcia şunları yazdı:
Hey Fuat,

Thanks for reaching out! I see you're aiming to pull the userId from your userService and then slot it into the backend request header. While it might seem straightforward, there are some important considerations.

Performance Impact: Your method involves a sequential proxy, which can be a real drag on performance (multiple calls for EACH request). For more insights on why this isn't the best route, check out our sequential proxy documentation: https://www.krakend.io/docs/endpoints/sequential-proxy/. A more efficient alternative would be to use an Identity Provider like Keycloak, which offers built-in support for user federation.

Martian Limitations: You hit the nail on the head; Martian is more about static header manipulation and won't suit your dynamic needs.

Lua Scripts in Community Edition: If you're using the KrakenD Community Edition, Lua scripts can help you out with dynamic header manipulation and making those extra calls. However, keep in mind that this will still have the performance impact mentioned in point 1. More on Lua scripts: https://www.krakend.io/docs/endpoints/lua/#making-additional-requests-http_response

Dynamic Routing in Enterprise Edition: If you're on KrakenD Enterprise, you've got the option for dynamic routing. This feature allows you to add info from any part of the request when building the backend request. Learn more at: https://www.krakend.io/docs/enterprise/endpoints/dynamic-routing/

Feel free to write back if you have more questions or need further clarification.

Best,
LogoAlbert GarcíaBusiness DirectorLinkedIn icon   Twitter icon  

Confidentiality Notice: This email, including any attachments, may contain confidential and privileged information for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender immediately and destroy all copies of this email. Thank you. This email has been sent in accordance with the European Union General Data Protection Regulation (EU GDPR).

Albert Lombarte

unread,
Sep 15, 2023, 2:30:33 AM9/15/23
to KrakenD Community, fuatbo...@gmail.com, Albert Garcia, KrakenD Community
Hi Fuat,

Performance-wise, using Go is the way to go.  It will require you to extend the Docker image to include your plugin compilation, but once you have it working, it is worth the effort.

Think that a Lua script is compiled and evaluated on every execution over a Lua virtual machine. This is very far from the performance you get from a Go plugin that has been compiled into binary code and uses the exact interfaces of KrakenD with no translations need. Depending on the functionality, a x10 speed improvement over Lua should be an expected reasonable factor.

In the Enterprise edition, this is supported natively though (because there is a sophisticated Go component that does what you are trying to achieve)

So, hail to the plugins.

El dia dijous, 14 de setembre de 2023 a les 14:53:28 UTC+2, fuatbo...@gmail.com va escriure:

Fuat Bozkurt

unread,
Sep 19, 2023, 2:38:54 AM9/19/23
to KrakenD Community, Albert Lombarte
Hi Albert,

Sorry for the late answer. You explained it very well. I agree with you about Go hail to the plugins!

Thanks,

15 Eylül 2023 Cuma tarihinde saat 09:30:33 UTC+3 itibarıyla Albert Lombarte şunları yazdı:
Reply all
Reply to author
Forward
0 new messages