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

Metadata caching #239

Merged
merged 5 commits into from
Apr 8, 2022
Merged

Metadata caching #239

merged 5 commits into from
Apr 8, 2022

Conversation

guicassolato
Copy link
Collaborator

@guicassolato guicassolato commented Apr 5, 2022

Adds caching options for external metadata.

Cache size per metadata config can be set at the level of the Authorino instance, by injecting the environment variable METADATA_CACHE_SIZE (in megabytes).

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: talker-api
    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
  response:
  - name: x-authz-data
    json:
      properties:
      - name: cache-id
        valueFrom: { authJSON: auth.metadata.talker-api.uuid }
EOF

Send requests:

  1. To /hello
curl http://talker-api-authorino.127.0.0.1.nip.io:8000/hello
# […]
#  "X-Authz-Data": "{\"cache-id\":\"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": "{\"cache-id\":\"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": "{\"cache-id\":\"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": "{\"cache-id\":\"e708a3a6-5caf-4028-ab5c-573ad9be7188\"}",  <=== different cache-id
# […]

Closes #21

@guicassolato guicassolato self-assigned this Apr 5, 2022
@guicassolato
Copy link
Collaborator Author

A couple things with allegro/bigcache that I'm not very happy with:

  1. The GetWithTTL function always returns ttl = 0 (same with dgraph-io/ristretto) → it is not possible to evict a cache entry on access; we need to setup a goroutine that takes care of evictions in a loop
  2. There's no safe way to check if the goroutine in charge of evictions is active, and force-closing it can panic

The above does not happen when using coocood/freecache.

@guicassolato guicassolato force-pushed the metadata-caching branch 3 times, most recently from 027288f to 65bb618 Compare April 6, 2022 10:58
@guicassolato guicassolato marked this pull request as ready for review April 6, 2022 11:31
@guicassolato guicassolato force-pushed the metadata-caching branch 2 times, most recently from 97a68a2 to c9fdb53 Compare April 7, 2022 13:19
jjaferson
jjaferson previously approved these changes Apr 8, 2022
Copy link
Contributor

@jjaferson jjaferson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few suggestion but the code looks good, great work @guicassolato

controllers/auth_config_controller.go Outdated Show resolved Hide resolved
@@ -97,6 +100,7 @@ var (
oidcHTTPPort = fetchEnv(envOIDCHTTPPort, defaultOIDCHTTPPort)
oidcTLSCertPath = fetchEnv(envOIDCTLSCertPath, defaultOIDCTLSCertPath)
oidcTLSCertKeyPath = fetchEnv(envOIDCTLSCertKeyPath, defaultOIDCTLSCertKeyPath)
metadataCacheSize = fetchEnv(envMetadataCacheSize, defaultMetadataCacheSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be configured via the authorino operator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need to open an issue for that. However, let's make sure it's for the EVALUATOR_CACHE_SIZE env var from #247, instead of this one.

pkg/evaluators/metadata_cache.go Outdated Show resolved Hide resolved
func (c *jsonCache) Set(key, value interface{}) error {
if valueAsBytes, err := gojson.Marshal(value); err != nil {
return err
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, do we need the else statement?

Copy link
Collaborator Author

@guicassolato guicassolato Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one we actually do need... or valueAsBytes needs to be declared in the scope.

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.

Metadata caching
2 participants