Skip to content

Commit

Permalink
#2270 moving db container to a separate compose file
Browse files Browse the repository at this point in the history
  • Loading branch information
DarioGii committed Jan 6, 2025
1 parent 3c6cf56 commit d3525eb
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 67 deletions.
16 changes: 8 additions & 8 deletions exampleYmlFiles/docker-compose-latest-fat-security.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
include:
- docker-compose-postgres.yml

services:
stirling-pdf:
container_name: Stirling-PDF-Security-Fat
Expand All @@ -14,9 +17,9 @@ services:
ports:
- 8080:8080
volumes:
- /stirling/latest/data:/usr/share/tessdata:rw
- /stirling/latest/config:/configs:rw
- /stirling/latest/logs:/logs:rw
- ./stirling/latest/data:/usr/share/tessdata:rw
- ./stirling/latest/config:/configs:rw
- ./stirling/latest/logs:/logs:rw
environment:
DOCKER_ENABLE_SECURITY: "true"
SECURITY_ENABLELOGIN: "false"
Expand All @@ -30,11 +33,8 @@ services:
SYSTEM_MAXFILESIZE: "100"
METRICS_ENABLED: "true"
SYSTEM_GOOGLEVISIBILITY: "true"
SYSTEM_DATASOURCE_TYPE: "postgresql"
SYSTEM_DATASOURCE_HOSTNAME: "db"
SYSTEM_DATASOURCE_PORT: "5432"
SYSTEM_DATASOURCE_NAME: "stirling_pdf"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "false"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "true"
SYSTEM_DATASOURCE_CUSTOMDATABASEURL: "jdbc:postgresql://db:5432/stirling_pdf"
SYSTEM_DATASOURCE_USERNAME: "admin"
SYSTEM_DATASOURCE_PASSWORD: "stirling"
restart: on-failure:5
7 changes: 2 additions & 5 deletions exampleYmlFiles/docker-compose-latest-security-with-sso.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ services:
SYSTEM_MAXFILESIZE: "100"
METRICS_ENABLED: "true"
SYSTEM_GOOGLEVISIBILITY: "true"
SYSTEM_DATASOURCE_TYPE: "postgresql"
SYSTEM_DATASOURCE_HOSTNAME: "db"
SYSTEM_DATASOURCE_PORT: "5432"
SYSTEM_DATASOURCE_NAME: "stirling_pdf"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "false"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "true"
SYSTEM_DATASOURCE_CUSTOMDATABASEURL: "jdbc:postgresql://db:5432/stirling_pdf"
SYSTEM_DATASOURCE_USERNAME: "admin"
SYSTEM_DATASOURCE_PASSWORD: "stirling"
restart: on-failure:5
24 changes: 5 additions & 19 deletions exampleYmlFiles/docker-compose-latest-security.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
stirling-pdf:
container_name: Stirling-PDF-Security
image: stirlingtools/stirling-pdf:test
image: stirlingtools/stirling-pdf:latest
deploy:
resources:
limits:
Expand All @@ -16,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
- ./stirling/latest/data:/usr/share/tessdata:rw
- ./stirling/latest/config:/configs:rw
- ./stirling/latest/logs:/logs:rw
environment:
DOCKER_ENABLE_SECURITY: "true"
SECURITY_ENABLELOGIN: "true"
Expand All @@ -32,22 +32,8 @@ services:
SYSTEM_MAXFILESIZE: "100"
METRICS_ENABLED: "true"
SYSTEM_GOOGLEVISIBILITY: "true"
SYSTEM_DATASOURCE_TYPE: "postgresql"
SYSTEM_DATASOURCE_HOSTNAME: "db"
SYSTEM_DATASOURCE_PORT: "5432"
SYSTEM_DATASOURCE_NAME: "stirling_pdf"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "true"
SYSTEM_DATASOURCE_CUSTOMDATABASEURL: "jdbc:postgresql://db:5432/stirling_pdf"
SYSTEM_DATASOURCE_USERNAME: "admin"
SYSTEM_DATASOURCE_PASSWORD: "stirling"
restart: on-failure:5

db:
image: 'postgres:17.2-alpine'
restart: on-failure:5
container_name: db
ports:
- "5432:5432"
environment:
POSTGRES_DB: "stirling_pdf"
POSTGRES_USER: "admin"
POSTGRES_PASSWORD: "stirling"
7 changes: 2 additions & 5 deletions exampleYmlFiles/docker-compose-latest-ultra-lite-security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ services:
SYSTEM_MAXFILESIZE: "100"
METRICS_ENABLED: "true"
SYSTEM_GOOGLEVISIBILITY: "true"
SYSTEM_DATASOURCE_TYPE: "postgresql"
SYSTEM_DATASOURCE_HOSTNAME: "db"
SYSTEM_DATASOURCE_PORT: "5432"
SYSTEM_DATASOURCE_NAME: "stirling_pdf"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "false"
SYSTEM_DATASOURCE_ENABLECUSTOMDATABASE: "true"
SYSTEM_DATASOURCE_CUSTOMDATABASEURL: "jdbc:postgresql://db:5432/stirling_pdf"
SYSTEM_DATASOURCE_USERNAME: "admin"
SYSTEM_DATASOURCE_PASSWORD: "stirling"
restart: on-failure:5
18 changes: 18 additions & 0 deletions exampleYmlFiles/docker-compose-postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
db:
image: 'postgres:17.2-alpine'
restart: on-failure:5
container_name: db
ports:
- "5432:5432"
environment:
POSTGRES_DB: "stirling_pdf"
POSTGRES_USER: "admin"
POSTGRES_PASSWORD: "stirling"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U admin stirling_pdf" ]
interval: 1s
timeout: 5s
retries: 10
volumes:
- ./stirling/latest/data:/pgdata
10 changes: 5 additions & 5 deletions src/main/java/stirling/software/SPDF/EE/LicenseKeyChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LicenseKeyChecker {

private final ApplicationProperties applicationProperties;

private boolean enterpriseEnbaledResult = false;
private boolean enterpriseEnabledResult = false;

@Autowired
public LicenseKeyChecker(
Expand All @@ -35,12 +35,12 @@ public void checkLicensePeriodically() {

private void checkLicense() {
if (!applicationProperties.getEnterpriseEdition().isEnabled()) {
enterpriseEnbaledResult = false;
enterpriseEnabledResult = false;
} else {
enterpriseEnbaledResult =
enterpriseEnabledResult =
licenseService.verifyLicense(
applicationProperties.getEnterpriseEdition().getKey());
if (enterpriseEnbaledResult) {
if (enterpriseEnabledResult) {
log.info("License key is valid.");
} else {
log.info("License key is invalid.");
Expand All @@ -55,6 +55,6 @@ public void updateLicenseKey(String newKey) throws IOException {
}

public boolean getEnterpriseEnabledResult() {
return enterpriseEnbaledResult;
return enterpriseEnabledResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -25,10 +26,12 @@ public class DatabaseConfig {
public static final String POSTGRES_DRIVER = "org.postgresql.Driver";

private final ApplicationProperties applicationProperties;
private final boolean runningEE;

@Autowired
public DatabaseConfig(ApplicationProperties applicationProperties) {
public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) {
this.applicationProperties = applicationProperties;
this.runningEE = runningEE;
}

/**
Expand All @@ -40,35 +43,49 @@ public DatabaseConfig(ApplicationProperties applicationProperties) {
* @throws UnsupportedProviderException if the type of database selected is not supported
*/
@Bean
@Qualifier("dataSource")
public DataSource dataSource() throws UnsupportedProviderException {
ApplicationProperties.System system = applicationProperties.getSystem();
ApplicationProperties.Datasource datasource = system.getDatasource();
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();

if (!datasource.isEnableCustomDatabase()) {
log.info("Using default H2 database");
if (!runningEE) {
return useDefaultDataSource(dataSourceBuilder);
}

dataSourceBuilder.driverClassName(DEFAULT_DRIVER);
dataSourceBuilder.url(DATASOURCE_DEFAULT_URL);
dataSourceBuilder.username(DEFAULT_USERNAME);
ApplicationProperties.System system = applicationProperties.getSystem();
ApplicationProperties.Datasource datasource = system.getDatasource();

return dataSourceBuilder.build();
if (!datasource.isEnableCustomDatabase()) {
return useDefaultDataSource(dataSourceBuilder);
}

log.info("Using custom database");
dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType()));
dataSourceBuilder.url(
getDataSourceUrl(
datasource.getType(),
datasource.getHostName(),
datasource.getPort(),
datasource.getName()));
log.info("Using custom database configuration");

if (!datasource.getCustomDatabaseUrl().isBlank()) {
dataSourceBuilder.url(datasource.getCustomDatabaseUrl());
} else {
dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType()));
dataSourceBuilder.url(
generateCustomDataSourceUrl(
datasource.getType(),
datasource.getHostName(),
datasource.getPort(),
datasource.getName()));
}
dataSourceBuilder.username(datasource.getUsername());
dataSourceBuilder.password(datasource.getPassword());

return dataSourceBuilder.build();
}

private DataSource useDefaultDataSource(DataSourceBuilder<?> dataSourceBuilder) {
log.info("Using default H2 database");

dataSourceBuilder.url(DATASOURCE_DEFAULT_URL);
dataSourceBuilder.username(DEFAULT_USERNAME);

return dataSourceBuilder.build();
}

/**
* Generate the URL the <code>DataSource</code> will use to connect to the database
*
Expand All @@ -78,7 +95,7 @@ public DataSource dataSource() throws UnsupportedProviderException {
* @param dataSourceName the name the database to connect to
* @return the <code>DataSource</code> URL
*/
private String getDataSourceUrl(
private String generateCustomDataSourceUrl(
String dataSourceType, String hostname, Integer port, String dataSourceName) {
return DATASOURCE_URL_TEMPLATE.formatted(dataSourceType, hostname, port, dataSourceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public static class System {
@Data
public static class Datasource {
private boolean enableCustomDatabase;
private String customDatabaseUrl;
private String type;
private String hostName;
private Integer port;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/settings.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ 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 the default 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
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package stirling.software.SPDF.config.security.database;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -22,9 +23,22 @@ class DatabaseConfigTest {
@Mock
private ApplicationProperties applicationProperties;

@InjectMocks
private DatabaseConfig databaseConfig;

@BeforeEach
void setUp() {
databaseConfig = new DatabaseConfig(applicationProperties, true);
}

@Test
void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException {
databaseConfig = new DatabaseConfig(applicationProperties, false);

var result = databaseConfig.dataSource();

assertInstanceOf(DataSource.class, result);
}

@Test
void testDefaultConfigurationForDataSource() throws UnsupportedProviderException {
var system = mock(ApplicationProperties.System.class);
Expand All @@ -39,6 +53,23 @@ void testDefaultConfigurationForDataSource() throws UnsupportedProviderException
assertInstanceOf(DataSource.class, result);
}

@Test
void testCustomUrlForDataSource() throws UnsupportedProviderException {
var system = mock(ApplicationProperties.System.class);
var datasource = mock(ApplicationProperties.Datasource.class);

when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(true);
when(datasource.getCustomDatabaseUrl()).thenReturn("jdbc:postgresql://mockUrl");
when(datasource.getUsername()).thenReturn("test");
when(datasource.getPassword()).thenReturn("pass");

var result = databaseConfig.dataSource();

assertInstanceOf(DataSource.class, result);
}

@Test
void testCustomConfigurationForDataSource() throws UnsupportedProviderException {
var system = mock(ApplicationProperties.System.class);
Expand All @@ -47,12 +78,13 @@ void testCustomConfigurationForDataSource() throws UnsupportedProviderException
when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(true);
when(datasource.getCustomDatabaseUrl()).thenReturn("");
when(datasource.getType()).thenReturn("postgresql");
when(datasource.getHostName()).thenReturn("localhost");
when(datasource.getPort()).thenReturn(5432);
when(datasource.getName()).thenReturn("postgres");
when(datasource.getUsername()).thenReturn("postgres");
when(datasource.getPassword()).thenReturn("postgres");
when(datasource.getHostName()).thenReturn("test");
when(datasource.getPort()).thenReturn(1234);
when(datasource.getName()).thenReturn("test_db");
when(datasource.getUsername()).thenReturn("test");
when(datasource.getPassword()).thenReturn("pass");

var result = databaseConfig.dataSource();

Expand All @@ -68,6 +100,7 @@ void exceptionThrownWhenDBTypeIsUnsupported(String datasourceType) {
when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(true);
when(datasource.getCustomDatabaseUrl()).thenReturn("");
when(datasource.getType()).thenReturn(datasourceType);

assertThrows(UnsupportedProviderException.class, () -> databaseConfig.dataSource());
Expand Down

0 comments on commit d3525eb

Please sign in to comment.