Repository navigation
Client API v2 CLI client: provide help and autocomplete based on Keycloak version of the logged session #47525
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
vmuzikar
merged 1 commit into
keycloak:main
from
michalvavrik:feature/admin-api-v2-cli-fetching
Apr 10, 2026
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
98 changes: 98 additions & 0 deletions
98
...admin-cli/src/main/java/org/keycloak/client/admin/cli/v2/KcAdmV2ConfigCredentialsCmd.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,98 @@ | ||
| package org.keycloak.client.admin.cli.v2; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.net.URI; | ||
| import java.net.URL; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.keycloak.client.admin.cli.commands.ConfigCredentialsCmd; | ||
|
|
||
| import picocli.CommandLine.Command; | ||
| import picocli.CommandLine.Model.CommandSpec; | ||
| import picocli.CommandLine.Option; | ||
| import picocli.CommandLine.Spec; | ||
|
|
||
| import static org.keycloak.client.admin.cli.KcAdmMain.CMD; | ||
| import static org.keycloak.client.admin.cli.KcAdmMain.V2_FLAG; | ||
|
|
||
| @Command(name = "credentials", description = "--server SERVER_URL --realm REALM [ARGUMENTS]") | ||
| class KcAdmV2ConfigCredentialsCmd extends ConfigCredentialsCmd { | ||
|
|
||
| private static final int DEFAULT_MANAGEMENT_PORT = 9000; | ||
| private static final String OPENAPI_PATH = "/openapi"; | ||
|
|
||
| @Option(names = "--openapi-url", description = "URL of the OpenAPI endpoint for auto-fetching the server descriptor " + | ||
| "(default: <server-protocol>://<server-host>:" + DEFAULT_MANAGEMENT_PORT + OPENAPI_PATH + ")") | ||
| String openApiUrl; | ||
|
|
||
| @Spec | ||
| CommandSpec spec; | ||
|
|
||
| private final Path cacheDir; | ||
|
|
||
| KcAdmV2ConfigCredentialsCmd(Path cacheDir) { | ||
| this.cacheDir = cacheDir; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getCommand() { | ||
| return CMD + " " + V2_FLAG; | ||
| } | ||
|
|
||
| @Override | ||
| protected void printExtraOptions(PrintWriter out) { | ||
| out.println(" --openapi-url URL URL of the OpenAPI endpoint for auto-fetching the server descriptor"); | ||
| out.println(" (default: <server-protocol>://<server-host>:" + DEFAULT_MANAGEMENT_PORT + OPENAPI_PATH + ")"); | ||
| } | ||
|
|
||
| @Override | ||
| public void process() { | ||
| super.process(); | ||
| tryAutoFetchOpenApi(); | ||
| } | ||
|
|
||
| private void tryAutoFetchOpenApi() { | ||
| if (server == null) { | ||
| // --status without --server — no login happened, nothing to fetch | ||
| return; | ||
| } | ||
|
|
||
| String url = openApiUrl; | ||
| if (url == null) { | ||
| try { | ||
| url = deriveDefaultOpenApiUrl(); | ||
| } catch (Exception e) { | ||
| warnAutoFetchFailed("could not determine OpenAPI URL from server " + server | ||
| + (e.getMessage() != null ? ": " + e.getMessage() : "")); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| KcAdmV2CommandDescriptor descriptor = KcAdmV2ConfigOpenApiCmd.fetchDescriptorFromUrl(url); | ||
| KcAdmV2DescriptorCache cache = new KcAdmV2DescriptorCache(cacheDir); | ||
| cache.save(server, descriptor); | ||
| printToErr("OpenAPI descriptor fetched from " + url + " and cached for " + server | ||
| + " (version " + descriptor.getVersion() + ")"); | ||
| } catch (Exception e) { | ||
| warnAutoFetchFailed(url + (e.getMessage() != null ? ": " + e.getMessage() : "")); | ||
| } | ||
| } | ||
|
|
||
| private void warnAutoFetchFailed(String detail) { | ||
| printToErr("Failed to fetch OpenAPI descriptor from " + detail + ". " | ||
| + "CLI commands may not match your server version. " | ||
| + "You can use 'config openapi' to manually load the descriptor."); | ||
| } | ||
|
|
||
| private void printToErr(String message) { | ||
| spec.commandLine().getErr().println(message); | ||
| } | ||
|
|
||
| private String deriveDefaultOpenApiUrl() throws Exception { | ||
| URL serverParsed = new URL(server); | ||
| URI managementUri = new URI(serverParsed.getProtocol(), null, | ||
| serverParsed.getHost(), DEFAULT_MANAGEMENT_PORT, OPENAPI_PATH, null, null); | ||
| return managementUri.toString(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.