Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : switch from sql to mongodb for event publication storage #389

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ services:
- folioman-network
environment:
- OTEL_METRIC_EXPORT_INTERVAL=500
- ENABLE_LOGS_ALL=true
- ENABLE_LOGS_ALL=false
ports:
- "3000:3000"
- "4317:4317"
Expand All @@ -74,22 +74,31 @@ services:
hostname: mongodb
container_name: mongodb
image: mongo:8.0.3
restart: unless-stopped
volumes:
- mongodb:/data/db
ports:
- "27017:27017"
networks:
- folioman-network
healthcheck:
test: |
mongosh --eval "try { rs.status().ok } catch (e) { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] }).ok }"
interval: 10s
start_period: 30s
timeout: 5s
retries: 3
command: [ "/usr/bin/mongod", "--replSet", "rs0", "--bind_ip_all" ]

mongo-express:
image: mongo-express
container_name: mongo-express
ports:
- "8081:8081"
environment:
# - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
# - ME_CONFIG_MONGODB_ADMINPASSWORD=passcode
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=pass
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
restart: unless-stopped
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix security vulnerabilities in mongo-express configuration

Several security and configuration issues need attention:

  1. MongoDB connection lacks authentication
  2. Basic auth credentials are in plaintext
  3. Missing dependency on MongoDB service
  4. Network isolation not configured
  5. Commented configuration suggests incomplete setup

Apply these security fixes:

   mongo-express:
     image: mongo-express
     container_name: mongo-express
     ports:
       - "8081:8081"
     environment:
-      #      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
-      #      - ME_CONFIG_MONGODB_ADMINPASSWORD=passcode
+      - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ADMIN_USERNAME:-admin}
+      - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ADMIN_PASSWORD:-secret}
       - ME_CONFIG_MONGODB_SERVER=mongodb
-      - ME_CONFIG_BASICAUTH_USERNAME=admin
-      - ME_CONFIG_BASICAUTH_PASSWORD=pass
+      - ME_CONFIG_BASICAUTH_USERNAME=${ME_BASIC_AUTH_USERNAME}
+      - ME_CONFIG_BASICAUTH_PASSWORD=${ME_BASIC_AUTH_PASSWORD}
       - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
     restart: unless-stopped
+    networks:
+      - folioman-network
+    depends_on:
+      mongodb:
+        condition: service_healthy

Create a .env file for sensitive credentials:

MONGO_ADMIN_USERNAME=admin
MONGO_ADMIN_PASSWORD=secret
ME_BASIC_AUTH_USERNAME=admin
ME_BASIC_AUTH_PASSWORD=strong-password


networks:
folioman-network:
driver: bridge
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jpa</artifactId>
<artifactId>spring-modulith-starter-mongodb</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Migration to MongoDB is incomplete - JPA dependencies and entities still present

The codebase still heavily relies on JPA/Hibernate with multiple entity classes using Jakarta Persistence annotations:

  • 12 JPA entity classes found with @Entity annotations
  • Extensive usage of JPA annotations like @Table, @Column, @Id, etc.
  • No MongoDB annotations (@Document) or repositories found

The change of dependency to spring-modulith-starter-mongodb appears premature as the codebase is still using JPA/Hibernate for persistence.

🔗 Analysis chain

Verify complete migration from JPA to MongoDB

While the Spring Modulith starter has been changed to MongoDB, there might be remaining JPA dependencies or code that needs to be updated.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any remaining JPA annotations or repositories
ast-grep --pattern 'import javax.persistence.$_'
ast-grep --pattern 'import jakarta.persistence.$_'
ast-grep --pattern '@Entity'
ast-grep --pattern 'extends JpaRepository'

# Check for any MongoDB related code to ensure proper migration
ast-grep --pattern 'import org.springframework.data.mongodb.$_'
ast-grep --pattern '@Document'
ast-grep --pattern 'extends MongoRepository'

Length of output: 16289

</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
Expand Down Expand Up @@ -206,6 +206,11 @@
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>grafana</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.redis.testcontainers</groupId>
<artifactId>testcontainers-redis</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ spring:
url: jdbc:postgresql://localhost:5432/appdb
username: appuser
password: secret
hikari:
maximum-pool-size: 100
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
spring.application.name=hilla-folioman
server.port=${PORT:8080}
logging.level.org.atmosphere = warn
spring.main.allow-bean-definition-overriding=true

spring.mustache.check-template-location=false
spring.mvc.problemdetails.enabled=true
Expand All @@ -16,6 +17,7 @@ spring.devtools.restart.exclude=META-INF/maven/**,META-INF/resources/**,resource
spring.devtools.add-properties=false

spring.modulith.events.republish-outstanding-events-on-restart=true
spring.modulith.events.completion-mode=archive
spring.testcontainers.beans.startup=parallel

spring.data.redis.repositories.enabled=false
Expand Down
12 changes: 0 additions & 12 deletions src/main/resources/db/changelog/migration/11-event-publication.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.app.folioman.common;

import com.redis.testcontainers.RedisStackContainer;
import java.time.Duration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.grafana.LgtmStackContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
Expand All @@ -22,4 +24,11 @@ RedisStackContainer redisStackContainer() {
MongoDBContainer mongoDBContainer() {
return new MongoDBContainer(DockerImageName.parse("mongo").withTag("8.0.3"));
}

@Bean
@ServiceConnection
LgtmStackContainer lgtmContainer() {
return new LgtmStackContainer(DockerImageName.parse("grafana/otel-lgtm:0.7.8"))
.withStartupTimeout(Duration.ofMinutes(2));
}
}
Loading