carlin 6.0.2 → 6.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.
Files changed (2) hide show
  1. package/dist/index.mjs +80 -14
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -3967,6 +3967,7 @@ const getViewerRequestFunctionCode = ({ code }) => {
3967
3967
  const PACKAGE_VERSION = getPackageVersion();
3968
3968
  const STATIC_APP_BUCKET_LOGICAL_ID = "StaticBucket";
3969
3969
  const CLOUDFRONT_DISTRIBUTION_LOGICAL_ID = "CloudFrontDistribution";
3970
+ const ACCESS_LOGS_BUCKET_LOGICAL_ID = "AccessLogsBucket";
3970
3971
  const CLOUDFRONT_ORIGIN_ACCESS_CONTROL_LOGICAL_ID = "OriginAccessControl";
3971
3972
  const CLOUDFRONT_RESPONSE_HEADERS_POLICY_LOGICAL_ID = "ResponseHeadersPolicy";
3972
3973
  const CLOUDFRONT_VIEWER_REQUEST_FUNCTION_LOGICAL_ID = "ViewerRequestFunction";
@@ -4092,7 +4093,55 @@ const getBucketStaticWebsiteTemplate = ({ spa }) => {
4092
4093
  } }
4093
4094
  };
4094
4095
  };
4095
- const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, redirectToTrailingSlash, responseHeaders = [], responseHeadersPolicy, spa, hostedZoneName, viewerRequestFunctionCode }) => {
4096
+ /**
4097
+ * The bucket CloudFront standard (legacy) logging writes to. CloudFront grants
4098
+ * its log delivery account access through a bucket ACL, so ACLs stay enabled
4099
+ * (`BucketOwnerPreferred`); every other public access path is blocked. The
4100
+ * bucket is retained when the stack is deleted, because CloudFormation cannot
4101
+ * delete a bucket that still holds log files.
4102
+ *
4103
+ * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/standard-logging-legacy-s3.html
4104
+ */
4105
+ const getAccessLogsBucket = ({ accessLogsRetentionDays }) => {
4106
+ return {
4107
+ Type: "AWS::S3::Bucket",
4108
+ DeletionPolicy: "Retain",
4109
+ UpdateReplacePolicy: "Retain",
4110
+ Properties: {
4111
+ LifecycleConfiguration: { Rules: [{
4112
+ Id: "ExpireAccessLogs",
4113
+ Status: "Enabled",
4114
+ ExpirationInDays: accessLogsRetentionDays
4115
+ }] },
4116
+ OwnershipControls: { Rules: [{ ObjectOwnership: "BucketOwnerPreferred" }] },
4117
+ PublicAccessBlockConfiguration: {
4118
+ BlockPublicAcls: true,
4119
+ BlockPublicPolicy: true,
4120
+ IgnorePublicAcls: true,
4121
+ RestrictPublicBuckets: true
4122
+ }
4123
+ }
4124
+ };
4125
+ };
4126
+ /**
4127
+ * Adds the access logs bucket and points the distribution's logging at it.
4128
+ */
4129
+ const addAccessLogs = ({ accessLogsRetentionDays, cloudFrontResources }) => {
4130
+ if (!accessLogsRetentionDays) return;
4131
+ cloudFrontResources[ACCESS_LOGS_BUCKET_LOGICAL_ID] = getAccessLogsBucket({ accessLogsRetentionDays });
4132
+ const distribution = cloudFrontResources[CLOUDFRONT_DISTRIBUTION_LOGICAL_ID];
4133
+ distribution.Properties = {
4134
+ ...distribution.Properties,
4135
+ DistributionConfig: {
4136
+ ...distribution.Properties?.DistributionConfig,
4137
+ Logging: {
4138
+ Bucket: { "Fn::GetAtt": [ACCESS_LOGS_BUCKET_LOGICAL_ID, "DomainName"] },
4139
+ IncludeCookies: false
4140
+ }
4141
+ }
4142
+ };
4143
+ };
4144
+ const getCloudFrontTemplate = ({ accessLogsRetentionDays, acm, aliases = [], appendIndexHtml, redirectToTrailingSlash, responseHeaders = [], responseHeadersPolicy, spa, hostedZoneName, viewerRequestFunctionCode }) => {
4096
4145
  const template = {
4097
4146
  AWSTemplateFormatVersion: "2010-09-09",
4098
4147
  Resources: {
@@ -4221,6 +4270,10 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, redirectToT
4221
4270
  Properties: { ResponseHeadersPolicyConfig: getResponseHeadersPolicyConfig({ responseHeaders }) }
4222
4271
  } } : {}
4223
4272
  };
4273
+ addAccessLogs({
4274
+ accessLogsRetentionDays,
4275
+ cloudFrontResources
4276
+ });
4224
4277
  if (acm) {
4225
4278
  const acmCertificateArn = /^arn:aws:acm:[-a-z0-9]+:\d{12}:certificate\/[-a-z0-9]+$/.test(acm) ? acm : { "Fn::ImportValue": acm };
4226
4279
  /**
@@ -4363,8 +4416,17 @@ const getCloudFrontTemplate = ({ acm, aliases = [], appendIndexHtml, redirectToT
4363
4416
  };
4364
4417
  return template;
4365
4418
  };
4366
- const getStaticAppTemplate = ({ acm, aliases, appendIndexHtml, cloudfront, redirectToTrailingSlash, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, viewerRequestFunctionCode }) => {
4419
+ const getStaticAppTemplate = ({ accessLogsRetentionDays, acm, aliases, appendIndexHtml, cloudfront, redirectToTrailingSlash, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, viewerRequestFunctionCode }) => {
4420
+ /**
4421
+ * Access logs are written by CloudFront, so a bucket only deploy has nothing
4422
+ * to log them.
4423
+ */
4424
+ if (accessLogsRetentionDays !== void 0) {
4425
+ if (!cloudfront) throw new Error("The access-logs-retention-days option requires the cloudfront option.");
4426
+ if (!Number.isInteger(accessLogsRetentionDays) || accessLogsRetentionDays < 1) throw new Error("The access-logs-retention-days option must be a whole number of days, 1 or more.");
4427
+ }
4367
4428
  if (cloudfront) return getCloudFrontTemplate({
4429
+ accessLogsRetentionDays,
4368
4430
  acm,
4369
4431
  aliases,
4370
4432
  appendIndexHtml,
@@ -4449,21 +4511,12 @@ const logPrefix$4 = "static-app";
4449
4511
  * 1. Upload static files to the host bucket S3.
4450
4512
  * 1. Remove old deployment versions. Keep only the 3 most recent ones.
4451
4513
  */
4452
- const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, cloudfront, contentTypes, redirectToTrailingSlash, responseHeaders, responseHeadersPolicy, spa, hostedZoneName, region, skipUpload, uploadSourceMaps, viewerRequestFunctionCode }) => {
4514
+ const deployStaticApp = async ({ buildFolder, contentTypes, skipUpload, uploadSourceMaps, viewerRequestFunctionCode, ...templateOptions }) => {
4453
4515
  try {
4454
4516
  const { stackName } = await handleDeployInitialization({ logPrefix: logPrefix$4 });
4455
4517
  const params = { StackName: stackName };
4456
4518
  const template = getStaticAppTemplate({
4457
- acm,
4458
- aliases,
4459
- appendIndexHtml,
4460
- cloudfront,
4461
- redirectToTrailingSlash,
4462
- responseHeaders,
4463
- responseHeadersPolicy,
4464
- spa,
4465
- hostedZoneName,
4466
- region,
4519
+ ...templateOptions,
4467
4520
  viewerRequestFunctionCode: viewerRequestFunctionCode ? readViewerRequestFunctionCode({ filePath: viewerRequestFunctionCode }) : void 0
4468
4521
  });
4469
4522
  const bucket = await getStaticAppBucket({ stackName });
@@ -4476,7 +4529,7 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4476
4529
  return uploadBuiltAppToS3({
4477
4530
  buildFolder,
4478
4531
  bucket,
4479
- cloudfront,
4532
+ cloudfront: templateOptions.cloudfront,
4480
4533
  contentTypes,
4481
4534
  uploadSourceMaps
4482
4535
  });
@@ -4515,6 +4568,10 @@ const deployStaticApp = async ({ acm, aliases, appendIndexHtml, buildFolder, clo
4515
4568
  //#endregion
4516
4569
  //#region src/deploy/staticApp/command.ts
4517
4570
  const options$4 = {
4571
+ "access-logs-retention-days": {
4572
+ describe: "Turn on CloudFront access logs and keep them this many days. The logs go to a private S3 bucket of the stack, which is kept when the stack is deleted. Requires the `cloudfront` option.",
4573
+ type: "number"
4574
+ },
4518
4575
  acm: {
4519
4576
  describe: "The ARN of the certificate or the name of the exported variable whose value is the ARN of the certificate that will be associated to CloudFront.",
4520
4577
  type: "string"
@@ -4632,6 +4689,15 @@ const deployStaticAppCommand = {
4632
4689
  */
4633
4690
  if (spa) throw new Error("The redirect-to-trailing-slash and spa options are mutually exclusive. An extension-less URI of a SPA is a client route, not a directory, so redirecting it to a trailing slash changes the URL of every route.");
4634
4691
  return true;
4692
+ }).check(({ accessLogsRetentionDays, cloudfront }) => {
4693
+ if (accessLogsRetentionDays === void 0) return true;
4694
+ /**
4695
+ * Access logs are written by CloudFront, so a bucket only deploy has
4696
+ * nothing to log them.
4697
+ */
4698
+ if (!cloudfront) throw new Error("The access-logs-retention-days option requires the cloudfront option.");
4699
+ if (!Number.isInteger(accessLogsRetentionDays) || accessLogsRetentionDays < 1) throw new Error("The access-logs-retention-days option must be a whole number of days, 1 or more.");
4700
+ return true;
4635
4701
  }).check(({ appendIndexHtml, cloudfront, viewerRequestFunctionCode }) => {
4636
4702
  if (!viewerRequestFunctionCode) return true;
4637
4703
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carlin",
3
- "version": "6.0.2",
3
+ "version": "6.1.0",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "homepage": "https://ttoss.dev/docs/carlin/",
@@ -61,9 +61,9 @@
61
61
  "ts-node": "^10.9.2",
62
62
  "uglify-js": "^3.19.3",
63
63
  "yargs": "^18.2.0",
64
- "@ttoss/read-config-file": "^4.0.0",
65
- "@ttoss/config": "^4.0.0",
66
- "@ttoss/cloudformation": "^0.20.1"
64
+ "@ttoss/cloudformation": "^0.20.2",
65
+ "@ttoss/read-config-file": "^4.0.1",
66
+ "@ttoss/config": "^4.0.1"
67
67
  },
68
68
  "devDependencies": {
69
69
  "@types/aws-lambda": "^8.10.164",
@@ -79,7 +79,7 @@
79
79
  "jest": "^30.5.2",
80
80
  "tsdown": "^0.23.1",
81
81
  "typescript": "~6.0.3",
82
- "@ttoss/test-utils": "^7.1.0"
82
+ "@ttoss/test-utils": "^7.1.1"
83
83
  },
84
84
  "publishConfig": {
85
85
  "access": "public",