DianaIonita / serverless-api-gateway-caching

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
ISC License
136 stars 35 forks source link

headers caching for multi-tenant #137

Open fernandohonig opened 2 months ago

fernandohonig commented 2 months ago

Hi! We have a SaaS app and we use a custom header to define the customer-id and send that to the APIs we have. In every request, we have that customer-id header. When I enable caching, data from one customer is shown in another customer, like it's not respecting the customer-id key I set in every function.

For instance:

    events:
      - http:
          method: GET
          request:
            parameters:
              querystrings:
                module: true
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.customer-id
              - name: request.querystring.module

What am I doing wrong?. Thanks!

DianaIonita commented 2 months ago

Hi @fernandohonig,

Thanks for raising the issue. Can you please confirm via the AWS Console that caching is enabled for the API Gateway you're using in this project and that the customer-id cache key parameter is set?

For example:

image

here I set a "limit" query string parameter: image

fernandohonig commented 2 months ago

Hi @DianaIonita, thanks for taking the time.

Yes, it’s enabled but the difference is that the caching is not on the query string as shown in your picture, but in the request headers.

fernandohonig commented 1 month ago

@DianaIonita any ideas about this?. Thanks.

DianaIonita commented 1 month ago

Hi @fernandohonig,

Apologies for the delay. Here's what I tried:

custom: apiGatewayCaching: enabled: true

- I added it to my function:

```yml
functions:
  list-cats:
    handler: rest_api/cats/get/handler.handle
    events:
      - http:
          path: /cats
          method: get
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.customer-id

In your configuration, can you tell if anything differs from what I tried?

fernandohonig commented 1 month ago

Hi @DianaIonita, thanks again for taking the time. Does this only work with GET or also POST methods? (Some functions we have despite using a POST method, are used to get information.

Also, is request.header case sensitive? If I am sending "Customer-Id" the name: needs to be "Customer-Id" or it could be "customer-id" ?

DianaIonita commented 1 month ago

Hi @fernandohonig,

All cache key parameters are case-sensitive, yes, this is how API Gateway works. If you call an endpoint with header customer-id=123, that will creates a cache entry, and if you call it with header Customer-Id=123, it will create another entry.

Caching works with both GET and POST methods.

fernandohonig commented 1 month ago

So then I don’t understand why it doesn’t work on my end. I have it configured just like you but when I execute a get or a post with 1 Customer-Id and then try the same with another Customer-Id the results for the second one is the same as the first one.

DianaIonita commented 1 month ago

Hey @fernandohonig,

Can you try reproducing it in a very simple public repository so that I can debug based on that?