Skip to content

Commit

Permalink
#2270 adding postgres to Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
DarioGii committed Dec 12, 2024
1 parent 9dabcf5 commit 7c4bf87
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 114 deletions.
22 changes: 19 additions & 3 deletions exampleYmlFiles/docker-compose-latest-security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import stirling.software.SPDF.utils.FileInfo;

public interface DatabaseInterface {
void setAdminUser();

void exportDatabase() throws IOException;

List<FileInfo> getBackupList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
@Component
public class InitialSecuritySetup {

public static final String POSTGRES_ENV = "postgres";

@Autowired private UserService userService;

@Autowired private ApplicationProperties applicationProperties;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileInfo> getBackupList() {
List<FileInfo> backupFiles = new ArrayList<>();
Expand Down Expand Up @@ -91,7 +76,7 @@ public List<FileInfo> 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;
Expand Down Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions src/main/resources/setup_pg_admin_user.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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> 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);
Expand Down

This file was deleted.

0 comments on commit 7c4bf87

Please sign in to comment.