Skip to content

Commit

Permalink
fix: deca authent database (#3831)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gaucher <[email protected]>
  • Loading branch information
Pomarom and Pomarom authored Sep 24, 2024
1 parent a930703 commit f14dc9b
Show file tree
Hide file tree
Showing 11 changed files with 952 additions and 926 deletions.
2 changes: 2 additions & 0 deletions .infra/.env_server
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ MNA_TDB_LOG_LEVEL=info
MNA_TDB_LOG_DESTINATIONS=stdout,mongodb,slack,json
MNA_TDB_PUBLIC_URL={{ vault[env_type].MNA_TDB_PUBLIC_URL }}
MNA_TDB_MONGODB_URI={{ vault[env_type].MNA_TDB_MONGODB_URI }}
MNA_TDB_MONGODB_DB_NAME={{ vault[env_type].MNA_TDB_MONGODB_DB_NAME }}
MNA_TDB_MONGODB_OPTIONS={{ vault[env_type].MNA_TDB_MONGODB_OPTIONS }}
MNA_TDB_SLACK_WEBHOOK_URL={{ vault[env_type].MNA_TDB_SLACK_WEBHOOK_URL }}
MNA_TDB_AUTH_PASSWORD_HASH_ROUNDS={{ vault[env_type].MNA_TDB_AUTH_PASSWORD_HASH_ROUNDS }}
MNA_TDB_AUTH_USER_JWT_SECRET={{ vault[env_type].MNA_TDB_AUTH_USER_JWT_SECRET }}
Expand Down
2 changes: 1 addition & 1 deletion .infra/ansible/tasks/preview_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@

- name: "[{{ pr_number }}] Remove database"
shell:
cmd: docker run --rm -i --network mna_network mongo:6.0.2-focal mongosh "{{ vault[env_type].MNA_TDB_MONGODB_URI }}" --eval "db.dropDatabase()"
cmd: docker run --rm -i --network mna_network mongo:6.0.2-focal mongosh "{{ vault[env_type].MNA_TDB_MONGODB_URI }}{{ vault[env_type].MNA_TDB_MONGODB_DB_NAME }}?tls=true" --eval "db.dropDatabase()"

- name: "[{{ pr_number }}] Removing preview directory"
shell:
Expand Down
1,843 changes: 929 additions & 914 deletions .infra/vault/vault.yml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion server/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## APPLICATION & ENV VARIABLES
MNA_TDB_ENV=test
MNA_TDB_PUBLIC_URL=test
MNA_TDB_MONGODB_URI=mongodb://127.0.0.1:27017/TDB-test-{{JEST_WORKER_ID}}?retryWrites=false
MNA_TDB_MONGODB_URI=mongodb://127.0.0.1:27017/
MNA_TDB_MONGODB_DB_NAME=TDB-test-{{JEST_WORKER_ID}}
MNA_TDB_MONGODB_OPTIONS="retryWrites=false"
MNA_TDB_BODY_PARSER_LIMIT=100kb

## LOG VARIABLES
Expand Down
4 changes: 3 additions & 1 deletion server/scripts/generate-import-effectif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IFormationCatalogue } from "shared/models/data/formationsCatalogue.mode
import { IOrganisme } from "shared/models/data/organismes.model";
import { write, utils } from "xlsx";

import { getMongodbUri } from "@/common/mongodb";

config({ path: ".env" });
config({ path: ".env.local", override: true });

Expand Down Expand Up @@ -128,7 +130,7 @@ program
})
.showSuggestionAfterError()
.hook("preAction", async () => {
client = new MongoClient(process.env.MNA_TDB_MONGODB_URI ?? "");
client = new MongoClient(getMongodbUri() ?? "");
await client.connect();
})
.hook("postAction", async () => {
Expand Down
3 changes: 3 additions & 0 deletions server/src/common/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const ensureInitialization = (client: MongoClient | null): MongoClient => {
return client;
};

export const getMongodbUri = (dbName = config.mongodb.dbName) => {
return `${config.mongodb.uri}${dbName}?${config.mongodb.options}`;
};
/**
* @param {string} uri
* @returns client
Expand Down
2 changes: 2 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const config = {
bodyParserLimit: env.get("MNA_TDB_BODY_PARSER_LIMIT").default("10mb").asString(),
mongodb: {
uri: env.get("MNA_TDB_MONGODB_URI").required().asString(),
dbName: env.get("MNA_TDB_MONGODB_DB_NAME").required().asString(),
options: env.get("MNA_TDB_MONGODB_OPTIONS").required().asString(),
},
auth: {
passwordHashRounds: 10000,
Expand Down
5 changes: 3 additions & 2 deletions server/src/jobs/hydrate/deca/hydrate-deca-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import { checkIfEffectifExists } from "@/common/actions/engine/engine.actions";
import { getOrganismeByUAIAndSIRET } from "@/common/actions/organismes/organismes.actions";
import parentLogger from "@/common/logger";
import { effectifsDECADb } from "@/common/model/collections";
import { getMongodbUri } from "@/common/mongodb";
import { __dirname } from "@/common/utils/esmUtils";

const logger = parentLogger.child({ module: "job:hydrate:contrats-deca-raw" });

const client = new MongoClient(process.env.MNA_TDB_MONGODB_URI ?? "");
const client = new MongoClient(getMongodbUri("airbyte") ?? "");

export async function hydrateDecaRaw() {
let count = 0;
Expand All @@ -36,7 +37,7 @@ export async function hydrateDecaRaw() {
"_airbyte_data.formation.date_fin_formation": { $exists: true },
};

const cursor = client.db("airbyte").collection<IAirbyteRawBalDeca>("airbyte_raw_bal_deca").find(query);
const cursor = client.db().collection<IAirbyteRawBalDeca>("airbyte_raw_bal_deca").find(query);

// await effectifsDECADb().drop();

Expand Down
5 changes: 2 additions & 3 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logger from "@/common/logger";
import { modelDescriptors } from "@/common/model/collections";
import config from "@/config";

import { startCLI } from "./commands";
import { connectToMongodb, configureDbSchemaValidation } from "./common/mongodb";
import { connectToMongodb, configureDbSchemaValidation, getMongodbUri } from "./common/mongodb";
import { setupJobProcessor } from "./jobs/jobs";
import createGlobalServices from "./services";

Expand All @@ -12,7 +11,7 @@ process.on("uncaughtException", (err) => logger.error(err, "uncaughtException"))

try {
logger.warn("starting application");
await connectToMongodb(config.mongodb.uri);
await connectToMongodb(getMongodbUri());
await configureDbSchemaValidation(modelDescriptors); // à supprimer d'ici et mettre dans une commande distincte

// We need to setup even for server to be able to call addJob
Expand Down
3 changes: 2 additions & 1 deletion server/tests/jest/globalTeardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { MongoClient } from "mongodb";
export default async () => {
dotenv.config({ path: "./server/.env.test" });

const mongoUri = `${process.env.MNA_TDB_MONGODB_URI}${process.env.MNA_TDB_MONGODB_DB_NAME}?${process.env.MNA_TDB_MONGODB_OPTIONS}`;
const workerId = `${process.env.JEST_WORKER_ID ?? ""}`;
const client = new MongoClient(process.env.MNA_TDB_MONGODB_URI?.replace("{{JEST_WORKER_ID}}", workerId) ?? "");
const client = new MongoClient(mongoUri.replace("{{JEST_WORKER_ID}}", workerId) ?? "");
try {
if (process.env.CI) {
return;
Expand Down
5 changes: 2 additions & 3 deletions server/tests/utils/mongoUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { connectToMongodb, closeMongodbConnection } from "@/common/mongodb";
import config from "@/config";
import { connectToMongodb, closeMongodbConnection, getMongodbUri } from "@/common/mongodb";

export const startAndConnectMongodb = async () => {
const workerId = `${process.env.JEST_WORKER_ID}`;
await connectToMongodb(config.mongodb.uri.replace("{{JEST_WORKER_ID}}", workerId));
await connectToMongodb(getMongodbUri().replace("{{JEST_WORKER_ID}}", workerId));
};

export const stopMongodb = async () => {
Expand Down

0 comments on commit f14dc9b

Please sign in to comment.