-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ubuntu agent and cdn with lambda@edge resource for public access (#…
…134) Signed-off-by: Rishabh Singh <[email protected]>
- Loading branch information
1 parent
6421528
commit 131de5a
Showing
17 changed files
with
791 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Bucket } from '@aws-cdk/aws-s3'; | ||
import { | ||
CloudFrontAllowedMethods, CloudFrontWebDistribution, LambdaEdgeEventType, OriginAccessIdentity, | ||
} from '@aws-cdk/aws-cloudfront'; | ||
import { CanonicalUserPrincipal, PolicyStatement } from '@aws-cdk/aws-iam'; | ||
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs'; | ||
import { Architecture, Runtime } from '@aws-cdk/aws-lambda'; | ||
import { CfnOutput, Duration } from '@aws-cdk/core'; | ||
import { CiCdnStack } from '../ci-cdn-stack'; | ||
|
||
export class ArtifactsPublicAccess { | ||
constructor(stack: CiCdnStack, buildBucketArn: string) { | ||
const buildBucket = Bucket.fromBucketArn(stack, 'artifactBuildBucket', `${buildBucketArn.toString()}`); | ||
|
||
const originAccessIdentity = new OriginAccessIdentity(stack, 'cloudfront-OAI', { | ||
comment: `OAI for ${buildBucket.bucketName}`, | ||
}); | ||
|
||
buildBucket.addToResourcePolicy(new PolicyStatement({ | ||
actions: ['s3:GetObject'], | ||
resources: [buildBucket.arnForObjects('*')], | ||
principals: [new CanonicalUserPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId)], | ||
})); | ||
|
||
const urlRewriter = new NodejsFunction(stack, 'CfUrlRewriter', { | ||
runtime: Runtime.NODEJS_14_X, | ||
entry: `${__dirname}/../../resources/cf-url-rewriter/cf-url-rewriter.ts`, | ||
handler: 'handler', | ||
memorySize: 128, | ||
architecture: Architecture.X86_64, | ||
}); | ||
|
||
const distro = new CloudFrontWebDistribution(stack, 'CloudFrontBuildBucket', { | ||
originConfigs: [ | ||
{ | ||
s3OriginSource: { | ||
s3BucketSource: buildBucket, | ||
originAccessIdentity, | ||
}, | ||
behaviors: [ | ||
{ | ||
isDefaultBehavior: true, | ||
compress: true, | ||
allowedMethods: CloudFrontAllowedMethods.GET_HEAD, | ||
lambdaFunctionAssociations: [{ | ||
eventType: LambdaEdgeEventType.VIEWER_REQUEST, | ||
lambdaFunction: urlRewriter.currentVersion, | ||
}], | ||
defaultTtl: Duration.seconds(300), | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
new CfnOutput(stack, 'BuildDistributionDomainName', { | ||
value: distro.distributionDomainName, | ||
description: 'The domain name where the build artifacts will be available', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Stack } from '@aws-cdk/core'; | ||
import { | ||
ArnPrincipal, Effect, Policy, PolicyStatement, Role, | ||
} from '@aws-cdk/aws-iam'; | ||
import { CiCdnStack } from '../ci-cdn-stack'; | ||
|
||
export interface buildArtifactProps { | ||
mainNodeArn: string, | ||
agentNodeArn: string, | ||
buildBucketArn: string | ||
} | ||
|
||
export class BuildArtifactsPermissions { | ||
private static readonly BUNDLE_ROLE_NAME = 'opensearch-bundle'; | ||
|
||
constructor(stack: CiCdnStack, props: buildArtifactProps) { | ||
const opensearchBundleRole = new Role(stack, BuildArtifactsPermissions.BUNDLE_ROLE_NAME, { | ||
assumedBy: Role.fromRoleArn(stack, 'MainNodeRole', `${props.mainNodeArn}`), | ||
roleName: 'opensearch-bundle', | ||
}); | ||
|
||
opensearchBundleRole.assumeRolePolicy?.addStatements(new PolicyStatement({ | ||
actions: ['sts:AssumeRole'], | ||
principals: [new ArnPrincipal(`${props.agentNodeArn}`)], | ||
})); | ||
|
||
const opensearchBundlePolicies = BuildArtifactsPermissions.getOpensearchBundlePolicies(stack, props.buildBucketArn); | ||
opensearchBundlePolicies.forEach((policy) => { | ||
policy.attachToRole(opensearchBundleRole); | ||
}); | ||
} | ||
|
||
private static getOpensearchBundlePolicies(scope: Stack, s3BucketArn: string): Policy[] { | ||
const policies: Policy[] = []; | ||
const signingPolicy = new Policy(scope, 'signing-policy', { | ||
statements: [ | ||
new PolicyStatement( | ||
{ | ||
actions: ['sts:AssumeRole'], | ||
resources: ['arn:aws:iam::447201093745:role/OpenSearchSignerPGPSigning-ArtifactAccessRole'], | ||
effect: Effect.ALLOW, | ||
}, | ||
), | ||
], | ||
}); | ||
|
||
const openSearchBundlePolicy = new Policy(scope, 'opensearch-bundle-policy', { | ||
statements: [ | ||
new PolicyStatement({ | ||
actions: [ | ||
's3:GetObject*', | ||
's3:ListBucket', | ||
], | ||
resources: [`${s3BucketArn}`], | ||
effect: Effect.ALLOW, | ||
}), | ||
new PolicyStatement( | ||
{ | ||
actions: [ | ||
's3:GetObject*', | ||
's3:ListBucket', | ||
's3:PutObject', | ||
's3:PutObjectLegalHold', | ||
's3:PutObjectRetention', | ||
's3:PutObjectTagging', | ||
's3:PutObjectVersionTagging', | ||
's3:Abort*', | ||
], | ||
resources: [ | ||
`${s3BucketArn}/*/builds/*`, | ||
`${s3BucketArn}/*/shas/*`, | ||
`${s3BucketArn}/*/dist/*`, | ||
`${s3BucketArn}/*/index.json`, | ||
], | ||
effect: Effect.ALLOW, | ||
}, | ||
), | ||
], | ||
}); | ||
policies.push(signingPolicy, openSearchBundlePolicy); | ||
return policies; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { | ||
Construct, Fn, Stack, StackProps, | ||
} from '@aws-cdk/core'; | ||
import { BuildArtifactsPermissions } from './buildArtifacts/build-artifacts-permissions'; | ||
import { ArtifactsPublicAccess } from './buildArtifacts/artifacts-public-access'; | ||
|
||
export class CiCdnStack extends Stack { | ||
constructor(scope: Construct, id: string, props: StackProps) { | ||
super(scope, id, props); | ||
const mainNodeRoleArn = Fn.importValue('mainNodeRoleArn'); | ||
const agentNodeRoleArn = Fn.importValue('agentNodeRoleArn'); | ||
const buildBucketArn = Fn.importValue('buildBucketArn'); | ||
|
||
const buildArtifacts = new BuildArtifactsPermissions(this, { | ||
mainNodeArn: mainNodeRoleArn, | ||
agentNodeArn: agentNodeRoleArn, | ||
buildBucketArn, | ||
}); | ||
|
||
const artifactPublicAccess = new ArtifactsPublicAccess(this, buildBucketArn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.