Keycloak readOnlyRootFilesystem Implementation Plan
Overview
This document outlines the plan to enablereadOnlyRootFilesystem: true for Keycloak pods while maintaining functionality. Currently disabled due to Quarkus runtime requiring writable filesystem for build artifacts.
Current Status
Date: 2025-11-12 Status:readOnlyRootFilesystem: false (temporarily disabled)
File: deployments/overlays/staging-gke/keycloak-patch.yaml:30
Issue: Keycloak Quarkus runtime attempts to build JAR artifacts at startup
Problem Analysis
Root Cause
Keycloak uses Quarkus runtime which performs ahead-of-time (AOT) compilation at startup:Attempted Solutions
-
emptyDir volume mounts - Tried mounting:
/tmp/opt/keycloak/data/opt/keycloak/providers/opt/keycloak/themes/opt/keycloak/lib/quarkus(❌ breaks - deletes application files)
- Result: Quarkus still requires write access to directories we can’t easily override
Recommended Solution: Pre-Built Keycloak Image
Approach
Create a custom Keycloak image with pre-built/optimized configuration:- Build-time optimization: Run Keycloak build process during Docker image build
- Runtime-only image: Final image contains only optimized artifacts
- No runtime compilation: Keycloak starts without building
Implementation Steps
Phase 1: Create Custom Dockerfile
Phase 2: Build and Push Image
Phase 3: Update Kubernetes Configuration
Phase 4: Testing
-
Build test:
-
Read-only filesystem test:
-
Kubernetes test:
Alternative Solutions
Option 2: Use Keycloak Operator
Pros:- Official Keycloak Kubernetes integration
- Handles optimization automatically
- Better lifecycle management
- Additional complexity
- More moving parts
- Learning curve
Option 3: Accept readOnlyRootFilesystem: false
Pros:- No additional work
- Keycloak works as-is
- Reduced security posture
- Fails security audits
- Non-compliant with best practices
Testing Strategy
Test Cases
-
Container starts successfully
-
Health checks pass
-
Read-only filesystem enforced
-
Can write to mounted volumes
-
Keycloak functionality works
- Login to admin console
- Create test realm
- Create test client
- Test authentication flow
Rollback Plan
If issues occur:Success Criteria
- ✅ Keycloak pods start without errors
- ✅ All health checks pass
- ✅
readOnlyRootFilesystem: trueenforced - ✅ Admin console accessible
- ✅ Authentication flows work correctly
- ✅ No filesystem write errors in logs
- ✅ Security scans pass
- ✅ Performance comparable to current setup
Timeline
Phase 1: Research & Prototype (1-2 days)- Research Keycloak build optimization
- Create prototype Dockerfile
- Test locally with Docker
- Build optimized image
- Push to Artifact Registry
- Test in development environment
- Update staging configuration
- Deploy and monitor
- Run comprehensive tests
- Deploy to production
- Monitor for issues
- Document lessons learned
References
- Keycloak Server Installation
- Keycloak on Kubernetes
- Quarkus Native Images
- Kubernetes Security Best Practices
- Stack Overflow: Running Keycloak with readOnlyRootFilesystem
Related Files
deployments/overlays/staging-gke/keycloak-patch.yaml- Current configurationdeployments/base/keycloak-deployment.yaml- Base deploymenttests/deployment/test_configmap_secret_validation.py- Validation tests
Last Updated: 2025-11-12 Status: Planning Phase Owner: DevOps/Security Team Priority: Medium (Security Hardening)