Skip to content

Commit e6ba18a

Browse files
authored
[fga] check workspace relationships (#18793)
1 parent 7a74d1c commit e6ba18a

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

components/server/src/authorization/authorizer.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,54 @@ export class Authorizer {
416416
if (!(await isFgaWritesEnabled(userID))) {
417417
return;
418418
}
419-
return this.bulkAddWorkspaceToOrg([{ orgID, userID, workspaceID, shared }]);
420-
}
421-
422-
async bulkAddWorkspaceToOrg(
423-
ids: { orgID: string; userID: string; workspaceID: string; shared: boolean }[],
424-
): Promise<void> {
425419
const rels: v1.RelationshipUpdate[] = [];
426-
for (const { orgID, userID, workspaceID, shared } of ids) {
427-
rels.push(set(rel.workspace(workspaceID).org.organization(orgID)));
428-
rels.push(set(rel.workspace(workspaceID).owner.user(userID)));
429-
if (shared) {
430-
rels.push(set(rel.workspace(workspaceID).shared.anyUser));
431-
}
420+
rels.push(set(rel.workspace(workspaceID).org.organization(orgID)));
421+
rels.push(set(rel.workspace(workspaceID).owner.user(userID)));
422+
if (shared) {
423+
rels.push(set(rel.workspace(workspaceID).shared.anyUser));
432424
}
433425
await this.authorizer.writeRelationships(...rels);
426+
427+
//TODO(se) remove this double checking once we're confident that the above works
428+
// check if the relationships were written
429+
try {
430+
const wsToOrgRel = this.find(rel.workspace(workspaceID).org.organization(orgID));
431+
const wsToOwnerRel = this.find(rel.workspace(workspaceID).owner.user(userID));
432+
const wsSharedRel = shared ? this.find(rel.workspace(workspaceID).shared.anyUser) : Promise.resolve(true);
433+
if (!(await wsToOrgRel)) {
434+
log.error("Failed to write workspace to org relationship", {
435+
orgID,
436+
userID,
437+
workspaceID,
438+
439+
shared,
440+
});
441+
}
442+
if (!(await wsToOwnerRel)) {
443+
log.error("Failed to write workspace to owner relationship", {
444+
orgID,
445+
userID,
446+
workspaceID,
447+
shared,
448+
});
449+
}
450+
if (!(await wsSharedRel)) {
451+
log.error("Failed to write workspace shared relationship", {
452+
orgID,
453+
userID,
454+
workspaceID,
455+
shared,
456+
});
457+
}
458+
} catch (error) {
459+
log.error("Failed to check workspace relationships", {
460+
orgID,
461+
userID,
462+
workspaceID,
463+
shared,
464+
error,
465+
});
466+
}
434467
}
435468

436469
async removeWorkspaceFromOrg(orgID: string, userID: string, workspaceID: string): Promise<void> {

components/server/src/authorization/relationship-updater.spec.db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe("RelationshipUpdater", async () => {
286286
it("should create relationships for all user workspaces", async function () {
287287
const user = await userDB.newUser();
288288
const org = await orgDB.createTeam(user.id, "MyOrg");
289-
const totalWorkspaces = 20;
289+
const totalWorkspaces = 50;
290290
const expectedWorkspaces: Workspace[] = [];
291291
for (let i = 0; i < totalWorkspaces; i++) {
292292
const workspace = await workspaceDB.store({

components/server/src/authorization/relationship-updater.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { rel } from "./definitions";
1717

1818
@injectable()
1919
export class RelationshipUpdater {
20-
public static readonly version = 2;
20+
public static readonly version = 3;
2121

2222
constructor(
2323
@inject(UserDB) private readonly userDB: UserDB,
@@ -155,14 +155,18 @@ export class RelationshipUpdater {
155155
limit: 500, // The largest amount of workspaces is 189 today (2023-08-24)
156156
});
157157

158-
await this.authorizer.bulkAddWorkspaceToOrg(
159-
workspaces.map((ws) => ({
160-
orgID: ws.workspace.organizationId,
161-
userID: ws.workspace.ownerId,
162-
workspaceID: ws.workspace.id,
163-
shared: !!ws.workspace.shareable,
164-
})),
165-
);
158+
for (const ws of workspaces) {
159+
await this.authorizer
160+
.addWorkspaceToOrg(
161+
ws.workspace.organizationId,
162+
ws.workspace.ownerId,
163+
ws.workspace.id,
164+
!!ws.workspace.shareable,
165+
)
166+
.catch((err) => {
167+
log.error({ userId: user.id, workspaceId: ws.workspace.id }, "Failed to update workspace", err);
168+
});
169+
}
166170
}
167171

168172
private async updateUser(user: User): Promise<void> {

0 commit comments

Comments
 (0)