memory-anywhere 0.0.0-stage → 0.5.1
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/LICENSE +21 -0
- package/README.md +79 -2
- package/bin/memory-anywhere.mjs +59615 -0
- package/deploy/app.mjs +972 -0
- package/deploy/cdk.json +8 -0
- package/deploy/host/firstboot.sh +43 -0
- package/deploy/host/ma-updater.service +10 -0
- package/deploy/host/ma-updater.sh +252 -0
- package/deploy/host/ma-updater.timer +10 -0
- package/deploy/host/pins.env +24 -0
- package/deploy/host/release/first-store.sh +32 -0
- package/deploy/host/release/install.sh +249 -0
- package/deploy/host/release/libexec/ma-maint.sh +49 -0
- package/deploy/host/release/libexec/ma-selfcheck.sh +14 -0
- package/deploy/host/release/libexec/manifest-at.sh +42 -0
- package/deploy/host/release/libexec/outbox.sh +83 -0
- package/deploy/host/release/libexec/secrets.sh +15 -0
- package/deploy/host/release/tmpfiles.conf +13 -0
- package/deploy/host/release/units/caddy.service +26 -0
- package/deploy/host/release/units/litestream.service +28 -0
- package/deploy/host/release/units/ma-maint.service +7 -0
- package/deploy/host/release/units/ma-selfcheck.path +9 -0
- package/deploy/host/release/units/ma-selfcheck.service +9 -0
- package/deploy/host/release/units/memory-anywhere-outbox.path +9 -0
- package/deploy/host/release/units/memory-anywhere-outbox.service +21 -0
- package/deploy/host/release/units/memory-anywhere-secrets.service +25 -0
- package/deploy/host/release/units/memory-anywhere.service +35 -0
- package/deploy/release-keys/release.pub +2 -0
- package/package.json +28 -4
- package/plugins/claude-code/.claude-plugin/icon.png +0 -0
- package/plugins/claude-code/.claude-plugin/marketplace.json +12 -0
- package/plugins/claude-code/.claude-plugin/plugin.json +6 -0
- package/plugins/claude-code/.mcp.json +9 -0
- package/plugins/claude-code/README.md +11 -0
- package/plugins/claude-code/dist/memory-anywhere.mjs +59615 -0
- package/plugins/claude-code/hooks/hooks.json +30 -0
- package/plugins/claude-code/skills/memory-anywhere/SKILL.md +11 -0
- package/plugins/cursor/.cursor-plugin/plugin.json +6 -0
- package/plugins/cursor/dist/memory-anywhere.mjs +59615 -0
- package/plugins/cursor/rules/memory-anywhere.mdc +10 -0
package/deploy/app.mjs
ADDED
|
@@ -0,0 +1,972 @@
|
|
|
1
|
+
// deploy/aws/bin/app.ts
|
|
2
|
+
import { App } from "aws-cdk-lib";
|
|
3
|
+
|
|
4
|
+
// deploy/aws/lib/dir.ts
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
var ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
|
+
var PACKAGED_APP = join(ROOT, "deploy", "app.mjs");
|
|
10
|
+
var PACKAGED = existsSync(PACKAGED_APP);
|
|
11
|
+
var DEPLOY_AWS_DIR = PACKAGED ? join(ROOT, "deploy") : ROOT;
|
|
12
|
+
var AMI_CONTEXT_KEY = "memory-anywhere:ami-al2023-arm64";
|
|
13
|
+
|
|
14
|
+
// deploy/aws/lib/inputs.ts
|
|
15
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
|
|
16
|
+
import { join as join2 } from "node:path";
|
|
17
|
+
|
|
18
|
+
// deploy/aws/lib/names.ts
|
|
19
|
+
var STAGE = /^(prod|spike|eval-[a-z0-9]{1,12})$/;
|
|
20
|
+
function parseStage(value) {
|
|
21
|
+
if (!value || !STAGE.test(value)) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`stage must be prod, spike or eval-<id> (lowercase letters and digits, at most 12); got ${JSON.stringify(value)}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
var TAG_STAGE = "memory-anywhere:stage";
|
|
29
|
+
var CDK_QUALIFIER = "hnb659fds";
|
|
30
|
+
var BOUNDARY_POLICY = "memory-anywhere-boundary";
|
|
31
|
+
function stackName(stage) {
|
|
32
|
+
return `MemoryAnywhere-${stage}`;
|
|
33
|
+
}
|
|
34
|
+
function parameterPrefix(stage) {
|
|
35
|
+
return stage === "prod" ? "/memory-anywhere" : `/memory-anywhere-${stage}`;
|
|
36
|
+
}
|
|
37
|
+
function stageHost(stage, host) {
|
|
38
|
+
if (stage === "prod") return host;
|
|
39
|
+
if (stage === "spike") return `spike.${host}`;
|
|
40
|
+
return `eval.${host}`;
|
|
41
|
+
}
|
|
42
|
+
function bucketName(kind, account, region, stage) {
|
|
43
|
+
return `memory-anywhere-${kind}-${account}-${region}-${stage}`;
|
|
44
|
+
}
|
|
45
|
+
var names = {
|
|
46
|
+
instanceRole: (stage) => `memory-anywhere-instance-${stage}`,
|
|
47
|
+
instanceProfile: (stage) => `memory-anywhere-instance-${stage}`,
|
|
48
|
+
dlmRole: (stage) => `memory-anywhere-dlm-${stage}`,
|
|
49
|
+
keyAlias: (stage) => `alias/memory-anywhere-${stage}`,
|
|
50
|
+
alertsTopic: (stage) => `memory-anywhere-alerts-${stage}`,
|
|
51
|
+
logGroup: (stage) => `/memory-anywhere/${stage}/server`,
|
|
52
|
+
trail: (stage) => `memory-anywhere-${stage}`,
|
|
53
|
+
budget: (stage) => `memory-anywhere-${stage}`,
|
|
54
|
+
anomalyMonitor: (stage) => `memory-anywhere-${stage}`,
|
|
55
|
+
alarm: (stage, what) => `memory-anywhere-${stage}-${what}`,
|
|
56
|
+
rule: (stage, what) => `memory-anywhere-${stage}-${what}`,
|
|
57
|
+
launchTemplate: (stage) => `memory-anywhere-${stage}`,
|
|
58
|
+
cfnExecRole: (account, region) => `cdk-${CDK_QUALIFIER}-cfn-exec-role-${account}-${region}`
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// deploy/aws/lib/inputs.ts
|
|
62
|
+
var AMI = /^ami-[0-9a-f]{8,17}$/;
|
|
63
|
+
var REGION = /^[a-z]{2}(-[a-z]+)+-\d$/;
|
|
64
|
+
function loadStackInputs(ctx) {
|
|
65
|
+
const stage = parseStage(ctx.stage);
|
|
66
|
+
const generation = String(ctx.instanceGeneration ?? "0");
|
|
67
|
+
if (!/^(0|[1-9]\d{0,5})$/u.test(generation))
|
|
68
|
+
throw new Error(`context instanceGeneration must be a whole number; got ${JSON.stringify(generation)}`);
|
|
69
|
+
const region = ctx.region ?? "";
|
|
70
|
+
if (!REGION.test(region))
|
|
71
|
+
throw new Error(`context region must be an AWS region such as us-east-1; got ${JSON.stringify(ctx.region)}`);
|
|
72
|
+
const amiId = ctx.amiPins?.[region] ?? "";
|
|
73
|
+
if (!AMI.test(amiId)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`no AL2023 arm64 AMI pinned for ${region} in cdk.context.json ("memory-anywhere:ami-al2023-arm64"); run mema deploy aws, which pins it`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
stage,
|
|
80
|
+
region,
|
|
81
|
+
amiId,
|
|
82
|
+
releaseKeys: readReleaseKeys(stage, { devKeyFile: ctx.devKeyFile }),
|
|
83
|
+
pins: readHost("pins.env"),
|
|
84
|
+
hostFiles: hostFiles(),
|
|
85
|
+
instanceGeneration: Number(generation)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function readHost(name) {
|
|
89
|
+
return readFileSync2(join2(DEPLOY_AWS_DIR, "host", name), "utf8");
|
|
90
|
+
}
|
|
91
|
+
function readReleaseKeys(stage, sources = {}) {
|
|
92
|
+
const dir = sources.dir ?? join2(DEPLOY_AWS_DIR, "release-keys");
|
|
93
|
+
const release = () => {
|
|
94
|
+
const file = join2(dir, "release.pub");
|
|
95
|
+
if (!existsSync2(file))
|
|
96
|
+
throw new Error(`stage ${stage} trusts release.pub, but deploy/aws/release-keys/release.pub does not exist`);
|
|
97
|
+
return releaseKey("release.pub", file);
|
|
98
|
+
};
|
|
99
|
+
const dev = sources.devKeyFile !== void 0 && existsSync2(sources.devKeyFile) ? sources.devKeyFile : void 0;
|
|
100
|
+
if (stage === "prod") return [release()];
|
|
101
|
+
if (stage === "spike") {
|
|
102
|
+
if (!dev)
|
|
103
|
+
throw new Error(
|
|
104
|
+
sources.devKeyFile ? `the spike stage runs only builds signed with your dev key, but ${sources.devKeyFile} does not exist; create the pair with: minisign -G -W -p "${sources.devKeyFile}" -s "<the same folder>/dev-release.key"` : "the spike stage runs only builds signed with your dev key: pass its public key file as context devReleaseKey (mema deploy aws does)"
|
|
105
|
+
);
|
|
106
|
+
return [releaseKey("dev.pub", dev)];
|
|
107
|
+
}
|
|
108
|
+
return dev ? [release(), releaseKey("dev.pub", dev)] : [release()];
|
|
109
|
+
}
|
|
110
|
+
function releaseKey(name, file) {
|
|
111
|
+
const content = readFileSync2(file, "utf8").replace(/\r\n/g, "\n");
|
|
112
|
+
if (!/^untrusted comment: .*\nRW[A-Za-z0-9+/]{54}\n?$/.test(content))
|
|
113
|
+
throw new Error(`${file} is not a minisign public key`);
|
|
114
|
+
return { name, content };
|
|
115
|
+
}
|
|
116
|
+
function hostFiles() {
|
|
117
|
+
return [
|
|
118
|
+
{ path: "/usr/local/sbin/ma-firstboot", mode: "0755", content: readHost("firstboot.sh") },
|
|
119
|
+
{ path: "/usr/local/sbin/ma-updater", mode: "0755", content: readHost("ma-updater.sh") },
|
|
120
|
+
{ path: "/etc/systemd/system/ma-updater.service", mode: "0644", content: readHost("ma-updater.service") },
|
|
121
|
+
{ path: "/etc/systemd/system/ma-updater.timer", mode: "0644", content: readHost("ma-updater.timer") }
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// deploy/aws/lib/stack.ts
|
|
126
|
+
import { CfnOutput, CfnParameter, DefaultStackSynthesizer, Stack, Tags } from "aws-cdk-lib";
|
|
127
|
+
|
|
128
|
+
// deploy/aws/lib/stack/alerts.ts
|
|
129
|
+
import { Duration } from "aws-cdk-lib";
|
|
130
|
+
import * as budgets from "aws-cdk-lib/aws-budgets";
|
|
131
|
+
import * as ce from "aws-cdk-lib/aws-ce";
|
|
132
|
+
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
|
|
133
|
+
import * as cwActions from "aws-cdk-lib/aws-cloudwatch-actions";
|
|
134
|
+
import * as iam from "aws-cdk-lib/aws-iam";
|
|
135
|
+
import * as logs from "aws-cdk-lib/aws-logs";
|
|
136
|
+
import * as sns from "aws-cdk-lib/aws-sns";
|
|
137
|
+
function alertsTopic({ scope, stage }, alertEmail) {
|
|
138
|
+
const topic = new sns.Topic(scope, "Alerts", { topicName: names.alertsTopic(stage) });
|
|
139
|
+
new sns.Subscription(scope, "AlertsEmail", {
|
|
140
|
+
topic,
|
|
141
|
+
protocol: sns.SubscriptionProtocol.EMAIL,
|
|
142
|
+
endpoint: alertEmail.valueAsString
|
|
143
|
+
});
|
|
144
|
+
return topic;
|
|
145
|
+
}
|
|
146
|
+
var LOG_ALARMS = [
|
|
147
|
+
{ name: "ma_5xx", pattern: logs.FilterPattern.numberValue("$.status", ">=", 500), threshold: 5, minutes: 5 },
|
|
148
|
+
{
|
|
149
|
+
name: "ma_auth_fail",
|
|
150
|
+
pattern: logs.FilterPattern.stringValue("$.reason", "=", "auth_fail"),
|
|
151
|
+
threshold: 5,
|
|
152
|
+
minutes: 10
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "ma_backup_lag",
|
|
156
|
+
pattern: logs.FilterPattern.stringValue("$.reason", "=", "backup_lag"),
|
|
157
|
+
threshold: 1,
|
|
158
|
+
minutes: 5
|
|
159
|
+
}
|
|
160
|
+
];
|
|
161
|
+
function alarms({ scope, stage }, topic, logGroup, instance) {
|
|
162
|
+
const namespace = `memory-anywhere/${stage}`;
|
|
163
|
+
const alarmAction = new cwActions.SnsAction(topic);
|
|
164
|
+
for (const f of LOG_ALARMS) {
|
|
165
|
+
const metric = new logs.MetricFilter(scope, `Filter_${f.name}`, {
|
|
166
|
+
logGroup,
|
|
167
|
+
filterName: f.name,
|
|
168
|
+
filterPattern: f.pattern,
|
|
169
|
+
metricNamespace: namespace,
|
|
170
|
+
metricName: f.name,
|
|
171
|
+
metricValue: "1",
|
|
172
|
+
defaultValue: 0
|
|
173
|
+
}).metric({ statistic: "Sum", period: Duration.minutes(f.minutes) });
|
|
174
|
+
new cloudwatch.Alarm(scope, `Alarm_${f.name}`, {
|
|
175
|
+
alarmName: names.alarm(stage, f.name),
|
|
176
|
+
metric,
|
|
177
|
+
threshold: f.threshold,
|
|
178
|
+
evaluationPeriods: 1,
|
|
179
|
+
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
180
|
+
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING
|
|
181
|
+
}).addAlarmAction(alarmAction);
|
|
182
|
+
}
|
|
183
|
+
new cloudwatch.Alarm(scope, "Alarm_status_check", {
|
|
184
|
+
alarmName: names.alarm(stage, "status-check"),
|
|
185
|
+
metric: new cloudwatch.Metric({
|
|
186
|
+
namespace: "AWS/EC2",
|
|
187
|
+
metricName: "StatusCheckFailed",
|
|
188
|
+
dimensionsMap: { InstanceId: instance.ref },
|
|
189
|
+
statistic: "Maximum",
|
|
190
|
+
period: Duration.minutes(5)
|
|
191
|
+
}),
|
|
192
|
+
threshold: 1,
|
|
193
|
+
evaluationPeriods: 2,
|
|
194
|
+
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
195
|
+
treatMissingData: cloudwatch.TreatMissingData.BREACHING
|
|
196
|
+
}).addAlarmAction(alarmAction);
|
|
197
|
+
}
|
|
198
|
+
function costWatch({ scope, stage, isProd }, topic) {
|
|
199
|
+
const toTopic = [{ subscriptionType: "SNS", address: topic.topicArn }];
|
|
200
|
+
const above = (notificationType, threshold) => ({
|
|
201
|
+
notification: { notificationType, comparisonOperator: "GREATER_THAN", threshold, thresholdType: "ABSOLUTE_VALUE" },
|
|
202
|
+
subscribers: toTopic
|
|
203
|
+
});
|
|
204
|
+
new budgets.CfnBudget(scope, "Budget", {
|
|
205
|
+
budget: {
|
|
206
|
+
budgetName: names.budget(stage),
|
|
207
|
+
budgetType: "COST",
|
|
208
|
+
timeUnit: "MONTHLY",
|
|
209
|
+
budgetLimit: { amount: 30, unit: "USD" }
|
|
210
|
+
},
|
|
211
|
+
notificationsWithSubscribers: [above("FORECASTED", 25), above("ACTUAL", 30)]
|
|
212
|
+
});
|
|
213
|
+
if (!isProd) return;
|
|
214
|
+
const monitor = new ce.CfnAnomalyMonitor(scope, "AnomalyMonitor", {
|
|
215
|
+
monitorName: names.anomalyMonitor(stage),
|
|
216
|
+
monitorType: "DIMENSIONAL",
|
|
217
|
+
monitorDimension: "SERVICE"
|
|
218
|
+
});
|
|
219
|
+
new ce.CfnAnomalySubscription(scope, "AnomalySubscription", {
|
|
220
|
+
subscriptionName: names.anomalyMonitor(stage),
|
|
221
|
+
// SNS subscribers require IMMEDIATE (Cost Anomaly Detection API).
|
|
222
|
+
frequency: "IMMEDIATE",
|
|
223
|
+
monitorArnList: [monitor.attrMonitorArn],
|
|
224
|
+
subscribers: [{ type: "SNS", address: topic.topicArn }],
|
|
225
|
+
thresholdExpression: JSON.stringify({
|
|
226
|
+
Dimensions: { Key: "ANOMALY_TOTAL_IMPACT_ABSOLUTE", MatchOptions: ["GREATER_THAN_OR_EQUAL"], Values: ["5"] }
|
|
227
|
+
})
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
function topicPublishers({ isProd, account }, topic, instanceRole) {
|
|
231
|
+
topic.addToResourcePolicy(
|
|
232
|
+
new iam.PolicyStatement({
|
|
233
|
+
sid: "InstanceRole",
|
|
234
|
+
principals: [new iam.ArnPrincipal(instanceRole.roleArn)],
|
|
235
|
+
actions: ["sns:Publish"],
|
|
236
|
+
resources: [topic.topicArn]
|
|
237
|
+
})
|
|
238
|
+
);
|
|
239
|
+
topic.addToResourcePolicy(
|
|
240
|
+
new iam.PolicyStatement({
|
|
241
|
+
sid: "AwsServices",
|
|
242
|
+
principals: [
|
|
243
|
+
new iam.ServicePrincipal("budgets.amazonaws.com"),
|
|
244
|
+
new iam.ServicePrincipal("events.amazonaws.com"),
|
|
245
|
+
new iam.ServicePrincipal("cloudwatch.amazonaws.com")
|
|
246
|
+
],
|
|
247
|
+
actions: ["sns:Publish"],
|
|
248
|
+
resources: [topic.topicArn],
|
|
249
|
+
conditions: { StringEquals: { "aws:SourceAccount": account } }
|
|
250
|
+
})
|
|
251
|
+
);
|
|
252
|
+
if (isProd) {
|
|
253
|
+
topic.addToResourcePolicy(
|
|
254
|
+
new iam.PolicyStatement({
|
|
255
|
+
sid: "CostAnomalyDetection",
|
|
256
|
+
principals: [new iam.ServicePrincipal("costalerts.amazonaws.com")],
|
|
257
|
+
actions: ["sns:Publish"],
|
|
258
|
+
resources: [topic.topicArn]
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// deploy/aws/lib/stack/audit.ts
|
|
265
|
+
import * as cloudtrail from "aws-cdk-lib/aws-cloudtrail";
|
|
266
|
+
import * as events from "aws-cdk-lib/aws-events";
|
|
267
|
+
var API_CALL = "AWS API Call via CloudTrail";
|
|
268
|
+
function audit({ scope, stage, prefix }, trailBucket, releasesBucket, topic) {
|
|
269
|
+
const trail = new cloudtrail.Trail(scope, "Trail", {
|
|
270
|
+
trailName: names.trail(stage),
|
|
271
|
+
bucket: trailBucket,
|
|
272
|
+
isMultiRegionTrail: true,
|
|
273
|
+
includeGlobalServiceEvents: true,
|
|
274
|
+
managementEvents: cloudtrail.ReadWriteType.ALL,
|
|
275
|
+
sendToCloudWatchLogs: false
|
|
276
|
+
});
|
|
277
|
+
trail.addS3EventSelector([{ bucket: releasesBucket }], {
|
|
278
|
+
readWriteType: cloudtrail.ReadWriteType.WRITE_ONLY,
|
|
279
|
+
includeManagementEvents: true
|
|
280
|
+
});
|
|
281
|
+
const rules = [
|
|
282
|
+
{
|
|
283
|
+
name: "kms-key-changes",
|
|
284
|
+
pattern: {
|
|
285
|
+
source: ["aws.kms"],
|
|
286
|
+
detailType: [API_CALL],
|
|
287
|
+
detail: { eventName: ["ScheduleKeyDeletion", "DisableKey", "PutKeyPolicy"] }
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
name: "bucket-controls",
|
|
292
|
+
pattern: {
|
|
293
|
+
source: ["aws.s3"],
|
|
294
|
+
detailType: [API_CALL],
|
|
295
|
+
detail: {
|
|
296
|
+
eventName: [
|
|
297
|
+
"PutBucketPolicy",
|
|
298
|
+
"DeleteBucketPolicy",
|
|
299
|
+
"PutBucketVersioning",
|
|
300
|
+
"PutBucketLifecycle",
|
|
301
|
+
"PutBucketLifecycleConfiguration"
|
|
302
|
+
]
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
name: "iam-changes",
|
|
308
|
+
pattern: {
|
|
309
|
+
source: ["aws.iam"],
|
|
310
|
+
detailType: [API_CALL],
|
|
311
|
+
detail: {
|
|
312
|
+
eventName: [
|
|
313
|
+
"CreatePolicy",
|
|
314
|
+
"CreatePolicyVersion",
|
|
315
|
+
"DeletePolicy",
|
|
316
|
+
"DeletePolicyVersion",
|
|
317
|
+
"SetDefaultPolicyVersion",
|
|
318
|
+
"CreateRole",
|
|
319
|
+
"DeleteRole",
|
|
320
|
+
"UpdateAssumeRolePolicy",
|
|
321
|
+
"PutRolePolicy",
|
|
322
|
+
"DeleteRolePolicy",
|
|
323
|
+
"AttachRolePolicy",
|
|
324
|
+
"DetachRolePolicy",
|
|
325
|
+
"PutRolePermissionsBoundary",
|
|
326
|
+
"DeleteRolePermissionsBoundary"
|
|
327
|
+
]
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
name: "console-login-outside-identity-center",
|
|
333
|
+
pattern: {
|
|
334
|
+
source: ["aws.signin"],
|
|
335
|
+
detailType: ["AWS Console Sign In via CloudTrail"],
|
|
336
|
+
detail: { userIdentity: { type: ["Root", "IAMUser"] } }
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
name: "stack-changes",
|
|
341
|
+
pattern: {
|
|
342
|
+
source: ["aws.cloudformation"],
|
|
343
|
+
detailType: [API_CALL],
|
|
344
|
+
detail: {
|
|
345
|
+
eventName: ["CreateStack", "UpdateStack", "ExecuteChangeSet", "DeleteStack"],
|
|
346
|
+
requestParameters: { stackName: [{ wildcard: `*${stackName(stage)}*` }] }
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
name: "instance-changes",
|
|
352
|
+
pattern: {
|
|
353
|
+
source: ["aws.ec2"],
|
|
354
|
+
detailType: [API_CALL],
|
|
355
|
+
detail: { eventName: ["ModifyInstanceAttribute", "RunInstances", "TerminateInstances"] }
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
name: "release-and-key-parameters",
|
|
360
|
+
pattern: {
|
|
361
|
+
source: ["aws.ssm"],
|
|
362
|
+
detailType: [API_CALL],
|
|
363
|
+
detail: {
|
|
364
|
+
eventName: ["PutParameter"],
|
|
365
|
+
requestParameters: { name: [`${prefix}/release/desired`, { prefix: `${prefix}/llm/` }] }
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
name: "sessions-and-commands",
|
|
371
|
+
pattern: { source: ["aws.ssm"], detailType: [API_CALL], detail: { eventName: ["SendCommand", "StartSession"] } }
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
name: "release-uploads",
|
|
375
|
+
pattern: {
|
|
376
|
+
source: ["aws.s3"],
|
|
377
|
+
detailType: [API_CALL],
|
|
378
|
+
detail: { eventName: ["PutObject"], requestParameters: { bucketName: [releasesBucket.bucketName] } }
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
];
|
|
382
|
+
for (const r of rules) {
|
|
383
|
+
new events.CfnRule(scope, `Rule_${r.name}`, {
|
|
384
|
+
name: names.rule(stage, r.name),
|
|
385
|
+
state: "ENABLED",
|
|
386
|
+
eventPattern: renderPattern(r.pattern),
|
|
387
|
+
targets: [{ id: "alerts", arn: topic.topicArn }]
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function renderPattern(p) {
|
|
392
|
+
const out = {};
|
|
393
|
+
if (p.source) out["source"] = p.source;
|
|
394
|
+
if (p.detailType) out["detail-type"] = p.detailType;
|
|
395
|
+
if (p.detail) out["detail"] = p.detail;
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// deploy/aws/lib/stack/context.ts
|
|
400
|
+
import { Aws, RemovalPolicy } from "aws-cdk-lib";
|
|
401
|
+
import * as iam2 from "aws-cdk-lib/aws-iam";
|
|
402
|
+
var SSO_OPERATOR_ROLE = "AWSReservedSSO_memory-anywhere-operator_*";
|
|
403
|
+
var SSO_DEPLOY_ROLE = "AWSReservedSSO_memory-anywhere-deploy_*";
|
|
404
|
+
function stackContext(scope, stage) {
|
|
405
|
+
const isProd = stage === "prod";
|
|
406
|
+
const account = Aws.ACCOUNT_ID;
|
|
407
|
+
const region = Aws.REGION;
|
|
408
|
+
const role = (path) => `arn:${Aws.PARTITION}:iam::${account}:role/${path}`;
|
|
409
|
+
return {
|
|
410
|
+
scope,
|
|
411
|
+
stage,
|
|
412
|
+
isProd,
|
|
413
|
+
removal: isProd ? RemovalPolicy.RETAIN : RemovalPolicy.DESTROY,
|
|
414
|
+
account,
|
|
415
|
+
region,
|
|
416
|
+
prefix: parameterPrefix(stage),
|
|
417
|
+
principals: {
|
|
418
|
+
accountRootArn: `arn:${Aws.PARTITION}:iam::${account}:root`,
|
|
419
|
+
cfnExecRoleArn: role(names.cfnExecRole(account, region)),
|
|
420
|
+
operatorRoleArnPattern: role(`aws-reserved/sso.amazonaws.com/*${SSO_OPERATOR_ROLE}`),
|
|
421
|
+
deployRoleArnPattern: role(`aws-reserved/sso.amazonaws.com/*${SSO_DEPLOY_ROLE}`),
|
|
422
|
+
boundary: iam2.ManagedPolicy.fromManagedPolicyName(scope, "Boundary", BOUNDARY_POLICY)
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function denyUnlessPrincipal(sid, actions, resources, allowed, notAction = false) {
|
|
427
|
+
return new iam2.PolicyStatement({
|
|
428
|
+
sid,
|
|
429
|
+
effect: iam2.Effect.DENY,
|
|
430
|
+
principals: [new iam2.AnyPrincipal()],
|
|
431
|
+
...notAction ? { notActions: actions } : { actions },
|
|
432
|
+
resources,
|
|
433
|
+
conditions: { ArnNotLike: { "aws:PrincipalArn": allowed } }
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// deploy/aws/lib/stack/instance.ts
|
|
438
|
+
import { Aws as Aws2 } from "aws-cdk-lib";
|
|
439
|
+
import * as dlm from "aws-cdk-lib/aws-dlm";
|
|
440
|
+
import * as ec2 from "aws-cdk-lib/aws-ec2";
|
|
441
|
+
import * as iam3 from "aws-cdk-lib/aws-iam";
|
|
442
|
+
import * as logs2 from "aws-cdk-lib/aws-logs";
|
|
443
|
+
|
|
444
|
+
// deploy/aws/lib/user-data.ts
|
|
445
|
+
import { gzipSync } from "node:zlib";
|
|
446
|
+
import { Fn } from "aws-cdk-lib";
|
|
447
|
+
var USER_DATA_LIMIT = 16 * 1024;
|
|
448
|
+
var PATH = /^\/[A-Za-z0-9._/-]+$/;
|
|
449
|
+
var MODE = /^0[0-7]{3}$/;
|
|
450
|
+
function userDataParts(input) {
|
|
451
|
+
const files = [
|
|
452
|
+
{
|
|
453
|
+
path: "/etc/memory-anywhere/stack.env",
|
|
454
|
+
mode: "0644",
|
|
455
|
+
content: ""
|
|
456
|
+
// filled with tokens below
|
|
457
|
+
},
|
|
458
|
+
{ path: "/etc/memory-anywhere/pins.env", mode: "0644", content: input.pins },
|
|
459
|
+
...input.releaseKeys.map((k) => ({
|
|
460
|
+
path: `/etc/memory-anywhere/release-keys/${k.name}`,
|
|
461
|
+
mode: "0644",
|
|
462
|
+
content: k.content
|
|
463
|
+
})),
|
|
464
|
+
...input.files
|
|
465
|
+
];
|
|
466
|
+
const parts = ["#!/bin/bash\nset -euo pipefail\numask 022\n"];
|
|
467
|
+
files.forEach((f, i) => {
|
|
468
|
+
if (!PATH.test(f.path)) throw new Error(`unsafe user-data path ${f.path}`);
|
|
469
|
+
if (!MODE.test(f.mode)) throw new Error(`bad mode ${f.mode} for ${f.path}`);
|
|
470
|
+
const delimiter = `MA_USER_DATA_EOF_${i}`;
|
|
471
|
+
const dir = f.path.slice(0, f.path.lastIndexOf("/"));
|
|
472
|
+
parts.push(`install -d -m 0755 ${dir}
|
|
473
|
+
`);
|
|
474
|
+
if (f.path === "/etc/memory-anywhere/stack.env") {
|
|
475
|
+
parts.push(`cat > ${f.path} <<'${delimiter}'
|
|
476
|
+
`, ...stackEnv(input));
|
|
477
|
+
} else {
|
|
478
|
+
parts.push(
|
|
479
|
+
`base64 -d <<'${delimiter}' | gunzip > ${f.path}
|
|
480
|
+
`,
|
|
481
|
+
packed(f.content.endsWith("\n") ? f.content : `${f.content}
|
|
482
|
+
`)
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
parts.push(`${delimiter}
|
|
486
|
+
chmod ${f.mode} ${f.path}
|
|
487
|
+
`);
|
|
488
|
+
});
|
|
489
|
+
parts.push("exec /usr/local/sbin/ma-firstboot\n");
|
|
490
|
+
return parts;
|
|
491
|
+
}
|
|
492
|
+
function packed(content) {
|
|
493
|
+
const b64 = gzipSync(Buffer.from(content, "utf8"), { level: 9 }).toString("base64");
|
|
494
|
+
return `${b64.replace(/.{1,76}/g, "$&\n")}`;
|
|
495
|
+
}
|
|
496
|
+
function stackEnv(input) {
|
|
497
|
+
const entries = [
|
|
498
|
+
["MA_STAGE", input.stage],
|
|
499
|
+
["MA_PREFIX", input.prefix],
|
|
500
|
+
["MA_REGION", input.region],
|
|
501
|
+
["MA_HOST", input.host],
|
|
502
|
+
["MA_ALERT_EMAIL", input.alertEmail],
|
|
503
|
+
["MA_OWNER_TZ", input.ownerTimezone],
|
|
504
|
+
["MA_RELEASES_BUCKET", input.releasesBucket],
|
|
505
|
+
["MA_BACKUP_BUCKET", input.backupBucket],
|
|
506
|
+
["MA_KEY_ARN", input.keyArn],
|
|
507
|
+
["MA_TOPIC_ARN", input.topicArn],
|
|
508
|
+
["MA_LOG_GROUP", input.logGroup]
|
|
509
|
+
];
|
|
510
|
+
const out = ["# Written by user data from the stack's parameters and outputs.\n"];
|
|
511
|
+
for (const [k, v] of entries) out.push(`${k}=`, v, "\n");
|
|
512
|
+
return out;
|
|
513
|
+
}
|
|
514
|
+
function buildUserData(input) {
|
|
515
|
+
return Fn.base64(Fn.join("", userDataParts(input)));
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// deploy/aws/lib/stack/instance.ts
|
|
519
|
+
function instanceAccess(c, s, topic) {
|
|
520
|
+
const { scope, stage, removal, account, region, prefix, principals } = c;
|
|
521
|
+
const instanceRole = new iam3.Role(scope, "InstanceRole", {
|
|
522
|
+
roleName: names.instanceRole(stage),
|
|
523
|
+
assumedBy: new iam3.ServicePrincipal("ec2.amazonaws.com"),
|
|
524
|
+
permissionsBoundary: principals.boundary,
|
|
525
|
+
description: `memory-anywhere ${stage} instance (root agents, Litestream, ma-aws helpers)`
|
|
526
|
+
});
|
|
527
|
+
const logGroup = new logs2.LogGroup(scope, "ServerLogs", {
|
|
528
|
+
logGroupName: names.logGroup(stage),
|
|
529
|
+
retention: logs2.RetentionDays.ONE_MONTH,
|
|
530
|
+
removalPolicy: removal
|
|
531
|
+
});
|
|
532
|
+
const paramArn = (path) => `arn:${Aws2.PARTITION}:ssm:${region}:${account}:parameter${prefix}/${path}`;
|
|
533
|
+
new iam3.Policy(scope, "InstancePolicy", {
|
|
534
|
+
roles: [instanceRole],
|
|
535
|
+
statements: [
|
|
536
|
+
new iam3.PolicyStatement({
|
|
537
|
+
sid: "SessionManagerAndRunCommand",
|
|
538
|
+
actions: [
|
|
539
|
+
"ssm:UpdateInstanceInformation",
|
|
540
|
+
"ssmmessages:CreateControlChannel",
|
|
541
|
+
"ssmmessages:CreateDataChannel",
|
|
542
|
+
"ssmmessages:OpenControlChannel",
|
|
543
|
+
"ssmmessages:OpenDataChannel",
|
|
544
|
+
"ec2messages:*"
|
|
545
|
+
],
|
|
546
|
+
resources: ["*"]
|
|
547
|
+
}),
|
|
548
|
+
new iam3.PolicyStatement({
|
|
549
|
+
sid: "ReadStageParameters",
|
|
550
|
+
actions: ["ssm:GetParameter", "ssm:GetParameters"],
|
|
551
|
+
resources: [paramArn("*")]
|
|
552
|
+
}),
|
|
553
|
+
new iam3.PolicyStatement({
|
|
554
|
+
sid: "WriteOutboxParameters",
|
|
555
|
+
actions: ["ssm:PutParameter", "ssm:DeleteParameter"],
|
|
556
|
+
resources: [paramArn("setup-token"), paramArn("acme/account-uri")]
|
|
557
|
+
}),
|
|
558
|
+
new iam3.PolicyStatement({ sid: "PublishAlerts", actions: ["sns:Publish"], resources: [topic.topicArn] }),
|
|
559
|
+
new iam3.PolicyStatement({
|
|
560
|
+
sid: "ListBuckets",
|
|
561
|
+
actions: ["s3:GetBucketLocation", "s3:ListBucket"],
|
|
562
|
+
resources: [s.backupBucket.bucketArn, s.releasesBucket.bucketArn]
|
|
563
|
+
}),
|
|
564
|
+
new iam3.PolicyStatement({
|
|
565
|
+
sid: "LitestreamReplica",
|
|
566
|
+
actions: ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
|
567
|
+
resources: [s.backupBucket.arnForObjects("litestream/*")]
|
|
568
|
+
}),
|
|
569
|
+
new iam3.PolicyStatement({
|
|
570
|
+
sid: "TransferFetch",
|
|
571
|
+
actions: ["s3:GetObject", "s3:DeleteObject"],
|
|
572
|
+
resources: [s.backupBucket.arnForObjects("transfer/*")]
|
|
573
|
+
}),
|
|
574
|
+
new iam3.PolicyStatement({
|
|
575
|
+
sid: "ReadReleases",
|
|
576
|
+
actions: ["s3:GetObject"],
|
|
577
|
+
resources: [s.releasesBucket.arnForObjects("*")]
|
|
578
|
+
}),
|
|
579
|
+
// The manifest job stores today's and reads the newest; a restore reads the one it checks against.
|
|
580
|
+
new iam3.PolicyStatement({
|
|
581
|
+
sid: "Manifests",
|
|
582
|
+
actions: ["s3:PutObject", "s3:GetObject"],
|
|
583
|
+
resources: [s.manifestBucket.arnForObjects("manifests/*")]
|
|
584
|
+
}),
|
|
585
|
+
new iam3.PolicyStatement({
|
|
586
|
+
sid: "ListManifests",
|
|
587
|
+
actions: ["s3:ListBucket"],
|
|
588
|
+
resources: [s.manifestBucket.bucketArn],
|
|
589
|
+
conditions: { StringEquals: { "s3:prefix": "manifests/" } }
|
|
590
|
+
}),
|
|
591
|
+
new iam3.PolicyStatement({
|
|
592
|
+
sid: "KeyViaS3",
|
|
593
|
+
actions: ["kms:GenerateDataKey", "kms:Decrypt"],
|
|
594
|
+
resources: [s.key.keyArn],
|
|
595
|
+
conditions: { StringEquals: { "kms:ViaService": `s3.${region}.amazonaws.com` } }
|
|
596
|
+
}),
|
|
597
|
+
new iam3.PolicyStatement({
|
|
598
|
+
sid: "ShipLogs",
|
|
599
|
+
actions: ["logs:CreateLogStream", "logs:PutLogEvents"],
|
|
600
|
+
// log-group:<name>:* covers the group and every stream in it.
|
|
601
|
+
resources: [`arn:${Aws2.PARTITION}:logs:${region}:${account}:log-group:${names.logGroup(stage)}:*`]
|
|
602
|
+
})
|
|
603
|
+
]
|
|
604
|
+
});
|
|
605
|
+
const dlmRole = new iam3.Role(scope, "DlmRole", {
|
|
606
|
+
roleName: names.dlmRole(stage),
|
|
607
|
+
assumedBy: new iam3.ServicePrincipal("dlm.amazonaws.com"),
|
|
608
|
+
permissionsBoundary: principals.boundary,
|
|
609
|
+
managedPolicies: [iam3.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSDataLifecycleManagerServiceRole")],
|
|
610
|
+
description: `memory-anywhere ${stage} daily EBS snapshots`
|
|
611
|
+
});
|
|
612
|
+
return { instanceRole, logGroup, dlmRole };
|
|
613
|
+
}
|
|
614
|
+
function server(c, spec, n, s, a, topic) {
|
|
615
|
+
const { scope, stage, region, prefix } = c;
|
|
616
|
+
const profile = new iam3.CfnInstanceProfile(scope, "InstanceProfile", {
|
|
617
|
+
instanceProfileName: names.instanceProfile(stage),
|
|
618
|
+
roles: [a.instanceRole.roleName]
|
|
619
|
+
});
|
|
620
|
+
const stageTags = [{ key: TAG_STAGE, value: stage }];
|
|
621
|
+
const userData = buildUserData({
|
|
622
|
+
stage,
|
|
623
|
+
prefix,
|
|
624
|
+
region,
|
|
625
|
+
host: spec.host.valueAsString,
|
|
626
|
+
alertEmail: spec.alertEmail.valueAsString,
|
|
627
|
+
ownerTimezone: spec.ownerTimezone.valueAsString,
|
|
628
|
+
releasesBucket: s.releasesBucket.bucketName,
|
|
629
|
+
backupBucket: s.backupBucket.bucketName,
|
|
630
|
+
keyArn: s.key.keyArn,
|
|
631
|
+
topicArn: topic.topicArn,
|
|
632
|
+
logGroup: a.logGroup.logGroupName,
|
|
633
|
+
releaseKeys: spec.releaseKeys,
|
|
634
|
+
pins: spec.pins,
|
|
635
|
+
files: spec.hostFiles
|
|
636
|
+
});
|
|
637
|
+
const launchTemplate = new ec2.CfnLaunchTemplate(scope, "LaunchTemplate", {
|
|
638
|
+
launchTemplateName: names.launchTemplate(stage),
|
|
639
|
+
launchTemplateData: {
|
|
640
|
+
imageId: spec.amiId,
|
|
641
|
+
instanceType: "t4g.small",
|
|
642
|
+
creditSpecification: { cpuCredits: "standard" },
|
|
643
|
+
iamInstanceProfile: { arn: profile.attrArn },
|
|
644
|
+
metadataOptions: {
|
|
645
|
+
httpTokens: "required",
|
|
646
|
+
httpPutResponseHopLimit: 1,
|
|
647
|
+
httpEndpoint: "enabled",
|
|
648
|
+
instanceMetadataTags: "disabled"
|
|
649
|
+
},
|
|
650
|
+
monitoring: { enabled: false },
|
|
651
|
+
blockDeviceMappings: [
|
|
652
|
+
{
|
|
653
|
+
deviceName: "/dev/xvda",
|
|
654
|
+
ebs: {
|
|
655
|
+
volumeType: "gp3",
|
|
656
|
+
volumeSize: 20,
|
|
657
|
+
encrypted: true,
|
|
658
|
+
kmsKeyId: s.key.keyArn,
|
|
659
|
+
deleteOnTermination: true
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
],
|
|
663
|
+
tagSpecifications: [
|
|
664
|
+
{
|
|
665
|
+
resourceType: "instance",
|
|
666
|
+
tags: [
|
|
667
|
+
...stageTags,
|
|
668
|
+
{ key: "Name", value: `memory-anywhere-${stage}` },
|
|
669
|
+
// A new generation is a new launch template version, so a new instance (inputs.ts).
|
|
670
|
+
...spec.instanceGeneration > 0 ? [{ key: "memory-anywhere:generation", value: String(spec.instanceGeneration) }] : []
|
|
671
|
+
]
|
|
672
|
+
},
|
|
673
|
+
{ resourceType: "volume", tags: [...stageTags, { key: "Name", value: `memory-anywhere-${stage}-root` }] }
|
|
674
|
+
],
|
|
675
|
+
userData
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
const instance = new ec2.CfnInstance(scope, "Instance", {
|
|
679
|
+
launchTemplate: { launchTemplateId: launchTemplate.ref, version: launchTemplate.attrLatestVersionNumber },
|
|
680
|
+
subnetId: n.subnet.subnetId,
|
|
681
|
+
securityGroupIds: [n.securityGroup.securityGroupId]
|
|
682
|
+
});
|
|
683
|
+
const eip = new ec2.CfnEIP(scope, "ElasticIp", { domain: "vpc" });
|
|
684
|
+
new ec2.CfnEIPAssociation(scope, "ElasticIpAssociation", {
|
|
685
|
+
allocationId: eip.attrAllocationId,
|
|
686
|
+
instanceId: instance.ref
|
|
687
|
+
});
|
|
688
|
+
new dlm.CfnLifecyclePolicy(scope, "DailySnapshots", {
|
|
689
|
+
// DLM allows only [0-9A-Za-z _-] here.
|
|
690
|
+
description: `memory-anywhere ${stage} root volume daily 0500 UTC keep 7`,
|
|
691
|
+
state: "ENABLED",
|
|
692
|
+
executionRoleArn: a.dlmRole.roleArn,
|
|
693
|
+
policyDetails: {
|
|
694
|
+
policyType: "EBS_SNAPSHOT_MANAGEMENT",
|
|
695
|
+
resourceTypes: ["VOLUME"],
|
|
696
|
+
targetTags: stageTags,
|
|
697
|
+
schedules: [
|
|
698
|
+
{
|
|
699
|
+
name: "daily",
|
|
700
|
+
copyTags: true,
|
|
701
|
+
createRule: { interval: 24, intervalUnit: "HOURS", times: ["05:00"] },
|
|
702
|
+
retainRule: { count: 7 }
|
|
703
|
+
}
|
|
704
|
+
]
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
return { instance, eip };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// deploy/aws/lib/stack/network.ts
|
|
711
|
+
import * as ec22 from "aws-cdk-lib/aws-ec2";
|
|
712
|
+
function network({ scope }) {
|
|
713
|
+
const vpc = new ec22.Vpc(scope, "Vpc", {
|
|
714
|
+
ipAddresses: ec22.IpAddresses.cidr("10.42.0.0/24"),
|
|
715
|
+
maxAzs: 1,
|
|
716
|
+
natGateways: 0,
|
|
717
|
+
subnetConfiguration: [{ name: "public", subnetType: ec22.SubnetType.PUBLIC, cidrMask: 26 }],
|
|
718
|
+
restrictDefaultSecurityGroup: false
|
|
719
|
+
// would add a Lambda-backed custom resource
|
|
720
|
+
});
|
|
721
|
+
const s3Endpoint = vpc.addGatewayEndpoint("S3Endpoint", { service: ec22.GatewayVpcEndpointAwsService.S3 });
|
|
722
|
+
const securityGroup = new ec22.SecurityGroup(scope, "InstanceSg", {
|
|
723
|
+
vpc,
|
|
724
|
+
description: "memory-anywhere: 443 in; 443 and NTP out",
|
|
725
|
+
allowAllOutbound: false,
|
|
726
|
+
allowAllIpv6Outbound: false
|
|
727
|
+
});
|
|
728
|
+
securityGroup.addIngressRule(ec22.Peer.anyIpv4(), ec22.Port.tcp(443), "HTTPS");
|
|
729
|
+
securityGroup.addIngressRule(ec22.Peer.anyIpv6(), ec22.Port.tcp(443), "HTTPS");
|
|
730
|
+
securityGroup.addEgressRule(ec22.Peer.anyIpv4(), ec22.Port.tcp(443), "HTTPS (AWS APIs, releases, ACME, DoH)");
|
|
731
|
+
securityGroup.addEgressRule(ec22.Peer.anyIpv4(), ec22.Port.udp(123), "NTP");
|
|
732
|
+
const [subnet] = vpc.publicSubnets;
|
|
733
|
+
if (!subnet) throw new Error("the VPC has no public subnet");
|
|
734
|
+
return { vpc, subnet, s3Endpoint, securityGroup };
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// deploy/aws/lib/stack/storage.ts
|
|
738
|
+
import { Aws as Aws3, Duration as Duration2 } from "aws-cdk-lib";
|
|
739
|
+
import * as iam4 from "aws-cdk-lib/aws-iam";
|
|
740
|
+
import * as kms from "aws-cdk-lib/aws-kms";
|
|
741
|
+
import * as s3 from "aws-cdk-lib/aws-s3";
|
|
742
|
+
function storage({ scope, stage, isProd, removal, account, region }) {
|
|
743
|
+
const key = new kms.Key(scope, "Key", {
|
|
744
|
+
alias: names.keyAlias(stage),
|
|
745
|
+
description: `memory-anywhere ${stage}: backups and EBS`,
|
|
746
|
+
enableKeyRotation: true,
|
|
747
|
+
pendingWindow: Duration2.days(30),
|
|
748
|
+
removalPolicy: removal
|
|
749
|
+
});
|
|
750
|
+
const common = {
|
|
751
|
+
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
|
|
752
|
+
objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_ENFORCED,
|
|
753
|
+
enforceSSL: true,
|
|
754
|
+
removalPolicy: removal
|
|
755
|
+
};
|
|
756
|
+
const backupBucket = new s3.Bucket(scope, "BackupBucket", {
|
|
757
|
+
...common,
|
|
758
|
+
bucketName: bucketName("backup", account, region, stage),
|
|
759
|
+
versioned: true,
|
|
760
|
+
encryption: s3.BucketEncryption.KMS,
|
|
761
|
+
encryptionKey: key,
|
|
762
|
+
bucketKeyEnabled: true,
|
|
763
|
+
lifecycleRules: [
|
|
764
|
+
{ noncurrentVersionExpiration: Duration2.days(14), abortIncompleteMultipartUploadAfter: Duration2.days(1) }
|
|
765
|
+
]
|
|
766
|
+
});
|
|
767
|
+
const releasesBucket = new s3.Bucket(scope, "ReleasesBucket", {
|
|
768
|
+
...common,
|
|
769
|
+
bucketName: bucketName("releases", account, region, stage),
|
|
770
|
+
versioned: true,
|
|
771
|
+
encryption: s3.BucketEncryption.S3_MANAGED,
|
|
772
|
+
lifecycleRules: [
|
|
773
|
+
{ noncurrentVersionExpiration: Duration2.days(30), abortIncompleteMultipartUploadAfter: Duration2.days(1) }
|
|
774
|
+
]
|
|
775
|
+
});
|
|
776
|
+
const trailBucket = new s3.Bucket(scope, "TrailBucket", {
|
|
777
|
+
...common,
|
|
778
|
+
bucketName: bucketName("trail", account, region, stage),
|
|
779
|
+
encryption: s3.BucketEncryption.S3_MANAGED,
|
|
780
|
+
lifecycleRules: [{ expiration: Duration2.days(90), abortIncompleteMultipartUploadAfter: Duration2.days(1) }]
|
|
781
|
+
});
|
|
782
|
+
const manifestBucket = new s3.Bucket(scope, "ManifestBucket", {
|
|
783
|
+
...common,
|
|
784
|
+
bucketName: bucketName("manifest", account, region, stage),
|
|
785
|
+
versioned: true,
|
|
786
|
+
encryption: s3.BucketEncryption.S3_MANAGED,
|
|
787
|
+
...isProd ? { objectLockEnabled: true, objectLockDefaultRetention: s3.ObjectLockRetention.compliance(Duration2.days(90)) } : {}
|
|
788
|
+
});
|
|
789
|
+
return { key, backupBucket, releasesBucket, trailBucket, manifestBucket };
|
|
790
|
+
}
|
|
791
|
+
function protectBackups(c, { key, backupBucket }, r) {
|
|
792
|
+
const { stage, isProd, account, region, principals: p } = c;
|
|
793
|
+
const viaS3 = { "kms:ViaService": `s3.${region}.amazonaws.com` };
|
|
794
|
+
const viaEc2 = { "kms:ViaService": `ec2.${region}.amazonaws.com` };
|
|
795
|
+
const backupArn = `arn:${Aws3.PARTITION}:s3:::${bucketName("backup", account, region, stage)}`;
|
|
796
|
+
key.addToResourcePolicy(
|
|
797
|
+
new iam4.PolicyStatement({
|
|
798
|
+
sid: "InstanceRoleViaS3OnBackupBucket",
|
|
799
|
+
principals: [new iam4.ArnPrincipal(r.instanceRole.roleArn)],
|
|
800
|
+
actions: ["kms:GenerateDataKey", "kms:Decrypt"],
|
|
801
|
+
resources: ["*"],
|
|
802
|
+
conditions: {
|
|
803
|
+
StringEquals: viaS3,
|
|
804
|
+
StringLike: { "kms:EncryptionContext:aws:s3:arn": [backupArn, `${backupArn}/*`] }
|
|
805
|
+
}
|
|
806
|
+
})
|
|
807
|
+
);
|
|
808
|
+
const ebsUsers = [new iam4.ArnPrincipal(p.cfnExecRoleArn), new iam4.ArnPrincipal(r.dlmRole.roleArn)];
|
|
809
|
+
key.addToResourcePolicy(
|
|
810
|
+
new iam4.PolicyStatement({
|
|
811
|
+
sid: "EbsGrantsForLaunchAndSnapshots",
|
|
812
|
+
principals: ebsUsers,
|
|
813
|
+
actions: ["kms:CreateGrant"],
|
|
814
|
+
resources: ["*"],
|
|
815
|
+
conditions: { Bool: { "kms:GrantIsForAWSResource": "true" }, StringEquals: viaEc2 }
|
|
816
|
+
})
|
|
817
|
+
);
|
|
818
|
+
key.addToResourcePolicy(
|
|
819
|
+
new iam4.PolicyStatement({
|
|
820
|
+
sid: "EbsUseForLaunchAndSnapshots",
|
|
821
|
+
principals: ebsUsers,
|
|
822
|
+
actions: ["kms:GenerateDataKeyWithoutPlaintext", "kms:Decrypt", "kms:DescribeKey", "kms:ReEncrypt*"],
|
|
823
|
+
resources: ["*"],
|
|
824
|
+
conditions: { StringEquals: viaEc2 }
|
|
825
|
+
})
|
|
826
|
+
);
|
|
827
|
+
key.addToResourcePolicy(
|
|
828
|
+
new iam4.PolicyStatement({
|
|
829
|
+
sid: "OperatorRestoreViaS3",
|
|
830
|
+
principals: [new iam4.AccountRootPrincipal()],
|
|
831
|
+
actions: ["kms:Decrypt"],
|
|
832
|
+
resources: ["*"],
|
|
833
|
+
conditions: { StringEquals: viaS3, ArnLike: { "aws:PrincipalArn": p.operatorRoleArnPattern } }
|
|
834
|
+
})
|
|
835
|
+
);
|
|
836
|
+
const backupResources = [backupBucket.bucketArn, backupBucket.arnForObjects("*")];
|
|
837
|
+
const always = [r.instanceRole.roleArn, p.operatorRoleArnPattern, p.cfnExecRoleArn, p.accountRootArn];
|
|
838
|
+
const destroyActions = ["s3:ListBucketVersions", "s3:DeleteObject", "s3:DeleteObjectVersion"];
|
|
839
|
+
if (isProd) {
|
|
840
|
+
backupBucket.addToResourcePolicy(denyUnlessPrincipal("OnlyStackPrincipals", ["s3:*"], backupResources, always));
|
|
841
|
+
} else {
|
|
842
|
+
backupBucket.addToResourcePolicy(
|
|
843
|
+
denyUnlessPrincipal("OnlyStackPrincipals", destroyActions, backupResources, always, true)
|
|
844
|
+
);
|
|
845
|
+
backupBucket.addToResourcePolicy(
|
|
846
|
+
denyUnlessPrincipal("DeployMayOnlyEmptyNonProd", destroyActions, backupResources, [
|
|
847
|
+
...always,
|
|
848
|
+
p.deployRoleArnPattern
|
|
849
|
+
])
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
backupBucket.addToResourcePolicy(
|
|
853
|
+
new iam4.PolicyStatement({
|
|
854
|
+
sid: "InstanceOnlyThroughTheGatewayEndpoint",
|
|
855
|
+
effect: iam4.Effect.DENY,
|
|
856
|
+
principals: [new iam4.AnyPrincipal()],
|
|
857
|
+
actions: ["s3:*"],
|
|
858
|
+
resources: backupResources,
|
|
859
|
+
conditions: {
|
|
860
|
+
ArnEquals: { "aws:PrincipalArn": r.instanceRole.roleArn },
|
|
861
|
+
StringNotEquals: { "aws:SourceVpce": r.s3Endpoint.vpcEndpointId }
|
|
862
|
+
}
|
|
863
|
+
})
|
|
864
|
+
);
|
|
865
|
+
backupBucket.addToResourcePolicy(
|
|
866
|
+
denyUnlessPrincipal(
|
|
867
|
+
"BucketControlsOnlyRootAndCfnExec",
|
|
868
|
+
[
|
|
869
|
+
"s3:PutBucketPolicy",
|
|
870
|
+
"s3:DeleteBucketPolicy",
|
|
871
|
+
"s3:PutLifecycleConfiguration",
|
|
872
|
+
"s3:PutBucketVersioning",
|
|
873
|
+
"s3:PutEncryptionConfiguration",
|
|
874
|
+
"s3:PutBucketObjectLockConfiguration"
|
|
875
|
+
],
|
|
876
|
+
[backupBucket.bucketArn],
|
|
877
|
+
[p.accountRootArn, p.cfnExecRoleArn]
|
|
878
|
+
)
|
|
879
|
+
);
|
|
880
|
+
if (isProd) {
|
|
881
|
+
backupBucket.addToResourcePolicy(
|
|
882
|
+
denyUnlessPrincipal("ProdVersionsOnlyRoot", ["s3:DeleteObjectVersion", "s3:DeleteBucket"], backupResources, [
|
|
883
|
+
p.accountRootArn
|
|
884
|
+
])
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
const putDenied = (sid, condition) => new iam4.PolicyStatement({
|
|
888
|
+
sid,
|
|
889
|
+
effect: iam4.Effect.DENY,
|
|
890
|
+
principals: [new iam4.AnyPrincipal()],
|
|
891
|
+
actions: ["s3:PutObject"],
|
|
892
|
+
resources: [backupBucket.arnForObjects("*")],
|
|
893
|
+
conditions: { StringNotEquals: condition }
|
|
894
|
+
});
|
|
895
|
+
backupBucket.addToResourcePolicy(
|
|
896
|
+
putDenied("PutOnlyWithTheStackKey", { "s3:x-amz-server-side-encryption-aws-kms-key-id": key.keyArn })
|
|
897
|
+
);
|
|
898
|
+
backupBucket.addToResourcePolicy(putDenied("PutOnlySseKms", { "s3:x-amz-server-side-encryption": "aws:kms" }));
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// deploy/aws/lib/stack.ts
|
|
902
|
+
var MemoryAnywhereStack = class extends Stack {
|
|
903
|
+
constructor(scope, props) {
|
|
904
|
+
super(scope, stackName(props.stage), {
|
|
905
|
+
...props,
|
|
906
|
+
analyticsReporting: false,
|
|
907
|
+
// No BootstrapVersion SSM parameter in the template: CloudFormation would resolve it
|
|
908
|
+
// with the cfn-exec role, which may read only stage parameters. The CDK CLI still
|
|
909
|
+
// checks the bootstrap version itself, through the CDK deploy role.
|
|
910
|
+
synthesizer: new DefaultStackSynthesizer({ generateBootstrapVersionRule: false })
|
|
911
|
+
});
|
|
912
|
+
const { stage } = props;
|
|
913
|
+
Tags.of(this).add(TAG_STAGE, stage);
|
|
914
|
+
const host = new CfnParameter(this, "Host", {
|
|
915
|
+
type: "String",
|
|
916
|
+
description: "The owner's subdomain ({host}); the stage serves " + (stage === "prod" ? "{host} and admin.{host}" : stageHost(stage, "{host}")),
|
|
917
|
+
allowedPattern: "^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z]{2,63}$",
|
|
918
|
+
constraintDescription: "a lowercase DNS name (the owner's subdomain)"
|
|
919
|
+
});
|
|
920
|
+
const alertEmail = new CfnParameter(this, "AlertEmail", {
|
|
921
|
+
type: "String",
|
|
922
|
+
description: "Address for SNS alerts and the ACME account",
|
|
923
|
+
allowedPattern: "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$",
|
|
924
|
+
constraintDescription: "an email address"
|
|
925
|
+
});
|
|
926
|
+
const ownerTimezone = new CfnParameter(this, "OwnerTimezone", {
|
|
927
|
+
type: "String",
|
|
928
|
+
description: "The owner's IANA time zone: the daily digest and the nightly maintenance run in owner local time",
|
|
929
|
+
default: "UTC",
|
|
930
|
+
allowedPattern: "^[A-Za-z][A-Za-z0-9_+\\-]*(/[A-Za-z0-9_+\\-]+){0,2}$",
|
|
931
|
+
constraintDescription: "an IANA time zone such as Europe/Berlin"
|
|
932
|
+
});
|
|
933
|
+
const c = stackContext(this, stage);
|
|
934
|
+
const net = network(c);
|
|
935
|
+
const store = storage(c);
|
|
936
|
+
const topic = alertsTopic(c, alertEmail);
|
|
937
|
+
const access = instanceAccess(c, store, topic);
|
|
938
|
+
protectBackups(c, store, { ...access, s3Endpoint: net.s3Endpoint });
|
|
939
|
+
const { instance, eip } = server(c, { ...props, host, alertEmail, ownerTimezone }, net, store, access, topic);
|
|
940
|
+
alarms(c, topic, access.logGroup, instance);
|
|
941
|
+
costWatch(c, topic);
|
|
942
|
+
topicPublishers(c, topic, access.instanceRole);
|
|
943
|
+
audit(c, store.trailBucket, store.releasesBucket, topic);
|
|
944
|
+
const output = (id, value) => new CfnOutput(this, `${id}Out`, { key: id, value });
|
|
945
|
+
output("ElasticIp", eip.ref);
|
|
946
|
+
output("InstanceId", instance.ref);
|
|
947
|
+
output("BackupBucket", store.backupBucket.bucketName);
|
|
948
|
+
output("ReleasesBucket", store.releasesBucket.bucketName);
|
|
949
|
+
output("TrailBucket", store.trailBucket.bucketName);
|
|
950
|
+
output("ManifestBucket", store.manifestBucket.bucketName);
|
|
951
|
+
output("InstanceGeneration", String(props.instanceGeneration));
|
|
952
|
+
output("KeyArn", store.key.keyArn);
|
|
953
|
+
output("AlertsTopic", topic.topicArn);
|
|
954
|
+
output("ParameterPrefix", c.prefix);
|
|
955
|
+
output("Stage", stage);
|
|
956
|
+
const adminHost = stage === "prod" ? `admin.${host.valueAsString}` : stage === "spike" ? `spike.${host.valueAsString}` : `admin.eval.${host.valueAsString}`;
|
|
957
|
+
output("AdminOrigin", `https://${adminHost}`);
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
// deploy/aws/bin/app.ts
|
|
962
|
+
var app = new App();
|
|
963
|
+
var rawPins = app.node.tryGetContext(AMI_CONTEXT_KEY);
|
|
964
|
+
var amiPins = typeof rawPins === "string" ? JSON.parse(rawPins) : rawPins;
|
|
965
|
+
var inputs = loadStackInputs({
|
|
966
|
+
stage: app.node.tryGetContext("stage"),
|
|
967
|
+
region: app.node.tryGetContext("region"),
|
|
968
|
+
amiPins,
|
|
969
|
+
instanceGeneration: app.node.tryGetContext("instanceGeneration"),
|
|
970
|
+
devKeyFile: app.node.tryGetContext("devReleaseKey")
|
|
971
|
+
});
|
|
972
|
+
new MemoryAnywhereStack(app, { ...inputs, env: { region: inputs.region } });
|