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:
stirling-pdf:
container_name: Stirling-PDF-Security-Fat-Postgres
image: stirlingtools/stirling-pdf:latest-fat
image: stirlingtools/stirling-pdf:latest-fat-postgres
deploy:
resources:
limits:

View File

@ -29,7 +29,7 @@ public class DatabaseConfig {
public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) {
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");
if (!datasource.getCustomDatabaseUrl().isBlank()) {
if (datasource.getCustomDatabaseUrl().contains("postgresql")) {
dataSourceBuilder.driverClassName(POSTGRES_DRIVER);
}
dataSourceBuilder.url(datasource.getCustomDatabaseUrl());
} else {
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.attribute.BasicFileAttributes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@ -20,11 +21,8 @@ import java.util.stream.Collectors;
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.ScriptException;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;
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
*/
@Override
public boolean hasBackup() {
Path filePath = Paths.get(BACKUP_DIR + "*");
Path filePath = Paths.get(BACKUP_DIR);
if (Files.exists(filePath)) {
return !getBackupList().isEmpty();
@ -116,9 +115,11 @@ public class DatabaseService implements DatabaseInterface {
if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found.");
List<FileInfo> backupList = this.getBackupList();
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. */
@ -149,7 +150,6 @@ public class DatabaseService implements DatabaseInterface {
return true;
}
/** Filter and delete old backups if there are more than 5 */
@Override
public void exportDatabase() {
List<FileInfo> filteredBackupList =
@ -166,16 +166,21 @@ public class DatabaseService implements DatabaseInterface {
Path insertOutputFilePath =
this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX);
try (Connection conn = dataSource.getConnection()) {
ScriptUtils.executeSqlScript(
conn, new EncodedResource(new PathResource(insertOutputFilePath)));
if (isH2Database()) {
String query = "SCRIPT SIMPLE COLUMNS DROP to ?;";
log.info("Database export completed: {}", insertOutputFilePath);
} catch (SQLException e) {
log.error("Error during database export: {}", e.getMessage(), e);
} catch (CannotReadScriptException e) {
log.error("Error during database export: File {} not found", insertOutputFilePath);
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, insertOutputFilePath.toString());
stmt.execute();
} catch (SQLException e) {
log.error("Error during database export: {}", e.getMessage(), e);
} catch (CannotReadScriptException e) {
log.error("Error during database export: File {} not found", insertOutputFilePath);
}
}
log.info("Database export completed: {}", insertOutputFilePath);
}
private static void deleteOldestBackup(List<FileInfo> filteredBackupList) {
@ -259,17 +264,20 @@ public class DatabaseService implements DatabaseInterface {
private void executeDatabaseScript(Path scriptPath) {
if (isH2Database()) {
try (Connection conn = dataSource.getConnection()) {
ScriptUtils.executeSqlScript(
conn, new EncodedResource(new PathResource(scriptPath)));
String query = "RUNSCRIPT from ?;";
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) {
log.error("Error during database import: {}", e.getMessage(), e);
} catch (ScriptException 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.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import lombok.Data;

View File

@ -28,7 +28,7 @@ spring.thymeleaf.encoding=UTF-8
spring.web.resources.mime-mappings.webmanifest=application/manifest+json
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.username=sa
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.
enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true
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
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')
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
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:
appName: '' # application's visible name

View File

@ -1,20 +1,17 @@
package stirling.software.SPDF.config.security.database;
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.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.model.provider.UnsupportedProviderException;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -32,7 +29,6 @@ class DatabaseConfigTest {
}
@Test
@Disabled
void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException {
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.Paths;
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.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -21,7 +18,7 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
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/";
@Mock
@ -33,33 +30,22 @@ class DatabaseServiceTest {
@InjectMocks
private DatabaseService databaseService;
@BeforeEach
void setUp() throws IOException {
Files.deleteIfExists(Paths.get(BACKUP_PATH + TEST_FILE));
}
@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.Datasource datasource = mock(ApplicationProperties.Datasource.class);
Files.createFile(testFile);
when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
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());
Files.deleteIfExists(testFile);
}
}

View File

@ -3,7 +3,6 @@ package stirling.software.SPDF.integrationtests;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
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 org.junit.jupiter.api.Assertions.assertTrue;
@Disabled
@SpringBootTest
public class SPDFApplicationIntegrationTest {