T-7: M0.3a — Harden dev Mongo safety: env-safety throws on unsafe data-pipeline QA reads, scheduled jobs default off, read-only bypass tests
This commit is contained in:
parent
d9a9dd23dd
commit
7cf940afce
14 changed files with 407 additions and 26 deletions
|
|
@ -79,7 +79,7 @@
|
|||
},
|
||||
"workers": 5,
|
||||
"concurrency": 2,
|
||||
"enableScheduledJobs": true,
|
||||
"enableScheduledJobs": false,
|
||||
"defaultJobOptions": {
|
||||
"attempts": 3,
|
||||
"backoff": {
|
||||
|
|
|
|||
|
|
@ -11,12 +11,18 @@ let configInstance: ConfigManager<StockAppConfig> | null = null;
|
|||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export interface InitializeStockConfigOptions {
|
||||
mongoReadOnly?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the stock application configuration
|
||||
* @param serviceName - Optional service name to override port configuration
|
||||
* @param options - Optional initialization overrides (e.g. mongoReadOnly)
|
||||
*/
|
||||
export function initializeStockConfig(
|
||||
serviceName?: StockServiceName
|
||||
serviceName?: StockServiceName,
|
||||
options?: InitializeStockConfigOptions
|
||||
): StockAppConfig {
|
||||
try {
|
||||
if (!configInstance) {
|
||||
|
|
@ -25,7 +31,20 @@ export function initializeStockConfig(
|
|||
});
|
||||
}
|
||||
|
||||
const config = configInstance.initialize(stockAppSchema);
|
||||
const rawConfig = configInstance.initialize(stockAppSchema);
|
||||
const config: StockAppConfig =
|
||||
options?.mongoReadOnly === true
|
||||
? {
|
||||
...rawConfig,
|
||||
database: {
|
||||
...rawConfig.database,
|
||||
mongodb: {
|
||||
...rawConfig.database.mongodb,
|
||||
readOnly: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
: rawConfig;
|
||||
assertEnvironmentSafety(config, serviceName);
|
||||
|
||||
// If a service name is provided, override the service port
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { StockAppConfig } from './schemas';
|
||||
|
||||
export type StockServiceName = 'dataIngestion' | 'dataPipeline' | 'webApi' | 'orchestrator';
|
||||
|
|
@ -17,7 +16,8 @@ function isLoopbackHost(host?: string): boolean {
|
|||
if (!host) {
|
||||
return false;
|
||||
}
|
||||
return LOOPBACK_HOSTS.has(host.toLowerCase());
|
||||
const normalized = host.replace(/^\[|\]$/g, '').toLowerCase();
|
||||
return LOOPBACK_HOSTS.has(normalized);
|
||||
}
|
||||
|
||||
function parseMongoTarget(uri?: string): {
|
||||
|
|
@ -44,10 +44,9 @@ function parseMongoTarget(uri?: string): {
|
|||
}
|
||||
}
|
||||
|
||||
function isQaMongoTarget(uri: string | undefined, fallbackDb?: string): boolean {
|
||||
function isQaMongoTarget(uri: string | undefined): boolean {
|
||||
const target = parseMongoTarget(uri);
|
||||
const db = target.database || fallbackDb;
|
||||
return isLoopbackHost(target.host) && target.port === 27017 && db === 'stock';
|
||||
return isLoopbackHost(target.host) && target.port === 27017;
|
||||
}
|
||||
|
||||
function isQaPostgresTarget(pg: StockAppConfig['database']['postgres']): boolean {
|
||||
|
|
@ -63,7 +62,6 @@ export function assertEnvironmentSafety(
|
|||
serviceName?: StockServiceName,
|
||||
env: Record<string, string | undefined> = process.env
|
||||
): void {
|
||||
const logger = getLogger('stock-config-safety');
|
||||
const environment = config.environment;
|
||||
|
||||
if (environment === 'test') {
|
||||
|
|
@ -86,19 +84,21 @@ export function assertEnvironmentSafety(
|
|||
const mongo = config.database.mongodb;
|
||||
if (mongo.enabled) {
|
||||
if (isReadOnlyPipeline) {
|
||||
const readTarget = parseMongoTarget(mongo.readUri ?? mongo.uri);
|
||||
const effectiveUri = mongo.readUri ?? mongo.uri;
|
||||
const readTarget = parseMongoTarget(effectiveUri);
|
||||
if (
|
||||
isQaMongoTarget(mongo.readUri ?? mongo.uri, mongo.database) &&
|
||||
readTarget.username === 'trading_admin'
|
||||
isQaMongoTarget(effectiveUri) &&
|
||||
(mongo.readOnly !== true || 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.'
|
||||
throw new Error(
|
||||
'Safety guard: development data-pipeline cannot read QA MongoDB (localhost:27017) ' +
|
||||
'with admin credentials or without read-only mode enabled. ' +
|
||||
'Configure MONGODB_READ_URI with the read-only user (studio_stock_bot_read) and enable mongoReadOnly: true.'
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
!mongo.readOnly &&
|
||||
isQaMongoTarget(mongo.uri, mongo.database) &&
|
||||
isQaMongoTarget(mongo.uri) &&
|
||||
env.STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE !== 'true'
|
||||
) {
|
||||
throw new Error(
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ export {
|
|||
getProviderConfig,
|
||||
isFeatureEnabled,
|
||||
resetStockConfig,
|
||||
type InitializeStockConfigOptions,
|
||||
} from './config-instance';
|
||||
|
||||
// Export environment safety guard
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import { createRoutes } from './routes/create-routes';
|
|||
import { initializeAllHandlers } from './handlers';
|
||||
|
||||
// Initialize configuration with service-specific overrides
|
||||
const config = initializeStockConfig('dataPipeline');
|
||||
const config = initializeStockConfig('dataPipeline', { mongoReadOnly: true });
|
||||
const security = createHttpSecurity({ ...httpSecurityOptions(), environment: config.environment });
|
||||
|
||||
// Log the full configuration
|
||||
|
|
@ -29,7 +29,7 @@ const app = new ServiceApplication(
|
|||
serviceName: 'data-pipeline',
|
||||
hostname: security.hostname,
|
||||
enableHandlers: true,
|
||||
enableScheduledJobs: config.queue?.enableScheduledJobs ?? true,
|
||||
enableScheduledJobs: config.queue?.enableScheduledJobs ?? false,
|
||||
corsConfig: {
|
||||
origin: '*',
|
||||
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue