This commit is contained in:
Dario Ghunney Ware 2025-01-06 11:30:18 +00:00
parent db1af5c8cf
commit 0e4aac4897
9 changed files with 50 additions and 59 deletions

View File

@ -1,7 +1,7 @@
services: services:
stirling-pdf: stirling-pdf:
container_name: Stirling-PDF-Security-Fat-Postgres container_name: Stirling-PDF-Security-Fat-Postgres
image: stirlingtools/stirling-pdf:latest-fat image: stirlingtools/stirling-pdf:latest-fat-postgres
deploy: deploy:
resources: resources:
limits: limits:

View File

@ -29,7 +29,7 @@ public class DatabaseConfig {
public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) { public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) {
this.applicationProperties = applicationProperties; this.applicationProperties = applicationProperties;
this.runningEE = true; // fixMe: change back this.runningEE = runningEE;
} }
/** /**
@ -59,6 +59,10 @@ public class DatabaseConfig {
log.info("Using custom database configuration"); log.info("Using custom database configuration");
if (!datasource.getCustomDatabaseUrl().isBlank()) { if (!datasource.getCustomDatabaseUrl().isBlank()) {
if (datasource.getCustomDatabaseUrl().contains("postgresql")) {
dataSourceBuilder.driverClassName(POSTGRES_DRIVER);
}
dataSourceBuilder.url(datasource.getCustomDatabaseUrl()); dataSourceBuilder.url(datasource.getCustomDatabaseUrl());
} else { } else {
dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType())); dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType()));

View File

@ -7,6 +7,7 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
@ -20,11 +21,8 @@ import java.util.stream.Collectors;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.CannotReadScriptException; import org.springframework.jdbc.datasource.init.CannotReadScriptException;
import org.springframework.jdbc.datasource.init.ScriptException; import org.springframework.jdbc.datasource.init.ScriptException;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -50,13 +48,14 @@ public class DatabaseService implements DatabaseInterface {
} }
/** /**
* Checks if there is at least one backup * Checks if there is at least one backup. First checks if the directory exists, then checks if
* there are backup scripts within the directory
* *
* @return true if there are backup scripts, false if there are not * @return true if there are backup scripts, false if there are not
*/ */
@Override @Override
public boolean hasBackup() { public boolean hasBackup() {
Path filePath = Paths.get(BACKUP_DIR + "*"); Path filePath = Paths.get(BACKUP_DIR);
if (Files.exists(filePath)) { if (Files.exists(filePath)) {
return !getBackupList().isEmpty(); return !getBackupList().isEmpty();
@ -116,9 +115,11 @@ public class DatabaseService implements DatabaseInterface {
if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found."); if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found.");
List<FileInfo> backupList = this.getBackupList(); List<FileInfo> backupList = this.getBackupList();
backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed()); backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed());
executeDatabaseScript(Paths.get(backupList.get(0).getFilePath()));
Path latestExport = Paths.get(backupList.get(0).getFilePath());
executeDatabaseScript(latestExport);
} }
/** Imports a database backup from the specified file. */ /** Imports a database backup from the specified file. */
@ -149,7 +150,6 @@ public class DatabaseService implements DatabaseInterface {
return true; return true;
} }
/** Filter and delete old backups if there are more than 5 */
@Override @Override
public void exportDatabase() { public void exportDatabase() {
List<FileInfo> filteredBackupList = List<FileInfo> filteredBackupList =
@ -166,11 +166,13 @@ public class DatabaseService implements DatabaseInterface {
Path insertOutputFilePath = Path insertOutputFilePath =
this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX); this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX);
try (Connection conn = dataSource.getConnection()) { if (isH2Database()) {
ScriptUtils.executeSqlScript( String query = "SCRIPT SIMPLE COLUMNS DROP to ?;";
conn, new EncodedResource(new PathResource(insertOutputFilePath)));
log.info("Database export completed: {}", insertOutputFilePath); try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, insertOutputFilePath.toString());
stmt.execute();
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error during database export: {}", e.getMessage(), e); log.error("Error during database export: {}", e.getMessage(), e);
} catch (CannotReadScriptException e) { } catch (CannotReadScriptException e) {
@ -178,6 +180,9 @@ public class DatabaseService implements DatabaseInterface {
} }
} }
log.info("Database export completed: {}", insertOutputFilePath);
}
private static void deleteOldestBackup(List<FileInfo> filteredBackupList) { private static void deleteOldestBackup(List<FileInfo> filteredBackupList) {
try { try {
filteredBackupList.sort( filteredBackupList.sort(
@ -259,17 +264,20 @@ public class DatabaseService implements DatabaseInterface {
private void executeDatabaseScript(Path scriptPath) { private void executeDatabaseScript(Path scriptPath) {
if (isH2Database()) { if (isH2Database()) {
try (Connection conn = dataSource.getConnection()) { String query = "RUNSCRIPT from ?;";
ScriptUtils.executeSqlScript(
conn, new EncodedResource(new PathResource(scriptPath)));
log.info("Database import completed: {}", scriptPath); try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, scriptPath.toString());
stmt.execute();
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error during database import: {}", e.getMessage(), e); log.error("Error during database import: {}", e.getMessage(), e);
} catch (ScriptException e) { } catch (ScriptException e) {
log.error("Error: File {} not found", scriptPath.toString(), e); log.error("Error: File {} not found", scriptPath.toString(), e);
} }
} }
log.info("Database import completed: {}", scriptPath);
} }
/** /**

View File

@ -5,7 +5,6 @@ import java.util.Date;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.Data; import lombok.Data;

View File

@ -28,7 +28,7 @@ spring.thymeleaf.encoding=UTF-8
spring.web.resources.mime-mappings.webmanifest=application/manifest+json spring.web.resources.mime-mappings.webmanifest=application/manifest+json
spring.mvc.async.request-timeout=${SYSTEM_CONNECTIONTIMEOUTMILLISECONDS:1200000} spring.mvc.async.request-timeout=${SYSTEM_CONNECTIONTIMEOUTMILLISECONDS:1200000}
spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL
spring.datasource.driver-class-name=org.h2.Driver spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa spring.datasource.username=sa
spring.datasource.password= spring.datasource.password=

View File

@ -86,14 +86,14 @@ system:
tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored. tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored.
enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true
datasource: datasource:
enableCustomDatabase: true # set this property to 'true' if you would like to use your own custom database configuration enableCustomDatabase: false # set this property to 'true' if you would like to use your own custom database configuration
customDatabaseUrl: jdbc:postgresql://localhost:5432/postgres # set the url for your own custom database connection. If provided, the type, hostName, port and name are not necessary and will not be used customDatabaseUrl: jdbc:postgresql://localhost:5432/postgres # set the url for your own custom database connection. If provided, the type, hostName, port and name are not necessary and will not be used
username: postgres # set the database username
password: postgres # set the database password
type: postgresql # the type of the database to set (e.g. 'h2', 'postgresql') type: postgresql # the type of the database to set (e.g. 'h2', 'postgresql')
hostName: localhost # the host name to use for the database url. Set to 'localhost' when running the app locally. Set to match the name of the container name of your database container when running the app on a server (Docker configuration) hostName: localhost # the host name to use for the database url. Set to 'localhost' when running the app locally. Set to match the name of the container name of your database container when running the app on a server (Docker configuration)
port: 5432 # set the port number of the database. Ensure this matches the port the database is listening to port: 5432 # set the port number of the database. Ensure this matches the port the database is listening to
name: postgres # set the name of your database. Should match the name of the database you create name: postgres # set the name of your database. Should match the name of the database you create
username: postgres # set the database username
password: postgres # set the database password
ui: ui:
appName: '' # application's visible name appName: '' # application's visible name

View File

@ -1,20 +1,17 @@
package stirling.software.SPDF.config.security.database; package stirling.software.SPDF.config.security.database;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.model.provider.UnsupportedProviderException; import stirling.software.SPDF.model.provider.UnsupportedProviderException;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import javax.sql.DataSource; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -32,7 +29,6 @@ class DatabaseConfigTest {
} }
@Test @Test
@Disabled
void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException { void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException {
databaseConfig = new DatabaseConfig(applicationProperties, false); databaseConfig = new DatabaseConfig(applicationProperties, false);

View File

@ -5,15 +5,12 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.ApplicationProperties;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -21,7 +18,7 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class DatabaseServiceTest { class DatabaseServiceTest {
public static final String TEST_FILE = "test"; public static final String TEST_FILE = "test.txt";
private final String BACKUP_PATH = "configs/db/backup/"; private final String BACKUP_PATH = "configs/db/backup/";
@Mock @Mock
@ -33,33 +30,22 @@ class DatabaseServiceTest {
@InjectMocks @InjectMocks
private DatabaseService databaseService; private DatabaseService databaseService;
@BeforeEach
void setUp() throws IOException {
Files.deleteIfExists(Paths.get(BACKUP_PATH + TEST_FILE));
}
@Test @Test
void testHasNoBackups() { void testHasBackups() throws IOException {
Path backupDir = Paths.get(BACKUP_PATH);
Files.createDirectories(backupDir);
Path testFile = Paths.get(BACKUP_PATH + TEST_FILE);
ApplicationProperties.System system = mock(ApplicationProperties.System.class); ApplicationProperties.System system = mock(ApplicationProperties.System.class);
ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class); ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class);
Files.createFile(testFile);
when(applicationProperties.getSystem()).thenReturn(system); when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource); when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(false); when(datasource.isEnableCustomDatabase()).thenReturn(false);
assertFalse(databaseService.hasBackup());
}
@Test
@Disabled
void testHasBackups() throws IOException {
Path backupDir = Paths.get(BACKUP_PATH);
Files.createDirectories(backupDir);
Path testFile = Paths.get(BACKUP_PATH + TEST_FILE);
Files.createFile(testFile);
Files.createTempFile(backupDir, TEST_FILE, null);
assertTrue(databaseService.hasBackup()); assertTrue(databaseService.hasBackup());
Files.deleteIfExists(testFile);
} }
} }

View File

@ -3,7 +3,6 @@ package stirling.software.SPDF.integrationtests;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import stirling.software.SPDF.SPDFApplication; import stirling.software.SPDF.SPDFApplication;
@ -13,7 +12,6 @@ import static java.nio.file.Files.delete;
import static java.nio.file.Files.exists; import static java.nio.file.Files.exists;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@Disabled
@SpringBootTest @SpringBootTest
public class SPDFApplicationIntegrationTest { public class SPDFApplicationIntegrationTest {