cleanup
This commit is contained in:
parent
c353ddbe01
commit
ce9e2df67c
310 changed files with 2550 additions and 36861 deletions
|
|
@ -2,6 +2,7 @@ import * as path from 'path';
|
|||
import { fileURLToPath } from 'url';
|
||||
import { ConfigManager, createAppConfig } from '@stock-bot/config';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { assertEnvironmentSafety, type StockServiceName } from './env-safety';
|
||||
import { stockAppSchema, type StockAppConfig } from './schemas';
|
||||
|
||||
let configInstance: ConfigManager<StockAppConfig> | null = null;
|
||||
|
|
@ -15,7 +16,7 @@ const __dirname = path.dirname(__filename);
|
|||
* @param serviceName - Optional service name to override port configuration
|
||||
*/
|
||||
export function initializeStockConfig(
|
||||
serviceName?: 'dataIngestion' | 'dataPipeline' | 'webApi' | 'orchestrator'
|
||||
serviceName?: StockServiceName
|
||||
): StockAppConfig {
|
||||
try {
|
||||
if (!configInstance) {
|
||||
|
|
@ -25,6 +26,7 @@ export function initializeStockConfig(
|
|||
}
|
||||
|
||||
const config = configInstance.initialize(stockAppSchema);
|
||||
assertEnvironmentSafety(config, serviceName);
|
||||
|
||||
// If a service name is provided, override the service port
|
||||
if (serviceName && config.services?.[serviceName]) {
|
||||
|
|
|
|||
151
apps/stock/config/src/env-safety.ts
Normal file
151
apps/stock/config/src/env-safety.ts
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { StockAppConfig } from './schemas';
|
||||
|
||||
export type StockServiceName = 'dataIngestion' | 'dataPipeline' | 'webApi' | 'orchestrator';
|
||||
|
||||
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '0.0.0.0']);
|
||||
const FORBIDDEN_PROD_SECRETS = new Set([
|
||||
'trading_pass_dev',
|
||||
'trading_mongo_dev',
|
||||
'dev_pass',
|
||||
'dev_mongo_pass',
|
||||
'production_redis_password',
|
||||
'admin123',
|
||||
]);
|
||||
|
||||
function isLoopbackHost(host?: string): boolean {
|
||||
if (!host) return false;
|
||||
return LOOPBACK_HOSTS.has(host.toLowerCase());
|
||||
}
|
||||
|
||||
function parseMongoTarget(uri?: string): {
|
||||
host?: string;
|
||||
port?: number;
|
||||
database?: string;
|
||||
username?: string;
|
||||
} {
|
||||
if (!uri) return {};
|
||||
try {
|
||||
const parsed = new URL(uri);
|
||||
const database = parsed.pathname.replace(/^\/+/, '').split('/')[0] || undefined;
|
||||
const port = parsed.port ? Number(parsed.port) : parsed.protocol === 'mongodb:' ? 27017 : undefined;
|
||||
return {
|
||||
host: parsed.hostname || undefined,
|
||||
port: Number.isFinite(port) ? port : undefined,
|
||||
database,
|
||||
username: parsed.username ? decodeURIComponent(parsed.username) : undefined,
|
||||
};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function isQaMongoTarget(uri: string | undefined, fallbackDb?: string): boolean {
|
||||
const target = parseMongoTarget(uri);
|
||||
const db = target.database || fallbackDb;
|
||||
return isLoopbackHost(target.host) && target.port === 27017 && db === 'stock';
|
||||
}
|
||||
|
||||
function isQaPostgresTarget(pg: StockAppConfig['database']['postgres']): boolean {
|
||||
return isLoopbackHost(pg.host) && pg.port === 5432 && pg.database === 'trading_bot';
|
||||
}
|
||||
|
||||
/**
|
||||
* Enforce environment isolation between dev, qa, and prod so services cannot
|
||||
* accidentally mutate QA or production databases from the wrong environment.
|
||||
*/
|
||||
export function assertEnvironmentSafety(
|
||||
config: StockAppConfig,
|
||||
serviceName?: StockServiceName,
|
||||
env: Record<string, string | undefined> = process.env
|
||||
): void {
|
||||
const logger = getLogger('stock-config-safety');
|
||||
const environment = config.environment;
|
||||
|
||||
if (environment === 'test') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (environment === 'development') {
|
||||
if (
|
||||
config.database.postgres.enabled &&
|
||||
isQaPostgresTarget(config.database.postgres) &&
|
||||
env.STOCK_BOT_ALLOW_DEV_ON_QA_DB !== 'true'
|
||||
) {
|
||||
throw new Error(
|
||||
'Safety guard: development environment cannot target QA PostgreSQL (localhost:5432/trading_bot). ' +
|
||||
'Start dev infrastructure on port 5433 (trading_bot_dev) or run with NODE_ENV=qa.'
|
||||
);
|
||||
}
|
||||
|
||||
const isReadOnlyPipeline = serviceName === 'dataPipeline';
|
||||
const mongo = config.database.mongodb;
|
||||
if (mongo.enabled) {
|
||||
if (isReadOnlyPipeline) {
|
||||
const readTarget = parseMongoTarget(mongo.readUri ?? mongo.uri);
|
||||
if (
|
||||
isQaMongoTarget(mongo.readUri ?? mongo.uri, mongo.database) &&
|
||||
readTarget.username === 'trading_admin'
|
||||
) {
|
||||
logger.warn(
|
||||
'Development data-pipeline is reading QA MongoDB using admin credentials; ' +
|
||||
'configure MONGODB_READ_URI with the read-only user (studio_stock_bot_read) for defense in depth.'
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
!mongo.readOnly &&
|
||||
isQaMongoTarget(mongo.uri, mongo.database) &&
|
||||
env.STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE !== 'true'
|
||||
) {
|
||||
throw new Error(
|
||||
`Safety guard: development service "${serviceName ?? 'app'}" cannot open a writable MongoDB ` +
|
||||
'connection to QA MongoDB (localhost:27017/stock). ' +
|
||||
'Point MONGODB_URI to dev MongoDB (localhost:27018/stock_dev) for ingestion writes, ' +
|
||||
'or use data-pipeline with MONGODB_READ_URI for read-only QA reads.'
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (environment === 'qa') {
|
||||
if (config.database.postgres.enabled && config.database.postgres.database.endsWith('_dev')) {
|
||||
throw new Error(
|
||||
`Safety guard: qa environment cannot target dev PostgreSQL database "${config.database.postgres.database}".`
|
||||
);
|
||||
}
|
||||
if (config.database.mongodb.enabled && config.database.mongodb.database.endsWith('_dev')) {
|
||||
throw new Error(
|
||||
`Safety guard: qa environment cannot target dev MongoDB database "${config.database.mongodb.database}".`
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (environment === 'production') {
|
||||
if (env.STOCK_BOT_AUTH_DISABLED === 'true') {
|
||||
throw new Error('Safety guard: STOCK_BOT_AUTH_DISABLED=true is forbidden in production.');
|
||||
}
|
||||
if (FORBIDDEN_PROD_SECRETS.has(config.database.postgres.password)) {
|
||||
throw new Error('Safety guard: production PostgreSQL password cannot use a default dev/qa credential.');
|
||||
}
|
||||
const mongoTarget = parseMongoTarget(config.database.mongodb.uri);
|
||||
const mongoUriHasDevPass = Array.from(FORBIDDEN_PROD_SECRETS).some(secret =>
|
||||
config.database.mongodb.uri.includes(`:${secret}@`)
|
||||
);
|
||||
if (
|
||||
mongoUriHasDevPass ||
|
||||
(config.database.mongodb.password && FORBIDDEN_PROD_SECRETS.has(config.database.mongodb.password))
|
||||
) {
|
||||
throw new Error(
|
||||
`Safety guard: production MongoDB (${mongoTarget.host ?? 'configured URI'}) cannot use a default dev/qa credential.`
|
||||
);
|
||||
}
|
||||
if (
|
||||
config.database.dragonfly.password &&
|
||||
FORBIDDEN_PROD_SECRETS.has(config.database.dragonfly.password)
|
||||
) {
|
||||
throw new Error('Safety guard: production Dragonfly/Redis password cannot use a placeholder/dev credential.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,5 +11,8 @@ export {
|
|||
resetStockConfig,
|
||||
} from './config-instance';
|
||||
|
||||
// Export environment safety guard
|
||||
export { assertEnvironmentSafety, type StockServiceName } from './env-safety';
|
||||
|
||||
// Re-export type for convenience
|
||||
export type { StockAppConfig } from './schemas/stock-app.schema';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue