Skip to content

Commit

Permalink
refactor(runner): add runner registration level secret to setup function
Browse files Browse the repository at this point in the history
  • Loading branch information
uid10804 committed Oct 16, 2023
1 parent 50de4cc commit 7c6f65f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/lambda-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GitHubSecrets {
domain: string;
appId: number;
personalAuthToken: string;
runnerLevel: string;
}

const octokitCache: {
Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class GitHubRunners extends Construct implements ec2.IConnectable {
private readonly webhook: GithubWebhookHandler;
private readonly orchestrator: stepfunctions.StateMachine;
private readonly setupUrl: string;
private readonly extraLambdaEnv: {[p: string]: string} = {};
private readonly extraLambdaEnv: { [p: string]: string } = {};
private readonly extraLambdaProps: lambda.FunctionOptions;
private stateMachineLogGroup?: logs.LogGroup;
private jobsCompletedMetricFilters?: logs.MetricFilter[];
Expand Down
19 changes: 1 addition & 18 deletions src/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ export class Secrets extends Construct {
*/
readonly githubPrivateKey: secretsmanager.Secret;


/**
* GitHub runner registration level.
*
* This secret is used to determine if the runner should be registered as an organization runner or a repository runner.
* @default "repo"
*/
readonly githubRunnerRegistrationLevel: secretsmanager.Secret;


/**
* Setup secret used to authenticate user for our setup wizard. Should be empty after setup has been completed.
*/
Expand Down Expand Up @@ -66,6 +56,7 @@ export class Secrets extends Construct {
domain: 'github.com',
appId: '',
personalAuthToken: '',
runnerLevel: 'repo',
}),
generateStringKey: 'dummy',
includeSpace: false,
Expand All @@ -83,14 +74,6 @@ export class Secrets extends Construct {
},
);

this.githubRunnerRegistrationLevel = new secretsmanager.Secret(
this,
'GitHub Runner Registration Level',
{
secretStringValue: cdk.SecretValue.unsafePlainText('repo'),
},
);

this.setup = new secretsmanager.Secret(
this,
'Setup',
Expand Down
17 changes: 10 additions & 7 deletions src/setup.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ async function handleDomain(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewa
if (!body.domain) {
return response(400, 'Invalid domain');
}
if (!body.runnerLevel) {
return response(400, 'Invalid runner regisration level');
}

const githubSecrets: GitHubSecrets = await getSecretJsonValue(process.env.GITHUB_SECRET_ARN);
githubSecrets.domain = body.domain;
githubSecrets.runnerLevel = body.runnerLevel;
await updateSecretValue(process.env.GITHUB_SECRET_ARN, JSON.stringify(githubSecrets));

return response(200, 'Domain set');
}

Expand All @@ -76,18 +79,18 @@ async function handlePat(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewayPr
}));
await updateSecretValue(process.env.SETUP_SECRET_ARN, JSON.stringify({ token: '' }));

return response( 200, 'Personal access token set');
return response(200, 'Personal access token set');
}

async function handleNewApp(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewayProxyResultV2> {
if (!event.queryStringParameters) {
return response( 400, 'Invalid code');
return response(400, 'Invalid code');
}

const code = event.queryStringParameters.code;

if (!code) {
return response( 400, 'Invalid code');
return response(400, 'Invalid code');
}

const githubSecrets: GitHubSecrets = await getSecretJsonValue(process.env.GITHUB_SECRET_ARN);
Expand All @@ -105,14 +108,14 @@ async function handleNewApp(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewa
}));
await updateSecretValue(process.env.SETUP_SECRET_ARN, JSON.stringify({ token: '' }));

return response( 200, `New app set. <a href="${newApp.data.html_url}/installations/new">Install it</a> for your repositories.`);
return response(200, `New app set. <a href="${newApp.data.html_url}/installations/new">Install it</a> for your repositories.`);
}

async function handleExistingApp(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewayProxyResultV2> {
const body = decodeBody(event);

if (!body.appid || !body.pk || !body.domain) {
return response( 400, 'Missing fields');
return response(400, 'Missing fields');
}

await updateSecretValue(process.env.GITHUB_SECRET_ARN, JSON.stringify(<GitHubSecrets>{
Expand All @@ -123,7 +126,7 @@ async function handleExistingApp(event: ApiGatewayEvent): Promise<AWSLambda.APIG
await updateSecretValue(process.env.GITHUB_PRIVATE_KEY_SECRET_ARN, body.pk as string);
await updateSecretValue(process.env.SETUP_SECRET_ARN, JSON.stringify({ token: '' }));

return response( 200, 'Existing app set. Don\'t forget to set up the webhook.');
return response(200, 'Existing app set. Don\'t forget to set up the webhook.');
}

export async function handler(event: ApiGatewayEvent): Promise<AWSLambda.APIGatewayProxyResultV2> {
Expand Down

0 comments on commit 7c6f65f

Please sign in to comment.