@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 247 lines 9.15 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { BackupPlan } from "@alchemy.run/aws/Backup/BackupPlan";
3 import { BackupSelection } from "@alchemy.run/aws/Backup/BackupSelection";
4 import { BackupVault } from "@alchemy.run/aws/Backup/BackupVault";
5 import { normalizePolicyDocument, type PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as backup from "@distilled.cloud/aws/backup";
9 import * as Provider from "alchemy/Provider";
10 import * as Effect from "effect/Effect";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const vaultName = "alchemy-test-backup-vault-lifecycle";
15
16 // A structured PolicyDocument (not a JSON string) — proves the typed prop
17 // deploys and that a re-deploy of the equivalent document is drift-free.
18 // NOTE: two actions on purpose — AWS Backup canonicalizes a single-element
19 // Action array to a bare string in the stored document, which would defeat
20 // a verbatim round-trip comparison.
21 const vaultAccessPolicy: PolicyDocument = {
22 Version: "2012-10-17",
23 Statement: [
24 {
25 Sid: "DenyRecoveryPointDeletion",
26 Effect: "Deny",
27 Principal: { AWS: "*" },
28 Action: ["backup:DeleteRecoveryPoint", "backup:UpdateRecoveryPointLifecycle"],
29 Resource: "*",
30 },
31 ],
32 };
33
34 // AWS Backup reports a missing vault as AccessDeniedException, not
35 // ResourceNotFoundException.
36 const getVault = backup
37 .describeBackupVault({ BackupVaultName: vaultName })
38 .pipe(
39 Effect.catchTag(["ResourceNotFoundException", "AccessDeniedException"], () =>
40 Effect.succeed(undefined),
41 ),
42 );
43
44 test.provider(
45 "lifecycle: vault + plan + selection, update plan, destroy",
46 (stack) =>
47 Effect.gen(function* () {
48 yield* stack.destroy();
49
50 // Create — vault, an IAM role AWS Backup can assume, a plan targeting the
51 // vault, and a tag-based selection assigned to that plan.
52 const deployed = yield* stack.deploy(
53 Effect.gen(function* () {
54 const vault = yield* BackupVault("LifecycleVault", {
55 backupVaultName: vaultName,
56 accessPolicy: vaultAccessPolicy,
57 tags: { env: "test" },
58 });
59 const role = yield* AWS.IAM.Role("LifecycleBackupRole", {
60 assumeRolePolicyDocument: {
61 Version: "2012-10-17",
62 Statement: [
63 {
64 Effect: "Allow",
65 Principal: { Service: "backup.amazonaws.com" },
66 Action: ["sts:AssumeRole"],
67 },
68 ],
69 },
70 managedPolicyArns: [
71 "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup",
72 ],
73 });
74 const plan = yield* BackupPlan("LifecyclePlan", {
75 rules: [
76 {
77 ruleName: "DailyBackups",
78 targetBackupVaultName: vault.backupVaultName,
79 scheduleExpression: "cron(0 5 ? * * *)",
80 startWindow: "1 hour",
81 completionWindow: "180 minutes",
82 lifecycle: { deleteAfter: "30 days" },
83 },
84 ],
85 tags: { env: "test" },
86 });
87 const selection = yield* BackupSelection("LifecycleSelection", {
88 backupPlanId: plan.backupPlanId,
89 iamRoleArn: role.roleArn,
90 listOfTags: [
91 {
92 conditionType: "STRINGEQUALS",
93 conditionKey: "aws:ResourceTag/backup",
94 conditionValue: "daily",
95 },
96 ],
97 });
98 return {
99 vaultArn: vault.backupVaultArn,
100 planId: plan.backupPlanId,
101 planName: plan.backupPlanName,
102 selectionId: selection.selectionId,
103 };
104 }),
105 );
106
107 expect(deployed.vaultArn).toContain(`backup-vault:${vaultName}`);
108
109 // Out-of-band verification via distilled.
110 const vault = yield* getVault;
111 expect(vault?.BackupVaultName).toBe(vaultName);
112
113 const plan = yield* backup.getBackupPlan({
114 BackupPlanId: deployed.planId,
115 });
116 expect(plan.BackupPlan?.BackupPlanName).toBe(deployed.planName);
117 expect(plan.BackupPlan?.Rules?.[0]?.RuleName).toBe("DailyBackups");
118 expect(plan.BackupPlan?.Rules?.[0]?.Lifecycle?.DeleteAfterDays).toBe(30);
119
120 const selection = yield* backup.getBackupSelection({
121 BackupPlanId: deployed.planId,
122 SelectionId: deployed.selectionId,
123 });
124 expect(selection.BackupSelection?.ListOfTags?.[0]?.ConditionKey).toBe(
125 "aws:ResourceTag/backup",
126 );
127
128 // Vault tags applied.
129 const vaultTags = yield* backup.listTags({
130 ResourceArn: deployed.vaultArn,
131 });
132 expect(vaultTags.Tags?.env).toBe("test");
133
134 // The PolicyDocument-valued access policy round-trips: the document
135 // AWS stored is canonically equal to the one we deployed.
136 const storedPolicy = yield* backup.getBackupVaultAccessPolicy({
137 BackupVaultName: vaultName,
138 });
139 expect(storedPolicy.Policy).toBeDefined();
140 expect(normalizePolicyDocument(storedPolicy.Policy!)).toBe(
141 normalizePolicyDocument(vaultAccessPolicy),
142 );
143
144 // Canonical list() coverage.
145 const vaultProvider = yield* Provider.findProvider(BackupVault);
146 const allVaults = yield* vaultProvider.list();
147 expect(allVaults.some((v) => v.backupVaultName === vaultName)).toBe(true);
148
149 // Update the plan in place — change retention and start window. The
150 // vault re-deploys with an equivalent PolicyDocument (statement keys
151 // reordered) plus a tag change, so its reconcile runs and the policy
152 // sync must conclude "no drift" against the stored document.
153 yield* stack.deploy(
154 Effect.gen(function* () {
155 const vault = yield* BackupVault("LifecycleVault", {
156 backupVaultName: vaultName,
157 accessPolicy: {
158 Statement: [
159 {
160 Resource: "*",
161 Action: ["backup:DeleteRecoveryPoint", "backup:UpdateRecoveryPointLifecycle"],
162 Principal: { AWS: "*" },
163 Effect: "Deny",
164 Sid: "DenyRecoveryPointDeletion",
165 },
166 ],
167 Version: "2012-10-17",
168 },
169 tags: { env: "test", phase: "two" },
170 });
171 const role = yield* AWS.IAM.Role("LifecycleBackupRole", {
172 assumeRolePolicyDocument: {
173 Version: "2012-10-17",
174 Statement: [
175 {
176 Effect: "Allow",
177 Principal: { Service: "backup.amazonaws.com" },
178 Action: ["sts:AssumeRole"],
179 },
180 ],
181 },
182 managedPolicyArns: [
183 "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup",
184 ],
185 });
186 const plan = yield* BackupPlan("LifecyclePlan", {
187 rules: [
188 {
189 ruleName: "DailyBackups",
190 targetBackupVaultName: vault.backupVaultName,
191 scheduleExpression: "cron(0 6 ? * * *)",
192 startWindow: "2 hours",
193 completionWindow: "6 hours",
194 lifecycle: { deleteAfter: "60 days" },
195 },
196 ],
197 tags: { env: "test" },
198 });
199 yield* BackupSelection("LifecycleSelection", {
200 backupPlanId: plan.backupPlanId,
201 iamRoleArn: role.roleArn,
202 listOfTags: [
203 {
204 conditionType: "STRINGEQUALS",
205 conditionKey: "aws:ResourceTag/backup",
206 conditionValue: "daily",
207 },
208 ],
209 });
210 return { planId: plan.backupPlanId };
211 }),
212 );
213
214 const updatedPlan = yield* backup.getBackupPlan({
215 BackupPlanId: deployed.planId,
216 });
217 expect(updatedPlan.BackupPlan?.Rules?.[0]?.Lifecycle?.DeleteAfterDays).toBe(60);
218 expect(updatedPlan.BackupPlan?.Rules?.[0]?.CompletionWindowMinutes).toBe(360);
219
220 // Re-deploy was clean: the stored access policy is still canonically
221 // equal to the original document, and the tag update landed.
222 const policyAfterRedeploy = yield* backup.getBackupVaultAccessPolicy({
223 BackupVaultName: vaultName,
224 });
225 expect(normalizePolicyDocument(policyAfterRedeploy.Policy!)).toBe(
226 normalizePolicyDocument(vaultAccessPolicy),
227 );
228 const tagsAfterRedeploy = yield* backup.listTags({
229 ResourceArn: deployed.vaultArn,
230 });
231 expect(tagsAfterRedeploy.Tags?.phase).toBe("two");
232
233 // Destroy — provider recursively deletes and everything is gone.
234 yield* stack.destroy();
235 const afterVault = yield* getVault;
236 expect(afterVault).toBeUndefined();
237
238 // The plan no longer appears in the account listing.
239 const planProvider = yield* Provider.findProvider(BackupPlan);
240 const remainingPlans = yield* planProvider.list();
241 expect(remainingPlans.some((p) => p.backupPlanId === deployed.planId)).toBe(false);
242 }),
243 {
244 tags: ["provider:aws", "provider:aws:backup", "provider:aws:iam", "live"],
245 timeout: 240_000,
246 },
247 );