Skip to content

Commit 8ee05c4

Browse files
committed
fix: resolve SonarQube security and code quality issues
- Replace hardcoded PostgreSQL passwords in docker-compose files with environment variables - Add random_state parameter to SVR model for reproducibility - Initialize seeded numpy random number generator (default_rng) for deterministic behavior - Add missing os import in generate_all_sku_forecasts.py Fixes: - Hardcoded database password in docker-compose.ci.yml and docker-compose.versioned.yaml - Missing random_state in SVR model and unseeded numpy random number generators
1 parent 5e5c5f2 commit 8ee05c4

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

deploy/compose/docker-compose.ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- GIT_SHA=${GIT_SHA:-unknown}
1919
- BUILD_TIME=${BUILD_TIME:-unknown}
2020
- ENVIRONMENT=ci
21-
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/warehouse_ops
21+
- DATABASE_URL=postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-warehouse_ops}
2222
- REDIS_HOST=redis
2323
- REDIS_PORT=6379
2424
- MILVUS_HOST=milvus

deploy/compose/docker-compose.versioned.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
- GIT_SHA=${GIT_SHA:-unknown}
1919
- BUILD_TIME=${BUILD_TIME:-unknown}
2020
- ENVIRONMENT=development
21-
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/warehouse_ops
21+
- DATABASE_URL=postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-warehouse_ops}
2222
- REDIS_HOST=redis
2323
- REDIS_PORT=6379
2424
- MILVUS_HOST=milvus

scripts/data/generate_all_sku_forecasts.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import asyncio
2323
import asyncpg
2424
import json
25+
import os
2526
import numpy as np
2627
import pandas as pd
2728
from datetime import datetime, timedelta
@@ -34,7 +35,12 @@
3435
warnings.filterwarnings('ignore')
3536

3637
class AllSKUForecastingEngine:
37-
def __init__(self):
38+
def __init__(self, random_seed=42):
39+
"""Initialize forecasting engine with a random seed for reproducibility."""
40+
self.random_seed = random_seed
41+
# Initialize numpy random number generator with seed
42+
self.rng = np.random.default_rng(random_seed)
43+
3844
self.db_config = {
3945
'host': 'localhost',
4046
'port': 5435,
@@ -49,7 +55,7 @@ def __init__(self):
4955
'Gradient Boosting': GradientBoostingRegressor(n_estimators=100, random_state=42),
5056
'Linear Regression': LinearRegression(),
5157
'Ridge Regression': Ridge(alpha=1.0),
52-
'Support Vector Regression': SVR(kernel='rbf', C=1.0, gamma='scale')
58+
'Support Vector Regression': SVR(kernel='rbf', C=1.0, gamma='scale', random_state=42)
5359
}
5460

5561
# Try to import XGBoost
@@ -134,9 +140,9 @@ async def generate_historical_data(self, sku, days=365):
134140
holiday_factor = 1.2
135141

136142
# Random noise
137-
# Security: Using np.random is appropriate here - generating forecast noise only
143+
# Security: Using seeded random number generator for forecast noise only
138144
# For security-sensitive values (tokens, keys, passwords), use secrets module instead
139-
noise = np.random.normal(0, 0.1)
145+
noise = self.rng.normal(0, 0.1)
140146

141147
# Calculate final demand
142148
final_demand = base_demand * seasonal_factor * monthly_factor * weekend_factor * holiday_factor
@@ -187,9 +193,9 @@ def create_features(self, df):
187193
df['demand_monthly_seasonal'] = df.groupby('month')['demand'].transform('mean') - df['demand'].mean()
188194

189195
# Promotional features
190-
# Security: Using np.random is appropriate here - generating forecast variations only
196+
# Security: Using seeded random number generator for forecast variations only
191197
# For security-sensitive values (tokens, keys, passwords), use secrets module instead
192-
df['promotional_boost'] = np.random.uniform(0.8, 1.2, len(df))
198+
df['promotional_boost'] = self.rng.uniform(0.8, 1.2, len(df))
193199

194200
# Interaction features
195201
df['weekend_summer'] = df['is_weekend'] * df['is_summer']

0 commit comments

Comments
 (0)