diff --git a/dht-node/package.json b/dht-node/package.json index 4c6868784..1869cc145 100644 --- a/dht-node/package.json +++ b/dht-node/package.json @@ -20,6 +20,7 @@ "cross-env": "^7.0.3", "dotenv": "10.0.0", "gradido-database": "file:../database", + "joi": "^17.13.3", "log4js": "^6.7.1", "nodemon": "^2.0.20", "tsconfig-paths": "^4.1.2", @@ -29,6 +30,7 @@ "@eslint-community/eslint-plugin-eslint-comments": "^3.2.1", "@types/dotenv": "^8.2.0", "@types/jest": "^27.0.2", + "@types/joi": "^17.2.3", "@types/node": "^18.11.18", "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^5.57.1", @@ -43,6 +45,7 @@ "eslint-plugin-prettier": "^4.2.1", "eslint-plugin-promise": "^6.1.1", "eslint-plugin-security": "^1.7.1", + "gradido-config": "../config", "jest": "^27.2.4", "prettier": "^2.8.7", "ts-jest": "^27.0.5", diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 70ec452bf..a711afbbf 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -1,18 +1,18 @@ /* eslint-disable n/no-process-env */ +import { latestDbVersion } from '@dbTools/config/detectLastDBVersion' import dotenv from 'dotenv' +import { validate } from 'gradido-config' + +import { schema } from './schema' + dotenv.config() const constants = { - DB_VERSION: '0087-add_index_on_user_roles', + DB_VERSION: latestDbVersion, LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL ?? 'info', - CONFIG_VERSION: { - DEFAULT: 'DEFAULT', - EXPECTED: 'v4.2024-01-17', - CURRENT: '', - }, } const server = { @@ -46,18 +46,6 @@ const federation = { FEDERATION_COMMUNITY_APIS: process.env.FEDERATION_COMMUNITY_APIS ?? '1_0', } -// Check config version -constants.CONFIG_VERSION.CURRENT = process.env.CONFIG_VERSION ?? constants.CONFIG_VERSION.DEFAULT -if ( - ![constants.CONFIG_VERSION.EXPECTED, constants.CONFIG_VERSION.DEFAULT].includes( - constants.CONFIG_VERSION.CURRENT, - ) -) { - throw new Error( - `Fatal: Config Version incorrect - expected "${constants.CONFIG_VERSION.EXPECTED}" or "${constants.CONFIG_VERSION.DEFAULT}", but found "${constants.CONFIG_VERSION.CURRENT}"`, - ) -} - export const CONFIG = { ...constants, ...server, @@ -65,3 +53,4 @@ export const CONFIG = { ...community, ...federation, } +validate(schema, CONFIG) diff --git a/dht-node/src/config/schema.ts b/dht-node/src/config/schema.ts new file mode 100644 index 000000000..28e09fca4 --- /dev/null +++ b/dht-node/src/config/schema.ts @@ -0,0 +1,64 @@ +import { + COMMUNITY_NAME, + COMMUNITY_DESCRIPTION, + DB_DATABASE, + DB_HOST, + DB_PASSWORD, + DB_PORT, + DB_USER, + DB_VERSION, + LOG4JS_CONFIG, + LOG_LEVEL, + NODE_ENV, + PRODUCTION, + TYPEORM_LOGGING_RELATIVE_PATH, +} from 'gradido-config' +import Joi from 'joi' + +export const schema = Joi.object({ + COMMUNITY_NAME, + COMMUNITY_DESCRIPTION, + DB_DATABASE, + DB_HOST, + DB_PASSWORD, + DB_PORT, + DB_USER, + DB_VERSION, + LOG4JS_CONFIG, + LOG_LEVEL, + NODE_ENV, + PRODUCTION, + TYPEORM_LOGGING_RELATIVE_PATH, + + FEDERATION_DHT_TOPIC: Joi.string() + .default('GRADIDO_HUB') + .description('The topic for the DHT (Distributed Hash Table), defaults to GRADIDO_HUB') + .required(), + + FEDERATION_DHT_SEED: Joi.string() + .when('NODE_ENV', { + is: 'development', + then: Joi.allow(null), // Allow empty for development + otherwise: Joi.string() + .hex() + .length(64) + .message('need to be valid hex with 64 character') + .required(), // Valid hex and 64 characters for other environments + }) + .description( + 'Seed for libsodium crypto_sign_seed_keypair. Valid hex with 64 characters in production', + ) + .default(''), // Default to empty string + + FEDERATION_COMMUNITY_URL: Joi.string() + .uri({ scheme: ['http', 'https'] }) + .default(Joi.ref('COMMUNITY_URL')) + .description('Community URL for federation, defaults to COMMUNITY_URL') + .required(), + + FEDERATION_COMMUNITY_APIS: Joi.string() + .valid('1_0', '2_0') + .default('1_0') + .description('Federation community API version, defaults to 1_0') + .required(), +}) diff --git a/dht-node/yarn.lock b/dht-node/yarn.lock index 4bf9fa916..b4292a644 100644 --- a/dht-node/yarn.lock +++ b/dht-node/yarn.lock @@ -345,6 +345,18 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.42.0.tgz#484a1d638de2911e6f5a30c12f49c7e4a3270fb6" integrity sha512-6SWlXpWU5AvId8Ac7zjzmIOqMOba/JWY8XZ4A7q7Gn1Vlfg/SFFIlrtHXt9nPn4op9ZPAkl91Jao+QQv3r/ukw== +"@hapi/hoek@^9.0.0", "@hapi/hoek@^9.3.0": + version "9.3.0" + resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.3.0.tgz#8368869dcb735be2e7f5cb7647de78e167a251fb" + integrity sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ== + +"@hapi/topo@^5.1.0": + version "5.1.0" + resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" + integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== + dependencies: + "@hapi/hoek" "^9.0.0" + "@humanwhocodes/config-array@^0.11.10": version "0.11.10" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.10.tgz#5a3ffe32cc9306365fb3fd572596cd602d5e12d2" @@ -665,6 +677,23 @@ picocolors "^1.0.0" tslib "^2.5.0" +"@sideway/address@^4.1.5": + version "4.1.5" + resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.5.tgz#4bc149a0076623ced99ca8208ba780d65a99b9d5" + integrity sha512-IqO/DUQHUkPeixNQ8n0JA6102hT9CmaljNTPmQ1u8MEhBo/R4Q8eKLN/vGZxuebwOroDB4cbpjheD4+/sKFK4Q== + dependencies: + "@hapi/hoek" "^9.0.0" + +"@sideway/formula@^3.0.1": + version "3.0.1" + resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.1.tgz#80fcbcbaf7ce031e0ef2dd29b1bfc7c3f583611f" + integrity sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg== + +"@sideway/pinpoint@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" + integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== + "@sinonjs/commons@^1.7.0": version "1.8.6" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.6.tgz#80c516a4dc264c2a69115e7578d62581ff455ed9" @@ -783,6 +812,13 @@ jest-matcher-utils "^27.0.0" pretty-format "^27.0.0" +"@types/joi@^17.2.3": + version "17.2.3" + resolved "https://registry.yarnpkg.com/@types/joi/-/joi-17.2.3.tgz#b7768ed9d84f1ebd393328b9f97c1cf3d2b94798" + integrity sha512-dGjs/lhrWOa+eO0HwgxCSnDm5eMGCsXuvLglMghJq32F6q5LyyNuXb41DHzrg501CKNOSSAHmfB7FDGeUnDmzw== + dependencies: + joi "*" + "@types/json-schema@^7.0.9": version "7.0.11" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3" @@ -2393,8 +2429,13 @@ graceful-fs@^4.2.4: resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3" integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ== +gradido-config@../config: + version "1.0.0" + dependencies: + joi "^17.13.3" + "gradido-database@file:../database": - version "2.2.1" + version "2.4.1" dependencies: "@types/uuid" "^8.3.4" cross-env "^7.0.3" @@ -3226,6 +3267,17 @@ jest@^27.2.4: import-local "^3.0.2" jest-cli "^27.5.1" +joi@*, joi@^17.13.3: + version "17.13.3" + resolved "https://registry.yarnpkg.com/joi/-/joi-17.13.3.tgz#0f5cc1169c999b30d344366d384b12d92558bcec" + integrity sha512-otDA4ldcIx+ZXsKHWmp0YizCweVRZG96J10b0FevjfuncLO1oX59THoAmHkNubYJ+9gWsYsp5k8v4ib6oDv1fA== + dependencies: + "@hapi/hoek" "^9.3.0" + "@hapi/topo" "^5.1.0" + "@sideway/address" "^4.1.5" + "@sideway/formula" "^3.0.1" + "@sideway/pinpoint" "^2.0.0" + js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"