Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,16 @@ lazy val spark = (project in file("connectors/spark"))
),
// Arrow-backed vectorized reads need java.nio opened for off-heap memory access.
Test / javaOptions += "--add-opens=java.base/java.nio=ALL-UNNAMED",
// Test-only Spark/Delta settings.
Test / javaOptions ++= Seq(
// No web UI, which would bind a port for every Spark session.
"-Dspark.ui.enabled=false",
// No progress bars in the test output.
"-Dspark.ui.showConsoleProgress=false",
// Delta rebuilds table snapshots with 50 partitions by default, sized for real tables; for
// tiny test tables that overhead dominates the Delta read/write suites.
"-Dspark.databricks.delta.snapshotPartitions=2",
),
javafmtCheckSettings(),
javaCheckstyleSettings("dev/checkstyle-config.xml"),
Compile / compile / javacOptions ++= javacRelease11,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testAuthLoginExchangeWithCorrectIssuerAndAudience() throws IOExcepti

// The exchanged principal must be an enabled user, so create one with the bootstrap admin
// token before exchanging as that user.
String adminToken = Files.readString(Path.of("etc", "conf", "token.txt"));
String adminToken = Files.readString(configurationFolder.resolve("token.txt"));
serverConfig.setAuthToken(adminToken);
executeCliCommand(
serverConfig,
Expand Down Expand Up @@ -156,7 +156,7 @@ public void testAuthAccess()
.isInstanceOf(RuntimeException.class);

// Test with Authentication on authenticated end point
Path path = Path.of("etc", "conf", "token.txt");
Path path = configurationFolder.resolve("token.txt");
String token = Files.readString(path);

serverConfig.setAuthToken(token);
Expand Down Expand Up @@ -212,7 +212,7 @@ public void testAuthAccess()
@Test
public void testUserCrud() throws IOException {
// Test with Authentication on authenticated end point
Path path = Path.of("etc", "conf", "token.txt");
Path path = configurationFolder.resolve("token.txt");
String token = Files.readString(path);

serverConfig.setAuthToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.nio.file.Path;
import java.time.Clock;
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -110,7 +111,7 @@ public class UnityCatalogServer implements AutoCloseable {
private UnityCatalogServer(UnityCatalogServer.Builder unityCatalogServerBuilder) {
setDefaults(unityCatalogServerBuilder);
this.shutdownTimeout = unityCatalogServerBuilder.serverProperties.getShutdownTimeout();
Path configurationFolder = Path.of("etc", "conf");
Path configurationFolder = unityCatalogServerBuilder.configurationFolder;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing direct test for the new output folder

SecurityConfiguration securityConfiguration = new SecurityConfiguration(configurationFolder);

this.securityContext =
Expand Down Expand Up @@ -578,6 +579,7 @@ public static class Builder {
private HibernateConfigurator hibernateConfigurator;
private CloudCredentialVendor cloudCredentialVendor;
private UnaryOperator<FileOperations> fileOperationsDecorator = UnaryOperator.identity();
private Path configurationFolder = Path.of("etc", "conf");

private Builder() {}

Expand Down Expand Up @@ -629,6 +631,23 @@ public UnityCatalogServer.Builder fileOperationsDecorator(
return this;
}

/**
* Sets the folder holding the signing keys, which the server creates if missing, and the {@code
* certs.json} and {@code token.txt} it writes when constructed. Defaults to {@code etc/conf};
* tests pass their own folder so concurrent servers do not share these files.
*
* <p>Only these files move: {@code server.properties}, {@code hibernate.properties} and the
* log4j configuration are still read from {@code etc/conf}.
*
* @param configurationFolder the folder for the signing keys, {@code certs.json} and {@code
* token.txt}
* @return this builder
*/
public UnityCatalogServer.Builder configurationFolder(Path configurationFolder) {
this.configurationFolder = Objects.requireNonNull(configurationFolder, "configurationFolder");
return this;
}

public UnityCatalogServer build() {
return new UnityCatalogServer(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void closeReturnsWithinShutdownTimeoutWhenBlockingTaskIsWedged() throws Exceptio
.port(port)
.serverProperties(serverProperties)
.hibernateConfigurator(hibernateConfigurator)
.configurationFolder(tempDir.resolve("conf"))
.credentialOperations(
new WedgedCredentialVendor(serverProperties, entered, release, interrupted))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.unitycatalog.server.utils.TestUtils;
import java.net.ServerSocket;
import java.net.URI;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -43,12 +44,21 @@ public class UnityCatalogServerTest extends BaseServerTest {

@Test
public void startFailsWhenThePortIsTaken() throws Exception {
// The server setUp() built writes certs.json and token.txt to configurationFolder, next to the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (not a blocker): these configurationFolder checks and the null check aren't about the port, so if one fails the report names startFailsWhenThePortIsTaken. How about a separate method in this class, e.g. serverWritesCertsAndTokenToConfigurationFolder? Feel free to take or skip.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Assisted] Thanks! I folded them in on purpose: every test method here starts its own server in setUp(), so a separate method would add one more server start just for a few assertions on the server setUp() already started. The comment above the checks says what they verify, so a failure there should still be easy to place. Happy to split it if you feel strongly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, saving a server start is worth it. Fine as is, thanks!

// signing keys copied there.
String keyId = Files.readString(configurationFolder.resolve("key_id.txt"));
assertThat(Files.readString(configurationFolder.resolve("certs.json"))).contains(keyId);
assertThat(configurationFolder.resolve("token.txt")).isNotEmptyFile();
assertThatThrownBy(() -> UnityCatalogServer.builder().configurationFolder(null))
.isInstanceOf(NullPointerException.class);

try (ServerSocket occupied = new ServerSocket(0)) {
UnityCatalogServer server =
UnityCatalogServer.builder()
.port(occupied.getLocalPort())
.serverProperties(new ServerProperties(serverProperties))
.hibernateConfigurator(hibernateConfigurator)
.configurationFolder(configurationFolder)
.build();
// BaseServerTest retries only a ServerPortBindException, so the cause is part of the
// contract, as is a close() that releases what the failed start left behind.
Expand All @@ -72,6 +82,7 @@ public void failedConstructionClosesTheAuditSink() {
.port(0)
.serverProperties(new ServerProperties(audited))
.hibernateConfigurator(hibernateConfigurator)
.configurationFolder(configurationFolder)
.fileOperationsDecorator(
fileOperations -> {
throw new IllegalStateException("decorator failed");
Expand All @@ -92,6 +103,7 @@ public void closeClosesTheAuditSinkOnce() {
.port(0)
.serverProperties(new ServerProperties(audited))
.hibernateConfigurator(hibernateConfigurator)
.configurationFolder(configurationFolder)
.build();
server.start();
server.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.unitycatalog.server.base;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import com.linecorp.armeria.server.ServerPortBindException;
import io.unitycatalog.server.UnityCatalogServer;
import io.unitycatalog.server.persist.utils.FileOperations;
import io.unitycatalog.server.persist.utils.HibernateConfigurator;
import io.unitycatalog.server.security.SecurityConfiguration;
import io.unitycatalog.server.service.credential.CloudCredentialVendor;
import io.unitycatalog.server.testdatabase.TempDatabase;
import io.unitycatalog.server.testdatabase.TestDatabase;
Expand All @@ -15,6 +18,7 @@
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.CompletionException;
import java.util.stream.Stream;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -42,6 +46,15 @@ public abstract class BaseServerTest {
// The storage root URL for managed tables to be set in server properties.
protected String tableStorageRoot;

/**
* The server's configuration folder for this test, used instead of the shared {@code etc/conf}
* because the server writes {@code certs.json} and {@code token.txt} there on every start.
*/
@TempDir protected Path configurationFolder;

/** Signing keys generated once per JVM and copied into each test's configuration folder. */
private static Path signingKeysFolder;

/**
* This function should be overriden if the test wants to start UC server to take emulated cloud
* path as managed storage. The emulated cloud FS is provided by subclasses of
Expand Down Expand Up @@ -104,10 +117,34 @@ public void setUp() {
ServerProperties initServerProperties = new ServerProperties(serverProperties);
setUpCredentialOperations(initServerProperties);
hibernateConfigurator = testDatabase.hibernateConfigurator();
copySigningKeys(configurationFolder);
startServerOnFreePorts(initServerProperties);
}
}

/**
* Copies this JVM's signing keys into {@code target}, generating them on first use. Saves the RSA
* key generation the server would otherwise do for every test.
*/
private static synchronized void copySigningKeys(Path target) throws IOException {
if (signingKeysFolder == null) {
Path folder = Files.createTempDirectory("uc-test-keys");
new SecurityConfiguration(folder);
folder.toFile().deleteOnExit();
try (Stream<Path> keys = Files.list(folder)) {
keys.forEach(key -> key.toFile().deleteOnExit());
}
signingKeysFolder = folder;
}
// Replace: setUp() runs twice in one test when a subclass declares another @BeforeEach method
// that calls super.setUp(), instead of overriding setUp().
try (Stream<Path> keys = Files.list(signingKeysFolder)) {
for (Path key : keys.toList()) {
Files.copy(key, target.resolve(key.getFileName()), REPLACE_EXISTING);
}
}
}

/**
* Starts the server on a random API port, plus a second random port for optional observability.
* The ports are only free when chosen, so another process can bind one before the server does;
Expand All @@ -124,7 +161,8 @@ private void startServerOnFreePorts(ServerProperties initServerProperties) throw
.serverProperties(initServerProperties)
.hibernateConfigurator(hibernateConfigurator)
.credentialOperations(cloudCredentialVendor)
.fileOperationsDecorator(this::decorateFileOperations);
.fileOperationsDecorator(this::decorateFileOperations)
.configurationFolder(configurationFolder);
if (observabilityEnabled) {
serverBuilder.observabilityPort(observabilityPort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.unitycatalog.server.security.SecurityConfiguration;
import io.unitycatalog.server.security.SecurityContext;
import io.unitycatalog.server.utils.ServerProperties.Property;
import java.nio.file.Path;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand All @@ -33,8 +32,6 @@ protected void setUpProperties() {
public void setUp() {
super.setUp();

Path configurationFolder = Path.of("etc", "conf");

securityConfiguration = new SecurityConfiguration(configurationFolder);
securityContext =
new SecurityContext(configurationFolder, securityConfiguration, "server", INTERNAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
Expand Down Expand Up @@ -150,8 +149,6 @@ private static byte[] toUnsignedBytes(BigInteger value) {
public void setUp() {
super.setUp();

Path configurationFolder = Path.of("etc", "conf");

securityConfiguration = new SecurityConfiguration(configurationFolder);
securityContext =
new SecurityContext(configurationFolder, securityConfiguration, "server", INTERNAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected void grantPermissions(
* configuration.
*
* <p>This method creates tokens the same way the production system does, using RSA512 algorithm
* with the keys from etc/conf directory.
* with the keys from the test's configuration folder.
*
* @param subject The subject (email) for the JWT token
* @return A JWT token string signed with the actual RSA private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;

/**
* How {@link AuthDecorator} answers a valid internal token whose user lookup fails or finds no
* enabled user. The token is real, signed with the keys in {@code etc/conf}; only the user
* enabled user. The token is real, signed with keys generated in a temp folder; only the user
* repository is mocked.
*/
class AuthDecoratorTest {
Expand All @@ -49,10 +50,11 @@ class AuthDecoratorTest {
private final HttpService delegate = mock();
private AuthDecorator decorator;
private String token;
// Shared by all tests, so the signing keys are generated once.
@TempDir static Path configurationFolder;

@BeforeEach
void setUp() {
Path configurationFolder = Path.of("etc", "conf");
SecurityContext securityContext =
new SecurityContext(
configurationFolder,
Expand Down
Loading