|
| 1 | +# Automated Testing for Path-Based RBAC |
| 2 | + |
| 3 | +This document describes the automated testing infrastructure for Observatorium API's path-based RBAC system. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The automated testing framework provides multiple ways to validate path-based RBAC functionality: |
| 8 | + |
| 9 | +1. **Go-based Integration Tests** - Comprehensive client-side testing |
| 10 | +2. **Shell Script Tests** - Quick manual and automated validation |
| 11 | +3. **Kubernetes Job Tests** - In-cluster testing with direct API access |
| 12 | +4. **Continuous Integration** - Automated testing pipeline integration |
| 13 | + |
| 14 | +## Test Components |
| 15 | + |
| 16 | +### 1. Go-based Test Suite (`demo/automated-test.go`) |
| 17 | + |
| 18 | +A comprehensive Go program that tests multiple user personas and access patterns: |
| 19 | + |
| 20 | +```go |
| 21 | +// Tests different user types with specific path permissions |
| 22 | +type TestCase struct { |
| 23 | + Name string |
| 24 | + CertFile string |
| 25 | + KeyFile string |
| 26 | + Tenant string |
| 27 | + Path string |
| 28 | + Method string |
| 29 | + ExpectedStatus int |
| 30 | + Description string |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +**Features:** |
| 35 | +- TLS client certificate authentication |
| 36 | +- Configurable test cases via code |
| 37 | +- Detailed pass/fail reporting |
| 38 | +- Support for different HTTP methods |
| 39 | +- Certificate validation and error handling |
| 40 | + |
| 41 | +**Usage:** |
| 42 | +```bash |
| 43 | +go run demo/automated-test.go <api-url> |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Shell Script Test Runner (`demo/run-automated-tests.sh`) |
| 47 | + |
| 48 | +Automated script that: |
| 49 | +- Extracts certificates from Kubernetes secrets |
| 50 | +- Sets up port-forwarding |
| 51 | +- Runs the Go test suite |
| 52 | +- Performs additional validation checks |
| 53 | +- Provides comprehensive status reporting |
| 54 | + |
| 55 | +**Features:** |
| 56 | +- Automatic certificate extraction |
| 57 | +- Port-forward management |
| 58 | +- Health checks and validation |
| 59 | +- Clean error handling and cleanup |
| 60 | +- Color-coded output |
| 61 | + |
| 62 | +**Usage:** |
| 63 | +```bash |
| 64 | +./demo/run-automated-tests.sh |
| 65 | +``` |
| 66 | + |
| 67 | +### 3. Kubernetes Test Job (`demo/test-suite.yaml`) |
| 68 | + |
| 69 | +In-cluster testing using Kubernetes Jobs: |
| 70 | +- Runs tests directly within the cluster |
| 71 | +- No port-forwarding required |
| 72 | +- Uses service discovery for API access |
| 73 | +- Configurable via ConfigMaps |
| 74 | + |
| 75 | +**Components:** |
| 76 | +- `ConfigMap` with test configuration |
| 77 | +- `Job` specification with test logic |
| 78 | +- Environment variables for certificate access |
| 79 | +- Built-in retry and backoff logic |
| 80 | + |
| 81 | +**Usage:** |
| 82 | +```bash |
| 83 | +kubectl apply -f demo/test-suite.yaml |
| 84 | +kubectl logs job/path-rbac-test-job -n proxy |
| 85 | +``` |
| 86 | + |
| 87 | +### 4. Enhanced Demo Setup (`demo/setup-with-tests.sh`) |
| 88 | + |
| 89 | +Complete demo environment with integrated testing: |
| 90 | +- Sets up KinD cluster |
| 91 | +- Deploys cert-manager and certificates |
| 92 | +- Configures Observatorium API |
| 93 | +- Runs initial test validation |
| 94 | +- Creates convenience scripts |
| 95 | + |
| 96 | +**Generated Scripts:** |
| 97 | +- `demo/quick-test.sh` - Run Kubernetes test job |
| 98 | +- `demo/watch-tests.sh` - Monitor test execution |
| 99 | +- `demo/port-forward.sh` - Start port-forwarding |
| 100 | + |
| 101 | +## Test Categories |
| 102 | + |
| 103 | +### 1. Admin User Tests |
| 104 | +- **Scope**: Full access to all paths and tenants |
| 105 | +- **Expected**: 200 responses for all endpoints |
| 106 | +- **Paths**: `/api/metrics/v1/*`, `/api/logs/v1/*`, `/api/traces/v1/*` |
| 107 | + |
| 108 | +### 2. Read-Only User Tests |
| 109 | +- **Scope**: Limited read access to specific tenant |
| 110 | +- **Expected**: 200 for read endpoints, 403 for write endpoints |
| 111 | +- **Paths**: Query and series endpoints only |
| 112 | + |
| 113 | +### 3. Query-Only User Tests |
| 114 | +- **Scope**: Restricted to query endpoints |
| 115 | +- **Expected**: 200 for `/query` and `/query_range`, 403 for others |
| 116 | +- **Paths**: `/api/metrics/v1/query*` only |
| 117 | + |
| 118 | +### 4. Write-Only User Tests |
| 119 | +- **Scope**: Write access only |
| 120 | +- **Expected**: 200 for `/receive`, 403 for read endpoints |
| 121 | +- **Paths**: `/api/metrics/v1/receive` only |
| 122 | + |
| 123 | +### 5. Cross-Tenant Tests |
| 124 | +- **Scope**: Validates tenant isolation |
| 125 | +- **Expected**: 403 when accessing unauthorized tenants |
| 126 | +- **Validation**: Proper tenant boundary enforcement |
| 127 | + |
| 128 | +### 6. Certificate Validation Tests |
| 129 | +- **Scope**: Authentication requirements |
| 130 | +- **Expected**: 403/SSL errors without valid certificates |
| 131 | +- **Validation**: mTLS enforcement |
| 132 | + |
| 133 | +## Running Tests |
| 134 | + |
| 135 | +### Quick Start |
| 136 | +```bash |
| 137 | +# Setup environment with testing |
| 138 | +./demo/setup-with-tests.sh |
| 139 | + |
| 140 | +# Run comprehensive tests |
| 141 | +./demo/run-automated-tests.sh |
| 142 | + |
| 143 | +# Run quick in-cluster test |
| 144 | +./demo/quick-test.sh |
| 145 | +``` |
| 146 | + |
| 147 | +### Manual Testing |
| 148 | +```bash |
| 149 | +# Extract certificates manually |
| 150 | +kubectl get secret -n proxy admin-client-cert -o jsonpath='{.data.tls\.crt}' | base64 -d > admin.crt |
| 151 | +kubectl get secret -n proxy admin-client-cert -o jsonpath='{.data.tls\.key}' | base64 -d > admin.key |
| 152 | + |
| 153 | +# Test specific endpoint |
| 154 | +curl --cert admin.crt --key admin.key --cacert ca.crt \ |
| 155 | + -H "X-Tenant: tenant-a" \ |
| 156 | + "https://localhost:8080/api/metrics/v1/query?query=up" |
| 157 | +``` |
| 158 | + |
| 159 | +### Continuous Integration |
| 160 | +```bash |
| 161 | +# In CI pipeline |
| 162 | +kubectl apply -f demo/test-suite.yaml |
| 163 | +kubectl wait --for=condition=complete job/path-rbac-test-job -n proxy --timeout=120s |
| 164 | +kubectl logs job/path-rbac-test-job -n proxy |
| 165 | +``` |
| 166 | + |
| 167 | +## Test Configuration |
| 168 | + |
| 169 | +### Environment Variables |
| 170 | +- `API_URL` - Observatorium API endpoint |
| 171 | +- `TENANT_A` - First tenant name (default: tenant-a) |
| 172 | +- `TENANT_B` - Second tenant name (default: tenant-b) |
| 173 | + |
| 174 | +### Certificate Files Expected |
| 175 | +- `admin-client.crt/key` - Admin user certificates |
| 176 | +- `test-client.crt/key` - Read-only user certificates |
| 177 | +- `query-user.crt/key` - Query-only user certificates |
| 178 | +- `write-user.crt/key` - Write-only user certificates |
| 179 | +- `logs-reader.crt/key` - Logs reader certificates |
| 180 | +- `ca.crt` - Root CA certificate |
| 181 | + |
| 182 | +### Test Customization |
| 183 | + |
| 184 | +Modify test cases in `automated-test.go`: |
| 185 | +```go |
| 186 | +testCases := []TestCase{ |
| 187 | + { |
| 188 | + Name: "custom_test", |
| 189 | + CertFile: "custom-user.crt", |
| 190 | + KeyFile: "custom-user.key", |
| 191 | + Tenant: "custom-tenant", |
| 192 | + Path: "/api/custom/v1/endpoint", |
| 193 | + Method: "GET", |
| 194 | + ExpectedStatus: 200, |
| 195 | + Description: "Custom test description", |
| 196 | + }, |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +## Troubleshooting |
| 201 | + |
| 202 | +### Common Issues |
| 203 | + |
| 204 | +1. **Certificate Errors** |
| 205 | + ```bash |
| 206 | + # Check certificate validity |
| 207 | + openssl x509 -in admin-client.crt -text -noout |
| 208 | + |
| 209 | + # Verify CA trust |
| 210 | + openssl verify -CAfile ca.crt admin-client.crt |
| 211 | + ``` |
| 212 | + |
| 213 | +2. **Port-Forward Issues** |
| 214 | + ```bash |
| 215 | + # Check if port is in use |
| 216 | + lsof -i :8080 |
| 217 | + |
| 218 | + # Restart port-forward |
| 219 | + kubectl port-forward -n proxy svc/observatorium-api 8080:8080 |
| 220 | + ``` |
| 221 | + |
| 222 | +3. **API Not Ready** |
| 223 | + ```bash |
| 224 | + # Check pod status |
| 225 | + kubectl get pods -n proxy -l app=observatorium-api |
| 226 | + |
| 227 | + # Check logs |
| 228 | + kubectl logs -n proxy deployment/observatorium-api |
| 229 | + ``` |
| 230 | + |
| 231 | +4. **Test Job Failures** |
| 232 | + ```bash |
| 233 | + # Check job status |
| 234 | + kubectl get jobs -n proxy |
| 235 | + |
| 236 | + # View detailed logs |
| 237 | + kubectl describe job path-rbac-test-job -n proxy |
| 238 | + ``` |
| 239 | + |
| 240 | +### Debug Mode |
| 241 | + |
| 242 | +Enable verbose logging: |
| 243 | +```bash |
| 244 | +export DEBUG=1 |
| 245 | +./demo/run-automated-tests.sh |
| 246 | +``` |
| 247 | + |
| 248 | +View detailed test output: |
| 249 | +```bash |
| 250 | +go run demo/automated-test.go localhost:8080 -v |
| 251 | +``` |
| 252 | + |
| 253 | +## Integration with CI/CD |
| 254 | + |
| 255 | +### GitHub Actions Example |
| 256 | +```yaml |
| 257 | +- name: Run RBAC Tests |
| 258 | + run: | |
| 259 | + ./demo/setup-with-tests.sh |
| 260 | + ./demo/run-automated-tests.sh |
| 261 | +``` |
| 262 | +
|
| 263 | +### Jenkins Pipeline Example |
| 264 | +```groovy |
| 265 | +stage('RBAC Tests') { |
| 266 | + steps { |
| 267 | + sh './demo/setup-with-tests.sh' |
| 268 | + sh './demo/run-automated-tests.sh' |
| 269 | + } |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +## Metrics and Monitoring |
| 274 | + |
| 275 | +The test framework provides: |
| 276 | +- **Test execution time** - Duration of test runs |
| 277 | +- **Pass/fail rates** - Success percentage over time |
| 278 | +- **Certificate expiry monitoring** - Alert on expiring certificates |
| 279 | +- **API health checks** - Endpoint availability validation |
| 280 | + |
| 281 | +## Security Considerations |
| 282 | + |
| 283 | +1. **Certificate Handling**: Tests properly handle certificate lifecycle |
| 284 | +2. **Secret Management**: Kubernetes secrets are used for certificate storage |
| 285 | +3. **Network Isolation**: Tests respect cluster network policies |
| 286 | +4. **Access Logging**: All test requests are logged for audit purposes |
| 287 | + |
| 288 | +## Extending Tests |
| 289 | + |
| 290 | +### Adding New User Personas |
| 291 | +1. Create certificates in `certificates-extended.yaml` |
| 292 | +2. Add RBAC roles in `rbac-with-paths.yaml` |
| 293 | +3. Add test cases in `automated-test.go` |
| 294 | +4. Update the test runner scripts |
| 295 | + |
| 296 | +### Adding New Endpoints |
| 297 | +1. Define endpoint paths in RBAC configuration |
| 298 | +2. Create test cases for new endpoints |
| 299 | +3. Update validation logic |
| 300 | +4. Test both positive and negative cases |
| 301 | + |
| 302 | +### Performance Testing |
| 303 | +The framework can be extended for performance testing: |
| 304 | +- Add load testing scenarios |
| 305 | +- Measure response times |
| 306 | +- Test concurrent access patterns |
| 307 | +- Monitor resource usage |
| 308 | + |
| 309 | +## Best Practices |
| 310 | + |
| 311 | +1. **Test Isolation**: Each test case is independent |
| 312 | +2. **Cleanup**: Proper cleanup of resources and connections |
| 313 | +3. **Error Handling**: Graceful handling of network and authentication errors |
| 314 | +4. **Documentation**: Clear descriptions for each test case |
| 315 | +5. **Automation**: Fully automated setup and execution |
| 316 | +6. **Monitoring**: Continuous monitoring of test health |
0 commit comments