-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for composite external auth redirect handler #10248
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
kokosing
merged 1 commit into
trinodb:master
from
wendigo:serafin/add-composite-redirect-handler
Feb 3, 2022
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
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
54 changes: 54 additions & 0 deletions
54
...nt/trino-client/src/main/java/io/trino/client/auth/external/CompositeRedirectHandler.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,54 @@ | ||
| /* | ||
| * 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.client.auth.external; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkState; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static java.lang.String.format; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class CompositeRedirectHandler | ||
| implements RedirectHandler | ||
| { | ||
| private final List<RedirectHandler> handlers; | ||
|
|
||
| public CompositeRedirectHandler(List<ExternalRedirectStrategy> strategies) | ||
| { | ||
| this.handlers = requireNonNull(strategies, "strategies is null") | ||
| .stream() | ||
| .map(ExternalRedirectStrategy::getHandler) | ||
| .collect(toImmutableList()); | ||
| checkState(!handlers.isEmpty(), "Expected at least one external redirect handler"); | ||
| } | ||
|
|
||
| @Override | ||
| public void redirectTo(URI uri) throws RedirectException | ||
| { | ||
| RedirectException redirectException = new RedirectException(format("Could not redirect to " + uri)); | ||
| for (RedirectHandler handler : handlers) { | ||
| try { | ||
| handler.redirectTo(uri); | ||
|
kokosing marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
| catch (RedirectException e) { | ||
| redirectException.addSuppressed(e); | ||
| } | ||
| } | ||
|
|
||
| throw redirectException; | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
...nt/trino-client/src/main/java/io/trino/client/auth/external/ExternalRedirectStrategy.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,40 @@ | ||
| /* | ||
| * 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.client.auth.external; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public enum ExternalRedirectStrategy | ||
| { | ||
| DESKTOP_OPEN(new DesktopBrowserRedirectHandler()), | ||
| SYSTEM_OPEN(new SystemOpenRedirectHandler()), | ||
| PRINT(new SystemOutPrintRedirectHandler()), | ||
| OPEN(new CompositeRedirectHandler(ImmutableList.of(SYSTEM_OPEN, DESKTOP_OPEN))), | ||
| ALL(new CompositeRedirectHandler(ImmutableList.of(OPEN, PRINT))) | ||
| /**/; | ||
|
|
||
| private final RedirectHandler handler; | ||
|
|
||
| ExternalRedirectStrategy(RedirectHandler handler) | ||
| { | ||
| this.handler = requireNonNull(handler, "handler is null"); | ||
| } | ||
|
|
||
| public RedirectHandler getHandler() | ||
| { | ||
| return handler; | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
...t/trino-client/src/main/java/io/trino/client/auth/external/SystemOpenRedirectHandler.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,102 @@ | ||
| /* | ||
| * 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.client.auth.external; | ||
|
|
||
| import com.google.common.base.Splitter; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static java.lang.String.format; | ||
| import static java.util.Locale.ENGLISH; | ||
|
|
||
| public class SystemOpenRedirectHandler | ||
| implements RedirectHandler | ||
| { | ||
| private static final List<String> LINUX_BROWSERS = ImmutableList.of( | ||
| "xdg-open", | ||
| "gnome-open", | ||
| "kde-open", | ||
| "chromium", | ||
| "google", | ||
| "google-chrome", | ||
|
wendigo marked this conversation as resolved.
Outdated
|
||
| "firefox", | ||
| "mozilla", | ||
| "opera", | ||
| "epiphany", | ||
| "konqueror"); | ||
|
|
||
| private static final String MACOS_OPEN_COMMAND = "open"; | ||
| private static final String WINDOWS_OPEN_COMMAND = "rundll32 url.dll,FileProtocolHandler"; | ||
|
|
||
| private static final Splitter SPLITTER = Splitter.on(":") | ||
| .omitEmptyStrings() | ||
| .trimResults(); | ||
|
|
||
| @Override | ||
| public void redirectTo(URI uri) | ||
| throws RedirectException | ||
| { | ||
| String operatingSystem = System.getProperty("os.name").toLowerCase(ENGLISH); | ||
|
|
||
| try { | ||
| if (operatingSystem.contains("mac")) { | ||
| exec(uri, MACOS_OPEN_COMMAND); | ||
| } | ||
| else if (operatingSystem.contains("windows")) { | ||
| exec(uri, WINDOWS_OPEN_COMMAND); | ||
| } | ||
| else { | ||
| String executablePath = findLinuxBrowser() | ||
| .orElseThrow(() -> new FileNotFoundException("Could not find any known linux browser in $PATH")); | ||
| exec(uri, executablePath); | ||
| } | ||
| } | ||
| catch (IOException e) { | ||
| throw new RedirectException(format("Could not open uri %s", uri), e); | ||
| } | ||
| } | ||
|
|
||
| private static Optional<String> findLinuxBrowser() | ||
| { | ||
| List<String> paths = SPLITTER.splitToList(System.getenv("PATH")); | ||
| for (String path : paths) { | ||
| File[] found = Paths.get(path) | ||
| .toFile() | ||
| .listFiles((dir, name) -> LINUX_BROWSERS.contains(name)); | ||
|
|
||
| if (found == null) { | ||
| continue; | ||
| } | ||
|
|
||
| if (found.length > 0) { | ||
| return Optional.of(found[0].getPath()); | ||
| } | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private static void exec(URI uri, String openCommand) | ||
| throws IOException | ||
| { | ||
| Runtime.getRuntime().exec(openCommand + " " + uri.toString()); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...ino-client/src/main/java/io/trino/client/auth/external/SystemOutPrintRedirectHandler.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,27 @@ | ||
| /* | ||
| * 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.client.auth.external; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| public class SystemOutPrintRedirectHandler | ||
| implements RedirectHandler | ||
| { | ||
| @Override | ||
| public void redirectTo(URI uri) throws RedirectException | ||
| { | ||
| System.out.println("External authentication required. Please go to:"); | ||
| System.out.println(uri.toString()); | ||
| } | ||
| } |
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
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.