Skip to content

Commit

Permalink
fix: escape special characters in mongodb password (#87)
Browse files Browse the repository at this point in the history
* fix: escape special characters in mongodb password

* chore: add dockerfile recommendations

* Update docker-compose.yaml

Co-authored-by: Hege Aalvik <[email protected]>

* chore: configure mongodb via spring data props

* chore: move java tool input to standard env

---------

Co-authored-by: Hege Aalvik <[email protected]>
  • Loading branch information
NilsOveTen and hegeaal authored Dec 6, 2024
1 parent a85bab2 commit 29d169d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM eclipse-temurin:21-jre-alpine

ARG USER=default
ENV HOME /home/$USER
ENV HOME=/home/$USER

ENV TZ=Europe/Oslo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
Expand All @@ -18,4 +18,4 @@ WORKDIR $HOME

COPY --chown=app:app /target/app.jar app.jar

CMD java -jar -XX:+UseZGC $JAVA_OPTS app.jar
CMD ["sh", "-c", "java -jar $JAVA_OPTS app.jar"]
2 changes: 2 additions & 0 deletions deploy/demo/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spec:
containers:
- name: service-catalog
env:
- name: JAVA_TOOL_OPTIONS
value: "-Xmx1g -XX:+UseZGC -XX:MaxRAMPercentage=80.0"
- name: MONGO_USERNAME
valueFrom:
secretKeyRef:
Expand Down
2 changes: 2 additions & 0 deletions deploy/prod/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spec:
containers:
- name: service-catalog
env:
- name: JAVA_TOOL_OPTIONS
value: "-Xmx1g -XX:+UseZGC -XX:MaxRAMPercentage=80.0"
- name: MONGO_USERNAME
valueFrom:
secretKeyRef:
Expand Down
2 changes: 2 additions & 0 deletions deploy/staging/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spec:
containers:
- name: service-catalog
env:
- name: JAVA_TOOL_OPTIONS
value: "-Xmx1g -XX:+UseZGC -XX:MaxRAMPercentage=80.0"
- name: MONGO_USERNAME
valueFrom:
secretKeyRef:
Expand Down
28 changes: 5 additions & 23 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
version: "3"
services:

app:
build: .
image: eu.gcr.io/digdir-fdk-infra/service-catalog
environment:
- MONGO_USERNAME=root
- MONGO_PASSWORD=admin
- MONGODB_HOST=mongodb:27017
- MONGODB_AUTH=admin
- MONGODB_REPLICASET=replicaset
- SSO_HOST=https://sso.staging.fellesdatakatalog.digdir.no
- SERVICE_CATALOG_URI=https://localhost:8080
ports:
- "9090:8080"
depends_on:
- mongodb

mongodb:
image: bitnami/mongodb:latest
image: mongo:latest
environment:
- MONGODB_ROOT_PASSWORD=admin
# Activate this if connecting from localhost
- MONGODB_ADVERTISED_HOSTNAME=localhost
- MONGODB_REPLICA_SET_MODE=primary
- MONGODB_REPLICA_SET_KEY=replicaset
- MONGO_INITDB_DATABASE=serviceCatalog
- MONGO_INITDB_ROOT_PASSWORD=adm?i=n
- MONGO_INITDB_ROOT_USERNAME=root
ports:
- "27017:27017"
- "27017:27017"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package no.digdir.servicecatalog.configuration

import com.mongodb.client.MongoClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.mongodb.core.MongoOperations
import org.springframework.data.mongodb.core.MongoTemplate


@Configuration
open class MongoConfig(
@Value("\${spring.data.mongodb.database}")
private val database: String
) {

@Bean
open fun mongoTemplate(mongoClient: MongoClient): MongoOperations {
return MongoTemplate(mongoClient, database)
}

}
25 changes: 23 additions & 2 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ management:
livenessState.enabled: true
readinessState.enabled: true
spring:
data.mongodb.uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGODB_HOST}/serviceCatalog?authSource=${MONGODB_AUTH}&replicaSet=${MONGODB_REPLICASET}
data.mongodb:
host: ${MONGODB_HOST}
username: ${MONGO_USERNAME}
password: ${MONGO_PASSWORD}
authentication-database: ${MONGODB_AUTH}
replica-set-name: ${MONGODB_REPLICASET}
port: 27017
database: serviceCatalog
auto-index-creation: true
security:
oauth2:
resourceserver:
Expand All @@ -24,6 +32,13 @@ application:
---
spring:
config.activate.on-profile: test
data:
mongodb:
host: localhost
username: testuser
password: testpassword
authentication-database: admin
replica-set-name: null
security:
oauth2:
resourceserver:
Expand All @@ -36,7 +51,13 @@ application:
---
spring:
config.activate.on-profile: dev
data.mongodb.uri: mongodb://root:admin@localhost:27017/serviceCatalog?authSource=admin&replicaSet=replicaset
data:
mongodb:
host: localhost
username: root
password: adm?i=n
authentication-database: admin
replica-set-name: null
security:
oauth2:
resourceserver:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class ApiTestContext {
internal class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) {
TestPropertyValues.of(
"spring.data.mongodb.uri=mongodb://$MONGO_USER:$MONGO_PASSWORD@localhost:${mongoContainer.getMappedPort(MONGO_PORT)}/$MONGO_DB_NAME?authSource=admin",
"spring.data.mongodb.port=${mongoContainer.getMappedPort(MONGO_PORT)}"
).applyTo(configurableApplicationContext.environment)
}
}
Expand Down

0 comments on commit 29d169d

Please sign in to comment.