-
Notifications
You must be signed in to change notification settings - Fork 3k
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 to the Preview Web UI #23230
Open
koszti
wants to merge
2
commits into
trinodb:master
Choose a base branch
from
koszti:webapp-preview-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,399
−2,290
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
26 changes: 26 additions & 0 deletions
26
core/trino-main/src/main/java/io/trino/server/ui/AuthInfo.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,26 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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 io.trino.server.ui; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public record AuthInfo(String authType, boolean passwordAllowed, boolean authenticated, Optional<String> username) | ||
{ | ||
public AuthInfo { | ||
requireNonNull(authType, "authType is null"); | ||
requireNonNull(username, "username is null"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/trino-main/src/main/java/io/trino/server/ui/FixedUserPreviewResource.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,47 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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 io.trino.server.ui; | ||
|
||
import com.google.inject.Inject; | ||
import io.trino.server.security.ResourceSecurity; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path(UI_PREVIEW_AUTH_INFO) | ||
@ResourceSecurity(WEB_UI) | ||
public class FixedUserPreviewResource | ||
{ | ||
private final String username; | ||
|
||
@Inject | ||
public FixedUserPreviewResource(FixedUserWebUiConfig config) | ||
{ | ||
this.username = requireNonNull(config, "config is null").getUsername(); | ||
} | ||
|
||
@GET | ||
@Produces(APPLICATION_JSON) | ||
public AuthInfo getAuthInfo() | ||
{ | ||
return new AuthInfo("fixed", false, true, Optional.of(username)); | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
core/trino-main/src/main/java/io/trino/server/ui/LoginPreviewResource.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,110 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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 io.trino.server.ui; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.inject.Inject; | ||
import io.trino.server.security.ResourceSecurity; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.ForbiddenException; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.NewCookie; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Strings.emptyToNull; | ||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGIN_FORM; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.getDeleteCookies; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path("") | ||
@ResourceSecurity(WEB_UI) | ||
@Consumes(APPLICATION_JSON) | ||
@Produces(APPLICATION_JSON) | ||
public class LoginPreviewResource | ||
{ | ||
private final FormWebUiAuthenticationFilter formWebUiAuthenticationManager; | ||
|
||
@Inject | ||
public LoginPreviewResource(FormWebUiAuthenticationFilter formWebUiAuthenticationManager) | ||
{ | ||
this.formWebUiAuthenticationManager = requireNonNull(formWebUiAuthenticationManager, "formWebUiAuthenticationManager is null"); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_AUTH_INFO) | ||
public AuthInfo getAuthInfo(ContainerRequestContext request, @Context SecurityContext securityContext) | ||
{ | ||
boolean isPasswordAllowed = formWebUiAuthenticationManager.isPasswordAllowed(securityContext.isSecure()); | ||
Optional<String> username = formWebUiAuthenticationManager.getAuthenticatedUsername(request); | ||
return new AuthInfo("form", isPasswordAllowed, username.isPresent(), username); | ||
} | ||
|
||
@POST | ||
@Path(UI_PREVIEW_LOGIN_FORM) | ||
public Response login(LoginForm loginForm, @Context SecurityContext securityContext) | ||
{ | ||
String username = emptyToNull(loginForm.username()); | ||
String password = emptyToNull(loginForm.password()); | ||
|
||
if (!formWebUiAuthenticationManager.isAuthenticationEnabled(securityContext.isSecure())) { | ||
throw new ForbiddenException(); | ||
} | ||
|
||
Optional<NewCookie[]> authenticationCookie = formWebUiAuthenticationManager.checkLoginCredentials(username, password, securityContext.isSecure()); | ||
if (authenticationCookie.isEmpty()) { | ||
throw new ForbiddenException(); | ||
} | ||
|
||
return Response.noContent() | ||
.cookie(authenticationCookie.get()) | ||
.build(); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_LOGOUT) | ||
public Response logout(@Context HttpHeaders httpHeaders, @Context SecurityContext securityContext) | ||
{ | ||
return Response.noContent() | ||
.cookie(getDeleteCookies(httpHeaders.getCookies(), securityContext.isSecure())) | ||
.build(); | ||
} | ||
|
||
public record LoginForm(@JsonProperty String username, @JsonProperty String password) | ||
{ | ||
@Override | ||
public String username() | ||
{ | ||
return username; | ||
} | ||
|
||
@Override | ||
public String password() | ||
{ | ||
return password; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
core/trino-main/src/main/java/io/trino/server/ui/OAuth2WebUiPreviewResource.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 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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 io.trino.server.ui; | ||
|
||
import com.google.inject.Inject; | ||
import io.trino.server.ExternalUriInfo; | ||
import io.trino.server.security.ResourceSecurity; | ||
import io.trino.server.security.oauth2.OAuth2Client; | ||
import io.trino.spi.security.Identity; | ||
import jakarta.ws.rs.BeanParam; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import static io.trino.server.ServletSecurityUtils.authenticatedIdentity; | ||
import static io.trino.server.security.ResourceSecurity.AccessType.WEB_UI; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_AUTH_INFO; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.UI_PREVIEW_LOGOUT; | ||
import static io.trino.server.ui.FormWebUiAuthenticationFilter.getDeleteCookies; | ||
import static io.trino.server.ui.OAuthWebUiCookie.delete; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@Path("") | ||
@ResourceSecurity(WEB_UI) | ||
public class OAuth2WebUiPreviewResource | ||
{ | ||
private final OAuth2Client oAuth2Client; | ||
|
||
@Inject | ||
public OAuth2WebUiPreviewResource(OAuth2Client oAuth2Client) | ||
{ | ||
this.oAuth2Client = requireNonNull(oAuth2Client, "oAuth2Client is null"); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_AUTH_INFO) | ||
@Produces(APPLICATION_JSON) | ||
public AuthInfo getAuthInfo(ContainerRequestContext request) | ||
{ | ||
Optional<String> username = authenticatedIdentity(request).map(Identity::getUser); | ||
return new AuthInfo("oauth2", false, username.isPresent(), username); | ||
} | ||
|
||
@GET | ||
@Path(UI_PREVIEW_LOGOUT) | ||
@Produces(APPLICATION_JSON) | ||
public Response logout(@Context HttpHeaders httpHeaders, @BeanParam ExternalUriInfo uriInfo, @Context SecurityContext securityContext) | ||
{ | ||
Optional<String> idToken = OAuthIdTokenCookie.read(httpHeaders.getCookies()); | ||
URI callbackUri = uriInfo.absolutePath(UI_LOGOUT + "/logout.html"); | ||
return Response.seeOther(oAuth2Client.getLogoutEndpoint(idToken, callbackUri) | ||
.orElse(callbackUri)) | ||
.cookie(OAuthIdTokenCookie.delete(httpHeaders.getCookies())) | ||
.cookie(getDeleteCookies(httpHeaders.getCookies(), securityContext.isSecure())) | ||
.cookie(delete(httpHeaders.getCookies())) | ||
.build(); | ||
} | ||
} |
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.
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.
@dain this prohibited @ResourceSecurity at the class level for io.trino Jax-RS resources. Was it on purpose?