Skip to content

Commit 2ae927a

Browse files
authored
[fga] fix and log missing relationships (#18692)
1 parent 225040d commit 2ae927a

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messag
1313
import { v1 } from "@authzed/authzed-node";
1414
import { fgaRelationsUpdateClientLatency } from "../prometheus-metrics";
1515
import { RedisMutex } from "../redis/mutex";
16+
import { rel } from "./definitions";
1617

1718
@injectable()
1819
export class RelationshipUpdater {
@@ -48,7 +49,7 @@ export class RelationshipUpdater {
4849
}
4950
return user;
5051
}
51-
if (this.isMigrated(user)) {
52+
if (await this.isMigrated(user)) {
5253
return user;
5354
}
5455
const stopTimer = fgaRelationsUpdateClientLatency.startTimer();
@@ -61,7 +62,7 @@ export class RelationshipUpdater {
6162
throw new ApplicationError(ErrorCodes.NOT_FOUND, "User not found");
6263
}
6364
user = updatedUser;
64-
if (this.isMigrated(user)) {
65+
if (await this.isMigrated(user)) {
6566
return user;
6667
}
6768
log.info({ userId: user.id }, `Updating FGA relationships for user.`, {
@@ -93,8 +94,23 @@ export class RelationshipUpdater {
9394
}
9495
}
9596

96-
private isMigrated(user: User) {
97-
return user.additionalData?.fgaRelationshipsVersion === RelationshipUpdater.version;
97+
private async isMigrated(user: User) {
98+
const isMigrated = user.additionalData?.fgaRelationshipsVersion === RelationshipUpdater.version;
99+
if (isMigrated) {
100+
const hasSelfRelationship = await this.authorizer.find(rel.user(user.id).self.user(user.id));
101+
if (!hasSelfRelationship) {
102+
log.warn({ userId: user.id }, `User is marked as migrated but doesn't have self relationship.`);
103+
//TODO(se) this is an extra safety net to detect
104+
// reset the fgaRelationshipsVersion to undefined, so the migration is triggered again when the feature is enabled
105+
AdditionalUserData.set(user, { fgaRelationshipsVersion: undefined });
106+
await this.userDB.updateUserPartial({
107+
id: user.id,
108+
additionalData: user.additionalData,
109+
});
110+
return false;
111+
}
112+
}
113+
return isMigrated;
98114
}
99115

100116
private async findAffectedOrganizations(userId: string): Promise<Organization[]> {

0 commit comments

Comments
 (0)