|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.auth.authorization; |
| 18 | + |
| 19 | +import feast.auth.generated.client.api.DefaultApi; |
| 20 | +import feast.auth.generated.client.invoker.ApiClient; |
| 21 | +import feast.auth.generated.client.invoker.ApiException; |
| 22 | +import feast.auth.generated.client.model.CheckAccessRequest; |
| 23 | +import java.util.Map; |
| 24 | +import org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.springframework.security.core.Authentication; |
| 28 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 29 | + |
| 30 | +/** |
| 31 | + * HTTPAuthorizationProvider uses an external HTTP service for authorizing requests. Please see |
| 32 | + * auth/src/main/resources/api.yaml for the API specification of this external service. |
| 33 | + */ |
| 34 | +public class HttpAuthorizationProvider implements AuthorizationProvider { |
| 35 | + |
| 36 | + private static final Logger log = LoggerFactory.getLogger(HttpAuthorizationProvider.class); |
| 37 | + |
| 38 | + private final DefaultApi defaultApiClient; |
| 39 | + |
| 40 | + /** |
| 41 | + * The default subject claim is the key within the Authentication object where the user's identity |
| 42 | + * can be found |
| 43 | + */ |
| 44 | + private final String DEFAULT_SUBJECT_CLAIM = "email"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Initializes the HTTPAuthorizationProvider |
| 48 | + * |
| 49 | + * @param options String K/V pair of options to initialize the provider with. Expects at least a |
| 50 | + * "basePath" for the provider URL |
| 51 | + */ |
| 52 | + public HttpAuthorizationProvider(Map<String, String> options) { |
| 53 | + if (options == null) { |
| 54 | + throw new IllegalArgumentException( |
| 55 | + "Cannot pass empty or null options to HTTPAuthorizationProvider"); |
| 56 | + } |
| 57 | + |
| 58 | + ApiClient apiClient = new ApiClient(); |
| 59 | + apiClient.setBasePath(options.get("authorizationUrl")); |
| 60 | + this.defaultApiClient = new DefaultApi(apiClient); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Validates whether a user has access to a project |
| 65 | + * |
| 66 | + * @param projectId Name of the Feast project |
| 67 | + * @param authentication Spring Security Authentication object |
| 68 | + * @return AuthorizationResult result of authorization query |
| 69 | + */ |
| 70 | + public AuthorizationResult checkAccessToProject(String projectId, Authentication authentication) { |
| 71 | + |
| 72 | + CheckAccessRequest checkAccessRequest = new CheckAccessRequest(); |
| 73 | + Object context = getContext(authentication); |
| 74 | + String subject = getSubjectFromAuth(authentication, DEFAULT_SUBJECT_CLAIM); |
| 75 | + checkAccessRequest.setAction("ALL"); |
| 76 | + checkAccessRequest.setContext(context); |
| 77 | + checkAccessRequest.setResource(projectId); |
| 78 | + checkAccessRequest.setSubject(subject); |
| 79 | + |
| 80 | + try { |
| 81 | + // Make authorization request to external service |
| 82 | + feast.auth.generated.client.model.AuthorizationResult authResult = |
| 83 | + defaultApiClient.checkAccessPost(checkAccessRequest); |
| 84 | + if (authResult == null) { |
| 85 | + throw new RuntimeException( |
| 86 | + String.format( |
| 87 | + "Empty response returned for access to project %s for subject %s", |
| 88 | + projectId, subject)); |
| 89 | + } |
| 90 | + if (authResult.getAllowed()) { |
| 91 | + // Successfully authenticated |
| 92 | + return AuthorizationResult.success(); |
| 93 | + } |
| 94 | + } catch (ApiException e) { |
| 95 | + log.error("API exception has occurred during authorization: {}", e.getMessage(), e); |
| 96 | + } |
| 97 | + |
| 98 | + // Could not determine project membership, deny access. |
| 99 | + return AuthorizationResult.failed( |
| 100 | + String.format("Access denied to project %s for subject %s", projectId, subject)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Extract a context object to send as metadata to the authorization service |
| 105 | + * |
| 106 | + * @param authentication Spring Security Authentication object |
| 107 | + * @return Returns a context object that will be serialized and sent as metadata to the |
| 108 | + * authorization service |
| 109 | + */ |
| 110 | + private Object getContext(Authentication authentication) { |
| 111 | + // Not implemented yet, left empty |
| 112 | + return new Object(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get user email from their authentication object. |
| 117 | + * |
| 118 | + * @param authentication Spring Security Authentication object, used to extract user details |
| 119 | + * @param subjectClaim Indicates the claim where the subject can be found |
| 120 | + * @return String user email |
| 121 | + */ |
| 122 | + private String getSubjectFromAuth(Authentication authentication, String subjectClaim) { |
| 123 | + Jwt principle = ((Jwt) authentication.getPrincipal()); |
| 124 | + Map<String, Object> claims = principle.getClaims(); |
| 125 | + String subjectValue = (String) claims.get(subjectClaim); |
| 126 | + |
| 127 | + if (subjectValue.isEmpty()) { |
| 128 | + throw new IllegalStateException( |
| 129 | + String.format("JWT does not have a valid claim %s.", subjectClaim)); |
| 130 | + } |
| 131 | + |
| 132 | + if (subjectClaim.equals("email")) { |
| 133 | + boolean validEmail = (new EmailValidator()).isValid(subjectValue, null); |
| 134 | + if (!validEmail) { |
| 135 | + throw new IllegalStateException("JWT contains an invalid email address"); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return subjectValue; |
| 140 | + } |
| 141 | +} |
0 commit comments