Token Management¶
Overview¶
Security Emphasis
Secure token management is critical for maintaining the security integrity of your Kubernetes scanning operations. This task implements best practices for generating, using, and rotating short-lived tokens to minimize the risk of credential compromise.
This task guides you through implementing secure token management for Kube CINC Secure Scanner. By following this approach, you'll create ephemeral, least-privilege access tokens for scanning operations that automatically expire after use.
Time to complete: 20-30 minutes
Security risk: 🔴 High - Involves creation and management of authentication credentials
Security approach: Implements ephemeral credentials with time-based expiration, least-privilege access controls, and secure token delivery mechanisms
Security Architecture¶
Understanding Permission Layers
Token management for container scanning involves multiple security layers:
1. Token Generation Permissions * Control: Who can create authentication tokens for service accounts * Risk area: Unrestricted token generation could lead to unauthorized cluster access * Mitigation: Limit token creation to authorized administrators or automated systems with strict controls
2. Token Usage Permissions * Control: What the token can access within the Kubernetes API * Risk area: Overly permissive tokens could grant excessive access * Mitigation: Bind tokens to service accounts with precisely scoped RBAC permissions
3. Token Storage & Transmission * Control: How tokens are stored, transmitted, and protected * Risk area: Token exposure could lead to credential theft * Mitigation: Implement secure storage, encrypted transmission, and automatic token expiration
Security Prerequisites¶
- Kubernetes cluster with TokenRequest API enabled (v1.22+)
- Service accounts and RBAC roles created (see RBAC Setup)
- Administrative access to generate tokens
- Understanding of Kubernetes RBAC and authentication mechanisms
Step-by-Step Instructions¶
Step 1: Understanding Token Types¶
Security Consideration
Different token types have different security properties and lifetimes. Short-lived tokens are strongly preferred.
Kubernetes supports several token types:
- Service Account Tokens (pre-v1.24): Long-lived tokens stored in secrets
- TokenRequest API Tokens (v1.22+): Short-lived tokens with configurable expiration
- Bound Service Account Tokens: Tokens bound to specific audiences and use cases
For security reasons, we'll use the TokenRequest API to generate short-lived tokens.
Step 2: Generate Short-Lived Tokens¶
Security Consideration
Always set an appropriate expiration time based on the expected duration of the scanning operation.
- Generate a token with a 15-minute expiration:
- For automated scripts, save the token securely:
Step 3: Create a Secure Kubeconfig File¶
Security Consideration
Kubeconfig files contain sensitive credentials and should be protected accordingly.
- Generate a kubeconfig file using the token:
Step 4: Implement Token Rotation¶
Security Consideration
Automated token rotation ensures that tokens are regularly refreshed, limiting the exposure window.
- Create a shell script for token rotation:
- Make the script executable:
Step 5: Implement Token Management in CI/CD Pipelines¶
Security Consideration
CI/CD pipelines should generate fresh tokens for each run to maintain security isolation between pipeline executions.
GitHub Actions example:
GitLab CI example:
Step 6: Implement Secure Token Storage for Non-Pipeline Use Cases¶
Security Consideration
For operations outside CI/CD pipelines, token storage requires additional security measures.
- Use environment variables with limited scope:
- For systems requiring persistent token storage, use a secrets management system:
Security Best Practices¶
- Generate new tokens for each scanning operation
- Set appropriate token expiration times (15-60 minutes maximum)
- Never store tokens in source code repositories
- Implement proper file permissions (600 or more restrictive) for files containing tokens
- Use dedicated service accounts with least-privilege RBAC
- Store tokens in secure, ephemeral locations
- Implement token rotation for long-running operations
- Always use HTTPS for API communications
- Log token creation and usage for auditing purposes
- Mask tokens in logs and console output
- Delete token files immediately after use
Verification Steps¶
- Verify token expiration
- Verify token permissions
- Check token security in kubeconfig
Troubleshooting¶
Issue | Solution |
---|---|
'create token' command not found | Upgrade to Kubernetes 1.24+ or use alternative token generation methods |
Token expired errors | Increase token duration slightly or optimize scanning process for faster completion |
Permission denied with token | Verify the service account has appropriate RBAC permissions |
Tokens visible in CI/CD logs | Use secret masking features in your CI/CD platform to prevent token exposure |
Cannot authenticate with token | Check that the token format is correct and hasn't been corrupted during transfer |
Next Steps¶
After completing this task, consider:
- Implement RBAC setup to align with token permissions
- Integrate with GitHub Actions using secure token generation
- Integrate with GitLab CI using secure token generation
- Configure kubectl for secure operations