How can I configure caddy v2 to be an API proxy that can persist cached responses?

Can caddy v2 be made to operate as an API proxy that behaves so:

  1. clients can present an API key, say APIKey1 - different from the API key (APIKey2) that an upstream API service (RealService) expects.
  2. It’s caddy (or a plugin) that rewrites
    a. APIKey1APIKey2, transparently so that RealService doesn’t see APIKey1 (which it will reject) but APIKey2
    b. the URL for the client request to map to RealService, transparently
    c. It would be awesome if we can rewrite an APIKey1 that’s in the Authorization header to APIKey2 in a query parameter or combinations thereof

Order of priority:

i. rewrite API keys and endpoint (API proxy)
ii. cache responses (cache)
iii. persist these cached responses so that caddy can be restarted/another instance started and have the cached responses apply (potentially: souin/plugins/caddy at master · darkweak/souin · GitHub)

The usecases are:

I have a webapp that needs access to a paid RealService - where each API call is expensive but doesn’t change over time - think of a stock ticker service.
If I code in the API Key (APIKey2) for RealService into my webapp, then any user of the webapp can trivially gain access to APIKey2 and make calls to RealService directly, leaving me paying for those charges.
This is why I would like to rewrite APIKey1APIKey2, where I issue the APIKey1 and have them map to APIKey2 in my pipeline.

Also, since the response doesn’t change over time, I would further like to cache responses from RealService (and persist them for future use) - but priority is rewriting API keys first (over caching).

You can use the header directive to perform a value replacement:

example.com {
	header Authorization APIKey1 APIKey2
	reverse_proxy upstream-api-app:8080
}

We have a WIP module GitHub - caddyserver/cache-handler: Distributed HTTP caching module for Caddy which is almost ready to go, it’ll essentially be Souin under the hood, so it can be used as a distributed cache with Olric support built-in.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.