mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-23 00:06:08 +01:00
adding checks for H2
This commit is contained in:
parent
6b9d85a119
commit
db1af5c8cf
@ -3,7 +3,6 @@ package stirling.software.SPDF.config.security;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
@ -17,11 +16,20 @@ import stirling.software.SPDF.model.provider.UnsupportedProviderException;
|
|||||||
@Component
|
@Component
|
||||||
public class InitialSecuritySetup {
|
public class InitialSecuritySetup {
|
||||||
|
|
||||||
@Autowired private UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
@Autowired private ApplicationProperties applicationProperties;
|
private final ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
@Autowired private DatabaseInterface databaseService;
|
private final DatabaseInterface databaseService;
|
||||||
|
|
||||||
|
public InitialSecuritySetup(
|
||||||
|
UserService userService,
|
||||||
|
ApplicationProperties applicationProperties,
|
||||||
|
DatabaseInterface databaseService) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.applicationProperties = applicationProperties;
|
||||||
|
this.databaseService = databaseService;
|
||||||
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
|
@ -2,7 +2,6 @@ package stirling.software.SPDF.config.security.database;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -28,7 +27,6 @@ public class DatabaseConfig {
|
|||||||
private final ApplicationProperties applicationProperties;
|
private final ApplicationProperties applicationProperties;
|
||||||
private final boolean runningEE;
|
private final boolean runningEE;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
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 = true; // fixMe: change back
|
||||||
|
@ -20,7 +20,6 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.core.io.PathResource;
|
import org.springframework.core.io.PathResource;
|
||||||
import org.springframework.core.io.support.EncodedResource;
|
import org.springframework.core.io.support.EncodedResource;
|
||||||
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
|
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
|
||||||
@ -45,7 +44,6 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
private final ApplicationProperties applicationProperties;
|
private final ApplicationProperties applicationProperties;
|
||||||
private final DataSource dataSource;
|
private final DataSource dataSource;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public DatabaseService(ApplicationProperties applicationProperties, DataSource dataSource) {
|
public DatabaseService(ApplicationProperties applicationProperties, DataSource dataSource) {
|
||||||
this.applicationProperties = applicationProperties;
|
this.applicationProperties = applicationProperties;
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
@ -75,6 +73,8 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
@Override
|
@Override
|
||||||
public List<FileInfo> getBackupList() {
|
public List<FileInfo> getBackupList() {
|
||||||
List<FileInfo> backupFiles = new ArrayList<>();
|
List<FileInfo> backupFiles = new ArrayList<>();
|
||||||
|
|
||||||
|
if (isH2Database()) {
|
||||||
Path backupPath = Paths.get(BACKUP_DIR);
|
Path backupPath = Paths.get(BACKUP_DIR);
|
||||||
|
|
||||||
try (DirectoryStream<Path> stream =
|
try (DirectoryStream<Path> stream =
|
||||||
@ -82,9 +82,12 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
backupPath,
|
backupPath,
|
||||||
path ->
|
path ->
|
||||||
path.getFileName().toString().startsWith(BACKUP_PREFIX)
|
path.getFileName().toString().startsWith(BACKUP_PREFIX)
|
||||||
&& path.getFileName().toString().endsWith(SQL_SUFFIX))) {
|
&& path.getFileName()
|
||||||
|
.toString()
|
||||||
|
.endsWith(SQL_SUFFIX))) {
|
||||||
for (Path entry : stream) {
|
for (Path entry : stream) {
|
||||||
BasicFileAttributes attrs = Files.readAttributes(entry, BasicFileAttributes.class);
|
BasicFileAttributes attrs =
|
||||||
|
Files.readAttributes(entry, BasicFileAttributes.class);
|
||||||
LocalDateTime modificationDate =
|
LocalDateTime modificationDate =
|
||||||
LocalDateTime.ofInstant(
|
LocalDateTime.ofInstant(
|
||||||
attrs.lastModifiedTime().toInstant(), ZoneId.systemDefault());
|
attrs.lastModifiedTime().toInstant(), ZoneId.systemDefault());
|
||||||
@ -103,6 +106,7 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Error reading backup directory: {}", e.getMessage(), e);
|
log.error("Error reading backup directory: {}", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return backupFiles;
|
return backupFiles;
|
||||||
}
|
}
|
||||||
@ -196,11 +200,7 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
public String getH2Version() {
|
public String getH2Version() {
|
||||||
String version = "Unknown";
|
String version = "Unknown";
|
||||||
|
|
||||||
if (applicationProperties
|
if (isH2Database()) {
|
||||||
.getSystem()
|
|
||||||
.getDatasource()
|
|
||||||
.getType()
|
|
||||||
.equals(ApplicationProperties.Driver.H2.name())) {
|
|
||||||
try (Connection conn = dataSource.getConnection()) {
|
try (Connection conn = dataSource.getConnection()) {
|
||||||
try (Statement stmt = conn.createStatement();
|
try (Statement stmt = conn.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("SELECT H2VERSION() AS version")) {
|
ResultSet rs = stmt.executeQuery("SELECT H2VERSION() AS version")) {
|
||||||
@ -217,6 +217,13 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isH2Database() {
|
||||||
|
ApplicationProperties.Datasource datasource =
|
||||||
|
applicationProperties.getSystem().getDatasource();
|
||||||
|
return !datasource.isEnableCustomDatabase()
|
||||||
|
|| datasource.getType().equals(ApplicationProperties.Driver.H2.name());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a backup file.
|
* Deletes a backup file.
|
||||||
*
|
*
|
||||||
@ -251,11 +258,7 @@ public class DatabaseService implements DatabaseInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void executeDatabaseScript(Path scriptPath) {
|
private void executeDatabaseScript(Path scriptPath) {
|
||||||
if (applicationProperties
|
if (isH2Database()) {
|
||||||
.getSystem()
|
|
||||||
.getDatasource()
|
|
||||||
.getType()
|
|
||||||
.equals(ApplicationProperties.Driver.H2.name())) {
|
|
||||||
try (Connection conn = dataSource.getConnection()) {
|
try (Connection conn = dataSource.getConnection()) {
|
||||||
ScriptUtils.executeSqlScript(
|
ScriptUtils.executeSqlScript(
|
||||||
conn, new EncodedResource(new PathResource(scriptPath)));
|
conn, new EncodedResource(new PathResource(scriptPath)));
|
||||||
|
@ -77,8 +77,7 @@ public class DatabaseController {
|
|||||||
@GetMapping("/import-database-file/{fileName}")
|
@GetMapping("/import-database-file/{fileName}")
|
||||||
public String importDatabaseFromBackupUI(
|
public String importDatabaseFromBackupUI(
|
||||||
@Parameter(description = "Name of the file to import", required = true) @PathVariable
|
@Parameter(description = "Name of the file to import", required = true) @PathVariable
|
||||||
String fileName)
|
String fileName) {
|
||||||
throws IOException {
|
|
||||||
if (fileName == null || fileName.isEmpty()) {
|
if (fileName == null || fileName.isEmpty()) {
|
||||||
return "redirect:/database?error=fileNullOrEmpty";
|
return "redirect:/database?error=fileNullOrEmpty";
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import lombok.Data;
|
|||||||
public class SessionEntity implements Serializable {
|
public class SessionEntity implements Serializable {
|
||||||
@Id private String sessionId;
|
@Id private String sessionId;
|
||||||
|
|
||||||
@Lob private String principalName;
|
private String principalName;
|
||||||
|
|
||||||
private Date lastRequest;
|
private Date lastRequest;
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class User implements Serializable {
|
|||||||
@ElementCollection
|
@ElementCollection
|
||||||
@MapKeyColumn(name = "setting_key")
|
@MapKeyColumn(name = "setting_key")
|
||||||
@Lob
|
@Lob
|
||||||
@Column(name = "setting_value", columnDefinition = "CLOB")
|
@Column(name = "setting_value", columnDefinition = "text")
|
||||||
@CollectionTable(name = "user_settings", joinColumns = @JoinColumn(name = "user_id"))
|
@CollectionTable(name = "user_settings", joinColumns = @JoinColumn(name = "user_id"))
|
||||||
private Map<String, String> settings = new HashMap<>(); // Key-value pairs of settings.
|
private Map<String, String> settings = new HashMap<>(); // Key-value pairs of settings.
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ system:
|
|||||||
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: true # 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
|
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
|
||||||
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
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package stirling.software.SPDF.config.security.database;
|
package stirling.software.SPDF.config.security.database;
|
||||||
|
|
||||||
import java.nio.file.attribute.FileAttribute;
|
import java.io.IOException;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
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.BeforeEach;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -9,13 +12,11 @@ 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 java.io.IOException;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import java.nio.file.Files;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import java.nio.file.Path;
|
import static org.mockito.Mockito.mock;
|
||||||
import java.nio.file.Paths;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class DatabaseServiceTest {
|
class DatabaseServiceTest {
|
||||||
@ -24,7 +25,10 @@ class DatabaseServiceTest {
|
|||||||
private final String BACKUP_PATH = "configs/db/backup/";
|
private final String BACKUP_PATH = "configs/db/backup/";
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private DatabaseConfig databaseConfig;
|
private ApplicationProperties applicationProperties;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private DatabaseService databaseService;
|
private DatabaseService databaseService;
|
||||||
@ -36,6 +40,13 @@ class DatabaseServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testHasNoBackups() {
|
void testHasNoBackups() {
|
||||||
|
ApplicationProperties.System system = mock(ApplicationProperties.System.class);
|
||||||
|
ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class);
|
||||||
|
|
||||||
|
when(applicationProperties.getSystem()).thenReturn(system);
|
||||||
|
when(system.getDatasource()).thenReturn(datasource);
|
||||||
|
when(datasource.isEnableCustomDatabase()).thenReturn(false);
|
||||||
|
|
||||||
assertFalse(databaseService.hasBackup());
|
assertFalse(databaseService.hasBackup());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user