From 743450630e721a5e3ebacd4f10d7c787e4e2af98 Mon Sep 17 00:00:00 2001 From: Dario Ghunney Ware Date: Tue, 10 Dec 2024 15:07:51 +0000 Subject: [PATCH] #2270 adding postgres to Docker --- .../docker-compose-latest-security.yml | 22 +++++++++-- .../config/interfaces/DatabaseInterface.java | 2 - .../config/security/InitialSecuritySetup.java | 7 ---- .../security/database/DatabaseService.java | 27 +------------ src/main/resources/setup_pg_admin_user.sql | 26 ------------- .../security/InitialSecuritySetupTest.java | 18 +++------ .../database/DatabaseServiceTest.java | 38 ------------------- 7 files changed, 26 insertions(+), 114 deletions(-) delete mode 100644 src/main/resources/setup_pg_admin_user.sql delete mode 100644 src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java diff --git a/exampleYmlFiles/docker-compose-latest-security.yml b/exampleYmlFiles/docker-compose-latest-security.yml index d29c185d99c..b4d6fd0d22a 100644 --- a/exampleYmlFiles/docker-compose-latest-security.yml +++ b/exampleYmlFiles/docker-compose-latest-security.yml @@ -6,6 +6,8 @@ services: resources: limits: memory: 4G + depends_on: + - db healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:8080/api/v1/info/status | grep -q 'UP' && curl -fL http://localhost:8080/ | grep -q 'Please sign in'"] interval: 5s @@ -14,9 +16,9 @@ services: ports: - "8080:8080" volumes: - - /stirling/latest/data:/usr/share/tessdata:rw - - /stirling/latest/config:/configs:rw - - /stirling/latest/logs:/logs:rw + - /Users/darioghunneyware/stirling/latest/data:/usr/share/tessdata:rw + - /Users/darioghunneyware/stirling/latest/config:/configs:rw + - /Users/darioghunneyware/stirling/latest/logs:/logs:rw environment: DOCKER_ENABLE_SECURITY: "true" SECURITY_ENABLELOGIN: "true" @@ -30,4 +32,18 @@ services: SYSTEM_MAXFILESIZE: "100" METRICS_ENABLED: "true" SYSTEM_GOOGLEVISIBILITY: "true" + SPRING_DATASOURCE_URL: "jdbc:postgresql://db:5432/stirling_pdf" + SPRING_DATASOURCE_USERNAME: "admin" + SPRING_DATASOURCE_PASSWORD: "stirling" + SPRING_JPA_HIBERNATE_DDL_AUTO: "update" restart: on-failure:5 + + db: + image: 'postgres:17.2-alpine' + container_name: db + ports: + - "5432:5432" + environment: + POSTGRES_DB: "stirling_pdf" + POSTGRES_USER: "admin" + POSTGRES_PASSWORD: "stirling" \ No newline at end of file diff --git a/src/main/java/stirling/software/SPDF/config/interfaces/DatabaseInterface.java b/src/main/java/stirling/software/SPDF/config/interfaces/DatabaseInterface.java index 81cb0f41c51..688ce3dcfcd 100644 --- a/src/main/java/stirling/software/SPDF/config/interfaces/DatabaseInterface.java +++ b/src/main/java/stirling/software/SPDF/config/interfaces/DatabaseInterface.java @@ -6,8 +6,6 @@ import stirling.software.SPDF.utils.FileInfo; public interface DatabaseInterface { - void setAdminUser(); - void exportDatabase() throws IOException; List getBackupList(); diff --git a/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java b/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java index 9116b5818bf..3fd2143e8f8 100644 --- a/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java +++ b/src/main/java/stirling/software/SPDF/config/security/InitialSecuritySetup.java @@ -16,8 +16,6 @@ @Component public class InitialSecuritySetup { - public static final String POSTGRES_ENV = "postgres"; - @Autowired private UserService userService; @Autowired private ApplicationProperties applicationProperties; @@ -26,11 +24,6 @@ public class InitialSecuritySetup { @PostConstruct public void init() { - if (applicationProperties.getSystem().getSpringProfilesActive().equals(POSTGRES_ENV)) { - log.debug("Postgres configuration settings detected. Creating admin user"); - databaseService.setAdminUser(); - } - try { if (!userService.hasUsers()) { initializeAdminUser(); diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java index 1cfbaa3f083..20f7400879d 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java @@ -36,24 +36,9 @@ public class DatabaseService implements DatabaseInterface { public static final String BACKUP_PREFIX = "backup_"; public static final String SQL_SUFFIX = ".sql"; private static final Path BACKUP_PATH = Paths.get("configs/db/backup/"); - private static final Path PG_ADMIN_SCRIPT_PATH = - Paths.get("src/main/resources/setup_pg_admin_user.sql"); @Autowired private DatabaseConfig databaseConfig; - @Override - public void setAdminUser() { - try (Connection connection = databaseConfig.connection(); - Statement statement = connection.createStatement()) { - String script = Files.readString(PG_ADMIN_SCRIPT_PATH); - statement.execute(script); - } catch (SQLException | IOException e) { - log.error("Error: Failed to create admin user for database", e); - } - - log.info("Created admin user for database"); - } - @Override public List getBackupList() { List backupFiles = new ArrayList<>(); @@ -91,7 +76,7 @@ public List getBackupList() { } // Imports a database backup from the specified file. - public boolean importDatabaseFromUI(String fileName) throws IOException { + public boolean importDatabaseFromUI(String fileName) { try { importDatabaseFromUI(getBackupFilePath(fileName)); return true; @@ -214,16 +199,6 @@ private void executeDatabaseScript(Path scriptPath) { } } - private void ensureBackupDirectoryExists() { - if (Files.notExists(BACKUP_PATH)) { - try { - Files.createDirectories(BACKUP_PATH); - } catch (IOException e) { - log.error("Error creating directories: {}", e.getMessage()); - } - } - } - private boolean isValidFileName(String fileName) { // Check for invalid characters or sequences return fileName != null diff --git a/src/main/resources/setup_pg_admin_user.sql b/src/main/resources/setup_pg_admin_user.sql deleted file mode 100644 index 5ab387b398e..00000000000 --- a/src/main/resources/setup_pg_admin_user.sql +++ /dev/null @@ -1,26 +0,0 @@ -DO -$do$ -BEGIN - IF EXISTS ( - SELECT FROM pg_catalog.pg_roles - WHERE rolname = 'admin') THEN - - RAISE NOTICE 'Role "admin" already exists. Skipping.'; - ELSE - CREATE USER admin WITH ENCRYPTED PASSWORD 'stirling'; - END IF; -END -$do$; - -CREATE SCHEMA IF NOT EXISTS stirling_pdf AUTHORIZATION admin; -GRANT ALL PRIVILEGES ON DATABASE postgres TO admin; -GRANT ALL PRIVILEGES ON SCHEMA stirling_pdf TO admin; -ALTER DATABASE postgres SET search_path TO stirling_pdf; -ALTER DATABASE postgres OWNER TO admin; -ALTER TABLE authorities OWNER TO admin; -ALTER TABLE persistent_logins OWNER TO admin; -ALTER TABLE sessions OWNER TO admin; -ALTER TABLE user_settings OWNER TO admin; -ALTER TABLE users OWNER TO admin; -ALTER USER admin SET search_path TO stirling_pdf; -SET ROLE admin; \ No newline at end of file diff --git a/src/test/java/stirling/software/SPDF/config/security/InitialSecuritySetupTest.java b/src/test/java/stirling/software/SPDF/config/security/InitialSecuritySetupTest.java index f38ef7e3923..e9c5594ef8e 100644 --- a/src/test/java/stirling/software/SPDF/config/security/InitialSecuritySetupTest.java +++ b/src/test/java/stirling/software/SPDF/config/security/InitialSecuritySetupTest.java @@ -1,7 +1,7 @@ package stirling.software.SPDF.config.security; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; +import java.io.IOException; +import java.util.Optional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -10,12 +10,10 @@ import stirling.software.SPDF.config.security.database.DatabaseService; import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.User; - -import java.io.IOException; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class InitialSecuritySetupTest { @@ -36,14 +34,10 @@ class InitialSecuritySetupTest { void testInit() throws IOException { String username = "admin"; String password = "stirling"; - ApplicationProperties.System system = mock(ApplicationProperties.System.class); ApplicationProperties.Security security = mock(ApplicationProperties.Security.class); ApplicationProperties.Security.InitialLogin initialLogin = mock(ApplicationProperties.Security.InitialLogin.class); Optional user = Optional.of(mock(User.class)); - when(applicationProperties.getSystem()).thenReturn(system); - when(system.getSpringProfilesActive()).thenReturn("postgres"); - doNothing().when(databaseService).setAdminUser(); when(userService.hasUsers()).thenReturn(false); when(applicationProperties.getSecurity()).thenReturn(security); when(security.getInitialLogin()).thenReturn(initialLogin); diff --git a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java b/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java deleted file mode 100644 index c153937bb63..00000000000 --- a/src/test/java/stirling/software/SPDF/config/security/database/DatabaseServiceTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package stirling.software.SPDF.config.security.database; - -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 java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -@ExtendWith(MockitoExtension.class) -class DatabaseServiceTest { - - @Mock - private DatabaseConfig databaseConfig; - - @InjectMocks - private DatabaseService databaseService; - - @Test - void setAdminUser() throws SQLException { - Connection connection = mock(Connection.class); - Statement statement = mock(Statement.class); - - when(databaseConfig.connection()).thenReturn(connection); - when(connection.createStatement()).thenReturn(statement); - - databaseService.setAdminUser(); - - verify(statement).execute(anyString()); - } - -} \ No newline at end of file