canopycms-cdk 0.0.68-int.98 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -15
- package/dist/constructs/asset-support.d.ts +104 -84
- package/dist/constructs/asset-support.js +193 -195
- package/dist/constructs/cms-distribution.d.ts +2 -3
- package/dist/constructs/cms-distribution.js +5 -6
- package/dist/constructs/cms-service.d.ts +150 -18
- package/dist/constructs/cms-service.js +498 -94
- package/dist/constructs/editor-routing.d.ts +2 -2
- package/dist/constructs/worker-bundle.d.ts +42 -0
- package/dist/constructs/worker-bundle.js +118 -0
- package/dist/constructs/worker-lifecycle.d.ts +70 -0
- package/dist/constructs/worker-lifecycle.js +111 -0
- package/dist/constructs/worker-units.d.ts +99 -0
- package/dist/constructs/worker-units.js +398 -0
- package/dist/index.d.ts +2 -1
- package/lambda/asset-transform/dist/handler.js +813 -30360
- package/package.json +11 -6
- package/worker/canopy-github.service +81 -0
- package/worker/canopy-github.socket +16 -0
- package/worker/canopy-worker.service +76 -15
- package/worker/dist/index.js +77784 -40975
- package/worker/dist/index.js.contract +1 -0
- package/worker/dist/index.js.sha256 +1 -0
- package/worker/dist/instance-files.sh +365 -0
- package/worker/gateway.env.example +25 -0
|
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
|
|
|
2
2
|
import * as path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { Construct } from 'constructs';
|
|
5
|
-
import { Duration, RemovalPolicy, Stack, Token, aws_cloudfront as cloudfront, aws_cloudfront_origins as origins, aws_lambda as lambda, aws_logs as logs, aws_s3 as s3, } from 'aws-cdk-lib';
|
|
5
|
+
import { Duration, RemovalPolicy, Stack, Token, aws_cloudfront as cloudfront, aws_cloudfront_origins as origins, aws_iam as iam, aws_lambda as lambda, aws_logs as logs, aws_s3 as s3, } from 'aws-cdk-lib';
|
|
6
6
|
import { attachLambdaExecutionPolicies } from './lambda-execution-role.js';
|
|
7
7
|
// This package (`canopycms-cdk`) is `"type": "module"`, so its compiled
|
|
8
8
|
// output is real ESM - `__dirname` is not a global there. Derive it from
|
|
@@ -42,6 +42,14 @@ const PREFIXES = {
|
|
|
42
42
|
public: 'assets',
|
|
43
43
|
transform: 'assets/t',
|
|
44
44
|
};
|
|
45
|
+
/** The content-addressed prefixes, whose objects no correct writer ever replaces. */
|
|
46
|
+
const CREATE_ONLY_PREFIXES = [PREFIXES.public, PREFIXES.originals, PREFIXES.meta];
|
|
47
|
+
/**
|
|
48
|
+
* The tag on every object the transform Lambda writes, copied as literals for
|
|
49
|
+
* the same reason as `PREFIXES`. Source of truth is `LAZY_TRANSFORM_TAG` in
|
|
50
|
+
* `packages/canopycms/src/assets/materialize.ts`.
|
|
51
|
+
*/
|
|
52
|
+
const LAZY_TRANSFORM_TAG = { key: 'canopy-transform', value: 'lazy' };
|
|
45
53
|
/**
|
|
46
54
|
* CORS preflight cache duration for presigned-POST uploads from the editor.
|
|
47
55
|
* Used for the bucket's own CORS rule and, when `uploadBehavior` is on, for
|
|
@@ -73,54 +81,39 @@ function toFunctionLiteral(values) {
|
|
|
73
81
|
}
|
|
74
82
|
/** Matches packages/canopycms/src/assets/store-s3.ts's `DEFAULT_MAX_UPLOAD_BYTES`. */
|
|
75
83
|
const DEFAULT_MAX_UPLOAD_BYTES = 50 * 1024 * 1024;
|
|
76
|
-
|
|
84
|
+
/** Fits a full-size transform at `MAX_INPUT_PIXELS`; see that constant in canopycms. */
|
|
85
|
+
const TRANSFORM_LAMBDA_MEMORY_MB = 2048;
|
|
77
86
|
const TRANSFORM_LAMBDA_TIMEOUT = Duration.seconds(30);
|
|
78
87
|
/**
|
|
79
|
-
* Concurrency cap on the transform Lambda.
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* Width and quality are allowlisted precisely to bound how many cache keys one
|
|
84
|
-
* asset can have, but `crop` is a 4-decimal float rect (~10^16 values), so a
|
|
85
|
-
* scripted loop of unique crops is an unbounded stream of guaranteed
|
|
86
|
-
* CloudFront+S3 misses, each a sharp transform on a 1536MB Lambda plus a
|
|
87
|
-
* permanently stored S3 object.
|
|
88
|
-
*
|
|
89
|
-
* A reservation is a CAP carved from the account's concurrency pool, not
|
|
90
|
-
* pre-warmed capacity, so it costs nothing when idle (that is
|
|
91
|
-
* `provisionedConcurrentExecutions`, which this is not). Genuine demand is
|
|
92
|
-
* first-render misses only, since every already-generated derivative is served
|
|
93
|
-
* by the S3 primary origin without invoking this function at all.
|
|
88
|
+
* Concurrency cap on the lazy transform Lambda. `hash32` is public and `crop`
|
|
89
|
+
* allows ~10^16 rects per asset, so a loop of unique crops is an unbounded
|
|
90
|
+
* stream of misses, each a sharp run plus a stored object. A reservation is a
|
|
91
|
+
* cap, not provisioned capacity, so it costs nothing idle.
|
|
94
92
|
*/
|
|
95
93
|
const TRANSFORM_LAMBDA_RESERVED_CONCURRENCY = 10;
|
|
96
94
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* `asset-originals/` and are never touched by this rule. Without it the bucket
|
|
101
|
-
* keeps every derivative forever, including every object minted by the crop
|
|
102
|
-
* amplifier above.
|
|
103
|
-
*
|
|
104
|
-
* S3 lifecycle is prefix+age based; there is no "expire if not recently read"
|
|
105
|
-
* mode. Expiry is self-healing anyway: the next request for an expired
|
|
106
|
-
* derivative misses S3, fails over to the transform Lambda, regenerates and
|
|
107
|
-
* re-stores it. 180 days is long enough that only genuinely cold or abusive
|
|
108
|
-
* objects age out.
|
|
95
|
+
* Lazy mode's expiry for the Lambda's tagged `assets/t/` outputs, which bounds
|
|
96
|
+
* how long the crop loop above keeps what it stores. The next request
|
|
97
|
+
* regenerates an expired object while the Lambda runs.
|
|
109
98
|
*/
|
|
110
99
|
const TRANSFORM_OUTPUT_RETENTION = Duration.days(180);
|
|
111
100
|
/**
|
|
112
|
-
* `/assets/t
|
|
113
|
-
* `
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* ANY nonzero min TTL caches that redirect regardless of the origin's
|
|
117
|
-
* `no-store`, and re-serving it while CloudFront's hit for the canonical key
|
|
118
|
-
* still 403s/404s off S3 falls back to the Lambda again - a self-sustaining
|
|
119
|
-
* redirect loop. `minTtl: 0` honors the origin's own `Cache-Control`
|
|
120
|
-
* immediately; `maxTtl`/`defaultTtl` stay generous so the normal case (an
|
|
121
|
-
* immutable 200 with a real `max-age`) still caches well.
|
|
101
|
+
* Lazy mode's `/assets/t/*` cache TTLs. The Lambda answers an oversized output
|
|
102
|
+
* with a `no-store` 302 to the canonical key, and any nonzero min TTL caches
|
|
103
|
+
* that redirect regardless, looping it back to the Lambda while the key still
|
|
104
|
+
* misses. `minTtl: 0` honors the origin's own `Cache-Control`.
|
|
122
105
|
*/
|
|
123
106
|
const TRANSFORM_CACHE_MIN_TTL = Duration.seconds(0);
|
|
107
|
+
/** Props that configure only the transform Lambda, refused without `lazyPublicTransforms`. */
|
|
108
|
+
const LAZY_ONLY_PROPS = [
|
|
109
|
+
'transformOutputRetention',
|
|
110
|
+
'transformReservedConcurrency',
|
|
111
|
+
'transformRole',
|
|
112
|
+
'transformLogGroupName',
|
|
113
|
+
'transformLogRetention',
|
|
114
|
+
];
|
|
115
|
+
/** S3 statuses that send a read to `replicaBucket`. A miss (403) never does. */
|
|
116
|
+
const REPLICA_FAILOVER_STATUS_CODES = [500, 502, 503, 504];
|
|
124
117
|
const TRANSFORM_CACHE_DEFAULT_TTL = Duration.days(1);
|
|
125
118
|
const TRANSFORM_CACHE_MAX_TTL = Duration.days(365);
|
|
126
119
|
/**
|
|
@@ -456,15 +449,8 @@ function buildUploadBehavior(scope, options, label) {
|
|
|
456
449
|
* ```
|
|
457
450
|
*
|
|
458
451
|
* Separate from `AssetSupport.uploadBehavior()` because the upload route needs
|
|
459
|
-
* the bucket and nothing else
|
|
460
|
-
*
|
|
461
|
-
* Those grants never reach the BUCKET policy: `Grant.addToPrincipalOrResource`
|
|
462
|
-
* stops after the identity statement for a same-account grantee, and
|
|
463
|
-
* `addToResourcePolicy` on a bucket CDK does not own is a no-op - an owned
|
|
464
|
-
* bucket, one imported by name, and one imported by ARN in another account all
|
|
465
|
-
* emit zero `AWS::S3::BucketPolicy`, so a cross-account adopter writes the
|
|
466
|
-
* resource half on the bucket's own side. Prefer the method when you already
|
|
467
|
-
* have an `AssetSupport`; both funnel into the same builder and cannot drift.
|
|
452
|
+
* the bucket and nothing else. Prefer the method when you already have an
|
|
453
|
+
* `AssetSupport`; both funnel into the same builder and cannot drift.
|
|
468
454
|
*
|
|
469
455
|
* No bucket CORS rule is written and none is needed: the edge supplies
|
|
470
456
|
* `Access-Control-Allow-Origin`, and S3's ACCEPTANCE of a presigned POST is
|
|
@@ -493,12 +479,12 @@ export function assetUploadBehavior(scope, options) {
|
|
|
493
479
|
* Wires:
|
|
494
480
|
*
|
|
495
481
|
* - The bucket's asset-prefix lifecycle rule + CORS (standalone mode only).
|
|
496
|
-
* - The
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
* OAC-locked Function URL
|
|
482
|
+
* - The public read path: `/assets/*` and `/assets/t/*` served from S3 (plus
|
|
483
|
+
* `replicaBucket` on 5xx). It computes nothing; derivatives come from
|
|
484
|
+
* `canopycms materialize-assets`.
|
|
485
|
+
* - With `lazyPublicTransforms`, the transform Lambda
|
|
486
|
+
* (`../../lambda/asset-transform/handler.ts`, built by `build:lambda`), its
|
|
487
|
+
* log group and its OAC-locked Function URL, behind `/assets/t/*`.
|
|
502
488
|
* - `attachTo(distribution)`, which attaches the two CloudFront behaviors a
|
|
503
489
|
* consuming distribution needs in the only safe order (see its doc comment
|
|
504
490
|
* for why the order is the whole point); `assetBehaviors()` is the escape
|
|
@@ -513,15 +499,40 @@ export function assetUploadBehavior(scope, options) {
|
|
|
513
499
|
export class AssetSupport extends Construct {
|
|
514
500
|
constructor(scope, id, props) {
|
|
515
501
|
super(scope, id);
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
502
|
+
const lazy = props.lazyPublicTransforms ?? false;
|
|
503
|
+
if (!lazy) {
|
|
504
|
+
// A retention silently ignored here is the dangerous case: materialized derivatives are
|
|
505
|
+
// kept forever, so the adopter would believe in an expiry that does not exist.
|
|
506
|
+
const lambdaOnly = LAZY_ONLY_PROPS.filter((name) => props[name] !== undefined);
|
|
507
|
+
if (lambdaOnly.length > 0) {
|
|
508
|
+
throw new Error(`AssetSupport: ${lambdaOnly.join(', ')} only appl${lambdaOnly.length === 1 ? 'ies' : 'y'} ` +
|
|
509
|
+
'with `lazyPublicTransforms: true`. Without it there is no transform Lambda and ' +
|
|
510
|
+
'materialized derivatives under assets/t/ are kept forever. Remove the prop(s), or ' +
|
|
511
|
+
'set `lazyPublicTransforms: true` if you mean to compute derivatives on request.');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
else if (props.bucket && props.transformOutputRetention === undefined) {
|
|
515
|
+
throw new Error('AssetSupport: `lazyPublicTransforms` on a BYO `bucket` needs an explicit ' +
|
|
516
|
+
'`transformOutputRetention`. This construct cannot write lifecycle rules on a bucket ' +
|
|
517
|
+
'it did not create, and anonymous callers can mint objects under assets/t/ in lazy ' +
|
|
518
|
+
'mode. Add an expiry rule for the assets/t/ prefix and the canopy-transform=lazy tag ' +
|
|
519
|
+
'(on a versioned bucket, also expiring noncurrent versions) to your bucket and pass ' +
|
|
520
|
+
'its duration as `transformOutputRetention`, or drop ' +
|
|
521
|
+
'`lazyPublicTransforms` and run `canopycms materialize-assets` in your release ' +
|
|
522
|
+
'pipeline instead.');
|
|
523
|
+
}
|
|
524
|
+
if (props.bucket && props.enforceCreateOnlyWrites) {
|
|
525
|
+
throw new Error('AssetSupport: `enforceCreateOnlyWrites` writes its Deny into the policy of a bucket this ' +
|
|
526
|
+
'construct creates, and cannot write one on a BYO `bucket`. Drop the prop and add the ' +
|
|
527
|
+
"statement to your bucket's own policy: Deny s3:PutObject to any principal on " +
|
|
528
|
+
`${CREATE_ONLY_PREFIXES.map((prefix) => `${prefix}/*`).join(', ')} under the condition ` +
|
|
529
|
+
'{ Null: { "s3:if-none-match": "true" } }.');
|
|
530
|
+
}
|
|
531
|
+
// Fail closed: a bundle without the marker has no linux/arm64 sharp and
|
|
532
|
+
// throws at cold start on the first image request, far from the cause.
|
|
533
|
+
// `pnpm test` leaves exactly such a fixture on disk.
|
|
534
|
+
if (lazy &&
|
|
535
|
+
(props.requireDeployableBundle ?? true) &&
|
|
525
536
|
!existsSync(path.join(transformAssetDir, DEPLOYABLE_MARKER))) {
|
|
526
537
|
throw new Error(`The transform Lambda code asset at ${transformAssetDir} carries no ${DEPLOYABLE_MARKER} ` +
|
|
527
538
|
'marker, so it was not built with its native sharp binary and must not be deployed. ' +
|
|
@@ -567,23 +578,30 @@ export class AssetSupport extends Construct {
|
|
|
567
578
|
versioned: props.versioned ?? false,
|
|
568
579
|
removalPolicy: props.removalPolicy ?? RemovalPolicy.RETAIN,
|
|
569
580
|
autoDeleteObjects: props.autoDeleteObjects ?? false,
|
|
570
|
-
//
|
|
571
|
-
//
|
|
572
|
-
//
|
|
573
|
-
//
|
|
581
|
+
// Everything else is content-addressed and kept forever, materialized
|
|
582
|
+
// derivatives included; only the Lambda's tagged outputs expire. On a
|
|
583
|
+
// versioned bucket an expiry only adds a delete marker, so each rule
|
|
584
|
+
// also expires noncurrent versions or it would free nothing.
|
|
574
585
|
lifecycleRules: [
|
|
575
586
|
{
|
|
576
587
|
id: 'expire-asset-staging',
|
|
577
588
|
enabled: true,
|
|
578
589
|
prefix: `${PREFIXES.staging}/`,
|
|
579
590
|
expiration: Duration.days(1),
|
|
591
|
+
noncurrentVersionExpiration: Duration.days(1),
|
|
580
592
|
},
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
593
|
+
...(lazy
|
|
594
|
+
? [
|
|
595
|
+
{
|
|
596
|
+
id: 'expire-transform-outputs',
|
|
597
|
+
enabled: true,
|
|
598
|
+
prefix: `${PREFIXES.transform}/`,
|
|
599
|
+
tagFilters: { [LAZY_TRANSFORM_TAG.key]: LAZY_TRANSFORM_TAG.value },
|
|
600
|
+
expiration: props.transformOutputRetention ?? TRANSFORM_OUTPUT_RETENTION,
|
|
601
|
+
noncurrentVersionExpiration: Duration.days(1),
|
|
602
|
+
},
|
|
603
|
+
]
|
|
604
|
+
: []),
|
|
587
605
|
],
|
|
588
606
|
// Omitted entirely when `editorOrigins` is absent - the upload is then
|
|
589
607
|
// going through `uploadBehavior`, which supplies ACAO at the edge, and
|
|
@@ -601,81 +619,23 @@ export class AssetSupport extends Construct {
|
|
|
601
619
|
: undefined,
|
|
602
620
|
});
|
|
603
621
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
logGroupName: props.transformLogGroupName ?? `/canopycms/${Stack.of(this).stackName}/transform`,
|
|
614
|
-
retention: props.transformLogRetention ?? logs.RetentionDays.THREE_MONTHS,
|
|
615
|
-
removalPolicy: RemovalPolicy.DESTROY,
|
|
616
|
-
});
|
|
617
|
-
// Re-attach what CDK silently drops for a caller-supplied role. MUST run
|
|
618
|
-
// for every passed role - see that function's doc comment. This function is
|
|
619
|
-
// not VPC-attached, so it takes basic execution only and must NOT be
|
|
620
|
-
// collapsed into the CMS Lambda's `vpc: true` call site.
|
|
621
|
-
if (props.transformRole) {
|
|
622
|
-
attachLambdaExecutionPolicies(props.transformRole, { vpc: false });
|
|
622
|
+
if (props.enforceCreateOnlyWrites) {
|
|
623
|
+
this.bucket.addToResourcePolicy(new iam.PolicyStatement({
|
|
624
|
+
sid: 'DenyAssetOverwrites',
|
|
625
|
+
effect: iam.Effect.DENY,
|
|
626
|
+
principals: [new iam.StarPrincipal()],
|
|
627
|
+
actions: ['s3:PutObject'],
|
|
628
|
+
resources: CREATE_ONLY_PREFIXES.map((prefix) => this.bucket.arnForObjects(`${prefix}/*`)),
|
|
629
|
+
conditions: { Null: { 's3:if-none-match': 'true' } },
|
|
630
|
+
}));
|
|
623
631
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
// run automatically here.
|
|
632
|
-
code: lambda.Code.fromAsset(transformAssetDir),
|
|
633
|
-
handler: 'handler.handler',
|
|
634
|
-
// nodejs20.x is deprecated and CDK's CloudFormation validation fails synth
|
|
635
|
-
// on it. The esbuild bundle (lambda/asset-transform/build.mjs) targets
|
|
636
|
-
// node22 to match; move the two together.
|
|
637
|
-
runtime: lambda.Runtime.NODEJS_22_X,
|
|
638
|
-
architecture: lambda.Architecture.ARM_64,
|
|
639
|
-
memorySize: TRANSFORM_LAMBDA_MEMORY_MB,
|
|
640
|
-
timeout: TRANSFORM_LAMBDA_TIMEOUT,
|
|
641
|
-
// See the constant's doc comment: this is an anonymous, uncapped compute
|
|
642
|
-
// and storage amplifier without it.
|
|
643
|
-
reservedConcurrentExecutions: props.transformReservedConcurrency ?? TRANSFORM_LAMBDA_RESERVED_CONCURRENCY,
|
|
644
|
-
// Pass the pre-created group via `logGroup`, NOT `logRetention` (CDK
|
|
645
|
-
// throws LogRetentionLogGroupConflict/ConflictingLogPolicyOptions if
|
|
646
|
-
// both are set on the same function) - the removal policy lives on the
|
|
647
|
-
// LogGroup construct above instead.
|
|
648
|
-
logGroup: this.transformLogGroup,
|
|
649
|
-
environment: {
|
|
650
|
-
ASSET_BUCKET: this.bucket.bucketName,
|
|
651
|
-
},
|
|
652
|
-
});
|
|
653
|
-
// Explicit, scoped grant - NOT a reliance on the auto-created execution
|
|
654
|
-
// role's AWSLambdaBasicExecutionRole managed policy, which CDK attaches
|
|
655
|
-
// regardless of `logGroup` and never adjusts for it (passing `logGroup`
|
|
656
|
-
// only points the function's LoggingConfig at this group; it grants no
|
|
657
|
-
// IAM). That policy's logs:CreateLogStream/logs:PutLogEvents statement is
|
|
658
|
-
// scoped to `arn:aws:logs:*:*:log-group:/aws/lambda/*:*`, so it grants
|
|
659
|
-
// nothing for a custom-named group. Without this grantWrite, log delivery
|
|
660
|
-
// fails its permission check with no error surfaced anywhere - logs simply
|
|
661
|
-
// vanish.
|
|
662
|
-
this.transformLogGroup.grantWrite(this.transformFunction);
|
|
663
|
-
// Originals are what it transforms; `asset-meta/{hash32}.json` carries the
|
|
664
|
-
// kind/ext it must look up before it can transform anything, so meta is
|
|
665
|
-
// read too.
|
|
666
|
-
this.bucket.grantRead(this.transformFunction, `${PREFIXES.originals}/*`);
|
|
667
|
-
this.bucket.grantRead(this.transformFunction, `${PREFIXES.meta}/*`);
|
|
668
|
-
// Write access to `assets/*` covers both the public prefix and
|
|
669
|
-
// `assets/t/*` (transform outputs nest under it) in one grant.
|
|
670
|
-
this.bucket.grantPut(this.transformFunction, `${PREFIXES.public}/*`);
|
|
671
|
-
// AWS_IAM (not NONE): only reachable through CloudFront's Origin Access
|
|
672
|
-
// Control, matching CanopyCmsService/CanopyCmsDistribution's existing
|
|
673
|
-
// Function URL pattern (see cms-service.ts / cms-distribution.ts) -
|
|
674
|
-
// direct hits to the Function URL are rejected.
|
|
675
|
-
this.transformFunctionUrl = this.transformFunction.addFunctionUrl({
|
|
676
|
-
authType: lambda.FunctionUrlAuthType.AWS_IAM,
|
|
677
|
-
});
|
|
678
|
-
this.behaviors = this.buildBehaviors();
|
|
632
|
+
if (lazy) {
|
|
633
|
+
const transform = this.buildTransformLambda(props);
|
|
634
|
+
this.transformFunction = transform.fn;
|
|
635
|
+
this.transformLogGroup = transform.logGroup;
|
|
636
|
+
this.transformFunctionUrl = transform.url;
|
|
637
|
+
}
|
|
638
|
+
this.behaviors = this.buildBehaviors(props.replicaBucket);
|
|
679
639
|
// Validated eagerly, built lazily (see `upload`'s comment) - the accessor
|
|
680
640
|
// may never be called, and a bad `allowedOrigins` should not wait for it
|
|
681
641
|
// to find out. The guards themselves are shared with
|
|
@@ -734,25 +694,79 @@ export class AssetSupport extends Construct {
|
|
|
734
694
|
},
|
|
735
695
|
});
|
|
736
696
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
//
|
|
740
|
-
//
|
|
741
|
-
//
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
697
|
+
buildTransformLambda(props) {
|
|
698
|
+
// Custom-named, never `/aws/lambda/<function name>`: Lambda auto-creates that
|
|
699
|
+
// group outside CloudFormation on first invoke, after which a CDK `LogGroup`
|
|
700
|
+
// of the same name fails every deploy with "already exists". Same convention
|
|
701
|
+
// as `CanopyCmsService`'s log groups.
|
|
702
|
+
const logGroup = new logs.LogGroup(this, 'TransformFunctionLogs', {
|
|
703
|
+
logGroupName: props.transformLogGroupName ?? `/canopycms/${Stack.of(this).stackName}/transform`,
|
|
704
|
+
retention: props.transformLogRetention ?? logs.RetentionDays.THREE_MONTHS,
|
|
705
|
+
removalPolicy: RemovalPolicy.DESTROY,
|
|
706
|
+
});
|
|
707
|
+
// Re-attach what CDK silently drops for a caller-supplied role. This function
|
|
708
|
+
// is not VPC-attached, so it takes basic execution only.
|
|
709
|
+
if (props.transformRole) {
|
|
710
|
+
attachLambdaExecutionPolicies(props.transformRole, { vpc: false });
|
|
711
|
+
}
|
|
712
|
+
const fn = new lambda.Function(this, 'TransformFunction', {
|
|
713
|
+
role: props.transformRole,
|
|
714
|
+
// Built by `build:lambda` (lambda/asset-transform/build.mjs), not here.
|
|
715
|
+
code: lambda.Code.fromAsset(transformAssetDir),
|
|
716
|
+
handler: 'handler.handler',
|
|
717
|
+
// build.mjs targets node22 to match; move the two together.
|
|
718
|
+
runtime: lambda.Runtime.NODEJS_22_X,
|
|
719
|
+
architecture: lambda.Architecture.ARM_64,
|
|
720
|
+
memorySize: TRANSFORM_LAMBDA_MEMORY_MB,
|
|
721
|
+
timeout: TRANSFORM_LAMBDA_TIMEOUT,
|
|
722
|
+
reservedConcurrentExecutions: props.transformReservedConcurrency ?? TRANSFORM_LAMBDA_RESERVED_CONCURRENCY,
|
|
723
|
+
// `logGroup`, not `logRetention`: CDK refuses both on one function.
|
|
724
|
+
logGroup,
|
|
725
|
+
environment: {
|
|
726
|
+
ASSET_BUCKET: this.bucket.bucketName,
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
// The managed basic-execution policy's log statement covers only
|
|
730
|
+
// `/aws/lambda/*`, so without this a custom-named group silently gets no logs.
|
|
731
|
+
logGroup.grantWrite(fn);
|
|
732
|
+
// `grantRead` includes s3:ListBucket, so an unknown hash is a 404 rather than
|
|
733
|
+
// a 403 the store throws on, and `readOriginal` can list when the meta's ext
|
|
734
|
+
// misses. `assets/*` covers `assets/t/*`, and `grantPut` includes the
|
|
735
|
+
// s3:PutObjectTagging a tagged PutObject needs.
|
|
736
|
+
this.bucket.grantRead(fn, `${PREFIXES.originals}/*`);
|
|
737
|
+
this.bucket.grantRead(fn, `${PREFIXES.meta}/*`);
|
|
738
|
+
this.bucket.grantPut(fn, `${PREFIXES.public}/*`);
|
|
739
|
+
// AWS_IAM: reachable only through CloudFront's OAC.
|
|
740
|
+
const url = fn.addFunctionUrl({ authType: lambda.FunctionUrlAuthType.AWS_IAM });
|
|
741
|
+
return { fn, logGroup, url };
|
|
742
|
+
}
|
|
743
|
+
buildBehaviors(replicaBucket) {
|
|
744
|
+
// READ only, so a miss is 403: without s3:ListBucket, S3 does not reveal
|
|
745
|
+
// whether a key exists. LIST would make it 404 but lets any behavior that
|
|
746
|
+
// reaches the bucket root list it, and CDK warns on every synth.
|
|
747
|
+
const readOrigin = (bucket) => origins.S3BucketOrigin.withOriginAccessControl(bucket);
|
|
748
|
+
const primary = readOrigin(this.bucket);
|
|
749
|
+
// Materialized mode gives both behaviors this one object, so the distribution
|
|
750
|
+
// emits one origin (or one origin group), not two. Lazy mode's `/assets/t/*`
|
|
751
|
+
// group binds `primary` again, as a second origin.
|
|
752
|
+
const read = replicaBucket
|
|
753
|
+
? new origins.OriginGroup({
|
|
754
|
+
primaryOrigin: primary,
|
|
755
|
+
fallbackOrigin: readOrigin(replicaBucket),
|
|
756
|
+
fallbackStatusCodes: REPLICA_FAILOVER_STATUS_CODES,
|
|
757
|
+
})
|
|
758
|
+
: primary;
|
|
745
759
|
const assets = {
|
|
746
|
-
origin:
|
|
760
|
+
origin: read,
|
|
747
761
|
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
|
|
748
762
|
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
|
|
749
763
|
compress: true,
|
|
750
764
|
};
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
// response
|
|
755
|
-
//
|
|
765
|
+
if (!this.transformFunctionUrl) {
|
|
766
|
+
return { assets, assetsTransform: { ...assets } };
|
|
767
|
+
}
|
|
768
|
+
// Directives live in the path and the response varies by nothing else, so
|
|
769
|
+
// nothing enters the cache key.
|
|
756
770
|
const transformCachePolicy = new cloudfront.CachePolicy(this, 'AssetsTransformCachePolicy', {
|
|
757
771
|
minTtl: TRANSFORM_CACHE_MIN_TTL,
|
|
758
772
|
defaultTtl: TRANSFORM_CACHE_DEFAULT_TTL,
|
|
@@ -764,14 +778,13 @@ export class AssetSupport extends Construct {
|
|
|
764
778
|
cookieBehavior: cloudfront.CacheCookieBehavior.none(),
|
|
765
779
|
});
|
|
766
780
|
const assetsTransform = {
|
|
781
|
+
// The group's one fallback slot is the Lambda, so no replica here. An
|
|
782
|
+
// OAC miss is 403; 404 is listed too for a bucket whose policy grants list.
|
|
767
783
|
origin: new origins.OriginGroup({
|
|
768
|
-
primaryOrigin:
|
|
769
|
-
|
|
770
|
-
//
|
|
771
|
-
|
|
772
|
-
// handling can still surface 404 in some paths, so both are
|
|
773
|
-
// configured. Confirmed working end-to-end by the sandbox spike
|
|
774
|
-
// (.claude/future-tasks/resolved/assets-media-system.md's SPIKE RESULT).
|
|
784
|
+
primaryOrigin: primary,
|
|
785
|
+
// readTimeout explicit: CloudFront's 30s default only happens to equal
|
|
786
|
+
// the Lambda's timeout, and raising one alone would 504 slow transforms.
|
|
787
|
+
fallbackOrigin: origins.FunctionUrlOrigin.withOriginAccessControl(this.transformFunctionUrl, { readTimeout: TRANSFORM_LAMBDA_TIMEOUT }),
|
|
775
788
|
fallbackStatusCodes: [403, 404],
|
|
776
789
|
}),
|
|
777
790
|
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
|
|
@@ -816,9 +829,8 @@ export class AssetSupport extends Construct {
|
|
|
816
829
|
* site's cookies and any cached basic-auth credential are live hazards
|
|
817
830
|
* there, stripped by policy and function rather than never sent at all.
|
|
818
831
|
* - NOT one shared distribution serving asset reads AND writes for every
|
|
819
|
-
* environment. That means one `AssetSupport`,
|
|
820
|
-
*
|
|
821
|
-
* every environment's asset pipeline at once. Only the upload route moves;
|
|
832
|
+
* environment. That means one `AssetSupport`, so a `canopycms-cdk` bump
|
|
833
|
+
* would move every environment's asset pipeline at once. Only the upload route moves;
|
|
822
834
|
* it depends on the bucket and nothing else, the same property that lets
|
|
823
835
|
* `assetUploadBehavior` build it from a bucket alone.
|
|
824
836
|
*/
|
|
@@ -852,17 +864,12 @@ export class AssetSupport extends Construct {
|
|
|
852
864
|
* Attach both asset behaviors to a concrete CloudFront distribution, in the
|
|
853
865
|
* only safe order.
|
|
854
866
|
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
*
|
|
861
|
-
* keep working. `'/assets/*'` also sorts BEFORE `'/assets/t/*'`
|
|
862
|
-
* lexicographically (`*` = 0x2A, `t` = 0x74), so alphabetizing the keys
|
|
863
|
-
* reproduces exactly this failure with no synth or deploy error to catch it -
|
|
864
|
-
* and `assetBehaviors()` attaches no path pattern, so nothing there stops a
|
|
865
|
-
* caller getting it wrong.
|
|
867
|
+
* CloudFront stops at the first matching pattern, and `/assets/*` also
|
|
868
|
+
* matches every `/assets/t/*` request. With `lazyPublicTransforms`, attaching
|
|
869
|
+
* it first means no miss ever reaches the transform Lambda, with no synth or
|
|
870
|
+
* deploy error; `'/assets/*'` also sorts first alphabetically. The two
|
|
871
|
+
* behaviors are identical otherwise, and the order is kept so that switching
|
|
872
|
+
* modes changes nothing else.
|
|
866
873
|
*
|
|
867
874
|
* `overrides` is merged into BOTH behaviors. The motivating case is a
|
|
868
875
|
* distribution running a viewer-request function on every behavior - tier
|
|
@@ -894,19 +901,10 @@ export class AssetSupport extends Construct {
|
|
|
894
901
|
`attachTo() call -- keep one.`);
|
|
895
902
|
}
|
|
896
903
|
new Construct(distribution, ATTACHED_MARKER_ID);
|
|
897
|
-
// Drop explicitly-`undefined` keys
|
|
898
|
-
//
|
|
899
|
-
//
|
|
900
|
-
//
|
|
901
|
-
// `cachePolicy: undefined` swaps this behavior's custom policy
|
|
902
|
-
// (TRANSFORM_CACHE_MIN_TTL = 0, which exists solely to stop the
|
|
903
|
-
// oversized-output redirect loop) for the managed CACHING_OPTIMIZED and its
|
|
904
|
-
// 1-second min TTL, and `viewerProtocolPolicy: undefined` downgrades BOTH
|
|
905
|
-
// behaviors from redirect-to-https to allow-all, serving assets over plain
|
|
906
|
-
// HTTP. Not a contrived input: `attachTo(dist, { cachePolicy:
|
|
907
|
-
// props.maybePolicy })` with the prop unset typechecks (every
|
|
908
|
-
// AddBehaviorOptions field is already optional), synthesizes and deploys
|
|
909
|
-
// clean.
|
|
904
|
+
// Drop explicitly-`undefined` keys: a spread copies them, so forwarding an
|
|
905
|
+
// unset optional prop would replace the construct's choice with CDK's
|
|
906
|
+
// default - lazy mode's minTtl-0 policy with CACHING_OPTIMIZED, and
|
|
907
|
+
// redirect-to-https with allow-all on both behaviors.
|
|
910
908
|
const definedOverrides = Object.fromEntries(Object.entries(overrides ?? {}).filter(([, value]) => value !== undefined));
|
|
911
909
|
const { origin: transformOrigin, ...transformRest } = this.behaviors.assetsTransform;
|
|
912
910
|
const { origin: assetsOrigin, ...assetsRest } = this.behaviors.assets;
|
|
@@ -37,9 +37,8 @@ export interface CanopyCmsDistributionProps {
|
|
|
37
37
|
*
|
|
38
38
|
* ORDER MATTERS. CloudFront matches path patterns in the order given, so a
|
|
39
39
|
* more specific pattern must be listed before a more general one that also
|
|
40
|
-
* matches: `/assets/t/*` before `/assets/*`, or
|
|
41
|
-
*
|
|
42
|
-
* Lambda.
|
|
40
|
+
* matches: `/assets/t/*` before `/assets/*`, or lazy mode's misses never
|
|
41
|
+
* reach the transform Lambda.
|
|
43
42
|
*
|
|
44
43
|
* Keys here override this construct's own behaviors on collision, which is
|
|
45
44
|
* deliberate — the caller is more specific than the default — and an
|
|
@@ -61,9 +61,8 @@ function normalizePathPattern(pattern) {
|
|
|
61
61
|
* type-checks and deploys clean, synthesizing two behaviors matching the
|
|
62
62
|
* literal patterns `assets` and `assetsTransform`, which nothing requests.
|
|
63
63
|
* 2. `/assets/*` listed before `/assets/t/*`: CloudFront's first-match-wins
|
|
64
|
-
* ordering serves every transform request off the broader
|
|
65
|
-
*
|
|
66
|
-
* computed (an OAC-signed S3 miss reports 403).
|
|
64
|
+
* ordering serves every transform request off the broader `/assets/*`
|
|
65
|
+
* behavior, so with `lazyPublicTransforms` no miss reaches the Lambda.
|
|
67
66
|
* 3. Either asset pattern in `merged` WHILE the `assetSupport` prop is also
|
|
68
67
|
* passed: both wiring routes are active at once, so each pattern is attached
|
|
69
68
|
* twice. Hazard 2 cannot catch it, because the hand-written order is usually
|
|
@@ -119,9 +118,9 @@ function assertNoAssetBehaviorOrderingHazards(merged, attachingAssetSupport) {
|
|
|
119
118
|
throw new Error(`CanopyCmsDistribution: additionalBehaviors lists '${ASSETS_PATH_PATTERN}' before ` +
|
|
120
119
|
`'${ASSETS_TRANSFORM_PATH_PATTERN}'. CloudFront matches path patterns in the order given ` +
|
|
121
120
|
`and stops at the first match, so every '${ASSETS_TRANSFORM_PATH_PATTERN}' request would ` +
|
|
122
|
-
`be served by the broader
|
|
123
|
-
|
|
124
|
-
`
|
|
121
|
+
`be served by the broader '${ASSETS_PATH_PATTERN}' behavior, and with ` +
|
|
122
|
+
`\`lazyPublicTransforms\` no miss would ever reach the transform Lambda. List ` +
|
|
123
|
+
`'${ASSETS_TRANSFORM_PATH_PATTERN}' first, or use ` +
|
|
125
124
|
`assetSupport.attachTo(distribution) / this construct's \`assetSupport\` prop, which get ` +
|
|
126
125
|
`the order right automatically.`);
|
|
127
126
|
}
|