Search code examples
dockerhaproxy

How to setup HAProxy to add access token to client requests


I have a client that can only make requests without authentication information.

I would like to use HAProxy or a similar proxy solution to add OAuth authentication to these client requests.

I already succeeded to add a Bearer token to the client requests. See below for the haproxy.cfg with some placeholders.

frontend front
  mode http
  bind *:8080
  default_backend   servers
  http-request add-header Authorization "Bearer {{ .Env.ACCESS_TOKEN}}"

backend servers
  mode http
  server server1 myserver.com:443 ssl

The problem is that the access tokens have a TTL of 24 hours. So I need to refresh them or get a new token periodically.

Does HAProxy support this already?

I can write some script to get a new access token periodically, update the config and restart HAProxy. Is this a good approach when running HAProxy in docker? Are there better solutions?


Solution

  • You could give a try to create/test your script using Lua, it is now supported in the latest versions, check How Lua runs in HAProxy.

    An example of this but using Nginx + Lua, can be found in this project: https://github.com/jirutka/ngx-oauth