Skip to content

Commit

Permalink
SubjectId: Address mismatch (I) (#19144)
Browse files Browse the repository at this point in the history
* [workspace] Use runWithRequestContext in reconcileWorkspaceStart

* [probot] Run prebuild webhook with correct subjectId

* [prebuods] Mopre SubjectId changes...
  • Loading branch information
geropl authored Nov 28, 2023
1 parent dfb3dc2 commit 5fc4561
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 248 deletions.
67 changes: 38 additions & 29 deletions components/server/src/prebuilds/bitbucket-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { UserService } from "../user/user-service";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { URL } from "url";
import { ProjectsService } from "../projects/projects-service";
import { SubjectId } from "../auth/subject-id";
import { runWithSubjectId } from "../util/request-context";
import { SYSTEM_USER, SYSTEM_USER_ID } from "../authorization/authorizer";

@injectable()
export class BitbucketApp {
Expand Down Expand Up @@ -120,7 +123,9 @@ export class BitbucketApp {
const span = TraceContext.startSpan("Bitbucket.handlePushHook", ctx);
try {
const cloneURL = data.gitCloneUrl;
const projects = await this.projectService.findProjectsByCloneUrl(user.id, cloneURL);
const projects = await runWithSubjectId(SYSTEM_USER, () =>
this.projectService.findProjectsByCloneUrl(SYSTEM_USER_ID, cloneURL),
);
for (const project of projects) {
try {
const projectOwner = await this.findProjectOwner(project, user);
Expand Down Expand Up @@ -151,34 +156,36 @@ export class BitbucketApp {
continue;
}

log.info("Starting prebuild.", { contextURL });
const { host, owner, repo } = RepoURL.parseRepoUrl(data.repoUrl)!;
const hostCtx = this.hostCtxProvider.get(host);
let commitInfo: CommitInfo | undefined;
if (hostCtx?.services?.repositoryProvider) {
commitInfo = await hostCtx.services.repositoryProvider.getCommitInfo(
user,
owner,
repo,
data.commitHash,
await runWithSubjectId(SubjectId.fromUserId(projectOwner.id), async () => {
log.info("Starting prebuild.", { contextURL });
const { host, owner, repo } = RepoURL.parseRepoUrl(data.repoUrl)!;
const hostCtx = this.hostCtxProvider.get(host);
let commitInfo: CommitInfo | undefined;
if (hostCtx?.services?.repositoryProvider) {
commitInfo = await hostCtx.services.repositoryProvider.getCommitInfo(
user,
owner,
repo,
data.commitHash,
);
}
const ws = await this.prebuildManager.startPrebuild(
{ span },
{
user: projectOwner,
project,
context,
commitInfo,
},
);
}
const ws = await this.prebuildManager.startPrebuild(
{ span },
{
user: projectOwner,
project,
context,
commitInfo,
},
);
if (!ws.done) {
await this.webhookEvents.updateEvent(event.id, {
prebuildStatus: "prebuild_triggered",
status: "processed",
prebuildId: ws.prebuildId,
});
}
if (!ws.done) {
await this.webhookEvents.updateEvent(event.id, {
prebuildStatus: "prebuild_triggered",
status: "processed",
prebuildId: ws.prebuildId,
});
}
});
} catch (error) {
log.error("Error processing Bitbucket Server webhook event", error);
}
Expand Down Expand Up @@ -218,7 +225,9 @@ export class BitbucketApp {
const hostContext = this.hostCtxProvider.get(new URL(project.cloneUrl).host);
const authProviderId = hostContext?.authProvider.authProviderId;
for (const teamMember of teamMembers) {
const user = await this.userService.findUserById(teamMember.userId, teamMember.userId);
const user = await runWithSubjectId(SubjectId.fromUserId(teamMember.userId), () =>
this.userService.findUserById(teamMember.userId, teamMember.userId),
);
if (user && user.identities.some((i) => i.authProviderId === authProviderId)) {
return user;
}
Expand Down
47 changes: 28 additions & 19 deletions components/server/src/prebuilds/bitbucket-server-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { UserService } from "../user/user-service";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { URL } from "url";
import { ProjectsService } from "../projects/projects-service";
import { SubjectId } from "../auth/subject-id";
import { runWithSubjectId } from "../util/request-context";
import { SYSTEM_USER, SYSTEM_USER_ID } from "../authorization/authorizer";

@injectable()
export class BitbucketServerApp {
Expand Down Expand Up @@ -117,7 +120,9 @@ export class BitbucketServerApp {
const span = TraceContext.startSpan("Bitbucket.handlePushHook", ctx);
try {
const cloneURL = this.getCloneUrl(payload);
const projects = await this.projectService.findProjectsByCloneUrl(user.id, cloneURL);
const projects = await runWithSubjectId(SYSTEM_USER, () =>
this.projectService.findProjectsByCloneUrl(SYSTEM_USER_ID, cloneURL),
);
for (const project of projects) {
try {
const projectOwner = await this.findProjectOwner(project, user);
Expand Down Expand Up @@ -155,23 +160,25 @@ export class BitbucketServerApp {

log.debug("Bitbucket Server push event: Starting prebuild.", { contextUrl });

const commitInfo = await this.getCommitInfo(user, cloneURL, commit);
const ws = await this.prebuildManager.startPrebuild(
{ span },
{
user: projectOwner,
project: project,
context,
commitInfo,
},
);
if (!ws.done) {
await this.webhookEvents.updateEvent(event.id, {
prebuildStatus: "prebuild_triggered",
status: "processed",
prebuildId: ws.prebuildId,
});
}
await runWithSubjectId(SubjectId.fromUserId(projectOwner.id), async () => {
const commitInfo = await this.getCommitInfo(user, cloneURL, commit);
const ws = await this.prebuildManager.startPrebuild(
{ span },
{
user: projectOwner,
project: project,
context,
commitInfo,
},
);
if (!ws.done) {
await this.webhookEvents.updateEvent(event.id, {
prebuildStatus: "prebuild_triggered",
status: "processed",
prebuildId: ws.prebuildId,
});
}
});
} catch (error) {
log.error("Error processing Bitbucket Server webhook event", error);
}
Expand Down Expand Up @@ -214,7 +221,9 @@ export class BitbucketServerApp {
const hostContext = this.hostCtxProvider.get(new URL(project.cloneUrl).host);
const authProviderId = hostContext?.authProvider.authProviderId;
for (const teamMember of teamMembers) {
const user = await this.userService.findUserById(webhookInstaller.id, teamMember.userId);
const user = await runWithSubjectId(SubjectId.fromUserId(webhookInstaller.id), () =>
this.userService.findUserById(webhookInstaller.id, teamMember.userId),
);
if (user && user.identities.some((i) => i.authProviderId === authProviderId)) {
return user;
}
Expand Down
Loading

0 comments on commit 5fc4561

Please sign in to comment.