Skip to content

Commit 40ca406

Browse files
authored
Feature/Dpra-91 (#40)
* adding the role permissions for tagging * adding the functional to use external repository via codeconnection * updating readme docs with the new changes * updating readme docs with indentation fixes * removing unnecessary comments * removing the unnecesary changes * updating xml file to make configuration changes * updating the trivy scan checks failures * bug fixes and updates with policy changes * packages requirement updates * updating the pipeline with new changes and version fixes * updating the readme with new changes and functionality * updating the readme with iamge fixes * updating the readme with image fixes * updating the account.ts file to fix the changes with security scan issues * finxing the comments
1 parent 1f8a2ba commit 40ca406

File tree

19 files changed

+6196
-4238
lines changed

19 files changed

+6196
-4238
lines changed

examples/cdk-application-pipeline/.gitignore

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cdk-application-pipeline/README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,35 @@ To learn more about the CDK boostrapping process, see: https://docs.aws.amazon.c
105105
To deploy the pipeline to the toolchain AWS account run:
106106

107107
```bash
108-
npx cdk deploy --profile toolchain --all --require-approval never
108+
npx ts-node infrastructure/src/setup.ts
109109
```
110+
Make a selection for your source:
111+
1. CodeCommit
112+
2. Github
113+
3. BitBucket
114+
4. Github Enterprise Server
110115

111-
![Pipeline-1 Diagram](docs/pipeline-1.png)
116+
![Version Control System Source](docs/SelectSource.png)
112117

113-
Using AWS management console, login to `toolchain` account and click [AWS CodePipeline](https://us-east-1.console.aws.amazon.com/codesuite/codepipeline/home?region=us-east-1) to check the different stages of the pipeline.
118+
If you choose CodeCommit as the source, no additional inputs required.
119+
120+
If you choose Github/BitBucket/GithubEnterpiseServer as source, then provide following parameters as asked:
121+
1. profile
122+
2. owner
123+
3. repositoryName
124+
4. branchName
125+
126+
![Version Control System Source](docs/parametersExternalSource.png)
127+
128+
Once the parameters are setup it will prompt you to update to codeconnection in the console.
129+
130+
![Updating CodeConnection](docs/updatingCodeconnection.png)
131+
132+
To connect the external suorce to the AWS account, a codeconnetion is setup and it is in the pending status.
133+
134+
Log into the console, search for Codepipeline in the search bar, in the left panel go to setting: go to codeconnections.
135+
136+
There you can see the codeconnection starting with (dpri-****), it is in pending status, Click on Update Pending Connection status and login with the credentials.
114137

115138
![Fruit API Diagram](docs/fruit-api.png)
116139

@@ -138,7 +161,7 @@ Here is the application running in production in us-east-1 region.
138161

139162
![App Diagram](docs/app-1.png)
140163

141-
(OPTIONAL) If you'd like to make changes and deploy with the pipeline, you'll need to [setup Git for AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up.html) and then clone the new CodeCommit repository:
164+
(OPTIONAL) If you'd like to make changes and deploy with the pipeline, you'll need to [setup Git for AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up.html) and then clone the new CodeCommit repository or your Git Repository configure during setup:
142165

143166
```bash
144167
git clone https://git-codecommit.us-west-2.amazonaws.com/v1/repos/fruit-api

examples/cdk-application-pipeline/cdk.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
143 KB
Loading
121 KB
Loading
274 KB
Loading

examples/cdk-application-pipeline/infrastructure/src/accounts.ts

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,112 @@ export interface Account {
55
profile: string;
66
}
77

8+
export enum AccountType {
9+
Toolchain = 'toolchain',
10+
Beta = 'beta',
11+
Gamma = 'gamma',
12+
Production = 'production',
13+
}
814
export class Accounts {
915
static readonly PATH = '.accounts.json';
1016

1117
static load(): Accounts {
1218
try {
13-
return Object.assign(new Accounts(), JSON.parse(fs.readFileSync(Accounts.PATH).toString()));
19+
const accounts = new Accounts();
20+
const jsonData = JSON.parse(fs.readFileSync(Accounts.PATH).toString());
21+
22+
// Explicitly assign only known properties with type checking
23+
// if (jsonData.toolchain) accounts.setAccount('toolchain', jsonData.toolchain);
24+
// if (jsonData.beta) accounts.setAccount('beta', jsonData.beta);
25+
// if (jsonData.gamma) accounts.setAccount('gamma', jsonData.gamma);
26+
// if (jsonData.production) accounts.setAccount('production', jsonData.production);
27+
28+
if (jsonData.toolchain) accounts.setAccount(AccountType.Toolchain, jsonData.toolchain);
29+
if (jsonData.beta) accounts.setAccount(AccountType.Beta, jsonData.beta);
30+
if (jsonData.gamma) accounts.setAccount(AccountType.Gamma, jsonData.gamma);
31+
if (jsonData.production) accounts.setAccount(AccountType.Production, jsonData.production);
32+
33+
return accounts;
1434
} catch (e) {
1535
return new Accounts();
1636
}
1737
}
1838

19-
toolchain?: Account;
20-
beta?: Account;
21-
gamma?: Account;
22-
production?: Account;
39+
_toolchain?: Account;
40+
_beta?: Account;
41+
_gamma?: Account;
42+
_production?: Account;
43+
44+
45+
get toolchain(): Account | undefined {
46+
return this._toolchain;
47+
}
48+
get beta(): Account | undefined {
49+
return this._beta;
50+
}
51+
get gamma(): Account | undefined {
52+
return this._toolchain;
53+
}
54+
get production(): Account | undefined {
55+
return this._production;
56+
}
57+
// Added setters
58+
set toolchain(value: Account | undefined) {
59+
this._toolchain = value;
60+
}
61+
62+
set beta(value: Account | undefined) {
63+
this._beta = value;
64+
}
65+
66+
set gamma(value: Account | undefined) {
67+
this._gamma = value;
68+
}
69+
70+
set production(value: Account | undefined) {
71+
this._production = value;
72+
}
73+
// Setter method with validation
74+
private setAccount(type: AccountType, account: unknown): void {
75+
if (!this.isValidAccount(account)) {
76+
throw new Error(`Invalid account data for ${type}`);
77+
}
78+
79+
switch (type) {
80+
case AccountType.Toolchain:
81+
this.toolchain=account;
82+
break;
83+
case AccountType.Beta:
84+
this.beta=account;
85+
break;
86+
case AccountType.Gamma:
87+
this.gamma=account;
88+
break;
89+
case AccountType.Production:
90+
this.production=account;
91+
break;
92+
}
93+
}
94+
95+
// Type guard to validate account structure
96+
private isValidAccount(account: unknown): account is Account {
97+
return (
98+
typeof account === 'object' &&
99+
account !== null &&
100+
'accountId' in account &&
101+
'profile' in account &&
102+
typeof (account as Account).accountId === 'string' &&
103+
typeof (account as Account).profile === 'string'
104+
);
105+
}
23106

24107
store() {
25-
fs.writeFileSync(Accounts.PATH, JSON.stringify(this, null, 2));
108+
const safeObject = {
109+
toolchain: this.toolchain,
110+
beta: this.beta,
111+
gamma: this.gamma,
112+
production: this.production
113+
};
114+
fs.writeFileSync(Accounts.PATH, JSON.stringify(safeObject, null, 2));
26115
}
27-
}
116+
}

examples/cdk-application-pipeline/infrastructure/src/codeguru-review-check/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class CodeGuruReviewCheck extends Step implements ICodePipelineActionFact
9797
public produceAction(stage: IStage, options: ProduceActionOptions): CodePipelineActionFactoryResult {
9898
const codeGuruLambda = new NodejsFunction(stage.pipeline, `${this.id}CodeGuruLambda`, {
9999
timeout: Duration.seconds(60),
100-
runtime: Runtime.NODEJS_18_X,
100+
runtime: Runtime.NODEJS_22_X,
101101
handler: 'main',
102102
entry: path.join(__dirname, 'lambda/index.ts'),
103103
});

examples/cdk-application-pipeline/infrastructure/src/dpra_bootstrap_template.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ Resources:
198198
Type: AWS::ECR::Repository
199199
Properties:
200200
ImageTagMutability: IMMUTABLE
201+
ImageScanningConfiguration:
202+
ScanOnPush: true
201203
RepositoryName:
202204
Fn::If:
203205
- HasCustomContainerAssetsRepositoryName
@@ -491,6 +493,7 @@ Resources:
491493
- cloudformation:GetTemplate
492494
- cloudformation:ListStackResources
493495
- cloudformation:ListStacks
496+
- cloudformation:TagResource
494497
- cloudtrail:DescribeTrails
495498
- cloudtrail:ListTags
496499
- cloudtrail:LookupEvents
@@ -512,9 +515,11 @@ Resources:
512515
- codecommit:TagResource
513516
- codecommit:UnTagResource
514517
- codedeploy:CreateApplication
518+
- codedeploy:GetApplication
515519
- codedeploy:*DeploymentGroup
516520
- codedeploy:DeleteApplication
517521
- codedeploy:GetDeployment*
522+
- codedeploy:TagResource
518523
- codeguru-reviewer:*TagResource
519524
- codeguru-reviewer:AssociateRepository
520525
- codeguru-reviewer:CreateCodeReview
@@ -527,6 +532,9 @@ Resources:
527532
- codepipeline:StartPipelineExecution
528533
- codepipeline:GetPipeline
529534
- codepipeline:GetPipelineState
535+
- codepipeline:Tagrole
536+
- codepipeline:Tagresource
537+
- codestar-connections:PassConnection
530538
- ec2:*Address
531539
- ec2:RevokeSecurityGroupIngress
532540
- ec2:*Tags
@@ -572,6 +580,7 @@ Resources:
572580
- ecs:Describe*
573581
- ecs:DeregisterTaskDefinition
574582
- ecs:RegisterTaskDefinition
583+
- ecs:TagResource
575584
- elasticloadbalancing:AddTags
576585
- elasticloadbalancing:CreateListener
577586
- elasticloadbalancing:CreateLoadBalancer
@@ -612,6 +621,7 @@ Resources:
612621
- iam:ListUsers
613622
- iam:PassRole
614623
- iam:PutRolePolicy
624+
- iam:TagRole
615625
- kms:CancelKeyDeletion
616626
- kms:Create*
617627
- kms:Decrypt
@@ -640,6 +650,7 @@ Resources:
640650
- lambda:ListFunctions
641651
- lambda:PublishVersion
642652
- lambda:TagResource
653+
- lambda:UpdateFunctionCode
643654
- lambda:UpdateFunctionConfiguration
644655
- logs:Tag*
645656
- logs:PutRetentionPolicy
@@ -649,8 +660,11 @@ Resources:
649660
- ram:ListResources
650661
- rds:AddTagsToResource
651662
- rds:CreateDBCluster
663+
- rds:CreateDBInstance
664+
- rds:DescribeDBInstances
652665
- rds:CreateDBSubnetGroup
653666
- rds:DeleteDBCluster
667+
- rds:DeleteDBInstance
654668
- rds:DeleteDBSubnetGroup
655669
- rds:DescribeDBClusters
656670
- rds:DescribeDBSubnetGroups

examples/cdk-application-pipeline/infrastructure/src/codecommit-source/index.ts renamed to examples/cdk-application-pipeline/infrastructure/src/pipeline-source/index.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'fs';
2-
import { IgnoreMode } from 'aws-cdk-lib';
2+
import { IgnoreMode, Stack } from 'aws-cdk-lib';
33
import { Code, Repository } from 'aws-cdk-lib/aws-codecommit';
44
import { CfnRepositoryAssociation } from 'aws-cdk-lib/aws-codegurureviewer';
55
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
@@ -46,5 +46,32 @@ export class CodeCommitSource extends Construct {
4646
this.codePipelineSource = CodePipelineSource.codeCommit(this.repository, this.trunkBranchName);
4747
}
4848
}
49+
export class CodePipelineSourceFactory{
50+
static createCodePipelineSource(pipelineStack: Stack){
51+
const providerType = pipelineStack.node.tryGetContext("providerType")==undefined?"codecommit":pipelineStack.node.tryGetContext("providerType");
4952

53+
switch(providerType){
54+
case 'codecommit':
55+
const appName = pipelineStack.node.tryGetContext('appName')
56+
if(!appName){
57+
throw new Error('appName is required')
58+
}
59+
// CodeCommitSource is an instance of Construct
60+
return new CodeCommitSource(pipelineStack, 'Source', {repositoryName: appName}).codePipelineSource
61+
default:
62+
const repoParameters = {
63+
"owner": pipelineStack.node.tryGetContext('owner'),
64+
"repositoryName": pipelineStack.node.tryGetContext('repositoryName'),
65+
"trunkBranchName": pipelineStack.node.tryGetContext('trunkBranchName'),
66+
"connectionArn": pipelineStack.node.tryGetContext('connectionArn'),
67+
};
5068

69+
// CodePipelineSource is not an instance of Construct
70+
return CodePipelineSource.connection(
71+
`${repoParameters.owner}/${repoParameters.repositoryName}`,
72+
repoParameters.trunkBranchName ?? 'main',
73+
{ connectionArn: repoParameters.connectionArn }
74+
)
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)