Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching option for all evaluators of all phases #247

Merged
merged 2 commits into from
Apr 8, 2022
Merged

Conversation

guicassolato
Copy link
Collaborator

@guicassolato guicassolato commented Apr 7, 2022

Rolls out the same idea of #239 to all phases, i.e. any resulting object once evaluated by any evaluator can now be cached, and the cached value reused automatically by Authorino whenever the user-defined cache key for that particular evaluator repeats, until the cache entry expires.

Effectively, this change enables, e.g.:

  • Caching of pre-validated access tokens (particularly useful in the case of OAuth2 opaque tokens introspected)
  • Caching of complex Rego policies that involve sending requests to external services

You will not want to enable caching in evaluators whose execution is as cheap as accessing the cache, but for the expensive ones, we believe this feature can be very useful to speed up external authorization.



The PR also renames the env variable METADATA_CACHE_SIZE (introduced in #239) to the more generic EVALUATOR_CACHE_SIZE.

Likely, this will have to be improved in the future, to provide finer-grained configuration for the different use cases for the cache.


Verification steps

Build and deploy based on this branch:

make local-setup
kubectl -n authorino port-forward deployment/envoy 8000:8000 &

Apply the AuthConfig:

kubectl -n authorino apply -f -<<EOF
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts:
  - talker-api-authorino.127.0.0.1.nip.io
  identity:
  - name: anonymous
    anonymous: {}
  metadata:
  - name: cached-metadata
    http:
      endpoint: http://talker-api.authorino.svc.cluster.local:3000/metadata/{context.request.http.path}
      method: GET
    cache:
      key:
        valueFrom: { authJSON: context.request.http.path }
      ttl: 60
  authorization:
  - name: cached-authz
    opa:
      inlineRego: |
        now = time.now_ns()
        allow = true
      allValues: true
    cache:
      key:
        valueFrom: { authJSON: context.request.http.path }
      ttl: 60
  response:
  - name: x-authz-data
    json:
      properties:
      - name: cached-metadata
        valueFrom: { authJSON: auth.metadata.cached-metadata.uuid }
      - name: cached-authz
        valueFrom: { authJSON: auth.authorization.cached-authz.now }
EOF

Send requests:

  1. To /hello
curl http://talker-api-authorino.127.0.0.1.nip.io:8000/hello
# […]
#  "X-Authz-Data": "{\"cached-authz\":\"1649343067462380300\",\"cached-metadata\":\"92c111cd-a10f-4e86-8bf0-e0cd646c6f79\"}",
# […]
  1. To a different path
curl http://talker-api-authorino.127.0.0.1.nip.io:8000/goodbye
# […]
#  "X-Authz-Data": "{\"cached-authz\":\"1649343097860450300\",\"cached-metadata\":\"37fce386-1ee8-40a7-aed1-bf8a208f283c\"}",
# […]
  1. To /hello again before the cache entry expires (60 seconds from the first request sent to this path)
curl http://talker-api-authorino.127.0.0.1.nip.io:8000/hello
# […]
#  "X-Authz-Data": "{\"cached-authz\":\"1649343067462380300\",\"cached-metadata\":\"92c111cd-a10f-4e86-8bf0-e0cd646c6f79\"}",  <=== same cache-id as before
# […]
  1. To /hello again after the cache entry expires (60 seconds from the first request sent to this path)
curl http://talker-api-authorino.127.0.0.1.nip.io:8000/hello
# […]
#  "X-Authz-Data": "{\"cached-authz\":\"1649343135702743800\",\"cached-metadata\":\"e708a3a6-5caf-4028-ab5c-573ad9be7188\"}",  <=== different cache-id
# […]

Closes #20

@guicassolato guicassolato self-assigned this Apr 7, 2022
@guicassolato guicassolato marked this pull request as ready for review April 7, 2022 15:20
@guicassolato guicassolato force-pushed the caching-for-all branch 3 times, most recently from 9adaf4a to 3ff2278 Compare April 7, 2022 15:53
jjaferson
jjaferson previously approved these changes Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Repeated requests caching
2 participants