Skip to content

Commit dc159e4

Browse files
authored
Replace Keto Authorization with External HTTP Authorization (#864)
* Replace Keto Auth with external HTTP Auth * Update dependencies for Auth Java client in Auth module * Allow JavaDoc linting to be disabled for Auth module * Add exclusion for DefaultAPI that causes failure in javadoc lint * Remove AuthUtil * Clean up dependencies * Improve class for comment for HTTPAuthorizationProvider * Fix casing for HTTPAuthProvider * Update OpenAPI specification to support resources for auth * Update External HTTP Authorization Provider to use a more generalized contract * Update formatting
1 parent bc7f3fb commit dc159e4

10 files changed

Lines changed: 360 additions & 126 deletions

File tree

‎auth/pom.xml‎

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
<artifactId>feast-parent</artifactId>
88
<version>${revision}</version>
99
</parent>
10+
1011
<artifactId>feast-auth</artifactId>
1112

1213
<name>Feast Authentication and Authorization</name>
1314

15+
<properties>
16+
<external.auth.client.package.name>feast.auth.generated.client</external.auth.client.package.name>
17+
<gson-fire-version>1.8.4</gson-fire-version>
18+
<swagger-core-version>1.5.24</swagger-core-version>
19+
<okhttp-version>3.14.7</okhttp-version>
20+
<gson-version>2.8.6</gson-version>
21+
<commons-lang3-version>3.10</commons-lang3-version>
22+
<javax-annotation-version>1.3.2</javax-annotation-version>
23+
<junit-version>4.13</junit-version>
24+
</properties>
1425
<dependencies>
1526
<dependency>
1627
<groupId>dev.feast</groupId>
@@ -32,11 +43,6 @@
3243
<artifactId>spring-security-oauth2-jose</artifactId>
3344
<version>5.3.0.RELEASE</version>
3445
</dependency>
35-
<dependency>
36-
<groupId>sh.ory.keto</groupId>
37-
<artifactId>keto-client</artifactId>
38-
<version>0.4.4-alpha.1</version>
39-
</dependency>
4046
<dependency>
4147
<groupId>org.projectlombok</groupId>
4248
<artifactId>lombok</artifactId>
@@ -46,6 +52,85 @@
4652
<artifactId>hibernate-validator</artifactId>
4753
<version>6.1.2.Final</version>
4854
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-databind</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.swagger</groupId>
65+
<artifactId>swagger-annotations</artifactId>
66+
<version>${swagger-core-version}</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.squareup.okhttp3</groupId>
70+
<artifactId>okhttp</artifactId>
71+
<version>${okhttp-version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.squareup.okhttp3</groupId>
75+
<artifactId>logging-interceptor</artifactId>
76+
<version>${okhttp-version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.code.gson</groupId>
80+
<artifactId>gson</artifactId>
81+
<version>${gson-version}</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>io.gsonfire</groupId>
85+
<artifactId>gson-fire</artifactId>
86+
<version>${gson-fire-version}</version>
87+
</dependency>
88+
<!-- @Nullable annotation -->
89+
<dependency>
90+
<groupId>com.google.code.findbugs</groupId>
91+
<artifactId>jsr305</artifactId>
92+
<version>3.0.2</version>
93+
</dependency>
4994
</dependencies>
50-
95+
<build>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.openapitools</groupId>
99+
<artifactId>openapi-generator-maven-plugin</artifactId>
100+
<version>4.3.1</version>
101+
<executions>
102+
<execution>
103+
<goals>
104+
<goal>generate</goal>
105+
</goals>
106+
<configuration>
107+
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
108+
<generatorName>java</generatorName>
109+
<packageName>${external.auth.client.package.name}</packageName>
110+
<modelPackage>${external.auth.client.package.name}.model</modelPackage>
111+
<apiPackage>${external.auth.client.package.name}.api</apiPackage>
112+
<invokerPackage>${external.auth.client.package.name}.invoker</invokerPackage>
113+
<configOptions>
114+
<groupId>${project.groupId}</groupId>
115+
<artifactId>${project.artifactId}</artifactId>
116+
<artifactVersion>${project.version}</artifactVersion>
117+
<java8>true</java8>
118+
<dateLibrary>java8</dateLibrary>
119+
<licenseName>Apache 2.0</licenseName>
120+
<licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
121+
<output>${project.build.directory}/generated-sources</output>
122+
</configOptions>
123+
</configuration>
124+
</execution>
125+
</executions>
126+
</plugin>
127+
<plugin>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-javadoc-plugin</artifactId>
130+
<configuration>
131+
<excludePackageNames>feast.auth.generated.client.api</excludePackageNames>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
51136
</project>

‎auth/src/main/java/feast/auth/authorization/AuthorizationProvider.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
public interface AuthorizationProvider {
2626

2727
/**
28-
* Validates whether a user is allowed access to the project
28+
* Validates whether a user is allowed access to a project
2929
*
30-
* @param project Name of the Feast project
30+
* @param projectId Id of the Feast project
3131
* @param authentication Spring Security Authentication object
3232
* @return AuthorizationResult result of authorization query
3333
*/
34-
AuthorizationResult checkAccess(String project, Authentication authentication);
34+
AuthorizationResult checkAccessToProject(String projectId, Authentication authentication);
3535
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

‎auth/src/main/java/feast/auth/authorization/Keto/KetoAuthorizationProvider.java‎

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)