Repository navigation
[Server] Stop writing the admin token to a file #2082
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Prints an admin token signed with the server's key, for example: | ||
| # bin/uc --auth_token "$(bin/uc-admin-token)" user list | ||
| # Like the server, it reads etc/conf in the current directory and the UC_ environment variables, | ||
| # so run it from the directory you start the server in, with the same environment. | ||
|
|
||
| SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" | ||
| ROOT_DIR="$(dirname "$SCRIPT_DIR")" | ||
|
|
||
| if [ -d "$ROOT_DIR/jars" ]; then | ||
| CLASSPATH_FILE="$ROOT_DIR/jars/classpath" | ||
| else | ||
| CLASSPATH_FILE="$ROOT_DIR/server/target/classpath" | ||
| fi | ||
| if [ ! -f "$CLASSPATH_FILE" ]; then | ||
| echo "No classpath file at $CLASSPATH_FILE; in a source checkout, run build/sbt package first." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| exec java -cp "$(cat "$CLASSPATH_FILE")" io.unitycatalog.server.security.AdminTokenCommand |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||
| package io.unitycatalog.server.security; | ||||||
|
|
||||||
| import io.unitycatalog.server.UnityCatalogServer; | ||||||
| import io.unitycatalog.server.security.SecurityContext.Issuers; | ||||||
| import io.unitycatalog.server.utils.ServerProperties; | ||||||
| import java.io.PrintStream; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | ||||||
| import org.apache.logging.log4j.Level; | ||||||
| import org.apache.logging.log4j.core.config.Configurator; | ||||||
|
|
||||||
| /** | ||||||
| * Prints an admin token signed with the server's key, for creating the first users and grants: | ||||||
| * {@code bin/uc --auth_token "$(bin/uc-admin-token)" user list}. It reads the key the server signs | ||||||
| * with, from the token signing key file setting or {@code etc/conf}, and writes no key or token | ||||||
| * files. Run it with the server's environment, which can hold that setting. | ||||||
| */ | ||||||
| public final class AdminTokenCommand { | ||||||
|
|
||||||
| private AdminTokenCommand() {} | ||||||
|
|
||||||
| public static void main(String[] args) throws Exception { | ||||||
|
shba24 marked this conversation as resolved.
Resolved
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (not a blocker): The server already runs one-shot commands through
Cost of keeping it: Once this ships, Add an option such as EvidenceAdminTokenCommand.main builds its own log setup, a fixed etc/conf path and System.exit, and bin/uc-admin-token resolves the classpath itself. Meanwhile UnityCatalogServer.main already sends one-shot commands through OptionParser and SchemaMigrator.runCommand, which returns an exit code, and bin/start-uc-server holds the existing classpath lookup (find under jars, build when a jar is missing). So the same concept now has two shapes. I looked for a shared launcher helper or a server option for the token and found none. How it was checkedChecked by 1 independent check: NITPICK Was a
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [AI Assisted]
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion. I'm keeping the code as it is. |
||||||
| // Logs go to the CLI log file, or nowhere when there is none (as in a Helm pod), so standard | ||||||
| // output holds only the token. | ||||||
| Path logConfig = Path.of("etc", "conf", "cli.log4j2.properties"); | ||||||
| if (Files.exists(logConfig)) { | ||||||
| Configurator.initialize(null, logConfig.toString()); | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (not a blocker): A failed token command can still create the CLI log
For example: The description says there are no file writes. With the default CLI configuration, a missing key exits with
Suggested change
EvidenceAdminTokenCommand initializes the checked-in CLI File appender before attempting to load the key. I compiled the changed command and security classes and confirmed that an invocation with a missing key created etc/logs/cli.log. The command's own Javadoc already states the narrower guarantee. How it was checkedReproduced: head: Checked by 1 independent check: CONFIRMED References:
Could the description use the same file guarantee as the command's Javadoc?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [AI Assisted] With the default CLI log configuration, a failed run creates
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, and thanks for the catch. The command initializes logging before it reads the key, so a failed run can create the CLI log file. The Javadoc already limits the claim to key and token files, but the PR description says the command writes no files, which is too broad. I kept the code as it is. The description should say that the command writes no key or token files and may create the CLI log file. |
||||||
| } else { | ||||||
| Configurator.setRootLevel(Level.OFF); | ||||||
| } | ||||||
| try { | ||||||
| run( | ||||||
| new ServerProperties(UnityCatalogServer.SERVER_PROPERTIES_FILE), | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocker: The command can print a token the running server rejects
For example: A deployment switches to an operator key and leaves Give the command a configuration-loading path that propagates read errors. Use the legacy key only after establishing that the setting is genuinely absent. EvidenceThe new command constructs ServerProperties inside its error handler, but that implementation catches and logs file-read IOExceptions itself. With no signing-key environment override, loadExisting then selects private_key.der. I confirmed that a server.properties path that exists but cannot be read as a file proceeds to the legacy key. The caller loses the distinction between an absent setting and an unread setting. How it was checkedReproduced: head: Checked by 2 independent checks: CONFIRMED Could the command propagate a failure to read an existing properties file? |
||||||
| Path.of("etc", "conf"), | ||||||
| System.out); | ||||||
| } catch (Exception e) { | ||||||
| // One line, not a stack trace: a missing key, or one this user cannot read. | ||||||
| System.err.println( | ||||||
| "uc-admin-token: " + (e instanceof IllegalStateException ? e.getMessage() : e)); | ||||||
|
shba24 marked this conversation as resolved.
Resolved
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (not a blocker): A missing-key error can span multiple lines The missing-key exception includes the configured pathname. A single For example: With an embedded newline between EvidenceThe missing-key exception includes the pathname verbatim, and the handler prints the message verbatim. With an embedded newline in the configured pathname, the compiled command returned exit 1 and wrote two stderr lines. How it was checkedReproduced: head: Checked by 1 independent check: CONFIRMED Could the command escape embedded line breaks when rendering its diagnostic? |
||||||
| System.exit(1); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Prints one admin token, signed with the key the server uses, to out. | ||||||
| static void run(ServerProperties serverProperties, Path configurationFolder, PrintStream out) { | ||||||
| // Only reads the key: a missing key is an error here, not a reason to generate one. | ||||||
| SecurityConfiguration securityConfiguration = | ||||||
| SecurityConfiguration.loadExisting(serverProperties, configurationFolder); | ||||||
| out.println( | ||||||
| SecurityContext.createServiceToken( | ||||||
| securityConfiguration.algorithmRSA(), "server", Issuers.INTERNAL)); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import java.util.Base64; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import lombok.Getter; | ||
| import lombok.SneakyThrows; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -68,17 +69,22 @@ public class SecurityConfiguration { | |
| private final RSAPrivateKey privateKey; | ||
| private final RSAPublicKey publicKey; | ||
|
|
||
| /** The file the private key was read from. */ | ||
| @Getter private final Path privateKeyFile; | ||
|
|
||
| /** Uses the key in the configuration folder, generating it if missing. */ | ||
| @SneakyThrows | ||
| public SecurityConfiguration(Path configurationFolder) { | ||
| Path privateKeyFile = configurationFolder.resolve("private_key.der"); | ||
| privateKeyFile = configurationFolder.resolve("private_key.der"); | ||
| Path publicKeyFile = configurationFolder.resolve("public_key.der"); | ||
| initializeIfMissing(privateKeyFile, publicKeyFile); | ||
| privateKey = readPrivateKey(privateKeyFile); | ||
| publicKey = readPublicKey(publicKeyFile); | ||
| } | ||
|
|
||
| private SecurityConfiguration(RSAPrivateKey privateKey, RSAPublicKey publicKey) { | ||
| private SecurityConfiguration( | ||
| Path privateKeyFile, RSAPrivateKey privateKey, RSAPublicKey publicKey) { | ||
| this.privateKeyFile = privateKeyFile; | ||
| this.privateKey = privateKey; | ||
| this.publicKey = publicKey; | ||
| } | ||
|
|
@@ -104,6 +110,15 @@ public static SecurityConfiguration load( | |
| return new SecurityConfiguration(configurationFolder); | ||
| } | ||
|
|
||
| /** Like {@link #load}, but never generates a key: a missing key is an error. */ | ||
| static SecurityConfiguration loadExisting( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [AI Assisted] |
||
| ServerProperties serverProperties, Path configurationFolder) { | ||
| SecurityConfiguration configured = fromSettings(serverProperties); | ||
| return configured != null | ||
| ? configured | ||
| : fromPrivateKeyFile(configurationFolder.resolve("private_key.der")); | ||
| } | ||
|
|
||
| // Returns null when the key file is not set. | ||
| private static SecurityConfiguration fromSettings(ServerProperties serverProperties) { | ||
| String privateKeyFile = serverProperties.get(Property.TOKEN_SIGNING_PRIVATE_KEY_FILE); | ||
|
|
@@ -117,7 +132,8 @@ private static SecurityConfiguration fromSettings(ServerProperties serverPropert | |
| private static SecurityConfiguration fromPrivateKeyFile(Path privateKeyFile) { | ||
| requireFile(privateKeyFile); | ||
| RSAPrivateKey privateKey = readPrivateKey(privateKeyFile); | ||
| return new SecurityConfiguration(privateKey, publicKeyOf(privateKey, privateKeyFile)); | ||
| return new SecurityConfiguration( | ||
| privateKeyFile, privateKey, publicKeyOf(privateKey, privateKeyFile)); | ||
| } | ||
|
|
||
| // A file that exists but cannot be read is reported by the read, with the real cause. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.