8000 fix: temp directory refinements by shawkins · Pull Request #48608 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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
< 8000 /div>
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class QuarkusKeycloakApplication extends KeycloakApplication {
@Override
protected String getDataDir() {
return Environment.getDataDir().orElseGet(() -> {
logger.warnf("%s is not set, the system temporary directory will be used as the Keycloak data directory.", Environment.KC_HOME_DIR);
return System.getProperty("java.io.tmpdir");
logger.warnf("%s is not set", Environment.KC_HOME_DIR);
return null;
});
}

Expand Down
10 changes: 6 additions & 4 deletions quarkus/tests/junit5/src/main/java/org/keycloak/Keycloak.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,20 @@ public static Path initTempDirectory(String name) {
String buildDir = System.getProperty("project.build.directory");
if (buildDir == null) {
try {
return Files.createTempDirectory(name).toAbsolutePath();
Path tempDirectory = Files.createTempDirectory(name);
tempDirectory.toFile().deleteOnExit();
return tempDirectory.toAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Could not create temporary directory", e);
}
} else {
Path homeDir = Path.of(buildDir, name);
Path tempDirectory = Path.of(buildDir, name);
try {
FileUtils.deleteDirectory(homeDir.toFile());
FileUtils.deleteDirectory(tempDirectory.toFile());
} catch (IOException e) {
e.printStackTrace();
}
return homeDir;
return tempDirectory;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ private synchronized File initCacheDir() {
}
}

if (!cacheDir.isDirectory() && !cacheDir.mkdirs()) {
logger.warn("Failed to create gzip cache directory");
cacheDir.mkdirs();
if (!cacheDir.isDirectory()) {
logger.warn("Failed to create gzip cache directory " + cacheDir.getAbsolutePath());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import jakarta.ws.rs.core.Application;
Expand Down Expand Up @@ -52,20 +53,21 @@ public abstract class KeycloakApplication extends Application {

private static volatile DefaultKeycloakSessionFactory sessionFactory;

/**
* Get the temp directory as initialized by the current KeycloakApplication.
* <br>
* The directory is not guaranteed to exist
*/
public static String getTmpDirectory() {
return System.getProperty(KC_TMPDIR, System.getProperty("java.io.tmpdir"));
return Optional.ofNullable(System.getProperty(KC_TMPDIR)).orElseThrow(() -> new RuntimeException("No temporary directory was configured."));
Comment thread
shawkins marked this conversation as resolved.
Resolved
}

protected void initTmpDirectory() {
String dataDir = getDataDir();
File tmpDir = new File(dataDir, "tmp");
tmpDir.mkdirs();
if (tmpDir.isDirectory()) {
logger.debugf("Using server tmp directory: %s", tmpDir.getAbsolutePath());
} else {
logger.warnf("Temporary directory %s does not exist and it was not possible to create it.", tmpDir.getAbsolutePath());
if (dataDir != null) {
File tmpDir = new File(dataDir, "tmp");
System.setProperty(KC_TMPDIR, tmpDir.getAbsolutePath());
}
System.setProperty(KC_TMPDIR, tmpDir.getAbsolutePath());
}

protected abstract String getDataDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ResteasyKeycloakApplication() {

@Override
protected String getDataDir() {
return System.getProperty("project.build.directory");
return System.getProperty("jboss.server.data.dir");
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion testsuite/utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@
<artifactId>mssql-jdbc</artifactId>
<version>${mssql-jdbc.version}</version>
</dependency>


<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-junit5</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-micrometer</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
Expand All @@ -33,7 +32,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
Expand All @@ -43,6 +41,7 @@
import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;

import org.keycloak.Keycloak;
import org.keycloak.authentication.AuthenticatorSpi;
import org.keycloak.authentication.authenticators.browser.DeployedScriptAuthenticatorFactory;
import org.keycloak.authorization.policy.provider.PolicySpi;
Expand Down Expand Up @@ -321,24 +320,7 @@ public static String detectDataDirectory() {
}

// we generate a dynamic jboss.server.data.dir and remove it at the end.
try {
String tempKeycloakFolder = KeycloakApplication.getTmpDirectory();
File tmpDataDir = new File(tempKeycloakFolder, "/data");

if (tmpDataDir.mkdirs()) {
tmpDataDir.deleteOnExit();
} else try (Stream<Path> dir = Files.list(tmpDataDir.toPath())) {
if (dir.findAny().isPresent()) { // Works well if directory is empty
throw new IOException("Could not create directory " + tmpDataDir);
}
}

dataPath = tmpDataDir.getAbsolutePath();
} catch (IOException ioe){
throw new RuntimeException("Could not create temporary " + JBOSS_SERVER_DATA_DIR, ioe);
}

return dataPath;
return Keycloak.initTempDirectory("keycloak-data").toFile().getAbsolutePath();
}

private KeycloakServerConfig config;
Expand Down
Loading
0