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
|
@@ -76,8 +76,8 @@ export interface CanopyCmsAttachOptions {
|
|
|
76
76
|
*
|
|
77
77
|
* It must set `x-forwarded-host` from the viewer's Host, or sign-in
|
|
78
78
|
* redirects point at the Function URL and 403. It must not gate on HTTP
|
|
79
|
-
* Basic auth
|
|
80
|
-
*
|
|
79
|
+
* Basic auth; the CMS authenticates these routes itself. A Basic-gated site sets
|
|
80
|
+
* the CMS config's `unauthenticatedStatus: 419` (see docs/deploying-to-aws.md).
|
|
81
81
|
*
|
|
82
82
|
* @default - a function that sets `x-forwarded-host` and nothing else
|
|
83
83
|
*/
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Construct } from 'constructs';
|
|
2
|
+
import { CfnParameter, aws_iam as iam, aws_s3 as s3 } from 'aws-cdk-lib';
|
|
3
|
+
/** Where user data downloads the bundle to and checks it, before installing it. */
|
|
4
|
+
export declare const WORKER_BUNDLE_DOWNLOAD_PATH = "/tmp/canopy-worker.js";
|
|
5
|
+
/** Where user data downloads the instance files to and checks them. */
|
|
6
|
+
export declare const INSTANCE_FILES_DOWNLOAD_PATH = "/tmp/canopy-instance-files.sh";
|
|
7
|
+
/**
|
|
8
|
+
* Where the worker's bundle comes from.
|
|
9
|
+
*
|
|
10
|
+
* - `'asset'` (default): a CDK asset, so a canopycms upgrade changes the
|
|
11
|
+
* template and only a template deploy moves the worker.
|
|
12
|
+
* - `'parameter'`: a bucket of bundles keyed by sha256, chosen by a template
|
|
13
|
+
* parameter, so a parameter-only change set (`--use-previous-template`)
|
|
14
|
+
* can roll the worker. While the parameter is empty the worker runs the
|
|
15
|
+
* template's own asset, as in `'asset'` mode. Once set, the parameter
|
|
16
|
+
* outlives template deploys: `cdk deploy` keeps a parameter's previous
|
|
17
|
+
* value.
|
|
18
|
+
*/
|
|
19
|
+
export type WorkerCode = {
|
|
20
|
+
source: 'asset';
|
|
21
|
+
} | {
|
|
22
|
+
source: 'parameter';
|
|
23
|
+
};
|
|
24
|
+
/** An S3 object user data downloads, and the sha256 it must have, as tokens for user data. */
|
|
25
|
+
interface CheckedObject {
|
|
26
|
+
readonly bucketName: string;
|
|
27
|
+
readonly objectKey: string;
|
|
28
|
+
readonly sha256: string;
|
|
29
|
+
}
|
|
30
|
+
interface WorkerBundleSource extends CheckedObject {
|
|
31
|
+
/** The instance files (see {@link INSTANCE_FILES_PATH}). */
|
|
32
|
+
readonly instanceFiles: CheckedObject;
|
|
33
|
+
/** Parameter mode only. */
|
|
34
|
+
readonly bundleBucket?: s3.Bucket;
|
|
35
|
+
readonly sha256Parameter?: CfnParameter;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The worker bundle's source, with the worker granted read on exactly what it
|
|
39
|
+
* may download.
|
|
40
|
+
*/
|
|
41
|
+
export declare function workerBundleSource(scope: Construct, workerCode: WorkerCode, workerRole: iam.IRole): WorkerBundleSource;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { CfnCondition, CfnOutput, CfnParameter, Duration, Fn, RemovalPolicy, Token, aws_iam as iam, aws_s3 as s3, aws_s3_assets as s3assets, } from 'aws-cdk-lib';
|
|
6
|
+
import { WORKER_CONTRACT_VERSION } from './worker-lifecycle.js';
|
|
7
|
+
// This package is `"type": "module"`, so `__dirname` is not a global in its
|
|
8
|
+
// compiled output. Vitest shims it, so this file's tests would not notice.
|
|
9
|
+
// Same fix as ./asset-support.ts.
|
|
10
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
/**
|
|
12
|
+
* The worker bundle: the single file esbuild writes (`pnpm run build:worker`),
|
|
13
|
+
* shipped to the instance as-is so its sha256 is known at synth. A directory
|
|
14
|
+
* asset would be zipped by cdk-assets at publish time, after synth.
|
|
15
|
+
*/
|
|
16
|
+
const WORKER_BUNDLE_PATH = path.join(__dirname, '../../worker/dist/index.js');
|
|
17
|
+
/** Where user data downloads the bundle to and checks it, before installing it. */
|
|
18
|
+
export const WORKER_BUNDLE_DOWNLOAD_PATH = '/tmp/canopy-worker.js';
|
|
19
|
+
/**
|
|
20
|
+
* The instance's units and scripts (`worker-units.ts` `instanceFilesScript`), which `build:worker`
|
|
21
|
+
* writes beside the bundle. Always the template's own asset, whichever `workerCode`: what the
|
|
22
|
+
* template provides to a bundle is the template's.
|
|
23
|
+
*/
|
|
24
|
+
const INSTANCE_FILES_PATH = path.join(__dirname, '../../worker/dist/instance-files.sh');
|
|
25
|
+
/** Where user data downloads the instance files to and checks them. */
|
|
26
|
+
export const INSTANCE_FILES_DOWNLOAD_PATH = '/tmp/canopy-instance-files.sh';
|
|
27
|
+
/** Where `workerCode: { source: 'parameter' }` expects bundles in its bucket: `<prefix><sha256>.js`. */
|
|
28
|
+
const WORKER_BUNDLE_KEY_PREFIX = 'canopy-worker/';
|
|
29
|
+
function sha256OfFile(file) {
|
|
30
|
+
return createHash('sha256').update(readFileSync(file)).digest('hex');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The worker bundle's source, with the worker granted read on exactly what it
|
|
34
|
+
* may download.
|
|
35
|
+
*/
|
|
36
|
+
export function workerBundleSource(scope, workerCode, workerRole) {
|
|
37
|
+
const asset = new s3assets.Asset(scope, 'WorkerCode', { path: WORKER_BUNDLE_PATH });
|
|
38
|
+
const assetSha256 = sha256OfFile(WORKER_BUNDLE_PATH);
|
|
39
|
+
const filesAsset = new s3assets.Asset(scope, 'WorkerInstanceFiles', { path: INSTANCE_FILES_PATH });
|
|
40
|
+
const instanceFiles = {
|
|
41
|
+
bucketName: filesAsset.s3BucketName,
|
|
42
|
+
objectKey: filesAsset.s3ObjectKey,
|
|
43
|
+
sha256: sha256OfFile(INSTANCE_FILES_PATH),
|
|
44
|
+
};
|
|
45
|
+
// These objects, not `asset.grantRead`'s whole bootstrap bucket. No KMS
|
|
46
|
+
// grant: the bootstrap bucket's key (aws/s3, or the key bootstrap creates)
|
|
47
|
+
// lets any principal in the account decrypt through S3.
|
|
48
|
+
const readable = [
|
|
49
|
+
asset.bucket.arnForObjects(asset.s3ObjectKey),
|
|
50
|
+
filesAsset.bucket.arnForObjects(filesAsset.s3ObjectKey),
|
|
51
|
+
];
|
|
52
|
+
if (workerCode.source === 'asset') {
|
|
53
|
+
workerRole.addToPrincipalPolicy(new iam.PolicyStatement({ actions: ['s3:GetObject'], resources: readable }));
|
|
54
|
+
return {
|
|
55
|
+
bucketName: asset.s3BucketName,
|
|
56
|
+
objectKey: asset.s3ObjectKey,
|
|
57
|
+
sha256: assetSha256,
|
|
58
|
+
instanceFiles,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const bundleBucket = new s3.Bucket(scope, 'WorkerBundleBucket', {
|
|
62
|
+
versioned: true,
|
|
63
|
+
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
|
|
64
|
+
encryption: s3.BucketEncryption.S3_MANAGED,
|
|
65
|
+
enforceSSL: true,
|
|
66
|
+
removalPolicy: RemovalPolicy.RETAIN,
|
|
67
|
+
// Re-uploading a bundle under its own hash leaves a noncurrent version of
|
|
68
|
+
// the same bytes; a launch template only ever names current ones.
|
|
69
|
+
lifecycleRules: [{ noncurrentVersionExpiration: Duration.days(30) }],
|
|
70
|
+
});
|
|
71
|
+
// A bundle a running or rolled-back launch template names must stay
|
|
72
|
+
// downloadable. Not PutBucketPolicy too: that would lock CloudFormation out
|
|
73
|
+
// of this policy. Emptying the bucket starts by removing this statement.
|
|
74
|
+
bundleBucket.addToResourcePolicy(new iam.PolicyStatement({
|
|
75
|
+
effect: iam.Effect.DENY,
|
|
76
|
+
principals: [new iam.AnyPrincipal()],
|
|
77
|
+
actions: ['s3:DeleteObject', 's3:DeleteObjectVersion'],
|
|
78
|
+
resources: [bundleBucket.arnForObjects('*')],
|
|
79
|
+
}));
|
|
80
|
+
readable.push(bundleBucket.arnForObjects(`${WORKER_BUNDLE_KEY_PREFIX}*`));
|
|
81
|
+
workerRole.addToPrincipalPolicy(new iam.PolicyStatement({ actions: ['s3:GetObject'], resources: readable }));
|
|
82
|
+
// One value names the object AND is what it is checked against, so a key
|
|
83
|
+
// and a hash cannot disagree.
|
|
84
|
+
const sha256Parameter = new CfnParameter(scope, 'WorkerBundleSha256', {
|
|
85
|
+
type: 'String',
|
|
86
|
+
default: '',
|
|
87
|
+
allowedPattern: '^([0-9a-f]{64})?$',
|
|
88
|
+
description: `sha256 of the CanopyCMS worker bundle to run, uploaded to the worker bundle bucket as ` +
|
|
89
|
+
`${WORKER_BUNDLE_KEY_PREFIX}<sha256>.js. Empty runs the bundle this template was synthesized with.`,
|
|
90
|
+
});
|
|
91
|
+
const fromParameter = new CfnCondition(scope, 'WorkerBundleFromParameter', {
|
|
92
|
+
expression: Fn.conditionNot(Fn.conditionEquals(sha256Parameter.valueAsString, '')),
|
|
93
|
+
});
|
|
94
|
+
const pick = (whenParameter, whenAsset) => Token.asString(Fn.conditionIf(fromParameter.logicalId, whenParameter, whenAsset));
|
|
95
|
+
new CfnOutput(scope, 'WorkerBundleSha256ParameterName', {
|
|
96
|
+
description: 'The template parameter that selects the worker bundle',
|
|
97
|
+
value: sha256Parameter.logicalId,
|
|
98
|
+
});
|
|
99
|
+
// Read by a change-set gate before a parameter-only roll: the bundle's
|
|
100
|
+
// `index.js.contract` must not exceed it, because such a change set runs the
|
|
101
|
+
// new bundle under this template's unit.
|
|
102
|
+
new CfnOutput(scope, 'WorkerContract', {
|
|
103
|
+
description: 'The worker contract version this template provides',
|
|
104
|
+
value: String(WORKER_CONTRACT_VERSION),
|
|
105
|
+
});
|
|
106
|
+
new CfnOutput(scope, 'WorkerBundleBucketName', {
|
|
107
|
+
description: `The bucket holding worker bundles, as ${WORKER_BUNDLE_KEY_PREFIX}<sha256>.js`,
|
|
108
|
+
value: bundleBucket.bucketName,
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
bucketName: pick(bundleBucket.bucketName, asset.s3BucketName),
|
|
112
|
+
objectKey: pick(Fn.join('', [WORKER_BUNDLE_KEY_PREFIX, sha256Parameter.valueAsString, '.js']), asset.s3ObjectKey),
|
|
113
|
+
sha256: pick(sha256Parameter.valueAsString, assetSha256),
|
|
114
|
+
instanceFiles,
|
|
115
|
+
bundleBucket,
|
|
116
|
+
sha256Parameter,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names the construct (cms-service.ts) and the worker it deploys
|
|
3
|
+
* (worker/index.ts) must agree on. Dependency-free, because the worker bundle
|
|
4
|
+
* imports it and must not pull in aws-cdk-lib.
|
|
5
|
+
*/
|
|
6
|
+
/** The ASG terminating lifecycle hook the worker completes once it has drained. */
|
|
7
|
+
export declare const WORKER_DRAIN_HOOK_NAME = "canopycms-worker-drain";
|
|
8
|
+
/**
|
|
9
|
+
* The worker's exit status after draining for an instance termination. The
|
|
10
|
+
* systemd unit lists it in `RestartPreventExitStatus=`, so `Restart=always`
|
|
11
|
+
* does not start a fresh worker on an instance about to disappear.
|
|
12
|
+
*/
|
|
13
|
+
export declare const EXIT_DRAINED_FOR_TERMINATION = 75;
|
|
14
|
+
/**
|
|
15
|
+
* The worker's exit status after it stopped itself (its EFS lock was
|
|
16
|
+
* compromised). Non-zero, and deliberately not {@link EXIT_DRAINED_FOR_TERMINATION}
|
|
17
|
+
* (75), which the unit lists in `RestartPreventExitStatus=`: this one must let
|
|
18
|
+
* `Restart=always` start a fresh worker. Not in `SuccessExitStatus=` either.
|
|
19
|
+
* 69 is sysexits' EX_UNAVAILABLE.
|
|
20
|
+
*/
|
|
21
|
+
export declare const EXIT_WORKER_SELF_STOPPED = 69;
|
|
22
|
+
/**
|
|
23
|
+
* Set to `spot` in the worker's environment when it runs on spot capacity, which
|
|
24
|
+
* arms the watch for a spot interruption notice.
|
|
25
|
+
*/
|
|
26
|
+
export declare const WORKER_CAPACITY_ENV = "CANOPYCMS_WORKER_CAPACITY";
|
|
27
|
+
/**
|
|
28
|
+
* On the worker unit: the path of the GitHub gateway's socket. Its presence selects split mode,
|
|
29
|
+
* where the worker holds no credential and reaches GitHub only through the gateway.
|
|
30
|
+
*/
|
|
31
|
+
export declare const GITHUB_GATEWAY_SOCKET_ENV = "CANOPYCMS_GITHUB_GATEWAY_SOCKET";
|
|
32
|
+
/**
|
|
33
|
+
* The worker contract at which a bundle runs only split: from it on, a worker unit without
|
|
34
|
+
* {@link GITHUB_GATEWAY_SOCKET_ENV} is refused rather than run holding the credential.
|
|
35
|
+
*/
|
|
36
|
+
export declare const GATEWAY_SPLIT_CONTRACT = 2;
|
|
37
|
+
/** Where `canopy-github.socket` listens, and the worker unit's {@link GITHUB_GATEWAY_SOCKET_ENV}. */
|
|
38
|
+
export declare const GITHUB_GATEWAY_SOCKET_PATH = "/run/canopy-github/gateway.sock";
|
|
39
|
+
/**
|
|
40
|
+
* The bundle's first lines carry `<prefix><n>`, `n` being {@link WORKER_CONTRACT_VERSION}
|
|
41
|
+
* (worker/build.ts writes it). User data reads it to choose the units it installs: a bundle without
|
|
42
|
+
* it needs at most contract 1.
|
|
43
|
+
*/
|
|
44
|
+
export declare const WORKER_CONTRACT_BANNER_PREFIX = "// canopy-worker-contract: ";
|
|
45
|
+
/**
|
|
46
|
+
* The worker contract version: stamped into the unit by the construct (and,
|
|
47
|
+
* in parameter mode, into the `WorkerContract` stack output), and published beside the
|
|
48
|
+
* bundle as `worker/dist/index.js.contract`, so a change-set gate can compare
|
|
49
|
+
* the two before rolling a bundle. See {@link WORKER_CONTRACT_REQUIREMENTS}.
|
|
50
|
+
*/
|
|
51
|
+
export declare const WORKER_CONTRACT_VERSION: 2;
|
|
52
|
+
/** The unit's `Environment=` variable carrying its worker contract version. */
|
|
53
|
+
export declare const WORKER_CONTRACT_ENV = "CANOPYCMS_WORKER_CONTRACT";
|
|
54
|
+
/**
|
|
55
|
+
* The unit's worker contract version, read from the worker's environment as
|
|
56
|
+
* {@link WORKER_CONTRACT_REQUIREMENTS} describes, or `NaN` for a stamp that is
|
|
57
|
+
* not a whole number.
|
|
58
|
+
*/
|
|
59
|
+
export declare function unitWorkerContract(env: Readonly<Record<string, string | undefined>>): number;
|
|
60
|
+
/**
|
|
61
|
+
* The fatal line for a unit older than this bundle, or `undefined` when the
|
|
62
|
+
* unit's contract is new enough.
|
|
63
|
+
*/
|
|
64
|
+
export declare function workerContractShortfall(env: Readonly<Record<string, string | undefined>>): string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* The text of the line `syncGit()` (canopycms `worker/git-sync.ts`) logs at the
|
|
67
|
+
* start of every git-sync cycle. The worker-down alarm's metric filter counts
|
|
68
|
+
* it, so the alarm misfires if the worker's text drifts from this one.
|
|
69
|
+
*/
|
|
70
|
+
export declare const WORKER_SYNC_LOG_PHRASE = "Syncing git...";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names the construct (cms-service.ts) and the worker it deploys
|
|
3
|
+
* (worker/index.ts) must agree on. Dependency-free, because the worker bundle
|
|
4
|
+
* imports it and must not pull in aws-cdk-lib.
|
|
5
|
+
*/
|
|
6
|
+
/** The ASG terminating lifecycle hook the worker completes once it has drained. */
|
|
7
|
+
export const WORKER_DRAIN_HOOK_NAME = 'canopycms-worker-drain';
|
|
8
|
+
/**
|
|
9
|
+
* The worker's exit status after draining for an instance termination. The
|
|
10
|
+
* systemd unit lists it in `RestartPreventExitStatus=`, so `Restart=always`
|
|
11
|
+
* does not start a fresh worker on an instance about to disappear.
|
|
12
|
+
*/
|
|
13
|
+
export const EXIT_DRAINED_FOR_TERMINATION = 75;
|
|
14
|
+
/**
|
|
15
|
+
* The worker's exit status after it stopped itself (its EFS lock was
|
|
16
|
+
* compromised). Non-zero, and deliberately not {@link EXIT_DRAINED_FOR_TERMINATION}
|
|
17
|
+
* (75), which the unit lists in `RestartPreventExitStatus=`: this one must let
|
|
18
|
+
* `Restart=always` start a fresh worker. Not in `SuccessExitStatus=` either.
|
|
19
|
+
* 69 is sysexits' EX_UNAVAILABLE.
|
|
20
|
+
*/
|
|
21
|
+
export const EXIT_WORKER_SELF_STOPPED = 69;
|
|
22
|
+
/**
|
|
23
|
+
* Set to `spot` in the worker's environment when it runs on spot capacity, which
|
|
24
|
+
* arms the watch for a spot interruption notice.
|
|
25
|
+
*/
|
|
26
|
+
export const WORKER_CAPACITY_ENV = 'CANOPYCMS_WORKER_CAPACITY';
|
|
27
|
+
/**
|
|
28
|
+
* On the worker unit: the path of the GitHub gateway's socket. Its presence selects split mode,
|
|
29
|
+
* where the worker holds no credential and reaches GitHub only through the gateway.
|
|
30
|
+
*/
|
|
31
|
+
export const GITHUB_GATEWAY_SOCKET_ENV = 'CANOPYCMS_GITHUB_GATEWAY_SOCKET';
|
|
32
|
+
/**
|
|
33
|
+
* The worker contract at which a bundle runs only split: from it on, a worker unit without
|
|
34
|
+
* {@link GITHUB_GATEWAY_SOCKET_ENV} is refused rather than run holding the credential.
|
|
35
|
+
*/
|
|
36
|
+
export const GATEWAY_SPLIT_CONTRACT = 2;
|
|
37
|
+
/** Where `canopy-github.socket` listens, and the worker unit's {@link GITHUB_GATEWAY_SOCKET_ENV}. */
|
|
38
|
+
export const GITHUB_GATEWAY_SOCKET_PATH = '/run/canopy-github/gateway.sock';
|
|
39
|
+
/**
|
|
40
|
+
* The bundle's first lines carry `<prefix><n>`, `n` being {@link WORKER_CONTRACT_VERSION}
|
|
41
|
+
* (worker/build.ts writes it). User data reads it to choose the units it installs: a bundle without
|
|
42
|
+
* it needs at most contract 1.
|
|
43
|
+
*/
|
|
44
|
+
export const WORKER_CONTRACT_BANNER_PREFIX = '// canopy-worker-contract: ';
|
|
45
|
+
/**
|
|
46
|
+
* What each worker contract version requires of the unit and template, in
|
|
47
|
+
* order: entry `i` is version `i + 1`. worker-units.ts stamps the latest version
|
|
48
|
+
* into the split units, and the version before {@link GATEWAY_SPLIT_CONTRACT}
|
|
49
|
+
* into the single-process unit for an older bundle (see
|
|
50
|
+
* {@link WORKER_CONTRACT_BANNER_PREFIX}). A bundle refuses to
|
|
51
|
+
* start under a unit whose version is lower.
|
|
52
|
+
*
|
|
53
|
+
* A unit with no stamp predates the stamp, so its version is read from what
|
|
54
|
+
* it observably provides: 1 when `STATE_DIRECTORY` is set (systemd sets it
|
|
55
|
+
* from `StateDirectory=`), else 0. Inference never reads past 1, so from
|
|
56
|
+
* version 2 on only an explicit stamp satisfies a bundle.
|
|
57
|
+
*
|
|
58
|
+
* Append an entry ONLY when the bundle starts to need something new from the
|
|
59
|
+
* unit or template. Anything a bundle merely tolerates the absence of does
|
|
60
|
+
* not count. The bundle's "template too old" line names every entry above the
|
|
61
|
+
* unit's version, so write it as the setting an operator would add.
|
|
62
|
+
*/
|
|
63
|
+
const WORKER_CONTRACT_REQUIREMENTS = [
|
|
64
|
+
'StateDirectory=canopy-worker',
|
|
65
|
+
`canopy-github.socket and canopy-github.service, and ${GITHUB_GATEWAY_SOCKET_ENV} on the worker unit`,
|
|
66
|
+
];
|
|
67
|
+
/**
|
|
68
|
+
* The worker contract version: stamped into the unit by the construct (and,
|
|
69
|
+
* in parameter mode, into the `WorkerContract` stack output), and published beside the
|
|
70
|
+
* bundle as `worker/dist/index.js.contract`, so a change-set gate can compare
|
|
71
|
+
* the two before rolling a bundle. See {@link WORKER_CONTRACT_REQUIREMENTS}.
|
|
72
|
+
*/
|
|
73
|
+
export const WORKER_CONTRACT_VERSION = WORKER_CONTRACT_REQUIREMENTS.length;
|
|
74
|
+
/** The unit's `Environment=` variable carrying its worker contract version. */
|
|
75
|
+
export const WORKER_CONTRACT_ENV = 'CANOPYCMS_WORKER_CONTRACT';
|
|
76
|
+
/**
|
|
77
|
+
* The unit's worker contract version, read from the worker's environment as
|
|
78
|
+
* {@link WORKER_CONTRACT_REQUIREMENTS} describes, or `NaN` for a stamp that is
|
|
79
|
+
* not a whole number.
|
|
80
|
+
*/
|
|
81
|
+
export function unitWorkerContract(env) {
|
|
82
|
+
const stamp = env[WORKER_CONTRACT_ENV];
|
|
83
|
+
if (stamp === undefined)
|
|
84
|
+
return env.STATE_DIRECTORY ? 1 : 0;
|
|
85
|
+
return /^\d+$/.test(stamp) ? Number(stamp) : NaN;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The fatal line for a unit older than this bundle, or `undefined` when the
|
|
89
|
+
* unit's contract is new enough.
|
|
90
|
+
*/
|
|
91
|
+
export function workerContractShortfall(env) {
|
|
92
|
+
const unit = unitWorkerContract(env);
|
|
93
|
+
if (Number.isNaN(unit)) {
|
|
94
|
+
return `canopy-worker: ${WORKER_CONTRACT_ENV}=${JSON.stringify(env[WORKER_CONTRACT_ENV])} on the worker unit is not a whole number`;
|
|
95
|
+
}
|
|
96
|
+
if (unit >= WORKER_CONTRACT_VERSION)
|
|
97
|
+
return undefined;
|
|
98
|
+
return (`canopy-worker: template too old for this bundle: needs worker contract ` +
|
|
99
|
+
`${WORKER_CONTRACT_VERSION}, unit has ${unit} (` +
|
|
100
|
+
WORKER_CONTRACT_REQUIREMENTS.slice(unit)
|
|
101
|
+
.map((setting, i) => `contract ${unit + i + 1} adds ${setting}`)
|
|
102
|
+
.join('; ') +
|
|
103
|
+
`). Deploy the stack template ` +
|
|
104
|
+
`before rolling this bundle.`);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The text of the line `syncGit()` (canopycms `worker/git-sync.ts`) logs at the
|
|
108
|
+
* start of every git-sync cycle. The worker-down alarm's metric filter counts
|
|
109
|
+
* it, so the alarm misfires if the worker's text drifts from this one.
|
|
110
|
+
*/
|
|
111
|
+
export const WORKER_SYNC_LOG_PHRASE = 'Syncing git...';
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The worker instance's systemd units and the `canopy-inspect` sandbox. `build:worker` packs them
|
|
3
|
+
* into `worker/dist/instance-files.sh` (`instanceFilesScript`), which user data (cms-service.ts)
|
|
4
|
+
* downloads, checks against its sha256 and installs from, by layout. The split's three units are
|
|
5
|
+
* also checked in under worker/, byte for byte, for a hand install; worker-units.test.ts holds the
|
|
6
|
+
* copies together.
|
|
7
|
+
*
|
|
8
|
+
* Two layouts, chosen at boot by the bundle's contract banner (WORKER_CONTRACT_BANNER_PREFIX):
|
|
9
|
+
* - split, for a bundle at GATEWAY_SPLIT_CONTRACT or later: `canopy-worker.service` with no
|
|
10
|
+
* network and no secret, `canopy-github.service` holding every secret with no view of EFS, and
|
|
11
|
+
* `canopy-github.socket` between them;
|
|
12
|
+
* - single-process, for an older bundle: one `canopy-worker.service` holding the secrets, as
|
|
13
|
+
* before the split.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Where the Lambda AND the worker mount the `WorkspaceAP` access point (rooted
|
|
17
|
+
* at EFS:/workspace), and so the workspace root of both.
|
|
18
|
+
*
|
|
19
|
+
* One path string, not merely one directory: git records absolute paths on
|
|
20
|
+
* EFS (a workspace clone's `origin` is `<root>/remote.git`), and a path written
|
|
21
|
+
* by one process must resolve for the other.
|
|
22
|
+
*/
|
|
23
|
+
export declare const EFS_MOUNT_PATH = "/mnt/efs";
|
|
24
|
+
/** The worker's code, env files and the inspect sandbox's scripts: root-owned. */
|
|
25
|
+
export declare const WORKER_INSTALL_DIR = "/opt/canopy-worker";
|
|
26
|
+
/** The GitHub gateway's system user (D8 of the split's plan): a static, non-login account. */
|
|
27
|
+
export declare const GATEWAY_USER = "canopy-github";
|
|
28
|
+
/** The worker's user; `canopy-github.socket` belongs to it, so only it (and root) can connect. */
|
|
29
|
+
export declare const WORKER_USER = "ec2-user";
|
|
30
|
+
/** The worker contract the single-process unit provides: the last before the split. */
|
|
31
|
+
export declare const SINGLE_PROCESS_UNIT_CONTRACT: number;
|
|
32
|
+
/**
|
|
33
|
+
* The sandbox both services share. Node and git need none of what these take away;
|
|
34
|
+
* `MemoryDenyWriteExecute` is left out because V8's JIT writes executable memory.
|
|
35
|
+
*/
|
|
36
|
+
export declare const WORKER_SANDBOX_DIRECTIVES: readonly ["NoNewPrivileges=yes", "ProtectSystem=strict", "ProtectHome=tmpfs", "PrivateTmp=yes", "PrivateDevices=yes", "ProtectProc=invisible", "ProtectKernelTunables=yes", "ProtectKernelModules=yes", "ProtectKernelLogs=yes", "ProtectControlGroups=yes", "ProtectClock=yes", "ProtectHostname=yes", "RestrictNamespaces=yes", "RestrictSUIDSGID=yes", "RestrictRealtime=yes", "LockPersonality=yes", "SystemCallArchitectures=native", "CapabilityBoundingSet="];
|
|
37
|
+
/**
|
|
38
|
+
* The split worker's sandbox, which `canopy-inspect` applies too, so an operator's command on EFS
|
|
39
|
+
* runs exactly as confined as the worker: the shared sandbox, the workspace writable, and the cut.
|
|
40
|
+
* - `PrivateNetwork=yes`: only `lo`, so no route to the instance metadata service.
|
|
41
|
+
* - `IPAddressDeny=any`: a second layer, which needs cgroup BPF (silently absent without it).
|
|
42
|
+
* - `RestrictAddressFamilies=AF_UNIX`: `socket()` only; the gateway's socket is a file.
|
|
43
|
+
* - `PrivateIPC=yes`, the hidden paths, `SystemCallFilter=@system-service` (node and git need
|
|
44
|
+
* nothing outside it), and `LimitCORE=0`.
|
|
45
|
+
*/
|
|
46
|
+
export declare const WORKER_SPLIT_SANDBOX: readonly ["NoNewPrivileges=yes", "ProtectSystem=strict", "ProtectHome=tmpfs", "PrivateTmp=yes", "PrivateDevices=yes", "ProtectProc=invisible", "ProtectKernelTunables=yes", "ProtectKernelModules=yes", "ProtectKernelLogs=yes", "ProtectControlGroups=yes", "ProtectClock=yes", "ProtectHostname=yes", "RestrictNamespaces=yes", "RestrictSUIDSGID=yes", "RestrictRealtime=yes", "LockPersonality=yes", "SystemCallArchitectures=native", "CapabilityBoundingSet=", "ReadWritePaths=/mnt/efs", "PrivateNetwork=yes", "IPAddressDeny=any", "RestrictAddressFamilies=AF_UNIX", "PrivateIPC=yes", "InaccessiblePaths=-/run/dbus", "InaccessiblePaths=-/dev/shm", "InaccessiblePaths=-/var/log/journal", "InaccessiblePaths=-/run/log/journal", "SystemCallFilter=@system-service", "LimitCORE=0"];
|
|
47
|
+
/**
|
|
48
|
+
* The gateway's sandbox: the shared one, no view of EFS, the network it needs (`AF_NETLINK` for
|
|
49
|
+
* glibc's `getaddrinfo`), and the rest of the worker's additions.
|
|
50
|
+
*/
|
|
51
|
+
export declare const GATEWAY_SANDBOX: readonly ["NoNewPrivileges=yes", "ProtectSystem=strict", "ProtectHome=tmpfs", "PrivateTmp=yes", "PrivateDevices=yes", "ProtectProc=invisible", "ProtectKernelTunables=yes", "ProtectKernelModules=yes", "ProtectKernelLogs=yes", "ProtectControlGroups=yes", "ProtectClock=yes", "ProtectHostname=yes", "RestrictNamespaces=yes", "RestrictSUIDSGID=yes", "RestrictRealtime=yes", "LockPersonality=yes", "SystemCallArchitectures=native", "CapabilityBoundingSet=", "InaccessiblePaths=/mnt/efs", "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK", "PrivateIPC=yes", "InaccessiblePaths=-/run/dbus", "InaccessiblePaths=-/dev/shm", "InaccessiblePaths=-/var/log/journal", "InaccessiblePaths=-/run/log/journal", "SystemCallFilter=@system-service", "LimitCORE=0"];
|
|
52
|
+
/** The worker's log; its directory and the file are root-owned. */
|
|
53
|
+
export declare const WORKER_LOG_DIR = "/var/log/canopy-worker";
|
|
54
|
+
export declare const WORKER_LOG = "/var/log/canopy-worker/worker.log";
|
|
55
|
+
/** The gateway's log; its directory and the file are root-owned. */
|
|
56
|
+
export declare const GATEWAY_LOG_DIR = "/var/log/canopy-github";
|
|
57
|
+
export declare const GATEWAY_LOG = "/var/log/canopy-github/gateway.log";
|
|
58
|
+
/**
|
|
59
|
+
* `canopy-worker.service`. Split: no network, no secret, the gateway's socket. Single-process:
|
|
60
|
+
* the unit as it was before the split, holding the secrets, for a bundle older than the split.
|
|
61
|
+
*/
|
|
62
|
+
export declare function workerUnit(layout: 'split' | 'single-process'): string[];
|
|
63
|
+
/** `canopy-github.service`: every secret, the network, no view of EFS. */
|
|
64
|
+
export declare function gatewayUnit(): string[];
|
|
65
|
+
/** `canopy-github.socket`: the worker's only way to the gateway. */
|
|
66
|
+
export declare function gatewaySocketUnit(): string[];
|
|
67
|
+
/** The `canopy-inspect` scripts' paths. */
|
|
68
|
+
export declare const CANOPY_INSPECT = "/opt/canopy-worker/canopy-inspect";
|
|
69
|
+
export declare const INSPECT_CHECKS = "/opt/canopy-worker/inspect-checks";
|
|
70
|
+
export declare const INSPECT_RC = "/opt/canopy-worker/inspect.rc";
|
|
71
|
+
/**
|
|
72
|
+
* `canopy-inspect`: run one command (or, with `--interactive`, a shell) as the worker's user,
|
|
73
|
+
* inside exactly the worker's sandbox, so a hostile repository on EFS reaches only what the
|
|
74
|
+
* worker reaches. `--interactive` relays the sandbox's terminal raw, and the worker can reach its
|
|
75
|
+
* pty (safeinsights/canopycms#604). Run as root. Nothing after `--` is composed by a shell
|
|
76
|
+
* outside the sandbox: inspect-checks runs its assertions, then execs the command.
|
|
77
|
+
*/
|
|
78
|
+
export declare function canopyInspectScript(): string[];
|
|
79
|
+
/**
|
|
80
|
+
* `inspect-checks`: inside the sandbox, prove it (the worker's own boot self-checks, then what a
|
|
81
|
+
* shell can see), then exec the operator's command in the same process.
|
|
82
|
+
*/
|
|
83
|
+
export declare function inspectChecksScript(): string[];
|
|
84
|
+
/** The interactive shell's rc file: a prompt that says where it is. */
|
|
85
|
+
export declare function inspectRc(): string[];
|
|
86
|
+
/**
|
|
87
|
+
* Where user-data unpacks the instance files (root-only), before installing those its layout
|
|
88
|
+
* needs.
|
|
89
|
+
*/
|
|
90
|
+
export declare const INSTANCE_FILES_DIR = "/run/canopy-instance-files";
|
|
91
|
+
/** The instance files, by their name in {@link INSTANCE_FILES_DIR}. */
|
|
92
|
+
export declare function instanceFiles(): Record<string, string[]>;
|
|
93
|
+
/**
|
|
94
|
+
* A script that writes {@link instanceFiles} into its working directory. `build:worker` writes it
|
|
95
|
+
* beside the bundle as `worker/dist/instance-files.sh`, and the construct ships it as an asset
|
|
96
|
+
* checked against its sha256, as the bundle is: EC2 caps user data at 16 KiB, which the files
|
|
97
|
+
* alone would nearly fill.
|
|
98
|
+
*/
|
|
99
|
+
export declare function instanceFilesScript(): string;
|