Skip to content

Commit

Permalink
fix: aws deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jul 18, 2024
1 parent e07d295 commit 1a945c4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
4 changes: 0 additions & 4 deletions action/docs/access-token.owner-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ allowed-repository-permissions: # https://docs.github.com/en/rest/authentication
# environments : write # read or write
# issues : write # read or write
# merge-queues : write # read or write
# metadata : write # read or write
# packages : write # read or write
# pages : write # read or write
# projects : write # read or write or admin
Expand All @@ -31,7 +30,6 @@ allowed-repository-permissions: # https://docs.github.com/en/rest/authentication
# secret-scanning-alerts : write # read or write
# secrets : write # read or write
# security-events : write # read or write
# single-file : write # read or write
# statuses : write # read or write
# team-discussions : write # read or write
# vulnerability-alerts : write # read or write
Expand Down Expand Up @@ -78,7 +76,6 @@ statements:
# environments : write # read or write
# issues : write # read or write
# merge-queues : write # read or write
# metadata : write # read or write
# packages : write # read or write
# pages : write # read or write
# projects : write # read or write or admin
Expand All @@ -89,7 +86,6 @@ statements:
# secret-scanning-alerts : write # read or write
# secrets : write # read or write
# security-events : write # read or write
# single-file : write # read or write
# statuses : write # read or write
# team-discussions : write # read or write
# vulnerability-alerts : write # read or write
Expand Down
2 changes: 0 additions & 2 deletions action/docs/access-token.repo-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ statements:
# environments : write # read or write
# issues : write # read or write
# merge-queues : write # read or write
# metadata : write # read or write
# packages : write # read or write
# pages : write # read or write
# projects : write # read or write or admin
Expand All @@ -52,7 +51,6 @@ statements:
# secret-scanning-alerts : write # read or write
# secrets : write # read or write
# security-events : write # read or write
# single-file : write # read or write
# statuses : write # read or write
# team-discussions : write # read or write
# vulnerability-alerts : write # read or write
Expand Down
4 changes: 3 additions & 1 deletion server/deployments/aws/infrastructure/bin/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as cdk from 'aws-cdk-lib'
import {AppStack} from '../lib/app-stack'
import {DeploymentStack} from '../lib/deployment-stack';

const app = new cdk.App()
new AppStack(app, 'GitHubActionsAccessTokens')
const appStack = new AppStack(app, 'GitHubActionsAccessTokens')
// new DeploymentStack(app, appStack.stackName + '-Deployment')
10 changes: 9 additions & 1 deletion server/deployments/aws/infrastructure/lib/app-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export class AppStack extends Stack {
timeout: Duration.seconds(30),
code: lambda.Code.fromAsset(path.join(__dirname, '../../../../dist')),
environment: {
LOG_LEVEL: 'INFO',
LOG_LEVEL: 'info',
GITHUB_APP_SECRETS_NAME: githubAppSecret.secretName,
GITHUB_ACTIONS_TOKEN_ALLOWED_SUBJECTS: GITHUB_ACTIONS_TOKEN_ALLOWED_SUBJECTS.join(','),
},
})
githubAppSecret.grantRead(httpApiAccessTokenFunction.role!)
new Policy(this, `${httpApiAccessTokenFunction.node.id}RolePolicy`, {
statements: [
new PolicyStatement({
actions: ['lambda:GetFunctionUrlConfig'],
resources: [ httpApiAccessTokenFunction.functionArn ],
})
]
}).attachToRole(httpApiAccessTokenFunction.role!)

// --- add function url
const httpApiAccessTokenFunctionUrl = httpApiAccessTokenFunction.addFunctionUrl({
Expand Down
30 changes: 30 additions & 0 deletions server/deployments/aws/infrastructure/lib/deployment-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Stack, StackProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import {OpenIdConnectPrincipal, OpenIdConnectProvider, Role} from 'aws-cdk-lib/aws-iam';

export class DeploymentStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props)

const githubOidcProvider = OpenIdConnectProvider.fromOpenIdConnectProviderArn(
this, "GithubOpenIdConnectProvider",
`arn:aws:iam::${this.account}:oidc-provider/token.actions.githubusercontent.com`);

new Role(this, 'DeploymentRole', {
roleName: this.stackName,
managedPolicies: [
{managedPolicyArn: 'arn:aws:iam::aws:policy/AdministratorAccess'},
],
assumedBy: new OpenIdConnectPrincipal(githubOidcProvider, {
'StringEquals': {
// Official AWS GitHub Action https://github.com/aws-actions/configure-aws-credentials set audience to `sts.amazonaws.com` by default
// https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services#adding-the-identity-provider-to-aws'
[`${githubOidcProvider.openIdConnectProviderIssuer}:aud`]: 'sts.amazonaws.com',
},
'ForAnyValue:StringLike': {
[`${githubOidcProvider.openIdConnectProviderIssuer}:sub`]: ['repo:JH-JDS/actions--access-token:ref:refs/heads/main'],
}
}),
})
}
}
4 changes: 2 additions & 2 deletions server/deployments/aws/lambda-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const secretsManager = new SecretsManager({region: process.env.AWS_REGION});
const githubAppSecret = await secretsManager.send(new GetSecretValueCommand({
SecretId: process.env.GITHUB_APP_SECRETS_NAME,
})).then((output) => JSON.parse(output.SecretString ?? '{}'));
process.env.GITHUB_ACTIONS_APP_ID = githubAppSecret.appId;
process.env.GITHUB_ACTIONS_APP_PRIVATE_KEY = githubAppSecret.privateKey;
process.env.GITHUB_APP_ID = githubAppSecret.appId;
process.env.GITHUB_APP_PRIVATE_KEY = githubAppSecret.privateKey;

process.env.REQUEST_ID_HEADER = 'x-request-id';

Expand Down
2 changes: 1 addition & 1 deletion server/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pino from 'pino';
import process from 'process';

const logger = pino({
level: process.env.LOG_LEVEL || 'info',
level: process.env.LOG_LEVEL?.toLowerCase() || 'info',
formatters: {
level: (label:string) => ({level: label.toUpperCase()}),
},
Expand Down

0 comments on commit 1a945c4

Please sign in to comment.