|
| 1 | +# Field Selector Authorization Tests |
| 2 | + |
| 3 | +This test suite validates that field selector authorization works correctly for |
| 4 | +UserIdentities and Sessions resources in the Identity API. |
| 5 | + |
| 6 | +## Test Scenarios |
| 7 | + |
| 8 | +### 1. Regular User - Self-Scoped Access (Default Behavior) |
| 9 | +**Given:** A regular user without staff privileges |
| 10 | +**When:** User lists useridentities without field selector |
| 11 | +**Then:** User sees only their own identity provider links |
| 12 | + |
| 13 | +**When:** User attempts to use field selector for another user |
| 14 | +**Then:** Request is rejected with 403 Forbidden error |
| 15 | + |
| 16 | +### 2. Staff User - Cross-User Access with Field Selector |
| 17 | +**Given:** A user in the staff-users group |
| 18 | +**When:** User lists useridentities with field selector for another user |
| 19 | +**Then:** User successfully retrieves the target user's identity provider links |
| 20 | + |
| 21 | +### 3. Field Selector Validation |
| 22 | +**Given:** Any authenticated user |
| 23 | +**When:** User provides invalid field selector (e.g., metadata.name) |
| 24 | +**Then:** Request is rejected with appropriate error message |
| 25 | + |
| 26 | +## Authorization Model |
| 27 | + |
| 28 | +``` |
| 29 | +┌─────────────────────────────────────────────────────────────┐ |
| 30 | +│ 1. Milo RBAC Check │ |
| 31 | +│ - PolicyBinding grants access to useridentities resource │ |
| 32 | +│ - Required for both regular and staff users │ |
| 33 | +└─────────────────────────────────────────────────────────────┘ |
| 34 | + ↓ |
| 35 | +┌─────────────────────────────────────────────────────────────┐ |
| 36 | +│ 2. Field Selector Passed to Backend │ |
| 37 | +│ - Milo passes field selector to auth-provider-zitadel │ |
| 38 | +│ - No validation at Milo layer │ |
| 39 | +└─────────────────────────────────────────────────────────────┘ |
| 40 | + ↓ |
| 41 | +┌─────────────────────────────────────────────────────────────┐ |
| 42 | +│ 3. Backend Authorization (auth-provider-zitadel) │ |
| 43 | +│ - If no field selector: use authenticated user's UID │ |
| 44 | +│ - If field selector with different UID: │ |
| 45 | +│ → Check user groups (staff-users, fraud-manager) │ |
| 46 | +│ → Allow if staff, deny if not │ |
| 47 | +└─────────────────────────────────────────────────────────────┘ |
| 48 | +``` |
| 49 | + |
| 50 | +## Required Setup |
| 51 | + |
| 52 | +### PolicyBindings |
| 53 | +```yaml |
| 54 | +# Grant staff-users access to useridentities |
| 55 | +apiVersion: iam.miloapis.com/v1alpha1 |
| 56 | +kind: PolicyBinding |
| 57 | +metadata: |
| 58 | + name: staff-useridentities-viewer |
| 59 | + namespace: milo-system |
| 60 | +spec: |
| 61 | + resourceSelector: |
| 62 | + resourceKind: |
| 63 | + apiGroup: identity.miloapis.com |
| 64 | + kind: UserIdentity |
| 65 | + roleRef: |
| 66 | + name: identity-user-session-viewer |
| 67 | + namespace: milo-system |
| 68 | + subjects: |
| 69 | + - kind: Group |
| 70 | + name: staff-users |
| 71 | + namespace: milo-system |
| 72 | + uid: <staff-users-group-uid> |
| 73 | +``` |
| 74 | +
|
| 75 | +### Zitadel Configuration |
| 76 | +- Create group: `staff-users` |
| 77 | +- Assign users to group via project roles |
| 78 | +- Configure JWT claims to include groups |
| 79 | + |
| 80 | +## Manual Testing |
| 81 | + |
| 82 | +### Test 1: Regular User Cannot Use Field Selector |
| 83 | +```bash |
| 84 | +# As regular user |
| 85 | +kubectl get useridentities --field-selector=status.userUID=<other-user-id> |
| 86 | +
|
| 87 | +# Expected: 403 Forbidden |
| 88 | +# Error: "only staff users can query other users' identities" |
| 89 | +``` |
| 90 | + |
| 91 | +### Test 2: Staff User Can Use Field Selector |
| 92 | +```bash |
| 93 | +# As staff user (member of staff-users group) |
| 94 | +kubectl get useridentities --field-selector=status.userUID=<target-user-id> |
| 95 | +
|
| 96 | +# Expected: 200 OK |
| 97 | +# Response: List of target user's identity provider links |
| 98 | +``` |
| 99 | + |
| 100 | +### Test 3: Regular User Can See Own Data |
| 101 | +```bash |
| 102 | +# As regular user |
| 103 | +kubectl get useridentities |
| 104 | +
|
| 105 | +# Expected: 200 OK |
| 106 | +# Response: List of own identity provider links |
| 107 | +``` |
| 108 | + |
| 109 | +## Security Considerations |
| 110 | + |
| 111 | +1. **Defense in Depth**: Two layers of authorization (Milo RBAC + Backend groups) |
| 112 | +2. **Audit Logging**: All requests logged with user context |
| 113 | +3. **Principle of Least Privilege**: Regular users cannot access others' data |
| 114 | +4. **Explicit Deny**: Field selector attempts by non-staff users are explicitly denied |
| 115 | + |
| 116 | +## Future Enhancements |
| 117 | + |
| 118 | +- [ ] Add automated E2E tests using Chainsaw |
| 119 | +- [ ] Add rate limiting for staff user queries |
| 120 | +- [ ] Add metrics for field selector usage |
| 121 | +- [ ] Consider adding SubjectAccessReview checks in Milo layer |
0 commit comments