-
Notifications
You must be signed in to change notification settings - Fork 998
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
Add Authentication and Authorization for feast serving #865
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07aed5a
Authentication and authorization for feast serving, squashed on 07/21
jmelinav e8859d5
fix e2e, add metadata plugin in jobs, merge labels, auth failure test…
jmelinav d8ebca5
fix rebase adaption.
jmelinav f5fdb3f
Fix core integration test.
jmelinav 2461ac1
Authentication integration test.
jmelinav 59bf780
Add authorization test and minor refactoring.
jmelinav 4d37a79
fix failing integration test.
jmelinav aae07e5
fix lint error.
jmelinav 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 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 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
56 changes: 56 additions & 0 deletions
56
auth/src/main/java/feast/auth/credentials/CoreAuthenticationProperties.java
This file contains 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,56 @@ | ||
/* | ||
* 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.credentials; | ||
|
||
import feast.common.validators.OneOfStrings; | ||
import java.util.Map; | ||
|
||
public class CoreAuthenticationProperties { | ||
// needs to be set to true if authentication is enabled on core | ||
private boolean enabled; | ||
|
||
// authentication provider to use | ||
@OneOfStrings({"google", "oauth"}) | ||
private String provider; | ||
|
||
// K/V options to initialize the provider. | ||
Map<String, String> options; | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public String getProvider() { | ||
return provider; | ||
} | ||
|
||
public void setProvider(String provider) { | ||
this.provider = provider; | ||
} | ||
|
||
public Map<String, String> getOptions() { | ||
return options; | ||
} | ||
|
||
public void setOptions(Map<String, String> options) { | ||
this.options = options; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
auth/src/main/java/feast/auth/credentials/GoogleAuthCredentials.java
This file contains 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,79 @@ | ||
/* | ||
* 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.credentials; | ||
|
||
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; | ||
|
||
import com.google.auth.oauth2.IdTokenCredentials; | ||
import com.google.auth.oauth2.ServiceAccountCredentials; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.concurrent.Executor; | ||
|
||
/* | ||
* Google auth provider's callCredentials Implementation for serving. | ||
* Used by CoreSpecService to connect to core. | ||
*/ | ||
public class GoogleAuthCredentials extends CallCredentials { | ||
private final IdTokenCredentials credentials; | ||
private static final String BEARER_TYPE = "Bearer"; | ||
private static final Metadata.Key<String> AUTHORIZATION_METADATA_KEY = | ||
Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER); | ||
|
||
public GoogleAuthCredentials(Map<String, String> options) throws IOException { | ||
|
||
String targetAudience = options.getOrDefault("audience", "https://localhost"); | ||
ServiceAccountCredentials serviceCreds = | ||
(ServiceAccountCredentials) | ||
ServiceAccountCredentials.getApplicationDefault() | ||
.createScoped(Arrays.asList("openid", "email")); | ||
|
||
credentials = | ||
IdTokenCredentials.newBuilder() | ||
.setIdTokenProvider(serviceCreds) | ||
.setTargetAudience(targetAudience) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void applyRequestMetadata( | ||
RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier) { | ||
appExecutor.execute( | ||
() -> { | ||
try { | ||
credentials.refreshIfExpired(); | ||
Metadata headers = new Metadata(); | ||
headers.put( | ||
AUTHORIZATION_METADATA_KEY, | ||
String.format("%s %s", BEARER_TYPE, credentials.getIdToken().getTokenValue())); | ||
applier.apply(headers); | ||
} catch (Throwable e) { | ||
applier.fail(Status.UNAUTHENTICATED.withCause(e)); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void thisUsesUnstableApi() { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
auth/src/main/java/feast/auth/credentials/OAuthCredentials.java
This file contains 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,120 @@ | ||
/* | ||
* 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.credentials; | ||
|
||
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; | ||
|
||
import com.nimbusds.jose.util.JSONObjectUtils; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.concurrent.Executor; | ||
import javax.security.sasl.AuthenticationException; | ||
import net.minidev.json.JSONObject; | ||
import okhttp3.FormBody; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; | ||
|
||
/* | ||
* Oauth Credentials Implementation for serving. | ||
* Used by CoreSpecService to connect to core. | ||
*/ | ||
public class OAuthCredentials extends CallCredentials { | ||
|
||
private static final String JWK_ENDPOINT_URI = "jwkEndpointURI"; | ||
static final String APPLICATION_JSON = "application/json"; | ||
static final String CONTENT_TYPE = "content-type"; | ||
static final String BEARER_TYPE = "Bearer"; | ||
static final String GRANT_TYPE = "grant_type"; | ||
static final String CLIENT_ID = "client_id"; | ||
static final String CLIENT_SECRET = "client_secret"; | ||
static final String AUDIENCE = "audience"; | ||
static final String OAUTH_URL = "oauth_url"; | ||
static final Metadata.Key<String> AUTHORIZATION_METADATA_KEY = | ||
Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER); | ||
|
||
private OkHttpClient httpClient; | ||
private Request request; | ||
private String accessToken; | ||
private Instant tokenExpiryTime; | ||
private NimbusJwtDecoder jwtDecoder; | ||
|
||
public OAuthCredentials(Map<String, String> options) { | ||
this.httpClient = new OkHttpClient(); | ||
if (!(options.containsKey(GRANT_TYPE) | ||
&& options.containsKey(CLIENT_ID) | ||
&& options.containsKey(AUDIENCE) | ||
&& options.containsKey(CLIENT_SECRET) | ||
&& options.containsKey(OAUTH_URL) | ||
&& options.containsKey(JWK_ENDPOINT_URI))) { | ||
throw new AssertionError( | ||
"please configure the properties:" | ||
+ " grant_type, client_id, client_secret, audience, oauth_url, jwkEndpointURI"); | ||
} | ||
RequestBody requestBody = | ||
new FormBody.Builder() | ||
.add(GRANT_TYPE, options.get(GRANT_TYPE)) | ||
.add(CLIENT_ID, options.get(CLIENT_ID)) | ||
.add(CLIENT_SECRET, options.get(CLIENT_SECRET)) | ||
.add(AUDIENCE, options.get(AUDIENCE)) | ||
.build(); | ||
this.request = | ||
new Request.Builder() | ||
.url(options.get(OAUTH_URL)) | ||
.addHeader(CONTENT_TYPE, APPLICATION_JSON) | ||
.post(requestBody) | ||
.build(); | ||
this.jwtDecoder = NimbusJwtDecoder.withJwkSetUri(options.get(JWK_ENDPOINT_URI)).build(); | ||
} | ||
|
||
@Override | ||
public void thisUsesUnstableApi() { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void applyRequestMetadata( | ||
RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier) { | ||
appExecutor.execute( | ||
() -> { | ||
try { | ||
// Fetches new token if it is not available or if token has expired. | ||
if (this.accessToken == null || Instant.now().isAfter(this.tokenExpiryTime)) { | ||
Response response = httpClient.newCall(request).execute(); | ||
if (!response.isSuccessful()) { | ||
throw new AuthenticationException(response.message()); | ||
} | ||
JSONObject json = JSONObjectUtils.parse(response.body().string()); | ||
this.accessToken = json.getAsString("access_token"); | ||
this.tokenExpiryTime = jwtDecoder.decode(this.accessToken).getExpiresAt(); | ||
} | ||
Metadata headers = new Metadata(); | ||
headers.put( | ||
AUTHORIZATION_METADATA_KEY, String.format("%s %s", BEARER_TYPE, this.accessToken)); | ||
applier.apply(headers); | ||
} catch (Throwable e) { | ||
applier.fail(Status.UNAUTHENTICATED.withCause(e)); | ||
} | ||
}); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
auth/src/main/java/feast/auth/service/AuthorizationService.java
This file contains 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,63 @@ | ||
/* | ||
* 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.service; | ||
|
||
import feast.auth.authorization.AuthorizationProvider; | ||
import feast.auth.authorization.AuthorizationResult; | ||
import feast.auth.config.SecurityProperties; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.stereotype.Service; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
public class AuthorizationService { | ||
woop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final SecurityProperties securityProperties; | ||
private final AuthorizationProvider authorizationProvider; | ||
|
||
@Autowired | ||
public AuthorizationService( | ||
SecurityProperties securityProperties, | ||
ObjectProvider<AuthorizationProvider> authorizationProvider) { | ||
this.securityProperties = securityProperties; | ||
this.authorizationProvider = authorizationProvider.getIfAvailable(); | ||
} | ||
|
||
/** | ||
* Determine whether a user has access to a project. | ||
* | ||
* @param securityContext Spring Security Context used to identify a user or service. | ||
* @param project Name of the project for which membership should be tested. | ||
*/ | ||
public void authorizeRequest(SecurityContext securityContext, String project) { | ||
Authentication authentication = securityContext.getAuthentication(); | ||
if (!this.securityProperties.getAuthorization().isEnabled()) { | ||
return; | ||
} | ||
|
||
AuthorizationResult result = | ||
this.authorizationProvider.checkAccessToProject(project, authentication); | ||
if (!result.isAllowed()) { | ||
throw new AccessDeniedException(result.getFailureReason().orElse("Access Denied")); | ||
} | ||
} | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this being used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is being used in
CoreServiceAuthTest
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed though? Seems like a bad practice. Unless this is a convention for the project, constructors should exist for a reason other than a test.