From 88e3102423d62636c6f15167f05365d9d5b0e7cb Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 28 Jul 2026 16:00:17 +0100 Subject: [PATCH] fix: add env var validation warnings and centralized config defaults Signed-off-by: --global --- backend/logs/combined.log | 28 +++++++++++++++++++++ backend/logs/error.log | 28 +++++++++++++++++++++ backend/src/config/redis.config.ts | 27 ++++++++++++-------- backend/src/utils/checkEnv.ts | 36 +++++++++++++++++++++++++-- backend/src/utils/redis.ts | 9 ++++--- backend/tests/checkEnv.test.ts | 40 +++++++++++++++++++++++++++++- 6 files changed, 151 insertions(+), 17 deletions(-) diff --git a/backend/logs/combined.log b/backend/logs/combined.log index 7d60ac16..d629b31b 100644 --- a/backend/logs/combined.log +++ b/backend/logs/combined.log @@ -10468,3 +10468,31 @@ {"level":"error","message":"Failed to emit OSCT webhook event","metadata":{"metadata":{"environment":"test","error":"queue down","eventType":"opensource.pr_submitted","service":"web3-student-lab-backend"}},"timestamp":"2026-06-27 09:13:45.247"} {"level":"error","message":"DB health check failed","metadata":{"metadata":{"environment":"test","error":"database unavailable","service":"web3-student-lab-backend"}},"timestamp":"2026-06-27 09:13:47.483"} {"level":"error","message":"Redis ping failed: Stream isn't writeable and enableOfflineQueue options is false","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend","stack":"Error: Stream isn't writeable and enableOfflineQueue options is false\n at EventEmitter.sendCommand (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/ioredis@5.11.0/node_modules/ioredis/built/Redis.js:376:32)\n at EventEmitter.ping (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/ioredis@5.11.0/node_modules/ioredis/built/utils/Commander.js:90:25)\n at RedisClient.ping (/workspaces/Web3-Student-Lab/backend/src/cache/RedisClient.ts:94:40)\n at RedisClient.connect (/workspaces/Web3-Student-Lab/backend/src/cache/RedisClient.ts:83:18)\n at Object. (/workspaces/Web3-Student-Lab/backend/tests/cache.test.ts:7:23)\n at Promise.finally.completed (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1561:28)\n at new Promise ()\n at callAsyncCircusFn (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1501:10)\n at _callCircusHook (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:980:40)\n at _runTestsForDescribeBlock (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:804:7)\n at _runTestsForDescribeBlock (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:861:11)\n at run (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:765:3)\n at runAndTransformResultsToJestFormat (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1993:21)\n at jestAdapter (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/runner.js:111:19)\n at runTestInternal (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-runner@30.4.2/node_modules/jest-runner/build/index.js:276:16)\n at runTest (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-runner@30.4.2/node_modules/jest-runner/build/index.js:344:7)"}},"timestamp":"2026-06-27 09:13:48.074"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.218"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.239"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.240"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.241"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.243"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.243"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.244"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.245"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.976"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.991"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.992"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.993"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.994"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.995"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.996"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.997"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.998"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.998"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.539"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.555"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.556"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.557"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.558"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.558"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.560"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.560"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.562"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.562"} diff --git a/backend/logs/error.log b/backend/logs/error.log index 651b140e..4cb44593 100644 --- a/backend/logs/error.log +++ b/backend/logs/error.log @@ -434,3 +434,31 @@ {"level":"error","message":"Failed to emit OSCT webhook event","metadata":{"metadata":{"environment":"test","error":"queue down","eventType":"opensource.pr_submitted","service":"web3-student-lab-backend"}},"timestamp":"2026-06-27 09:13:45.247"} {"level":"error","message":"DB health check failed","metadata":{"metadata":{"environment":"test","error":"database unavailable","service":"web3-student-lab-backend"}},"timestamp":"2026-06-27 09:13:47.482"} {"level":"error","message":"Redis ping failed: Stream isn't writeable and enableOfflineQueue options is false","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend","stack":"Error: Stream isn't writeable and enableOfflineQueue options is false\n at EventEmitter.sendCommand (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/ioredis@5.11.0/node_modules/ioredis/built/Redis.js:376:32)\n at EventEmitter.ping (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/ioredis@5.11.0/node_modules/ioredis/built/utils/Commander.js:90:25)\n at RedisClient.ping (/workspaces/Web3-Student-Lab/backend/src/cache/RedisClient.ts:94:40)\n at RedisClient.connect (/workspaces/Web3-Student-Lab/backend/src/cache/RedisClient.ts:83:18)\n at Object. (/workspaces/Web3-Student-Lab/backend/tests/cache.test.ts:7:23)\n at Promise.finally.completed (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1561:28)\n at new Promise ()\n at callAsyncCircusFn (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1501:10)\n at _callCircusHook (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:980:40)\n at _runTestsForDescribeBlock (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:804:7)\n at _runTestsForDescribeBlock (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:861:11)\n at run (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:765:3)\n at runAndTransformResultsToJestFormat (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/jestAdapterInit.js:1993:21)\n at jestAdapter (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-circus@30.4.2/node_modules/jest-circus/build/runner.js:111:19)\n at runTestInternal (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-runner@30.4.2/node_modules/jest-runner/build/index.js:276:16)\n at runTest (/workspaces/Web3-Student-Lab/backend/node_modules/.pnpm/jest-runner@30.4.2/node_modules/jest-runner/build/index.js:344:7)"}},"timestamp":"2026-06-27 09:13:48.074"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.218"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.239"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.240"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.241"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.242"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.243"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.244"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:49:20.245"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.976"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.991"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.992"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.993"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.994"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.995"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.996"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.996"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.998"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 15:51:15.998"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.539"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.555"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.556"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"short\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.556"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.558"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: JWT_SECRET\nDescription: Secret key for JWT token signing and verification\nCurrent value: \"your-secret-key-change-in-production\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.558"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.560"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nInvalid value for environment variable: DATABASE_URL\nDescription: PostgreSQL connection string for database connectivity\nCurrent value: \"invalid-url-format\"\nPlease check .env.example for the correct format.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.560"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.562"} +{"level":"error","message":"❌ Environment Configuration Error: Environment validation failed:\n\nMissing required environment variable: STELLAR_ISSUER_SECRET_KEY\nDescription: Stellar secret key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nMissing required environment variable: STELLAR_ISSUER_PUBLIC_KEY\nDescription: Stellar public key for certificate issuance (production only)\nPlease check your .env file and .env.example for guidance.\n\nPlease copy .env.example to .env and fill in the required values.","metadata":{"metadata":{"environment":"test","service":"web3-student-lab-backend"}},"timestamp":"2026-07-28 16:00:15.562"} diff --git a/backend/src/config/redis.config.ts b/backend/src/config/redis.config.ts index 6036d2c2..c9035780 100644 --- a/backend/src/config/redis.config.ts +++ b/backend/src/config/redis.config.ts @@ -1,3 +1,5 @@ +import logger from '../utils/logger.js'; + // Standalone Redis configuration export const redisConfig: any = process.env.REDIS_URL ? { @@ -9,17 +11,20 @@ export const redisConfig: any = process.env.REDIS_URL enableAutoPipelining: true, autoPipeliningIgnoredCommands: ['info', 'ping'], } - : { - host: process.env.REDIS_HOST || 'localhost', - port: parseInt(process.env.REDIS_PORT || '6379', 10), - password: process.env.REDIS_PASSWORD, - maxRetriesPerRequest: 3, - retryStrategy: (times: number) => Math.min(times * 50, 2000), - enableOfflineQueue: false, - enableReadyCheck: true, - enableAutoPipelining: true, - autoPipeliningIgnoredCommands: ['info', 'ping'], - }; + : (() => { + logger.warn('⚠️ REDIS_URL is not set, falling back to REDIS_HOST/REDIS_PORT (default localhost:6379).'); + return { + host: process.env.REDIS_HOST || 'localhost', + port: parseInt(process.env.REDIS_PORT || '6379', 10), + password: process.env.REDIS_PASSWORD, + maxRetriesPerRequest: 3, + retryStrategy: (times: number) => Math.min(times * 50, 2000), + enableOfflineQueue: false, + enableReadyCheck: true, + enableAutoPipelining: true, + autoPipeliningIgnoredCommands: ['info', 'ping'], + }; + })(); // Redis Cluster configuration for high availability export const redisClusterConfig = { diff --git a/backend/src/utils/checkEnv.ts b/backend/src/utils/checkEnv.ts index 08b776c0..bc32410c 100644 --- a/backend/src/utils/checkEnv.ts +++ b/backend/src/utils/checkEnv.ts @@ -90,7 +90,7 @@ const PRODUCTION_REQUIRED_VARS: EnvVarConfig[] = [ /** * Optional environment variables with defaults */ -const OPTIONAL_VARS: Record = { +const OPTIONAL_VARS: Record = { PORT: { defaultValue: '8080', description: 'Port number for the backend server', @@ -98,6 +98,7 @@ const OPTIONAL_VARS: Record { const memoryStore = new Map(); diff --git a/backend/tests/checkEnv.test.ts b/backend/tests/checkEnv.test.ts index 6db88f90..5b8eabe2 100644 --- a/backend/tests/checkEnv.test.ts +++ b/backend/tests/checkEnv.test.ts @@ -87,7 +87,9 @@ describe('Environment Variable Guard', () => { process.env.JWT_SECRET = 'this-is-a-very-long-secret-key-that-is-at-least-32-chars'; process.env.NODE_ENV = 'production'; process.env.REDIS_URL = 'redis://localhost:6379'; - // Missing production variables + // Clear production variables that may be set from .env.test + delete process.env.STELLAR_ISSUER_SECRET_KEY; + delete process.env.STELLAR_ISSUER_PUBLIC_KEY; expect(() => validateEnvironment()).toThrow(EnvironmentValidationError); expect(() => validateEnvironment()).toThrow( @@ -122,6 +124,42 @@ describe('Environment Variable Guard', () => { expect(process.env.STELLAR_NETWORK).toBe('testnet'); }); + it('should set default value for NODE_ENV when not set', () => { + process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/db'; + process.env.JWT_SECRET = 'this-is-a-very-long-secret-key-that-is-at-least-32-chars'; + process.env.REDIS_URL = 'redis://localhost:6379'; + // Explicitly unset NODE_ENV so it falls back to the default + delete process.env.NODE_ENV; + + validateEnvironment(); + + expect(process.env.NODE_ENV).toBe('development'); + }); + + it('should set default value for SOROBAN_RPC_URL when missing', () => { + process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/db'; + process.env.JWT_SECRET = 'this-is-a-very-long-secret-key-that-is-at-least-32-chars'; + process.env.NODE_ENV = 'development'; + process.env.REDIS_URL = 'redis://localhost:6379'; + delete process.env.SOROBAN_RPC_URL; + + validateEnvironment(); + + expect(process.env.SOROBAN_RPC_URL).toBe('https://soroban-testnet.stellar.org'); + }); + + it('should set default value for STELLAR_NETWORK when missing', () => { + process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/db'; + process.env.JWT_SECRET = 'this-is-a-very-long-secret-key-that-is-at-least-32-chars'; + process.env.NODE_ENV = 'development'; + process.env.REDIS_URL = 'redis://localhost:6379'; + delete process.env.STELLAR_NETWORK; + + validateEnvironment(); + + expect(process.env.STELLAR_NETWORK).toBe('testnet'); + }); + it('should not override existing optional variables', () => { process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/db'; process.env.JWT_SECRET = 'this-is-a-very-long-secret-key-that-is-at-least-32-chars';