Skip to content

[server] Cleanup adminBlockUser RPC #17033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,27 +620,19 @@ export class GitpodServerEEImpl extends GitpodServerImpl {

await this.guardAdminAccess("adminBlockUser", { req }, Permission.ADMIN_USERS);

let targetUser;
try {
targetUser = await this.userService.blockUser(req.id, req.blocked);
} catch (error) {
throw new ResponseError(ErrorCodes.NOT_FOUND, "not found");
}
const targetUser = await this.userService.blockUser(req.id, req.blocked);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to wrap in try, as we throw an API ResponseError from the userService.blockUser call already.


const workspaceDb = this.workspaceDb.trace(ctx);
const workspaces = await workspaceDb.findWorkspacesByUser(req.id);
const isDefined = <T>(x: T | undefined): x is T => x !== undefined;
(await Promise.all(workspaces.map((workspace) => workspaceDb.findRunningInstance(workspace.id))))
.filter(isDefined)
.forEach((instance) =>
this.workspaceStarter.stopWorkspaceInstance(
ctx,
instance.id,
instance.region,
"user blocked by admin",
StopWorkspacePolicy.IMMEDIATELY,
),
);
const stoppedWorkspaces = await this.workspaceStarter.stopRunningWorkspacesForUser(
ctx,
req.id,
"user blocked by admin",
StopWorkspacePolicy.IMMEDIATELY,
);

log.info(`Stopped ${stoppedWorkspaces.length} workspaces in response to admin initiated block.`, {
userId: targetUser.id,
workspaceIds: stoppedWorkspaces.map((w) => w.id),
});

// For some reason, returning the result of `this.userDB.storeUser(target)` does not work. The response never arrives the caller.
// Returning `target` instead (which should be equivalent).
Expand Down
2 changes: 1 addition & 1 deletion components/server/src/user/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class UserService {
async blockUser(targetUserId: string, block: boolean): Promise<User> {
const target = await this.userDb.findUserById(targetUserId);
if (!target) {
throw new Error("Not found.");
throw new ResponseError(ErrorCodes.NOT_FOUND, "not found");
}

target.blocked = !!block;
Expand Down
22 changes: 22 additions & 0 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,28 @@ export class WorkspaceStarter {
await client.stopWorkspace(ctx, req);
}

public async stopRunningWorkspacesForUser(
ctx: TraceContext,
userID: string,
reason: string,
policy?: StopWorkspacePolicy,
): Promise<Workspace[]> {
const workspaceDb = this.workspaceDb.trace(ctx);
const instances = await workspaceDb.findRunningInstancesWithWorkspaces(undefined, userID);
await Promise.all(
instances.map((instance) =>
this.stopWorkspaceInstance(
ctx,
instance.latestInstance.id,
instance.latestInstance.region,
reason,
policy,
),
),
);
return instances.map((instance) => instance.workspace);
}

protected async checkBlockedRepository(user: User, contextURL: string) {
const blockedRepository = await this.blockedRepositoryDB.findBlockedRepositoryByURL(contextURL);
if (!blockedRepository) return;
Expand Down