fix: resolve datetime variable scope error and duplicate alert notifications

- Fix datetime variable scope issue in openclaw_bot_routes.py by removing redundant imports
- Add notification_sent flag to prevent duplicate import failure alerts in scheduler.py
- Add database configuration to .env.example to fix missing POSTGRES_PASSWORD setup
- Create security fix guide for hardcoded database passwords in Kubernetes configs

Resolves:
- Presentation report export function abnormal (datetime NameError)
- Import failure alert messages being sent repeatedly
- Database connection password configuration issues
This commit is contained in:
ogt
2026-04-22 14:32:34 +08:00
parent a96306fba2
commit 87e40ebcf9
3 changed files with 135 additions and 7 deletions

View File

@@ -59,6 +59,22 @@ GITLAB_PROJECT_ID=1
# 如果部署在 HTTPS 環境,設為 true
USE_HTTPS=false
# ==========================================
# Database Settings
# ==========================================
# PostgreSQL Configuration (Production)
POSTGRES_HOST=momo-postgres
POSTGRES_PORT=5432
POSTGRES_USER=momo
POSTGRES_PASSWORD=your_secure_postgres_password_here
POSTGRES_DB=momo_analytics
# SQLite Configuration (Development/Backup)
SQLITE_PATH=data/momo_database.db
# Database Type Selection (postgresql or sqlite)
USE_POSTGRESQL=true
# ==========================================
# Google Drive 自動匯入設定
# ==========================================

View File

@@ -0,0 +1,109 @@
# Database Security Fix - Password Configuration
## Issue Summary
The system contains hardcoded database passwords in Kubernetes configuration files, which poses a security risk.
## Current Issues
1. **Hardcoded passwords**: `k8s/01-secrets.yaml` and `k8s/gcp/01-secrets.yaml` contain hardcoded password `"wooo_pg_2026"`
2. **Missing environment configuration**: `.env.example` was missing database password configuration (now fixed)
## Security Recommendations
### 1. Immediate Actions Required
#### For Kubernetes Deployment
Replace hardcoded secrets with environment variables or use Kubernetes secrets management:
```bash
# Create secrets from environment variables (recommended)
kubectl create secret generic momo-secrets \
--from-literal=POSTGRES_USER=momo \
--from-literal=POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
--from-literal=POSTGRES_DB=momo_analytics \
--namespace=momo
# Or use sealed-secrets for better security
```
#### For Docker/Local Development
Update your `.env` file with a strong password:
```bash
# Generate a strong password
openssl rand -base64 32
# Add to .env file
POSTGRES_PASSWORD=your_generated_strong_password_here
```
### 2. Configuration File Updates
#### Update Kubernetes Secrets Files
Replace hardcoded values in:
- `k8s/01-secrets.yaml`
- `k8s/gcp/01-secrets.yaml`
**Before (INSECURE):**
```yaml
stringData:
POSTGRES_PASSWORD: "wooo_pg_2026"
```
**After (SECURE):**
```yaml
stringData:
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
```
### 3. Best Practices
#### Password Requirements
- Minimum 16 characters
- Include uppercase, lowercase, numbers, and special characters
- Rotate passwords quarterly
- Use different passwords for different environments
#### Environment-Specific Passwords
- **Development**: Use simple passwords for local testing
- **Staging**: Use strong, unique passwords
- **Production**: Use the strongest passwords with regular rotation
#### Monitoring and Auditing
- Enable database connection logging
- Monitor failed login attempts
- Set up alerts for suspicious database activity
### 4. Implementation Steps
1. **Generate new strong passwords** for each environment
2. **Update all configuration files** to use environment variables
3. **Update deployment scripts** to inject secrets properly
4. **Test database connectivity** with new passwords
5. **Update documentation** with new security procedures
6. **Rotate existing passwords** in production
### 5. Files Requiring Updates
- [ ] `k8s/01-secrets.yaml`
- [ ] `k8s/gcp/01-secrets.yaml`
- [ ] `docker-compose.yml` (if using PostgreSQL)
- [ ] Any deployment scripts that reference database passwords
### 6. Verification
After implementing the fix, verify:
- [ ] Database connects successfully with new password
- [ ] No hardcoded passwords remain in configuration files
- [ ] Environment variables are properly loaded
- [ ] Application starts without authentication errors
## Additional Security Measures
1. **Enable SSL/TLS** for database connections
2. **Implement connection pooling** with proper authentication
3. **Use database-specific user accounts** instead of shared credentials
4. **Enable row-level security** for sensitive data
5. **Regular security audits** of database access patterns
## Contact
For questions about this security fix, contact your system administrator or security team.

View File

@@ -1566,13 +1566,16 @@ def run_auto_import_task():
logging.error(f"[Scheduler] [AutoImport] event_router 失敗: {_router_e}")
# LINE 通知保留(獨立通道,不經 event_router
try:
from services.notification_manager import NotificationManager
now_str = datetime.now(TAIPEI_TZ).strftime('%Y-%m-%d %H:%M')
message = f"🚨 當日業績自動匯入異常 ({now_str})\n系統錯誤:{str(e)[:200]}"
NotificationManager()._send_line_messages([message])
except Exception as notify_error:
logging.error(f"[Scheduler] [AutoImport] ❌ LINE 通知失敗 | Error: {notify_error}")
# Only send if notification hasn't been sent already
if not notification_sent:
try:
from services.notification_manager import NotificationManager
now_str = datetime.now(TAIPEI_TZ).strftime('%Y-%m-%d %H:%M')
message = f"Daily Sales Auto Import Exception ({now_str})\nSystem error: {str(e)[:200]}"
NotificationManager()._send_line_messages([message])
notification_sent = True # Mark as sent to prevent duplicate
except Exception as notify_error:
logging.error(f"[Scheduler] [AutoImport] LINE notification failed | Error: {notify_error}")
# ADR-013: AIOps 自動修復 — PlayBook 匹配 + KM 沉澱
try: