This Ansible project automates the process of rotating Kubernetes certificates that are stored in Hashicorp Vault. The project includes monitoring, notification, and rotation capabilities.
The following diagram illustrates the system architecture and component interactions:
- Ansible 2.9 or higher
- Hashicorp Vault access
- Kubernetes cluster access
- Slack webhook URL (for notifications)
- Splunk monitoring setup
- Systemd-based Linux system (for automatic monitoring)
The monitoring system should be deployed on a dedicated server with the following specifications:
- Minimum 2 CPU cores
- 4GB RAM
- 20GB storage
- Linux distribution with systemd (Ubuntu 20.04 LTS or RHEL 8+ recommended)
- Python 3.8 or higher
- Ansible 2.9 or higher
- OpenSSL
- Network connectivity to:
- Hashicorp Vault
- Kubernetes cluster
- Splunk
- Slack API
- SMTP server (if using email notifications)
- Outbound access to:
- Hashicorp Vault (default: 8200)
- Kubernetes API (default: 6443)
- Splunk (default: 8089)
- Slack API (443)
- SMTP server (if using email notifications)
- Firewall rules to allow these connections
- Secure storage for credentials
- Regular security updates
- Network isolation (recommended)
- Access control for monitoring server
.
├── inventory/
│ └── hosts.yml
├── group_vars/
│ └── all.yml
├── roles/
│ ├── certificate_monitor/
│ │ └── tasks/
│ │ └── main.yml
│ ├── certificate_rotation/
│ └── notification/
│ └── tasks/
│ ├── slack.yml
│ └── email.yml
├── playbooks/
│ ├── rotate_certificates.yml
│ ├── monitor_certificates.yml
│ └── setup_monitoring.yml
├── monitor_certificates.sh
├── ansible-cert-monitor.service
└── requirements.yml
-
Choose a suitable monitoring server that meets the requirements above.
-
Clone the repository to the monitoring server:
git clone https://github.com/gaurav-cicd/Ansible-Kubernetes-Certificate-Rotation-Project.git
cd Ansible-Kubernetes-Certificate-Rotation-Project- Update the
PROJECT_PATHinmonitor_certificates.shto point to your project directory:
PROJECT_PATH="/path/to/your/project"- Configure the environment:
# Install required packages
sudo apt update
sudo apt install -y python3-pip ansible openssl
# Install required Python packages
pip3 install -r requirements.txt- Run the setup playbook:
ansible-playbook playbooks/setup_monitoring.yml- Verify the monitoring service is running:
systemctl status ansible-cert-monitor.timer- Update
inventory/hosts.ymlwith your Kubernetes cluster details - Configure variables in
group_vars/all.yml:- Vault credentials
- Slack webhook URL
- Certificate paths
- Notification settings
- Email configuration (if using email notifications)
ansible-playbook playbooks/monitor_certificates.ymlansible-playbook playbooks/rotate_certificates.yml-
Update the
PROJECT_PATHinmonitor_certificates.shto point to your project directory. -
Run the setup playbook:
ansible-playbook playbooks/setup_monitoring.ymlThis will:
- Create necessary log files and directories
- Install the monitoring script
- Set up a systemd service and timer
- Enable and start the monitoring service
Check service status:
# Check service status
systemctl status ansible-cert-monitor.service
# Check timer status
systemctl status ansible-cert-monitor.timer
# View logs
tail -f /var/log/cert-monitor.logControl the monitoring service:
# Stop monitoring
sudo systemctl stop ansible-cert-monitor.timer
# Start monitoring
sudo systemctl start ansible-cert-monitor.timer
# Disable monitoring
sudo systemctl disable ansible-cert-monitor.timer
# Enable monitoring
sudo systemctl enable ansible-cert-monitor.timer- Automated certificate monitoring via Splunk and direct Vault validation
- Automatic monitoring every 5 minutes using systemd timer
- Slack and email notifications 5 days before certificate expiration
- Automated certificate rotation using Ansible
- Team notification after successful rotation
- Integration with Hashicorp Vault for secure certificate storage
- Comprehensive logging and error handling
- Automatic retry mechanism for failed checks
- Prevention of multiple monitoring instances
The monitoring system includes:
-
Automatic Execution:
- Runs every 5 minutes using systemd timer
- Starts automatically on system boot
- Handles failures and retries
-
Error Handling:
- Retries up to 3 times on failure
- 60-second delay between retries
- Prevents multiple instances from running simultaneously
-
Logging:
- Logs all activities to
/var/log/cert-monitor.log - Includes timestamps and detailed error messages
- Maintains execution history
- Logs all activities to
-
Dual Monitoring Approach:
- Direct certificate validation from Hashicorp Vault
- Splunk integration for additional monitoring and historical data
- Combined results for comprehensive certificate status
-
Health Monitoring:
- Continuous monitoring of the monitoring system itself
- Checks for:
- Service status
- Timer status
- Recent errors in logs
- Service execution duration
- Automatic notifications for:
- Service failures
- Timer issues
- Error detection
- Long-running processes
- Health checks run every 5 minutes
- Notifications via Slack and/or email
The system will send notifications in the following scenarios:
-
Service Status Issues:
- Service not running
- Timer not active
- Service running too long (>5 minutes)
-
Error Detection:
- Recent errors in logs
- Failed health checks
- Monitoring system failures
-
Notification Format:
- Detailed error messages
- Specific issues identified
- Timestamp of the problem
- Recommended actions
You can manually check the monitoring system health:
ansible-playbook playbooks/check_monitoring_health.ymlThis will:
- Check service and timer status
- Review recent logs for errors
- Verify execution times
- Send notifications if issues are found
This project contains sensitive information that should be handled carefully:
-
Never commit sensitive data:
- Vault tokens
- Slack webhook URLs
- SMTP credentials
- Splunk credentials
- Kubernetes cluster credentials
- Private keys and certificates
-
Use template files:
- Copy
group_vars/all.yml.templatetogroup_vars/all.yml - Update with your specific values
- Never commit the actual
all.ymlfile
- Copy
-
Credential Storage:
- Store sensitive credentials in Hashicorp Vault
- Use environment variables for local development
- Never hardcode credentials in playbooks
-
File Permissions:
- Set appropriate permissions for credential files
- Restrict access to log files
- Use secure file ownership
-
Network Security:
- Use HTTPS for all API communications
- Implement proper firewall rules
- Use secure ports for services
-
Version Control:
- Use
.gitignoreto exclude sensitive files - Never commit real certificates or keys
- Use template files for configuration
- Use
-
Access Control:
- Implement role-based access control
- Use service accounts with minimal privileges
- Regular audit of access permissions
-
Monitoring:
- Enable audit logging
- Monitor access to sensitive files
- Regular security reviews
-
Updates:
- Keep all dependencies updated
- Regular security patches
- Review security advisories
