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

feat: store github secrets in secret manager, not in insecure output #19

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 9 additions & 11 deletions dist/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubDeployStack = void 0;
const cdk = __importStar(require("aws-cdk-lib"));
const iam = __importStar(require("aws-cdk-lib/aws-iam"));
const secretsmanager = __importStar(require("aws-cdk-lib/aws-secretsmanager"));
/**
* Stack to generate a GitHub deployer, along with key and secret for loading into GitHub secrets
*/
Expand Down Expand Up @@ -54,17 +55,14 @@ class GithubDeployStack extends cdk.NestedStack {
this.githubActionsUserAccessKey = new iam.CfnAccessKey(this, 'GithubActionsUserAccessKey', {
userName: githubDeployUser.userName,
});
new cdk.CfnOutput(this, 'StackName', {
description: 'Stack name.',
value: this.stackName,
});
new cdk.CfnOutput(this, 'GithubUserAccessKeyID', {
description: `Value of AWS_ACCESS_KEY_ID for github secrets`,
value: this.githubActionsUserAccessKey.ref,
});
new cdk.CfnOutput(this, 'GithubUserSecretAccessKey', {
description: `Value of AWS_SECRET_ACCESS_KEY for github secrets`,
value: this.githubActionsUserAccessKey.attrSecretAccessKey,
// Store key in secret manager
new secretsmanager.Secret(this, 'GithubActionsUserSecret', {
secretName: 'bootstrap/github',
secretStringValue: cdk.SecretValue.unsafePlainText(JSON.stringify({
AWS_USER_NAME: this.githubActionsUserAccessKey.userName,
AWS_ACCESS_KEY_ID: this.githubActionsUserAccessKey.ref,
AWS_SECRET_ACCESS_KEY: this.githubActionsUserAccessKey.attrSecretAccessKey,
})),
});
}
}
Expand Down
32 changes: 18 additions & 14 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Construct } from 'constructs'
import * as cdk from 'aws-cdk-lib'
import * as iam from 'aws-cdk-lib/aws-iam'
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager'

export interface GithubDeployStackProps extends cdk.NestedStackProps {
secretName?: string
}

/**
* Stack to generate a GitHub deployer, along with key and secret for loading into GitHub secrets
*/
export class GithubDeployStack extends cdk.NestedStack {
public readonly githubActionsUserAccessKey: iam.CfnAccessKey

constructor(scope: Construct, id: string, props: cdk.NestedStackProps) {
constructor(scope: Construct, id: string, props: GithubDeployStackProps) {
super(scope, id, props)
const { secretName = 'bootstrap/github' } = props || {}

// Create IAM user
const githubDeployUser = new iam.User(this, 'GithubDeployUser', {
Expand Down Expand Up @@ -43,19 +49,17 @@ export class GithubDeployStack extends cdk.NestedStack {
},
)

new cdk.CfnOutput(this, 'StackName', {
description: 'Stack name.',
value: this.stackName,
})

new cdk.CfnOutput(this, 'GithubUserAccessKeyID', {
description: `Value of AWS_ACCESS_KEY_ID for github secrets`,
value: this.githubActionsUserAccessKey.ref,
})

new cdk.CfnOutput(this, 'GithubUserSecretAccessKey', {
description: `Value of AWS_SECRET_ACCESS_KEY for github secrets`,
value: this.githubActionsUserAccessKey.attrSecretAccessKey,
// Store key in secret manager
new secretsmanager.Secret(this, 'GithubActionsUserSecret', {
secretName,
secretStringValue: cdk.SecretValue.unsafePlainText(
JSON.stringify({
AWS_USER_NAME: this.githubActionsUserAccessKey.userName,
AWS_ACCESS_KEY_ID: this.githubActionsUserAccessKey.ref,
AWS_SECRET_ACCESS_KEY:
this.githubActionsUserAccessKey.attrSecretAccessKey,
}),
),
})
}
}
Loading