-
Notifications
You must be signed in to change notification settings - Fork 24
feat(authz): Add caching to keycloak ERS #2466
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06f78f6
add caching to ers
elizabethhealy c205c67
linting, reduce time
elizabethhealy e7ea6aa
Merge branch 'main' into dspx-1299-keycloak-ers-caching
elizabethhealy 7ed9d55
add close
elizabethhealy 04e49e4
defer, move logs to trace level
elizabethhealy 6305c20
suggested updates
elizabethhealy b16d7ea
lint
elizabethhealy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package keycloak | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/Nerzal/gocloak/v13" | ||
| "github.com/opentdf/platform/service/logger" | ||
| "github.com/opentdf/platform/service/pkg/cache" | ||
| ) | ||
|
|
||
| // Cache Key formats | ||
| // Client: {realm}::client::{clientid} | ||
| // User: {realm}::user::{emailaddress or username} | ||
| // Group: {realm}::group::{emailaddress or id} | ||
| // Group members: {realm}::group::{groupid}::members | ||
|
|
||
| func retrieveClients(ctx context.Context, logger *logger.Logger, clientID string, realm string, svcCache *cache.Cache, connector *Connector) ([]*gocloak.Client, error) { | ||
| cacheKey := fmt.Sprintf("%s::client::%s", realm, clientID) | ||
| retrievalFunc := func() ([]*gocloak.Client, error) { | ||
| return connector.client.GetClients(ctx, connector.token.AccessToken, realm, gocloak.GetClientsParams{ | ||
| ClientID: &clientID, | ||
| }) | ||
| } | ||
| clients, err := retrieveWithKey[[]*gocloak.Client](ctx, cacheKey, svcCache, logger, retrievalFunc) | ||
elizabethhealy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return clients, nil | ||
| } | ||
|
|
||
| func retrieveUsers(ctx context.Context, logger *logger.Logger, getUserParams gocloak.GetUsersParams, realm string, svcCache *cache.Cache, connector *Connector) ([]*gocloak.User, error) { | ||
| var cacheKey string | ||
| switch { | ||
| case getUserParams.Email != nil: | ||
| cacheKey = fmt.Sprintf("%s::user::%s", realm, *getUserParams.Email) | ||
| case getUserParams.Username != nil: | ||
| cacheKey = fmt.Sprintf("%s::user::%s", realm, *getUserParams.Username) | ||
| default: | ||
| return nil, errors.New("either email or username must be provided") | ||
| } | ||
|
|
||
| retrievalFunc := func() ([]*gocloak.User, error) { | ||
| return connector.client.GetUsers(ctx, connector.token.AccessToken, realm, getUserParams) | ||
| } | ||
| return retrieveWithKey[[]*gocloak.User](ctx, cacheKey, svcCache, logger, retrievalFunc) | ||
| } | ||
|
|
||
| func retrieveGroupsByEmail(ctx context.Context, logger *logger.Logger, groupEmail string, realm string, svcCache *cache.Cache, connector *Connector) ([]*gocloak.Group, error) { | ||
| cacheKey := fmt.Sprintf("%s::group::%s", realm, groupEmail) | ||
| retrievalFunc := func() ([]*gocloak.Group, error) { | ||
| return connector.client.GetGroups( | ||
| ctx, | ||
| connector.token.AccessToken, | ||
| realm, | ||
| gocloak.GetGroupsParams{Search: func() *string { t := groupEmail; return &t }()}, | ||
| ) | ||
| } | ||
| return retrieveWithKey[[]*gocloak.Group](ctx, cacheKey, svcCache, logger, retrievalFunc) | ||
| } | ||
|
|
||
| func retrieveGroupByID(ctx context.Context, logger *logger.Logger, groupID string, realm string, svcCache *cache.Cache, connector *Connector) (*gocloak.Group, error) { | ||
| cacheKey := fmt.Sprintf("%s::group::%s", realm, groupID) | ||
| retrievalFunc := func() (*gocloak.Group, error) { | ||
| return connector.client.GetGroup(ctx, connector.token.AccessToken, realm, groupID) | ||
| } | ||
| return retrieveWithKey[*gocloak.Group](ctx, cacheKey, svcCache, logger, retrievalFunc) | ||
| } | ||
|
|
||
| func retrieveGroupMembers(ctx context.Context, logger *logger.Logger, groupID string, realm string, svcCache *cache.Cache, connector *Connector) ([]*gocloak.User, error) { | ||
| cacheKey := fmt.Sprintf("%s::group::%s::members", realm, groupID) | ||
| retrievalFunc := func() ([]*gocloak.User, error) { | ||
| return connector.client.GetGroupMembers(ctx, connector.token.AccessToken, realm, groupID, gocloak.GetGroupsParams{}) | ||
| } | ||
| return retrieveWithKey[[]*gocloak.User](ctx, cacheKey, svcCache, logger, retrievalFunc) | ||
| } | ||
|
|
||
| func retrieveWithKey[T any](ctx context.Context, cacheKey string, svcCache *cache.Cache, logger *logger.Logger, retrieveFunc func() (T, error)) (T, error) { | ||
| if svcCache != nil { | ||
| cachedData, err := svcCache.Get(ctx, cacheKey) | ||
| if err == nil { | ||
| if retrieved, ok := cachedData.(T); ok { | ||
| return retrieved, nil | ||
| } | ||
| logger.Error("cache data type assertion failed") | ||
| } else if !errors.Is(err, cache.ErrCacheMiss) { | ||
| var zero T | ||
| return zero, err | ||
| } | ||
| } | ||
| retrieved, err := retrieveFunc() | ||
| if svcCache != nil && err == nil { | ||
| cacheErr := svcCache.Set(ctx, cacheKey, retrieved, []string{}) | ||
| if cacheErr != nil { | ||
| logger.Error("error setting cache", slog.String("error", cacheErr.Error())) | ||
| } | ||
| } | ||
| return retrieved, err | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.