Skip to content

Claude/convert to go worker 011 c ut ae x wm x5hpiu u qwy aqo#44

Merged
CodeMonkeyCybersecurity merged 3 commits intomainfrom
claude/convert-to-go-worker-011CUtAeXWmX5hpiuUQwyAqo
Nov 8, 2025
Merged

Claude/convert to go worker 011 c ut ae x wm x5hpiu u qwy aqo#44
CodeMonkeyCybersecurity merged 3 commits intomainfrom
claude/convert-to-go-worker-011CUtAeXWmX5hpiuUQwyAqo

Conversation

@CodeMonkeyCybersecurity
Copy link
Owner

No description provided.

Converted moni_setup.py (4.0) to Go-based worker following Eos patterns.
Implements full SSL, database configuration, and security hardening.

NEW FILES:
- pkg/moni/constants.go - Configuration constants (paths, UIDs, timeouts)
- pkg/moni/types.go - Type definitions (WorkerConfig, SetupResult, etc.)
- pkg/moni/ssl.go - SSL certificate generation and permission management
- pkg/moni/database.go - Database config (llama3→Moni rename) + security
- pkg/moni/verification.go - Health checks, RLS/CSP verification
- pkg/moni/worker.go - Main orchestration (9-phase setup)

UPDATED:
- cmd/update/moni.go - Added --init and worker flags

FEATURES:
- 9-phase setup: SSL → Permissions → Env → Restart → DB → API Keys → Security → Verification → Health
- SHIFT-LEFT v2 certificate testing (validates before deployment)
- Multi-strategy cert management (Alpine UID 70 vs Standard UID 999)
- Row Level Security (RLS) for 15 tables (CVSS 9.1 mitigation)
- Database security hardening (least privilege, readonly user)
- Security verification (RLS + CSP checks)

USAGE:
  sudo eos update moni --init              # Full initialization
  sudo eos update moni --validate-certs    # Certificate validation
  sudo eos update moni --verify-security   # Security verification
  sudo eos update moni --fix-certs         # Fix permissions
  sudo eos update moni --verify-rls        # RLS verification
  sudo eos update moni --verify-csp        # CSP verification

PATTERN COMPLIANCE:
✅ Business logic in pkg/moni/, orchestration in cmd/
✅ Assess→Intervene→Evaluate pattern
✅ RuntimeContext passed everywhere
✅ Structured logging (otelzap.Ctx)
✅ No hardcoded values (all in constants.go)
✅ Error context with remediation steps

SECURITY (P0):
- Row Level Security: 15 tables with tenant isolation policies
- Database hardening: NOSUPERUSER, DML-only, readonly user
- Certificate permissions: Tested with SHIFT-LEFT v2
- CSP verification: Scores 0-100, detects weak policies

Replaces Python script workflow with native Go implementation.
Deployable via: sudo eos update moni --init
…rial analysis

Implemented critical fixes identified in comprehensive security review:

P0 FIXES (BREAKING - Security Critical):

1. RLS Policy Silent Failure Fixed (database.go:178-280)
   - REMOVED 'true' parameter from current_setting() in all RLS policies
   - WHY: With 'true', missing app.current_team_id returned NULL (silent failure)
   - NOW: Missing app.current_team_id causes ERROR (fail loudly, not silently)
   - IMPACT: Prevents operational nightmare where users see no data
   - EVIDENCE: AWS Prescriptive Guidance, Permit.io RLS best practices

2. Superuser Bypass Verification Added (database.go:312-326)
   - Verifies bionic_application is NOT a superuser after hardening
   - WHY: Superusers bypass ALL RLS policies (makes security useless)
   - CHECK: SELECT rolsuper FROM pg_roles WHERE rolname = 'bionic_application'
   - FAILS if user is superuser with clear remediation steps
   - EVIDENCE: Bytebase "Common Postgres RLS Footguns"

P1 FIXES (CRITICAL - Production Readiness):

3. team_id Index Verification Added (verification.go:306-349)
   - Checks 13 critical tables for team_id indexes
   - WHY: Missing indexes cause O(n) sequential scans on EVERY query
   - WARNS: Lists tables missing indexes with CREATE INDEX commands
   - IMPACT: Prevents major performance issues under load
   - EVIDENCE: AWS RLS recommendations (always index policy columns)

4. Thread-Safe Working Directory Handling (worker.go:34-63, 490-522)
   - REMOVED: os.Chdir() (process-wide, not thread-safe)
   - ADDED: execute.Options.WorkDir for docker compose (thread-safe)
   - WHY: os.Chdir() causes race conditions if called concurrently
   - BENEFIT: Safe for API exposure, concurrent executions

5. Directory Existence Validation (worker.go:40-61)
   - Checks /opt/moni exists before attempting operations
   - Checks docker-compose.yml exists with clear error messages
   - WHY: Prevents confusing errors, provides remediation steps
   - UX: "Run 'eos create moni' first to install Moni"

6. CRITICAL Application Integration Warning (database.go:328-352)
   - Loud, clear warning that app MUST set app.current_team_id
   - Explains impact: queries will ERROR without this
   - Shows example code: SET app.current_team_id = <team_id>
   - WHY: Without this, RLS blocks everything (intentional fail-closed)

P2 FIXES (Important - Pattern Compliance):

7. README.md Created (pkg/moni/README.md) - 581 lines
   - REQUIRED: CLAUDE.md P0 Rule #9 (README in each directory)
   - Documents architecture, security, usage, troubleshooting
   - Includes security checklist, best practices, references
   - Comprehensive guide for maintenance and operations

SECURITY IMPROVEMENTS:

RLS Policies Now Fail Loudly:
  Before: app.current_team_id missing -> returns NULL -> user sees nothing
  After:  app.current_team_id missing -> ERROR: unrecognized parameter

Superuser Check:
  Before: No verification - RLS could be silently bypassed
  After:  Explicit check - fails with clear error if superuser detected

Performance Monitoring:
  Before: No index verification - silent performance degradation
  After:  Warns about missing indexes with remediation SQL

EVIDENCE-BASED:
- PostgreSQL 2024/2025 RLS best practices (AWS, Crunchy Data, Permit.io)
- Docker PostgreSQL SSL patterns (Red Gate, PostgreSQL docs)
- Multi-tenant security patterns (Picus Security, Bytebase)

FILES CHANGED:
- pkg/moni/database.go: RLS policy fix + superuser check + warnings
- pkg/moni/verification.go: team_id index verification
- pkg/moni/worker.go: Thread-safe WorkDir + directory checks
- pkg/moni/README.md: Comprehensive documentation (NEW)

TESTING RECOMMENDED:
1. Verify RLS fails without app.current_team_id set
2. Verify superuser check catches misconfiguration
3. Run --verify-rls to check index status
4. Test concurrent --init calls (thread-safety)

All fixes follow Eos patterns (AIE, structured logging, RuntimeContext).
Zero breaking changes to public API.
Implements three critical fixes for RLS security:

**Issue #8 (P0): RLS Policies Not Idempotent**
- Added DROP POLICY IF EXISTS before each CREATE POLICY
- Enables safe re-running of --init without failures
- Files: pkg/moni/database.go (15 policies updated)

**Issue #9 (P1): RLS Breaking Change Without Warning**
- Added interactive confirmation before enabling RLS
- Clear warning about application code requirements
- Added --force flag to skip confirmation (CI/CD)
- Files: pkg/moni/database.go, pkg/moni/types.go, cmd/update/moni.go

**Issue #11 (P1): Superuser Check Only Warns**
- Changed from logger.Warn to return error on query failure
- Provides clear remediation steps
- Prevents deploying RLS without verification
- Files: pkg/moni/database.go

**Changes:**
- pkg/moni/types.go: Added Force bool to WorkerConfig
- pkg/moni/database.go:
  - Updated EnableRowLevelSecurity signature to accept config
  - Added interaction.PromptYesNoSafe for breaking change confirmation
  - Added DROP POLICY IF EXISTS for all 15 RLS policies
  - Changed superuser verification to fail hard on error
- pkg/moni/worker.go: Updated EnableRowLevelSecurity call to pass config
- cmd/update/moni.go: Added --force flag and wired to WorkerConfig

**Usage:**
sudo eos update moni --init           # Interactive (prompts)
sudo eos update moni --init --force   # Automated (skips confirmation)

**Breaking Change:** RLS enablement now requires explicit confirmation
unless --force is provided. This ensures operators understand the
application code changes required (setting app.current_team_id).

Refs: Second adversarial analysis - Issues #8, #9, #11
@CodeMonkeyCybersecurity CodeMonkeyCybersecurity merged commit fcb1c10 into main Nov 8, 2025
6 of 20 checks passed
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 8, 2025
@CodeMonkeyCybersecurity CodeMonkeyCybersecurity deleted the claude/convert-to-go-worker-011CUtAeXWmX5hpiuUQwyAqo branch November 13, 2025 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants