Skip to content
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

fix(lambda): Prevent scale-up lambda from starting runner for user repo if org level runners is enabled #3909

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions lambdas/functions/control-plane/src/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const body: ActionRequestMessage = {
installationId: 1,
repositoryName: 'name',
repositoryOwner: 'owner',
repoOwnerType: "Organization",
};

const sqsRecord: SQSRecord = {
Expand Down
16 changes: 16 additions & 0 deletions lambdas/functions/control-plane/src/scale-runners/scale-up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ const TEST_DATA: scaleUpModule.ActionRequestMessage = {
repositoryName: 'hello-world',
repositoryOwner: 'Codertocat',
installationId: 2,
repoOwnerType: "Organization",
};

const TEST_DATA_USER_REPO: scaleUpModule.ActionRequestMessage = {
id: 1,
eventType: 'workflow_job',
repositoryName: 'hello-world',
repositoryOwner: 'Octocat',
installationId: 2,
repoOwnerType: "User",
};

// installationId 0 means no installationId is set.
Expand All @@ -62,6 +72,7 @@ const TEST_DATA_WITH_ZERO_INSTALL_ID: scaleUpModule.ActionRequestMessage = {
repositoryName: 'hello-world',
repositoryOwner: 'Codertocat',
installationId: 0,
repoOwnerType: "Organization",
};

const cleanEnv = process.env;
Expand Down Expand Up @@ -305,6 +316,11 @@ describe('scaleUp with GHES', () => {
expect(mockOctokit.paginate).toHaveBeenCalledTimes(1);
});

it('Throws error if it is a User repo and org level runners is enabled', () => {
process.env.ENABLE_ORGANIZATION_RUNNERS = 'true';
expect(() => scaleUpModule.scaleUp('aws:sqs', TEST_DATA_USER_REPO)).rejects.toBeInstanceOf(Error);
Copy link
Member

Choose a reason for hiding this comment

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

Would prefer not to add a global event (module scope) for only a single test. Please can you update the event in the test to set the repoUserType

});

it('create SSM parameter for runner group id if it doesnt exist', async () => {
mockSSMClient.on(GetParameterCommand).rejects();
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
Expand Down
15 changes: 15 additions & 0 deletions lambdas/functions/control-plane/src/scale-runners/scale-up.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Octokit } from '@octokit/rest';

Check notice on line 1 in lambdas/functions/control-plane/src/scale-runners/scale-up.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 4.92 to 4.86, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
import { addPersistentContextToChildLogger, createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
import { getParameter, putParameter } from '@terraform-aws-github-runner/aws-ssm-util';
import yn from 'yn';
Expand Down Expand Up @@ -27,6 +27,7 @@
repositoryName: string;
repositoryOwner: string;
installationId: number;
repoOwnerType: string;
}

interface CreateGitHubRunnerConfig {
Expand Down Expand Up @@ -250,6 +251,9 @@
`Please ensure you have enabled workflow_job events.`,
);
}

validateRepoOwnerTypeIfOrgLevelEnabled(payload, enableOrgLevel);

Check notice on line 256 in lambdas/functions/control-plane/src/scale-runners/scale-up.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

scaleUp already has high cyclomatic complexity, and now it increases in Lines of Code from 107 to 108. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
const ephemeral = ephemeralEnabled && payload.eventType === 'workflow_job';
const runnerType = enableOrgLevel ? 'Org' : 'Repo';
const runnerOwner = enableOrgLevel ? payload.repositoryOwner : `${payload.repositoryOwner}/${payload.repositoryName}`;
Expand Down Expand Up @@ -341,6 +345,17 @@
}
}

function validateRepoOwnerTypeIfOrgLevelEnabled(payload: ActionRequestMessage, enableOrgLevel: boolean) {
if (enableOrgLevel && payload.repoOwnerType !== 'Organization') {
logger.warn(`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.`);
throw Error(
Copy link
Member

Choose a reason for hiding this comment

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

Please do not throw an error, this will send the message back to SQS. And trigger a retry.

`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.`,
);
}
}

function addDelay(instances: string[]) {
const delay = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const ssmParameterStoreMaxThroughput = 40;
Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/sqs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Test sending message to SQS.', () => {
repositoryOwner: 'owner',
queueId: queueUrl,
queueFifo: false,
repoOwnerType: 'Organization',
};

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/sqs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ActionRequestMessage {
installationId: number;
queueId: string;
queueFifo: boolean;
repoOwnerType: string;
}

export interface MatcherConfig {
Expand Down
3 changes: 3 additions & 0 deletions lambdas/functions/webhook/src/webhook/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});
it('Check webhook will accept jobs for latest labels if workflow labels are not specific', async () => {
Expand Down Expand Up @@ -492,6 +493,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});
});
Expand Down Expand Up @@ -531,6 +533,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});

Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/webhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function handleWorkflowJob(
installationId: installationId,
queueId: queue.id,
queueFifo: queue.fifo,
repoOwnerType: body.repository.owner.type,
});
logger.info(`Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`);
return { statusCode: 201 };
Expand Down
Loading