Overview
The Auth module is a lightweight Go application responsible for handling authentication to the Kubernetes API. It provides secure login endpoints and manages user sessions through token validation.The Auth module acts as an authentication gateway, validating credentials before granting access to Dashboard features.
Module Architecture
Entry Point
The module starts inmodules/auth/main.go:
modules/auth/main.go:33-49
Package Structure
Core Responsibilities
1. User Authentication
The Auth module validates user credentials against the Kubernetes API Server.Login Request Flow
2. CSRF Token Generation
Provides CSRF tokens for state-changing operations:modules/auth/pkg/routes/csrftoken/handler.go
3. User Information
Returns authenticated user details:modules/auth/pkg/routes/me/handler.go
Authentication Methods
The Auth module supports multiple authentication strategies:Token-Based Authentication
Users provide a Kubernetes service account token or user token:Token Validation Process
- Receive token from login request
- Create TokenReview request to Kubernetes API
- Validate response from TokenReview API
- Extract user info (username, UID, groups)
- Generate session token or cookie
- Return authentication response
Kubeconfig-Based Authentication
Users can upload kubeconfig files containing:- Client certificates
- Bearer tokens
- Username/password credentials
API Routes
The Auth module exposes three primary endpoints:POST /api/v1/login
Authenticates a user and returns session information. Request:modules/auth/pkg/routes/login/handler.go:27-48
GET /api/v1/csrftoken/
Generates a CSRF token for the specified action. Parameters:action(path): Action name (e.g., “deploy”, “scale”)
GET /api/v1/me
Returns information about the currently authenticated user. Response:Security Features
CSRF Protection
The Auth module generates CSRF tokens using a shared secret:- Shared between Auth and API modules via Kubernetes Secret
- Base64-encoded 256-byte random string
- Auto-generated if not provided in Helm values
Session Management
Authentication sessions are managed through:- JWE Tokens: Encrypted JSON Web Tokens
- HTTP-only Cookies: Secure session cookies
- Token Refresh: Automatic token refresh mechanism
TLS/HTTPS
The Auth module supports both HTTP and HTTPS:Request Flow
Login Flow
Token Review API
The Auth module uses Kubernetes TokenReview API:Configuration Arguments
Key command-line arguments:| Argument | Description | Default |
|---|---|---|
--kubeconfig | Path to kubeconfig file | In-cluster config |
--apiserver-host | Kubernetes API server URL | Auto-detected |
--bind-address | Bind address | 0.0.0.0:8000 |
--csrf-key | CSRF token secret key | Auto-generated |
--token-ttl | Session token TTL | 15m |
modules/auth/pkg/args/args.go
Router Setup
The Auth module uses Gin web framework:modules/auth/pkg/router/setup.go
Route Registration
Routes are registered via init() functions:modules/auth/main.go:27-31
Error Handling
Consistent error responses:200 OK- Successful authentication400 Bad Request- Malformed request401 Unauthorized- Invalid credentials500 Internal Server Error- Server error
Integration with Other Modules
With API Module
The API module validates CSRF tokens generated by Auth module:With Kong Gateway
Kong routes authentication requests to Auth module:Deployment
Helm chart configuration:charts/kubernetes-dashboard/templates/deployments/auth.yaml
Testing
Run Auth module tests:Manual Testing
Logging
The Auth module uses structured logging:0- Info and errors1- Verbose info2+- Debug information
Security Best Practices
Token Storage
Token Storage
- Never log authentication tokens
- Clear tokens from memory after use
- Use secure session storage
CSRF Protection
CSRF Protection
- Rotate CSRF keys regularly
- Use different tokens for different actions
- Validate token expiration
TLS Configuration
TLS Configuration
- Always use HTTPS in production
- Use strong cipher suites
- Enable certificate validation
Rate Limiting
Rate Limiting
- Implement login attempt limiting
- Block suspicious IP addresses
- Monitor failed authentication attempts
Troubleshooting
Common Issues
401 Unauthorized
401 Unauthorized
CSRF Token Mismatch
CSRF Token Mismatch
Cause: Auth and API modules using different CSRF keysSolution:
- Check
kubernetes-dashboard-csrfsecret - Restart both Auth and API pods
- Verify secret is mounted correctly
Cannot Connect to Kubernetes API
Cannot Connect to Kubernetes API
Cause: Invalid API server configurationSolution:
- Check
--apiserver-hostargument - Verify RBAC permissions
- Check network connectivity
Related Resources
API Module
How API module validates CSRF tokens
Security
Dashboard security documentation
RBAC Configuration
Setting up proper permissions
Kubernetes TokenReview
Kubernetes TokenReview API documentation