-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Allow to store externalAuthenticationToken in SYSTEM Cache #28783
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 2 commits into
trinodb:master
from
ssheikin:ssheikin/43/trino/cli-cache-token-SYSTEM
Apr 1, 2026
Merged
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 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
151 changes: 151 additions & 0 deletions
151
client/trino-client/src/main/java/io/trino/client/auth/external/SystemCachedKnownToken.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,151 @@ | ||
| /* | ||
| * 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.ImmutableSet; | ||
| import dev.failsafe.Failsafe; | ||
| import dev.failsafe.RetryPolicy; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.file.FileAlreadyExistsException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; | ||
| import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; | ||
| import static java.time.temporal.ChronoUnit.MILLIS; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * This KnownToken instance persists the token to ~/.trino/.token on the filesystem, | ||
| * allowing it to be reused across separate CLI invocations. | ||
| * A lock file (~/.trino/.token.lck) is used to coordinate token acquisition | ||
| * across processes — its atomic creation acts as a cross-process tryLock. | ||
| * The implementation is similar to MemoryCachedKnownToken, but with LOCK_FILE acting as a Lock | ||
| */ | ||
| class SystemCachedKnownToken | ||
| implements KnownToken | ||
| { | ||
| private static final Path DEFAULT_TRINO_DIR = Path.of(System.getProperty("user.home"), ".trino"); | ||
| // duration for the user to authenticate within IDP. It involves clicking and typing from a real person within a browser, so should be counted in minutes. | ||
| private static final Duration DEFAULT_LOCK_MAX_WAIT = Duration.ofMinutes(10); | ||
|
|
||
| public static final SystemCachedKnownToken INSTANCE = new SystemCachedKnownToken(DEFAULT_TRINO_DIR); | ||
|
|
||
| private final Path trinoDir; | ||
| private final Path tokenFile; | ||
| private final Path lockFile; | ||
| private final Duration lockMaxWait; | ||
|
|
||
| SystemCachedKnownToken(Path trinoDir) | ||
| { | ||
| this(trinoDir, DEFAULT_LOCK_MAX_WAIT); | ||
| } | ||
|
|
||
| SystemCachedKnownToken(Path trinoDir, Duration lockMaxWait) | ||
| { | ||
| this.trinoDir = requireNonNull(trinoDir, "trinoDir is null"); | ||
| this.tokenFile = trinoDir.resolve(".token"); | ||
| this.lockFile = trinoDir.resolve(".token.lck"); | ||
| this.lockMaxWait = requireNonNull(lockMaxWait, "lockMaxWait is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Token> getToken() | ||
| { | ||
| // Wait while lock file exists, mimicking readLock blocking while writeLock is held | ||
| boolean lockFileExists = Failsafe.with(RetryPolicy.<Boolean>builder() | ||
| .handleResultIf(Boolean.TRUE::equals) | ||
| .withMaxAttempts(-1) | ||
| .withDelay(100, 1000, MILLIS) | ||
| .withMaxDuration(lockMaxWait) | ||
| .build()) | ||
| .get(() -> Files.exists(lockFile)); | ||
|
ssheikin marked this conversation as resolved.
|
||
| if (lockFileExists) { | ||
| throw new IllegalStateException("Lock file " + lockFile + " for System Cached token still exists after waiting " + lockMaxWait + ". " + | ||
| "It may be created by another concurrent authentication, which is still in progress. " + | ||
| "If it's not a case and another transaction was abandoned - please remove the lock file manually and retry authentication."); | ||
| } | ||
|
|
||
| if (!Files.exists(tokenFile)) { | ||
| return Optional.empty(); | ||
| } | ||
| try { | ||
| String content = Files.readString(tokenFile).trim(); | ||
| if (content.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(new Token(content)); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to read token from " + tokenFile, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setupToken(Supplier<Optional<Token>> tokenSource) | ||
| { | ||
| try { | ||
| Files.createDirectories(trinoDir); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to create directory " + trinoDir, e); | ||
| } | ||
|
|
||
| // Atomically create the lock file. If it already exists, another process | ||
| // is obtaining a token — skip, just like MemoryCachedKnownToken's tryLock. | ||
| try { | ||
| Files.createFile(lockFile); | ||
| } | ||
| catch (FileAlreadyExistsException e) { | ||
| return; | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to create lock file " + lockFile, e); | ||
| } | ||
|
|
||
| try { | ||
| // Clear token before obtaining new one, as it might fail leaving old invalid token. | ||
| Files.deleteIfExists(tokenFile); | ||
| Optional<Token> token = tokenSource.get(); | ||
| token.ifPresent(this::writeTokenToFile); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to update token file " + tokenFile, e); | ||
| } | ||
| finally { | ||
| try { | ||
| Files.deleteIfExists(lockFile); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to delete lock file " + lockFile, e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void writeTokenToFile(Token token) | ||
| { | ||
| try { | ||
| Files.writeString(tokenFile, token.token()); | ||
| Files.setPosixFilePermissions(tokenFile, ImmutableSet.of(OWNER_READ, OWNER_WRITE)); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to write token to " + tokenFile, e); | ||
| } | ||
| } | ||
| } | ||
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
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.