|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +import { Bucket } from '@aws-cdk/aws-s3'; |
| 10 | +import { |
| 11 | + CloudFrontAllowedMethods, CloudFrontWebDistribution, LambdaEdgeEventType, OriginAccessIdentity, |
| 12 | +} from '@aws-cdk/aws-cloudfront'; |
| 13 | +import { CanonicalUserPrincipal, PolicyStatement } from '@aws-cdk/aws-iam'; |
| 14 | +import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs'; |
| 15 | +import { Architecture, Runtime } from '@aws-cdk/aws-lambda'; |
| 16 | +import { CfnOutput, Duration } from '@aws-cdk/core'; |
| 17 | +import { CiCdnStack } from '../ci-cdn-stack'; |
| 18 | + |
| 19 | +export class ArtifactsPublicAccess { |
| 20 | + constructor(stack: CiCdnStack, buildBucketArn: string) { |
| 21 | + const buildBucket = Bucket.fromBucketArn(stack, 'artifactBuildBucket', `${buildBucketArn.toString()}`); |
| 22 | + |
| 23 | + const originAccessIdentity = new OriginAccessIdentity(stack, 'cloudfront-OAI', { |
| 24 | + comment: `OAI for ${buildBucket.bucketName}`, |
| 25 | + }); |
| 26 | + |
| 27 | + buildBucket.addToResourcePolicy(new PolicyStatement({ |
| 28 | + actions: ['s3:GetObject'], |
| 29 | + resources: [buildBucket.arnForObjects('*')], |
| 30 | + principals: [new CanonicalUserPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId)], |
| 31 | + })); |
| 32 | + |
| 33 | + const urlRewriter = new NodejsFunction(stack, 'CfUrlRewriter', { |
| 34 | + runtime: Runtime.NODEJS_14_X, |
| 35 | + entry: `${__dirname}/../../resources/cf-url-rewriter/cf-url-rewriter.ts`, |
| 36 | + handler: 'handler', |
| 37 | + memorySize: 128, |
| 38 | + architecture: Architecture.X86_64, |
| 39 | + }); |
| 40 | + |
| 41 | + const distro = new CloudFrontWebDistribution(stack, 'CloudFrontBuildBucket', { |
| 42 | + originConfigs: [ |
| 43 | + { |
| 44 | + s3OriginSource: { |
| 45 | + s3BucketSource: buildBucket, |
| 46 | + originAccessIdentity, |
| 47 | + }, |
| 48 | + behaviors: [ |
| 49 | + { |
| 50 | + isDefaultBehavior: true, |
| 51 | + compress: true, |
| 52 | + allowedMethods: CloudFrontAllowedMethods.GET_HEAD, |
| 53 | + lambdaFunctionAssociations: [{ |
| 54 | + eventType: LambdaEdgeEventType.VIEWER_REQUEST, |
| 55 | + lambdaFunction: urlRewriter.currentVersion, |
| 56 | + }], |
| 57 | + defaultTtl: Duration.seconds(300), |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + |
| 64 | + new CfnOutput(stack, 'BuildDistributionDomainName', { |
| 65 | + value: distro.distributionDomainName, |
| 66 | + description: 'The domain name where the build artifacts will be available', |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
0 commit comments