Skip to content

Commit 7a1394b

Browse files
geroplakosyakov
authored andcommitted
[server] WorkspaceService.startWorkspace
1 parent 4fdc6c7 commit 7a1394b

File tree

8 files changed

+267
-214
lines changed

8 files changed

+267
-214
lines changed

components/server/src/api/user.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { suite, test } from "@testdeck/mocha";
88
import { APIUserService } from "./user";
99
import { Container } from "inversify";
1010
import { testContainer } from "@gitpod/gitpod-db/lib";
11-
import { WorkspaceStarter } from "../workspace/workspace-starter";
1211
import { UserAuthentication } from "../user/user-authentication";
1312
import { BlockUserRequest, BlockUserResponse } from "@gitpod/public-api/lib/gitpod/experimental/v1/user_pb";
1413
import { User } from "@gitpod/gitpod-protocol";
@@ -18,22 +17,24 @@ import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
1817
import { v4 as uuidv4 } from "uuid";
1918
import { ConnectError, Code } from "@bufbuild/connect";
2019
import * as chai from "chai";
20+
import { WorkspaceService } from "../workspace/workspace-service";
2121

2222
const expect = chai.expect;
2323

2424
@suite()
2525
export class APIUserServiceSpec {
2626
private container: Container;
27-
private workspaceStarterMock: WorkspaceStarter = {
27+
private workspaceStarterMock: WorkspaceService = {
2828
stopRunningWorkspacesForUser: async (
2929
ctx: TraceContext,
30-
userID: string,
30+
userId: string,
31+
userIdToStop: string,
3132
reason: string,
3233
policy?: StopWorkspacePolicy,
3334
): Promise<Workspace[]> => {
3435
return [];
3536
},
36-
} as WorkspaceStarter;
37+
} as WorkspaceService;
3738
private userServiceMock: UserAuthentication = {
3839
blockUser: async (targetUserId: string, block: boolean): Promise<User> => {
3940
return {
@@ -45,7 +46,7 @@ export class APIUserServiceSpec {
4546
async before() {
4647
this.container = testContainer.createChild();
4748

48-
this.container.bind(WorkspaceStarter).toConstantValue(this.workspaceStarterMock);
49+
this.container.bind(WorkspaceService).toConstantValue(this.workspaceStarterMock);
4950
this.container.bind(UserAuthentication).toConstantValue(this.userServiceMock);
5051
this.container.bind(APIUserService).toSelf().inSingletonScope();
5152
}

components/server/src/api/user.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ import {
2525
} from "@gitpod/public-api/lib/gitpod/experimental/v1/user_pb";
2626
import { WorkspaceStarter } from "../workspace/workspace-starter";
2727
import { UserAuthentication } from "../user/user-authentication";
28-
import { validate } from "uuid";
29-
import { StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
30-
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
28+
import { WorkspaceService } from "../workspace/workspace-service";
3129

3230
@injectable()
3331
export class APIUserService implements ServiceImpl<typeof UserServiceInterface> {
34-
@inject(WorkspaceStarter) protected readonly workspaceStarter: WorkspaceStarter;
32+
@inject(WorkspaceStarter) protected readonly workspaceService: WorkspaceService;
3533
@inject(UserAuthentication) protected readonly userService: UserAuthentication;
3634

3735
public async getAuthenticatedUser(req: GetAuthenticatedUserRequest): Promise<GetAuthenticatedUserResponse> {
@@ -59,39 +57,41 @@ export class APIUserService implements ServiceImpl<typeof UserServiceInterface>
5957
}
6058

6159
public async blockUser(req: BlockUserRequest): Promise<BlockUserResponse> {
62-
const { userId, reason } = req;
60+
throw new ConnectError("unimplemented", Code.Unimplemented);
61+
// TODO(gpl) Had to comment this out because of missing authentication info: Who is executing this?
62+
// const { userId, reason } = req;
6363

64-
if (!userId) {
65-
throw new ConnectError("userId is a required parameter", Code.InvalidArgument);
66-
}
67-
if (!validate(userId)) {
68-
throw new ConnectError("userId must be a valid uuid", Code.InvalidArgument);
69-
}
70-
if (!reason) {
71-
throw new ConnectError("reason is a required parameter", Code.InvalidArgument);
72-
}
64+
// if (!userId) {
65+
// throw new ConnectError("userId is a required parameter", Code.InvalidArgument);
66+
// }
67+
// if (!validate(userId)) {
68+
// throw new ConnectError("userId must be a valid uuid", Code.InvalidArgument);
69+
// }
70+
// if (!reason) {
71+
// throw new ConnectError("reason is a required parameter", Code.InvalidArgument);
72+
// }
7373

74-
// TODO: Once connect-node supports middlewares, lift the tracing into the middleware.
75-
const trace = {};
76-
await this.userService.blockUser(userId, true);
77-
log.info(`Blocked user ${userId}.`, {
78-
userId,
79-
reason,
80-
});
74+
// // TODO: Once connect-node supports middlewares, lift the tracing into the middleware.
75+
// const trace = {};
76+
// await this.userService.blockUser(userId, true);
77+
// log.info(`Blocked user ${userId}.`, {
78+
// userId,
79+
// reason,
80+
// });
8181

82-
const stoppedWorkspaces = await this.workspaceStarter.stopRunningWorkspacesForUser(
83-
trace,
84-
userId,
85-
reason,
86-
StopWorkspacePolicy.IMMEDIATELY,
87-
);
82+
// const stoppedWorkspaces = await this.workspaceService.stopRunningWorkspacesForUser(
83+
// trace,
84+
// userId,
85+
// reason,
86+
// StopWorkspacePolicy.IMMEDIATELY,
87+
// );
8888

89-
log.info(`Stopped ${stoppedWorkspaces.length} workspaces in response to BlockUser.`, {
90-
userId,
91-
reason,
92-
workspaceIds: stoppedWorkspaces.map((w) => w.id),
93-
});
89+
// log.info(`Stopped ${stoppedWorkspaces.length} workspaces in response to BlockUser.`, {
90+
// userId,
91+
// reason,
92+
// workspaceIds: stoppedWorkspaces.map((w) => w.id),
93+
// });
9494

95-
return new BlockUserResponse();
95+
// return new BlockUserResponse();
9696
}
9797
}

components/server/src/authorization/authorizer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class Authorizer {
5959
permission: OrganizationPermission,
6060
orgId: string,
6161
): Promise<boolean> {
62-
if (userId === "SYSTEM_USER") {
62+
if (userId === SYSTEM_USER) {
6363
return true;
6464
}
6565

@@ -89,7 +89,7 @@ export class Authorizer {
8989
}
9090

9191
async hasPermissionOnProject(userId: string, permission: ProjectPermission, projectId: string): Promise<boolean> {
92-
if (userId === "SYSTEM_USER") {
92+
if (userId === SYSTEM_USER) {
9393
return true;
9494
}
9595

@@ -119,7 +119,7 @@ export class Authorizer {
119119
}
120120

121121
async hasPermissionOnUser(userId: string, permission: UserPermission, resourceUserId: string): Promise<boolean> {
122-
if (userId === "SYSTEM_USER") {
122+
if (userId === SYSTEM_USER) {
123123
return true;
124124
}
125125

@@ -152,7 +152,7 @@ export class Authorizer {
152152
permission: WorkspacePermission,
153153
workspaceId: string,
154154
): Promise<boolean> {
155-
if (userId === "SYSTEM_USER") {
155+
if (userId === SYSTEM_USER) {
156156
return true;
157157
}
158158

components/server/src/user/user-controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { ClientMetadata } from "../websocket/websocket-connection-manager";
2727
import * as fs from "fs/promises";
2828
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
2929
import { GitpodServerImpl } from "../workspace/gitpod-server-impl";
30-
import { WorkspaceStarter } from "../workspace/workspace-starter";
3130
import { StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
3231
import { UserService } from "./user-service";
32+
import { WorkspaceService } from "../workspace/workspace-service";
3333

3434
export const ServerFactory = Symbol("ServerFactory");
3535
export type ServerFactory = () => GitpodServerImpl;
@@ -47,7 +47,7 @@ export class UserController {
4747
@inject(SessionHandler) protected readonly sessionHandler: SessionHandler;
4848
@inject(OneTimeSecretServer) protected readonly otsServer: OneTimeSecretServer;
4949
@inject(OneTimeSecretDB) protected readonly otsDb: OneTimeSecretDB;
50-
@inject(WorkspaceStarter) protected readonly workspaceStarter: WorkspaceStarter;
50+
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
5151
@inject(ServerFactory) private readonly serverFactory: ServerFactory;
5252

5353
get apiRouter(): express.Router {
@@ -241,8 +241,8 @@ export class UserController {
241241
// stop all running workspaces
242242
const user = req.user as User;
243243
if (user) {
244-
this.workspaceStarter
245-
.stopRunningWorkspacesForUser({}, user.id, "logout", StopWorkspacePolicy.NORMALLY)
244+
this.workspaceService
245+
.stopRunningWorkspacesForUser({}, user.id, user.id, "logout", StopWorkspacePolicy.NORMALLY)
246246
.catch((error) =>
247247
log.error(logContext, "cannot stop workspaces on logout", { error, ...logPayload }),
248248
);

components/server/src/user/user-deletion-service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
1212
import { StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
1313
import { AuthProviderService } from "../auth/auth-provider-service";
1414
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";
15-
import { WorkspaceStarter } from "../workspace/workspace-starter";
15+
import { WorkspaceService } from "../workspace/workspace-service";
1616

1717
@injectable()
1818
export class UserDeletionService {
@@ -22,7 +22,7 @@ export class UserDeletionService {
2222
@inject(TeamDB) private readonly teamDb: TeamDB,
2323
@inject(ProjectDB) private readonly projectDb: ProjectDB,
2424
@inject(StorageClient) private readonly storageClient: StorageClient,
25-
@inject(WorkspaceStarter) private readonly workspaceStarter: WorkspaceStarter,
25+
@inject(WorkspaceService) private readonly workspaceService: WorkspaceService,
2626
@inject(AuthProviderService) private readonly authProviderService: AuthProviderService,
2727
@inject(IAnalyticsWriter) private readonly analytics: IAnalyticsWriter,
2828
) {}
@@ -33,7 +33,7 @@ export class UserDeletionService {
3333
* To guarantee that, but also maintain traceability
3434
* we anonymize data that might contain user related/relatable data and keep the entities itself (incl. ids).
3535
*/
36-
async deleteUser(id: string): Promise<void> {
36+
async deleteUser(subjectId: string, id: string): Promise<void> {
3737
const user = await this.db.findUserById(id);
3838
if (!user) {
3939
throw new Error(`No user with id ${id} found!`);
@@ -44,8 +44,9 @@ export class UserDeletionService {
4444
}
4545

4646
// Stop all workspaces
47-
await this.workspaceStarter.stopRunningWorkspacesForUser(
47+
await this.workspaceService.stopRunningWorkspacesForUser(
4848
{},
49+
subjectId,
4950
user.id,
5051
"user deleted",
5152
StopWorkspacePolicy.IMMEDIATELY,

0 commit comments

Comments
 (0)