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

Cache results of group checker #279

Merged
merged 3 commits into from
Jan 10, 2025
Merged

Cache results of group checker #279

merged 3 commits into from
Jan 10, 2025

Conversation

tomrf1
Copy link
Member

@tomrf1 tomrf1 commented Jan 9, 2025

The GoogleGroupChecker class makes requests to the Google directory service to find each user's groups.

Currently if the requireGroup Filter is used to check group memberships, a request is made to the Google API every time. I have found that these requests can take ~600ms each time, which can really add up if a webapp needs to make multiple requests, or if the requireGroup Filter is chained.

This PR introduces a cache to the GoogleGroupChecker class to reduce the number of calls to the Google directory. The cache duration is now a class parameter, and defaults to 1 minute.
Note - I've used Google's guava cache here because it's already available in this library.

@tomrf1 tomrf1 marked this pull request as ready for review January 9, 2025 10:00
@tomrf1 tomrf1 requested a review from a team as a code owner January 9, 2025 10:00
@davidfurey
Copy link
Member

Looks good, but have you tested this with an application in CODE?

@tomrf1
Copy link
Member Author

tomrf1 commented Jan 9, 2025

Looks good, but have you tested this with an application in CODE?

I've tested with an app running locally (using sbt publishLocal), which I think is as good as CODE?

@davidfurey
Copy link
Member

Difficult to test with multiple users locally? It would be good to demonstrate that a user logging in with the correct group membership doesn't then cause subsequent requests from a user who is not in the group to be accepted.

I've kicked off a preview release so you can ensure this works as expected in CODE.

@gu-scala-library-release
Copy link
Contributor

@davidfurey has published a preview version of this PR with release workflow run #56, based on commit 6a9d92e:

18.0.0-PREVIEW.tf-cache-groups.2025-01-09T1115.6a9d92ef

Want to make another preview release?

Click 'Run workflow' in the GitHub UI, specifying the tf-cache-groups branch, or use the GitHub CLI command:

gh workflow run release.yml --ref tf-cache-groups

Want to make a full release after this PR is merged?

Click 'Run workflow' in the GitHub UI, leaving the branch as the default, or use the GitHub CLI command:

gh workflow run release.yml

@tomrf1
Copy link
Member Author

tomrf1 commented Jan 9, 2025

Difficult to test with multiple users locally? It would be good to demonstrate that a user logging in with the correct group membership doesn't then cause subsequent requests from a user who is not in the group to be accepted.

I've kicked off a preview release so you can ensure this works as expected in CODE.

Thanks @davidfurey
I've tested in CODE with an internal app.
I was able to view pages, and another person who does not have membership of the necessary groups was not able to. In the logs I can see this:
Excluding [EMAIL ADDRESS] from '/epic-tests' - not in accepted groups: Set([GROUP1], [GROUP2]).

@tomrf1 tomrf1 requested a review from davidfurey January 10, 2025 09:21
} yield resp.getGroups.asScala.map(_.getEmail).toSet

type Email = String
private val cache: LoadingCache[Email, Set[String]] = CacheBuilder.newBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

Only authed users will end up in the cache, so the unbounded cache size is fine.

Copy link
Member

@davidfurey davidfurey left a comment

Choose a reason for hiding this comment

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

lgtm

@tomrf1 tomrf1 merged commit 0e92770 into main Jan 10, 2025
8 checks passed
@tomrf1 tomrf1 deleted the tf-cache-groups branch January 10, 2025 10:17
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.

2 participants