Skip to content

Commit 9b89fe8

Browse files
committed
[server] WorkspaceService.getIDECredentials
1 parent c2efa41 commit 9b89fe8

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
897897

898898
const workspace = await this.workspaceService.getWorkspace(user.id, workspaceId);
899899
await this.guardAccess({ kind: "workspace", subject: workspace }, "get");
900-
if (workspace.config.ideCredentials) {
901-
return workspace.config.ideCredentials;
902-
}
903-
return this.workspaceDb.trace(ctx).transaction(async (db) => {
904-
const ws = await this.workspaceService.getWorkspace(user.id, workspaceId);
905-
ws.config.ideCredentials = crypto.randomBytes(32).toString("base64");
906-
await db.store(ws);
907-
return ws.config.ideCredentials;
908-
});
900+
901+
return await this.workspaceService.getIDECredentials(user.id, workspaceId);
909902
}
910903

911904
public async startWorkspace(

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ describe("WorkspaceService", async () => {
115115
);
116116
});
117117

118+
it("should getIDECredentials", async () => {
119+
const svc = container.get(WorkspaceService);
120+
const ws = await createTestWorkspace(svc, org, owner, project);
121+
122+
const ideCredentials = await svc.getIDECredentials(owner.id, ws.id);
123+
expect(ideCredentials, "IDE credentials should be present").to.not.be.undefined;
124+
125+
await expectError(
126+
ErrorCodes.NOT_FOUND,
127+
() => svc.getWorkspace(stranger.id, ws.id),
128+
"NOT_FOUND if stranger asks for the IDE credentials",
129+
);
130+
});
131+
118132
it("should stopWorkspace", async () => {
119133
const svc = container.get(WorkspaceService);
120134
const ws = await createTestWorkspace(svc, org, owner, project);

components/server/src/workspace/workspace-service.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { WorkspaceFactory } from "./workspace-factory";
1414
import { StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
1515
import { WorkspaceStarter } from "./workspace-starter";
1616
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
17+
import * as crypto from "crypto";
1718

1819
@injectable()
1920
export class WorkspaceService {
@@ -78,6 +79,21 @@ export class WorkspaceService {
7879
return ownerToken;
7980
}
8081

82+
async getIDECredentials(userId: string, workspaceId: string): Promise<string> {
83+
await this.auth.checkPermissionOnWorkspace(userId, "access", workspaceId);
84+
85+
const workspace = await this.doGetWorkspace(workspaceId);
86+
if (workspace.config.ideCredentials) {
87+
return workspace.config.ideCredentials;
88+
}
89+
return this.db.transaction(async (db) => {
90+
const ws = await this.doGetWorkspace(workspaceId, db);
91+
ws.config.ideCredentials = crypto.randomBytes(32).toString("base64");
92+
await db.store(ws);
93+
return ws.config.ideCredentials;
94+
});
95+
}
96+
8197
async stopWorkspace(
8298
userId: string,
8399
workspaceId: string,
@@ -150,8 +166,8 @@ export class WorkspaceService {
150166
log.info(`Purged Workspace ${workspaceId} and all WorkspaceInstances for this workspace`, { workspaceId });
151167
}
152168

153-
private async doGetWorkspace(workspaceId: string): Promise<Workspace> {
154-
const workspace = await this.db.findById(workspaceId);
169+
private async doGetWorkspace(workspaceId: string, workspaceDB?: WorkspaceDB): Promise<Workspace> {
170+
const workspace = await (workspaceDB || this.db).findById(workspaceId);
155171
if (!workspace || !!workspace.softDeleted || workspace.deleted) {
156172
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Workspace not found.");
157173
}

0 commit comments

Comments
 (0)