Skip to content

Commit c2efa41

Browse files
committed
[server] WorkspaceService.getOwnerToken
1 parent 3755662 commit c2efa41

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
875875
traceAPIParams(ctx, { workspaceId });
876876
traceWI(ctx, { workspaceId });
877877

878-
await this.checkAndBlockUser("getOwnerToken");
878+
const user = await this.checkAndBlockUser("getOwnerToken");
879879

880880
const workspace = await this.workspaceDb.trace(ctx).findById(workspaceId);
881881
if (!workspace) {
@@ -886,11 +886,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
886886
const latestInstance = await this.workspaceDb.trace(ctx).findCurrentInstance(workspaceId);
887887
await this.guardAccess({ kind: "workspaceInstance", subject: latestInstance, workspace }, "get");
888888

889-
const ownerToken = latestInstance?.status.ownerToken;
890-
if (!ownerToken) {
891-
throw new Error("owner token not found");
892-
}
893-
return ownerToken;
889+
return await this.workspaceService.getOwnerToken(user.id, workspaceId);
894890
}
895891

896892
public async getIDECredentials(ctx: TraceContext, workspaceId: string): Promise<string> {
@@ -3261,7 +3257,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
32613257

32623258
async adminForceStopWorkspace(ctx: TraceContext, workspaceId: string): Promise<void> {
32633259
traceAPIParams(ctx, { workspaceId });
3264-
3260+
32653261
const admin = await this.guardAdminAccess(
32663262
"adminForceStopWorkspace",
32673263
{ id: workspaceId },

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ describe("WorkspaceService", async () => {
9898
await expectError(ErrorCodes.NOT_FOUND, () => svc.getWorkspace(stranger.id, ws.id));
9999
});
100100

101+
it("should getOwnerToken", async () => {
102+
const svc = container.get(WorkspaceService);
103+
const ws = await createTestWorkspace(svc, org, owner, project);
104+
105+
await expectError(
106+
ErrorCodes.NOT_FOUND,
107+
() => svc.getWorkspace(owner.id, ws.id),
108+
"NOT_FOUND for non-running workspace",
109+
);
110+
111+
await expectError(
112+
ErrorCodes.NOT_FOUND,
113+
() => svc.getWorkspace(stranger.id, ws.id),
114+
"NOT_FOUND if stranger asks for the owner token",
115+
);
116+
});
117+
101118
it("should stopWorkspace", async () => {
102119
const svc = container.get(WorkspaceService);
103120
const ws = await createTestWorkspace(svc, org, owner, project);
@@ -144,7 +161,7 @@ describe("WorkspaceService", async () => {
144161
);
145162
});
146163

147-
it("should dhardDeleteWorkspace", async () => {
164+
it("should hardDeleteWorkspace", async () => {
148165
const svc = container.get(WorkspaceService);
149166
const ws = await createTestWorkspace(svc, org, owner, project);
150167

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ export class WorkspaceService {
6161
async getWorkspace(userId: string, workspaceId: string): Promise<Workspace> {
6262
await this.auth.checkPermissionOnWorkspace(userId, "access", workspaceId);
6363

64-
const workspace = await this.db.findById(workspaceId);
65-
if (!workspace || !!workspace.softDeleted || workspace.deleted) {
66-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Workspace not found.");
64+
return this.doGetWorkspace(workspaceId);
65+
}
66+
67+
async getOwnerToken(userId: string, workspaceId: string): Promise<string> {
68+
await this.auth.checkPermissionOnWorkspace(userId, "access", workspaceId);
69+
70+
// Check: is deleted?
71+
await this.doGetWorkspace(workspaceId);
72+
73+
const latestInstance = await this.db.findCurrentInstance(workspaceId);
74+
const ownerToken = latestInstance?.status.ownerToken;
75+
if (!ownerToken) {
76+
throw new ApplicationError(ErrorCodes.NOT_FOUND, "owner token not found");
6777
}
68-
return workspace;
78+
return ownerToken;
6979
}
7080

7181
async stopWorkspace(
@@ -76,7 +86,7 @@ export class WorkspaceService {
7686
): Promise<void> {
7787
await this.auth.checkPermissionOnWorkspace(userId, "stop", workspaceId);
7888

79-
const workspace = await this.getWorkspace(userId, workspaceId);
89+
const workspace = await this.doGetWorkspace(workspaceId);
8090
const instance = await this.db.findRunningInstance(workspace.id);
8191
if (!instance) {
8292
// there's no instance running - we're done
@@ -139,4 +149,12 @@ export class WorkspaceService {
139149
}
140150
log.info(`Purged Workspace ${workspaceId} and all WorkspaceInstances for this workspace`, { workspaceId });
141151
}
152+
153+
private async doGetWorkspace(workspaceId: string): Promise<Workspace> {
154+
const workspace = await this.db.findById(workspaceId);
155+
if (!workspace || !!workspace.softDeleted || workspace.deleted) {
156+
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Workspace not found.");
157+
}
158+
return workspace;
159+
}
142160
}

0 commit comments

Comments
 (0)