-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
341 lines (276 loc) · 13.4 KB
/
firestore.rules
File metadata and controls
341 lines (276 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
rules_version = '2';
// Git With Intent - Firestore Security Rules
// Phase 11: Production-ready multi-tenant security with RBAC
service cloud.firestore {
match /databases/{database}/documents {
// =================================================================
// Helper Functions
// =================================================================
// Check if user is authenticated
function isAuthenticated() {
return request.auth != null;
}
// Get user's membership data for a tenant
function getMembership(tenantId) {
return get(/databases/$(database)/documents/gwi_memberships/$(request.auth.uid + '_' + tenantId));
}
// Check if user has any active membership in tenant
function hasTenantAccess(tenantId) {
let membership = getMembership(tenantId);
return isAuthenticated() &&
membership != null &&
membership.data.status == 'active';
}
// Check if user has at least DEVELOPER role (can trigger runs)
function isDeveloper(tenantId) {
let membership = getMembership(tenantId);
return membership != null &&
membership.data.role in ['owner', 'admin', 'developer'] &&
membership.data.status == 'active';
}
// Check if user is tenant owner or admin
function isTenantAdmin(tenantId) {
let membership = getMembership(tenantId);
return membership != null &&
membership.data.role in ['owner', 'admin'] &&
membership.data.status == 'active';
}
// Check if user is tenant owner
function isTenantOwner(tenantId) {
let membership = getMembership(tenantId);
return membership != null &&
membership.data.role == 'owner' &&
membership.data.status == 'active';
}
// Check if request is from service account (Cloud Run)
function isServiceAccount() {
return request.auth.token.firebase.sign_in_provider == 'custom' ||
(request.auth.token.email != null &&
request.auth.token.email.matches('.*@.*\\.iam\\.gserviceaccount\\.com'));
}
// Check if tenant is active (not suspended)
function isTenantActive(tenantId) {
let tenant = get(/databases/$(database)/documents/gwi_tenants/$(tenantId));
return tenant != null && tenant.data.status == 'active';
}
// =================================================================
// Tenant Collection
// =================================================================
match /gwi_tenants/{tenantId} {
// Members can read tenant info (VIEWER and above)
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
// Only service accounts can create tenants (via installation webhook)
allow create: if isServiceAccount();
// Admins can update tenant settings
allow update: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
// Only owners can delete tenant
allow delete: if isTenantOwner(tenantId) || isServiceAccount();
// Repos subcollection - admins can connect/disconnect repos
match /repos/{repoId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow create, update: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
allow delete: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
}
// Settings subcollection - developers can read, admins can update
match /settings/{settingId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
}
// Phase 12: Connector configs - admins can manage
match /connector_configs/{connectorId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
}
// Phase 13: Workflow instances - developers can read, admins can manage
match /instances/{instanceId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow create, update: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
allow delete: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
}
// Phase 13: Workflow schedules - admins can manage
match /schedules/{scheduleId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if (isTenantAdmin(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
}
// Phase 14: Signals - members can read, service accounts write
match /signals/{signalId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
// Phase 14: Work items - members can read, developers can update status
match /work_items/{workItemId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow create: if isServiceAccount();
// Developers can claim/assign work items
allow update: if (isDeveloper(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
allow delete: if isServiceAccount();
}
// Phase 14: PR candidates - members can read, developers can approve
match /pr_candidates/{candidateId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow create: if isServiceAccount();
// Developers can approve/reject candidates
allow update: if (isDeveloper(tenantId) && isTenantActive(tenantId)) || isServiceAccount();
allow delete: if isServiceAccount();
}
}
// =================================================================
// Runs Collection
// =================================================================
match /gwi_runs/{runId} {
// VIEWER and above can read runs for their tenant
allow read: if hasTenantAccess(resource.data.tenantId) || isServiceAccount();
// Only service accounts can create runs (from webhook triggers)
// Plan limits are enforced at the API layer
allow create: if isServiceAccount();
// Service accounts update run status as it progresses
allow update: if isServiceAccount();
// DEVELOPER and above can cancel runs, service accounts for cleanup
allow delete: if isDeveloper(resource.data.tenantId) || isServiceAccount();
// Steps subcollection - detailed run progress
match /steps/{stepId} {
allow read: if hasTenantAccess(get(/databases/$(database)/documents/gwi_runs/$(runId)).data.tenantId) ||
isServiceAccount();
allow write: if isServiceAccount();
}
// Logs subcollection - run execution logs
match /logs/{logId} {
allow read: if hasTenantAccess(get(/databases/$(database)/documents/gwi_runs/$(runId)).data.tenantId) ||
isServiceAccount();
allow write: if isServiceAccount();
}
}
// =================================================================
// Users Collection
// =================================================================
match /gwi_users/{userId} {
// Users can read their own profile
allow read: if request.auth.uid == userId || isServiceAccount();
// Users can update their own profile (except role fields)
allow update: if request.auth.uid == userId &&
!('role' in request.resource.data) ||
isServiceAccount();
// Service accounts handle creation/deletion
allow create, delete: if isServiceAccount();
}
// =================================================================
// Memberships Collection
// =================================================================
match /gwi_memberships/{membershipId} {
// Users can read their own memberships, admins can read tenant memberships
allow read: if request.auth.uid == resource.data.userId ||
isTenantAdmin(resource.data.tenantId) ||
isServiceAccount();
// Admins can invite members (create), owners only for admin role
// Prevent creating owner memberships (service account only)
allow create: if (isTenantAdmin(request.resource.data.tenantId) &&
request.resource.data.role != 'owner' &&
isTenantActive(request.resource.data.tenantId)) ||
isServiceAccount();
// Admins can update member roles (but not to owner)
// Users cannot update their own role (prevent self-elevation)
allow update: if (isTenantAdmin(resource.data.tenantId) &&
request.auth.uid != resource.data.userId &&
request.resource.data.role != 'owner' &&
isTenantActive(resource.data.tenantId)) ||
isServiceAccount();
// Only owners can remove members (admins can suspend via update)
allow delete: if isTenantOwner(resource.data.tenantId) || isServiceAccount();
}
// =================================================================
// Installations Index (for webhook lookup)
// =================================================================
match /gwi_installations/{installationId} {
// Only service accounts can access (GitHub App installation mapping)
allow read, write: if isServiceAccount();
}
// =================================================================
// Phase 11: Approvals Collection
// =================================================================
match /gwi_approvals/{approvalId} {
// Admins can read approvals for their tenant
allow read: if isTenantAdmin(resource.data.tenantId) || isServiceAccount();
// Only service accounts can write approvals
allow write: if isServiceAccount();
}
// =================================================================
// Phase 11: Audit Events Collection (Immutable)
// =================================================================
match /gwi_audit_events/{eventId} {
// Admins can read audit events for their tenant
allow read: if isTenantAdmin(resource.data.tenantId) || isServiceAccount();
// Only service accounts can write (and never update/delete)
allow create: if isServiceAccount();
allow update, delete: if false; // Immutable audit trail
}
// =================================================================
// Legacy: Audit Logs Collection (deprecated, use gwi_audit_events)
// =================================================================
match /gwi_audit_logs/{logId} {
allow read: if isTenantAdmin(resource.data.tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
// =================================================================
// Usage Metrics Collection (Phase 11: Plan enforcement)
// =================================================================
match /gwi_usage/{tenantId} {
// Members can read their tenant's usage
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
// Only service accounts update usage metrics
allow write: if isServiceAccount();
// Monthly breakdown subcollection
match /months/{monthId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
}
// =================================================================
// Phase 16: Reliability Infrastructure (Service Account Only)
// =================================================================
// Distributed run locks
match /gwi_run_locks/{lockId} {
allow read, write: if isServiceAccount();
}
// Idempotency records
match /gwi_idempotency/{idempotencyKey} {
allow read, write: if isServiceAccount();
}
// Run checkpoints
match /gwi_checkpoints/{checkpointId} {
allow read, write: if isServiceAccount();
}
// =================================================================
// Phase 22: Usage Metering Collections
// =================================================================
// Usage events (append-only ledger)
match /gwi_usage_events/{eventId} {
// Members can read their tenant's usage events
allow read: if hasTenantAccess(resource.data.tenantId) || isServiceAccount();
// Only service accounts can append
allow create: if isServiceAccount();
allow update, delete: if false; // Append-only
}
// Daily usage aggregates
match /gwi_usage_daily/{aggregateId} {
allow read: if hasTenantAccess(resource.data.tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
// Monthly usage aggregates
match /gwi_usage_monthly/{aggregateId} {
allow read: if hasTenantAccess(resource.data.tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
// Usage snapshots (current state)
match /gwi_usage_snapshots/{tenantId} {
allow read: if hasTenantAccess(tenantId) || isServiceAccount();
allow write: if isServiceAccount();
}
// =================================================================
// Default Deny
// =================================================================
// Deny all other access
match /{document=**} {
allow read, write: if false;
}
}
}