-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add caching to authorization #884
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
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
107 changes: 107 additions & 0 deletions
107
auth/src/main/java/feast/auth/config/CacheConfiguration.java
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,107 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright 2018-2020 The Feast Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package feast.auth.config; | ||
|
|
||
| import com.google.common.cache.CacheBuilder; | ||
| import feast.auth.utils.AuthUtils; | ||
| import java.lang.reflect.Method; | ||
| import java.util.concurrent.TimeUnit; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.CachingConfigurer; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.cache.concurrent.ConcurrentMapCache; | ||
| import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | ||
| import org.springframework.cache.interceptor.CacheErrorHandler; | ||
| import org.springframework.cache.interceptor.CacheResolver; | ||
| import org.springframework.cache.interceptor.KeyGenerator; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.core.Authentication; | ||
|
|
||
| /** CacheConfiguration class defines Cache settings for HttpAuthorizationProvider class. */ | ||
| @Configuration | ||
| @EnableCaching | ||
| @Setter | ||
| @Getter | ||
| public class CacheConfiguration implements CachingConfigurer { | ||
|
|
||
| private static final int CACHE_SIZE = 10000; | ||
|
|
||
| public static int TTL = 60; | ||
|
|
||
| public static final String AUTHORIZATION_CACHE = "authorization"; | ||
|
|
||
| @Autowired SecurityProperties secutiryProps; | ||
|
|
||
| @Bean | ||
| public CacheManager cacheManager() { | ||
| ConcurrentMapCacheManager cacheManager = | ||
| new ConcurrentMapCacheManager(AUTHORIZATION_CACHE) { | ||
|
|
||
| @Override | ||
| protected Cache createConcurrentMapCache(final String name) { | ||
| return new ConcurrentMapCache( | ||
| name, | ||
| CacheBuilder.newBuilder() | ||
| .expireAfterWrite(TTL, TimeUnit.SECONDS) | ||
| .maximumSize(CACHE_SIZE) | ||
| .build() | ||
| .asMap(), | ||
| false); | ||
| } | ||
| }; | ||
|
|
||
| return cacheManager; | ||
| } | ||
|
|
||
| /* | ||
| * KeyGenerator used by {@link Cacheable} for caching authorization requests. | ||
| * Key format : checkAccessToProject-<projectId>-<subjectClaim> | ||
| */ | ||
| @Bean | ||
| public KeyGenerator authKeyGenerator() { | ||
| return (Object target, Method method, Object... params) -> { | ||
| String projectId = (String) params[0]; | ||
| Authentication authentication = (Authentication) params[1]; | ||
| String subject = | ||
| AuthUtils.getSubjectFromAuth( | ||
| authentication, secutiryProps.getAuthorization().getOptions().get("subjectClaim")); | ||
| return String.format("%s-%s-%s", method.getName(), projectId, subject); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public CacheResolver cacheResolver() { | ||
| // TODO Auto-generated method stub | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public KeyGenerator keyGenerator() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public CacheErrorHandler errorHandler() { | ||
| // TODO Auto-generated method stub | ||
| return null; | ||
| } | ||
| } | ||
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,54 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright 2018-2020 The Feast Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package feast.auth.utils; | ||
|
|
||
| import java.util.Map; | ||
| import org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.oauth2.jwt.Jwt; | ||
|
|
||
| public class AuthUtils { | ||
|
|
||
| // Suppresses default constructor, ensuring non-instantiability. | ||
| private AuthUtils() {} | ||
|
|
||
| /** | ||
| * Get user email from their authentication object. | ||
| * | ||
| * @param authentication Spring Security Authentication object, used to extract user details | ||
| * @param subjectClaim Indicates the claim where the subject can be found | ||
| * @return String user email | ||
| */ | ||
| public static String getSubjectFromAuth(Authentication authentication, String subjectClaim) { | ||
| Jwt principle = ((Jwt) authentication.getPrincipal()); | ||
| Map<String, Object> claims = principle.getClaims(); | ||
| String subjectValue = (String) claims.getOrDefault(subjectClaim, ""); | ||
|
|
||
| if (subjectValue.isEmpty()) { | ||
| throw new IllegalStateException( | ||
| String.format("JWT does not have a valid claim %s.", subjectClaim)); | ||
| } | ||
|
|
||
| if (subjectClaim.equals("email")) { | ||
| boolean validEmail = (new EmailValidator()).isValid(subjectValue, null); | ||
| if (!validEmail) { | ||
| throw new IllegalStateException("JWT contains an invalid email address"); | ||
| } | ||
| } | ||
| return subjectValue; | ||
| } | ||
| } |
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.