Skip to content

Commit 9b4a6db

Browse files
authored
[server] fix flaky tests (#18363)
1 parent efb573c commit 9b4a6db

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

components/server/src/authorization/authorizer.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66

77
import { v1 } from "@authzed/authzed-node";
8-
import { inject, injectable } from "inversify";
98

9+
import { Organization, Project, TeamMemberInfo, TeamMemberRole } from "@gitpod/gitpod-protocol";
10+
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1011
import {
1112
InstallationID,
1213
OrganizationPermission,
@@ -17,23 +18,29 @@ import {
1718
UserPermission,
1819
} from "./definitions";
1920
import { SpiceDBAuthorizer } from "./spicedb-authorizer";
20-
import { Organization, TeamMemberInfo, Project, TeamMemberRole } from "@gitpod/gitpod-protocol";
21-
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
2221
import { BUILTIN_INSTLLATION_ADMIN_USER_ID } from "@gitpod/gitpod-db/lib";
23-
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
2422

25-
@injectable()
26-
export class Authorizer {
27-
constructor(
28-
@inject(SpiceDBAuthorizer)
29-
private authorizer: SpiceDBAuthorizer,
30-
) {
31-
this.initialize().catch((err) => log.error("Failed to add installation admin", err));
32-
}
23+
export function createInitializingAuthorizer(spiceDbAuthorizer: SpiceDBAuthorizer): Authorizer {
24+
const target = new Authorizer(spiceDbAuthorizer);
25+
const initialized = target.addAdminRole(BUILTIN_INSTLLATION_ADMIN_USER_ID);
26+
return new Proxy(target, {
27+
get(target, propKey, receiver) {
28+
const originalMethod = target[propKey as keyof typeof target];
29+
30+
if (typeof originalMethod === "function") {
31+
return async function (...args: any[]) {
32+
await initialized;
33+
return (originalMethod as any).apply(target, args);
34+
};
35+
} else {
36+
return originalMethod;
37+
}
38+
},
39+
});
40+
}
3341

34-
private async initialize(): Promise<void> {
35-
await this.addAdminRole(BUILTIN_INSTLLATION_ADMIN_USER_ID);
36-
}
42+
export class Authorizer {
43+
constructor(private authorizer: SpiceDBAuthorizer) {}
3744

3845
async hasPermissionOnOrganization(
3946
userId: string,

components/server/src/authorization/spicedb-authorizer.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ export class SpiceDBAuthorizer {
2222
private client: SpiceDBClient,
2323
) {}
2424

25-
/**
26-
* @deprecated only for testing
27-
*/
28-
async logRelationships() {
29-
// const resources = await this.client?.readRelationships({});
30-
//log.info(JSON.stringify(resources, undefined, 2));
31-
}
32-
3325
async check(
3426
req: v1.CheckPermissionRequest,
3527
experimentsFields: {

components/server/src/container-module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { HostContextProviderImpl } from "./auth/host-context-provider-impl";
4949
import { AuthJWT, SignInJWT } from "./auth/jwt";
5050
import { LoginCompletionHandler } from "./auth/login-completion-handler";
5151
import { VerificationService } from "./auth/verification-service";
52-
import { Authorizer } from "./authorization/authorizer";
52+
import { Authorizer, createInitializingAuthorizer } from "./authorization/authorizer";
5353
import { SpiceDBClient, spicedbClientFromEnv } from "./authorization/spicedb";
5454
import { BillingModes } from "./billing/billing-mode";
5555
import { EntitlementService, EntitlementServiceImpl } from "./billing/entitlement-service";
@@ -305,7 +305,12 @@ export const productionContainerModule = new ContainerModule(
305305
.toDynamicValue(() => spicedbClientFromEnv())
306306
.inSingletonScope();
307307
bind(SpiceDBAuthorizer).toSelf().inSingletonScope();
308-
bind(Authorizer).toSelf().inSingletonScope();
308+
bind(Authorizer)
309+
.toDynamicValue((ctx) => {
310+
const authorizer = ctx.container.get<SpiceDBAuthorizer>(SpiceDBAuthorizer);
311+
return createInitializingAuthorizer(authorizer);
312+
})
313+
.inSingletonScope();
309314

310315
// grpc / Connect API
311316
bind(APIUserService).toSelf().inSingletonScope();

components/server/src/projects/projects-service.spec.db.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { OrganizationService } from "../orgs/organization-service";
1414
import { createTestContainer } from "../test/service-testing-container-module";
1515
import { ProjectsService } from "./projects-service";
1616
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
17-
import { SpiceDBAuthorizer } from "../authorization/spicedb-authorizer";
1817
import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db";
1918
import { expectError } from "../test/expect-utils";
2019

@@ -41,8 +40,6 @@ describe("ProjectsService", async () => {
4140
const orgService = container.get(OrganizationService);
4241
org = await orgService.createOrganization(owner.id, "my-org");
4342

44-
const a = container.get(SpiceDBAuthorizer);
45-
await a.logRelationships();
4643
// create and add a member
4744
member = await userDB.newUser();
4845
const invite = await orgService.getOrCreateInvite(owner.id, org.id);

0 commit comments

Comments
 (0)