|
1 |
| -import cloudformation = require('@aws-cdk/aws-cloudformation'); |
2 |
| -import cloudfront = require('@aws-cdk/aws-cloudfront'); |
3 |
| -import iam = require('@aws-cdk/aws-iam'); |
4 |
| -import lambda = require('@aws-cdk/aws-lambda'); |
5 |
| -import s3 = require('@aws-cdk/aws-s3'); |
6 |
| -import cdk = require('@aws-cdk/core'); |
7 |
| -import { Token } from '@aws-cdk/core'; |
| 1 | +import cloudformation = require("@aws-cdk/aws-cloudformation"); |
| 2 | +import cloudfront = require("@aws-cdk/aws-cloudfront"); |
| 3 | +import iam = require("@aws-cdk/aws-iam"); |
| 4 | +import lambda = require("@aws-cdk/aws-lambda"); |
| 5 | +import s3 = require("@aws-cdk/aws-s3"); |
| 6 | +import cdk = require("@aws-cdk/core"); |
| 7 | +import { Token } from "@aws-cdk/core"; |
8 | 8 | import crypto = require('crypto');
|
9 | 9 | import fs = require('fs');
|
10 |
| -import path = require('path'); |
11 |
| -import { ISource, SourceConfig } from './source'; |
| 10 | +import path = require("path"); |
| 11 | +import { ISource, SourceConfig } from "./source"; |
12 | 12 |
|
13 |
| -const handlerCodeBundle = path.join(__dirname, '..', 'lambda', 'bundle.zip'); |
| 13 | +const handlerCodeBundle = path.join(__dirname, "..", "lambda", "bundle.zip"); |
14 | 14 | const handlerSourceDirectory = path.join(__dirname, '..', 'lambda', 'src');
|
15 | 15 |
|
16 | 16 | export interface BucketDeploymentProps {
|
@@ -77,6 +77,80 @@ export interface BucketDeploymentProps {
|
77 | 77 | * @default - A role is automatically created
|
78 | 78 | */
|
79 | 79 | readonly role?: iam.IRole;
|
| 80 | + |
| 81 | + /** |
| 82 | + * User-defined object metadata to be set on all objects in the deployment |
| 83 | + * @default - No user metadata is set |
| 84 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#UserMetadata |
| 85 | + */ |
| 86 | + readonly metadata?: UserDefinedObjectMetadata; |
| 87 | + |
| 88 | + /** |
| 89 | + * System-defined cache-control metadata to be set on all objects in the deployment. |
| 90 | + * @default - Not set. |
| 91 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 92 | + */ |
| 93 | + readonly cacheControl?: CacheControl[]; |
| 94 | + /** |
| 95 | + * System-defined cache-disposition metadata to be set on all objects in the deployment. |
| 96 | + * @default - Not set. |
| 97 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 98 | + */ |
| 99 | + readonly contentDisposition?: string; |
| 100 | + /** |
| 101 | + * System-defined content-encoding metadata to be set on all objects in the deployment. |
| 102 | + * @default - Not set. |
| 103 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 104 | + */ |
| 105 | + readonly contentEncoding?: string; |
| 106 | + /** |
| 107 | + * System-defined content-language metadata to be set on all objects in the deployment. |
| 108 | + * @default - Not set. |
| 109 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 110 | + */ |
| 111 | + readonly contentLanguage?: string; |
| 112 | + /** |
| 113 | + * System-defined content-type metadata to be set on all objects in the deployment. |
| 114 | + * @default - Not set. |
| 115 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 116 | + */ |
| 117 | + readonly contentType?: string; |
| 118 | + /** |
| 119 | + * System-defined expires metadata to be set on all objects in the deployment. |
| 120 | + * @default - The objects in the distribution will not expire. |
| 121 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 122 | + */ |
| 123 | + readonly expires?: Expires; |
| 124 | + /** |
| 125 | + * System-defined x-amz-server-side-encryption metadata to be set on all objects in the deployment. |
| 126 | + * @default - Server side encryption is not used. |
| 127 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 128 | + */ |
| 129 | + readonly serverSideEncryption?: ServerSideEncryption; |
| 130 | + /** |
| 131 | + * System-defined x-amz-storage-class metadata to be set on all objects in the deployment. |
| 132 | + * @default - Default storage-class for the bucket is used. |
| 133 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 134 | + */ |
| 135 | + readonly storageClass?: StorageClass; |
| 136 | + /** |
| 137 | + * System-defined x-amz-website-redirect-location metadata to be set on all objects in the deployment. |
| 138 | + * @default - No website redirection. |
| 139 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 140 | + */ |
| 141 | + readonly websiteRedirectLocation?: string; |
| 142 | + /** |
| 143 | + * System-defined x-amz-server-side-encryption-aws-kms-key-id metadata to be set on all objects in the deployment. |
| 144 | + * @default - Not set. |
| 145 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 146 | + */ |
| 147 | + readonly serverSideEncryptionAwsKmsKeyId?: string; |
| 148 | + /** |
| 149 | + * System-defined x-amz-server-side-encryption-customer-algorithm metadata to be set on all objects in the deployment. |
| 150 | + * @default - Not set. |
| 151 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 152 | + */ |
| 153 | + readonly serverSideEncryptionCustomerAlgorithm?: string; |
80 | 154 | }
|
81 | 155 |
|
82 | 156 | export class BucketDeployment extends cdk.Construct {
|
@@ -123,6 +197,8 @@ export class BucketDeployment extends cdk.Construct {
|
123 | 197 | DestinationBucketName: props.destinationBucket.bucketName,
|
124 | 198 | DestinationBucketKeyPrefix: props.destinationKeyPrefix,
|
125 | 199 | RetainOnDelete: props.retainOnDelete,
|
| 200 | + UserMetadata: props.metadata ? mapUserMetadata(props.metadata) : undefined, |
| 201 | + SystemMetadata: mapSystemMetadata(props), |
126 | 202 | DistributionId: props.distribution ? props.distribution.distributionId : undefined,
|
127 | 203 | DistributionPaths: props.distributionPaths
|
128 | 204 | }
|
@@ -165,3 +241,129 @@ function calcSourceHash(srcDir: string): string {
|
165 | 241 |
|
166 | 242 | return sha.digest('hex');
|
167 | 243 | }
|
| 244 | + |
| 245 | +/** |
| 246 | + * Metadata |
| 247 | + */ |
| 248 | + |
| 249 | +function mapUserMetadata(metadata: UserDefinedObjectMetadata) { |
| 250 | + const mapKey = (key: string) => |
| 251 | + key.toLowerCase().startsWith("x-amzn-meta-") |
| 252 | + ? key.toLowerCase() |
| 253 | + : `x-amzn-meta-${key.toLowerCase()}`; |
| 254 | + |
| 255 | + return Object.keys(metadata).reduce((o, key) => ({ ...o, [mapKey(key)]: metadata[key] }), {}); |
| 256 | +} |
| 257 | + |
| 258 | +function mapSystemMetadata(metadata: BucketDeploymentProps) { |
| 259 | + function mapCacheControlDirective(cacheControl: CacheControl) { |
| 260 | + const { value } = cacheControl; |
| 261 | + |
| 262 | + if (typeof value === "string") { return value; } |
| 263 | + if ("max-age" in value) { return `max-age=${value["max-age"].toSeconds()}`; } |
| 264 | + if ("s-max-age" in value) { return `s-max-age=${value["s-max-age"].toSeconds()}`; } |
| 265 | + |
| 266 | + throw new Error(`Unsupported cache-control directive ${value}`); |
| 267 | + } |
| 268 | + function mapExpires(expires: Expires) { |
| 269 | + const { value } = expires; |
| 270 | + |
| 271 | + if (typeof value === "string") { return value; } |
| 272 | + if (value instanceof Date) { return value.toUTCString(); } |
| 273 | + if (value instanceof cdk.Duration) { return new Date(Date.now() + value.toMilliseconds()).toUTCString(); } |
| 274 | + |
| 275 | + throw new Error(`Unsupported expires ${expires}`); |
| 276 | + } |
| 277 | + |
| 278 | + const res: { [key: string]: string } = {}; |
| 279 | + |
| 280 | + if (metadata.cacheControl) { res["cache-control"] = metadata.cacheControl.map(mapCacheControlDirective).join(", "); } |
| 281 | + if (metadata.expires) { res.expires = mapExpires(metadata.expires); } |
| 282 | + if (metadata.contentDisposition) { res["content-disposition"] = metadata.contentDisposition; } |
| 283 | + if (metadata.contentEncoding) { res["content-encoding"] = metadata.contentEncoding; } |
| 284 | + if (metadata.contentLanguage) { res["content-language"] = metadata.contentLanguage; } |
| 285 | + if (metadata.contentType) { res["content-type"] = metadata.contentType; } |
| 286 | + if (metadata.serverSideEncryption) { res["server-side-encryption"] = metadata.serverSideEncryption; } |
| 287 | + if (metadata.storageClass) { res["storage-class"] = metadata.storageClass; } |
| 288 | + if (metadata.websiteRedirectLocation) { res["website-redirect-location"] = metadata.websiteRedirectLocation; } |
| 289 | + if (metadata.serverSideEncryptionAwsKmsKeyId) { res["ssekms-key-id"] = metadata.serverSideEncryptionAwsKmsKeyId; } |
| 290 | + if (metadata.serverSideEncryptionCustomerAlgorithm) { res["sse-customer-algorithm"] = metadata.serverSideEncryptionCustomerAlgorithm; } |
| 291 | + |
| 292 | + return Object.keys(res).length === 0 ? undefined : res; |
| 293 | +} |
| 294 | + |
| 295 | +/** |
| 296 | + * Used for HTTP cache-control header, which influences downstream caches. |
| 297 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 298 | + */ |
| 299 | +export class CacheControl { |
| 300 | + public static mustRevalidate() { return new CacheControl("must-revalidate"); } |
| 301 | + public static noCache() { return new CacheControl("no-cache"); } |
| 302 | + public static noTransform() { return new CacheControl("no-transform"); } |
| 303 | + public static setPublic() { return new CacheControl("public"); } |
| 304 | + public static setPrivate() { return new CacheControl("private"); } |
| 305 | + public static proxyRevalidate() { return new CacheControl("proxy-revalidate"); } |
| 306 | + public static maxAge(t: cdk.Duration) { return new CacheControl({ "max-age": t }); } |
| 307 | + public static sMaxAge(t: cdk.Duration) { return new CacheControl({ "s-max-age": t }); } |
| 308 | + public static fromString(s: string) { return new CacheControl(s); } |
| 309 | + |
| 310 | + private constructor(public value: any) {} |
| 311 | +} |
| 312 | + |
| 313 | +/** |
| 314 | + * Indicates whether server-side encryption is enabled for the object, and whether that encryption is |
| 315 | + * from the AWS Key Management Service (AWS KMS) or from Amazon S3 managed encryption (SSE-S3). |
| 316 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 317 | + */ |
| 318 | +export enum ServerSideEncryption { |
| 319 | + AES_256 = 'AES256', |
| 320 | + AWS_KMS = 'aws:kms' |
| 321 | +} |
| 322 | + |
| 323 | +/** |
| 324 | + * Storage class used for storing the object. |
| 325 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 326 | + */ |
| 327 | +export enum StorageClass { |
| 328 | + STANDARD = 'STANDARD', |
| 329 | + REDUCED_REDUNDANCY = 'REDUCED_REDUNDANCY', |
| 330 | + STANDARD_IA = 'STANDARD_IA', |
| 331 | + ONEZONE_IA = 'ONEZONE_IA', |
| 332 | + INTELLIGENT_TIERING = 'INTELLIGENT_TIERING', |
| 333 | + GLACIER = 'GLACIER', |
| 334 | + DEEP_ARCHIVE = 'DEEP_ARCHIVE' |
| 335 | +} |
| 336 | + |
| 337 | +/** |
| 338 | + * Used for HTTP expires header, which influences downstream caches. Does NOT influence deletion of the object. |
| 339 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 340 | + */ |
| 341 | +export class Expires { |
| 342 | + /** |
| 343 | + * Expire at the specified date |
| 344 | + * @param d date to expire at |
| 345 | + */ |
| 346 | + public static atDate(d: Date) { return new Expires(d); } |
| 347 | + /** |
| 348 | + * Expire at the specified timestamp |
| 349 | + * @param t timestamp in unix milliseconds |
| 350 | + */ |
| 351 | + public static atTimestamp(t: number) { return new Expires(t); } |
| 352 | + /** |
| 353 | + * Expire once the specified duration has passed since deployment time |
| 354 | + * @param t the duration to wait before expiring |
| 355 | + */ |
| 356 | + public static after(t: cdk.Duration) { return new Expires(t); } |
| 357 | + public static fromString(s: string) { return new Expires(s); } |
| 358 | + |
| 359 | + private constructor(public value: any) {} |
| 360 | +} |
| 361 | + |
| 362 | +export interface UserDefinedObjectMetadata { |
| 363 | + /** |
| 364 | + * Arbitrary metadata key-values |
| 365 | + * Keys must begin with `x-amzn-meta-` (will be added automatically if not provided) |
| 366 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#UserMetadata |
| 367 | + */ |
| 368 | + readonly [key: string]: string; |
| 369 | +} |
0 commit comments