@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 149 lines 5.31 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 IdentityPool,
4 IdentityPoolRoleAttachment,
5 UserPool,
6 UserPoolClient,
7 } from "@alchemy.run/aws/Cognito";
8 import { Role } from "@alchemy.run/aws/IAM";
9 import { expect } from "@alchemy.run/test";
10 import * as Test from "@alchemy.run/test/Alchemy";
11 import * as ci from "@distilled.cloud/aws/cognito-identity";
12 import * as Output from "alchemy/Output";
13 import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 import * as Schedule from "effect/Schedule";
16
17 const { test } = Test.make({ providers: AWS.providers() });
18
19 class IdentityPoolStillExists extends Data.TaggedError("IdentityPoolStillExists")<{
20 readonly identityPoolId: string;
21 }> {}
22
23 const assertIdentityPoolDeleted = (identityPoolId: string) =>
24 ci.describeIdentityPool({ IdentityPoolId: identityPoolId }).pipe(
25 Effect.flatMap(() => Effect.fail(new IdentityPoolStillExists({ identityPoolId }))),
26 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
27 Effect.retry({
28 while: (e) => e._tag === "IdentityPoolStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 test.provider(
34 "identity pool lifecycle with user pool federation and tags",
35 (stack) =>
36 Effect.gen(function* () {
37 yield* stack.destroy();
38
39 const outputs = yield* stack.deploy(
40 Effect.gen(function* () {
41 const identities = yield* IdentityPool("Identities", {
42 allowUnauthenticatedIdentities: false,
43 tags: { Environment: "test" },
44 });
45 return { identities };
46 }),
47 );
48
49 expect(outputs.identities.identityPoolId).toContain(":");
50 expect(outputs.identities.identityPoolArn).toContain(":identitypool/");
51
52 // out-of-band verification via distilled
53 const created = yield* ci.describeIdentityPool({
54 IdentityPoolId: outputs.identities.identityPoolId,
55 });
56 expect(created.IdentityPoolName).toBe(outputs.identities.identityPoolName);
57 expect(created.AllowUnauthenticatedIdentities).toBe(false);
58 const tags = yield* ci.listTagsForResource({
59 ResourceArn: outputs.identities.identityPoolArn,
60 });
61 expect(tags.Tags?.Environment).toBe("test");
62 expect(tags.Tags?.["alchemy::id"]).toBe("Identities");
63
64 // update in place: federate a user pool + allow guests
65 const updated = yield* stack.deploy(
66 Effect.gen(function* () {
67 const pool = yield* UserPool("FederatedPool", {});
68 const client = yield* UserPoolClient("FederatedClient", {
69 userPoolId: pool.userPoolId,
70 });
71 const identities = yield* IdentityPool("Identities", {
72 allowUnauthenticatedIdentities: true,
73 cognitoIdentityProviders: [
74 {
75 providerName: Output.interpolate`cognito-idp.us-west-2.amazonaws.com/${pool.userPoolId}`,
76 clientId: client.clientId,
77 },
78 ],
79 tags: { Environment: "test" },
80 });
81 return { pool, client, identities };
82 }),
83 );
84 expect(updated.identities.identityPoolId).toBe(outputs.identities.identityPoolId);
85
86 const afterUpdate = yield* ci.describeIdentityPool({
87 IdentityPoolId: outputs.identities.identityPoolId,
88 });
89 expect(afterUpdate.AllowUnauthenticatedIdentities).toBe(true);
90 expect(afterUpdate.CognitoIdentityProviders).toHaveLength(1);
91
92 yield* stack.destroy();
93 yield* assertIdentityPoolDeleted(outputs.identities.identityPoolId);
94 }),
95 { tags: ["provider:aws", "provider:aws:cognito", "live"], timeout: 120_000 },
96 );
97
98 test.provider(
99 "role attachment lifecycle",
100 (stack) =>
101 Effect.gen(function* () {
102 yield* stack.destroy();
103
104 const outputs = yield* stack.deploy(
105 Effect.gen(function* () {
106 const identities = yield* IdentityPool("RolesPool", {
107 allowUnauthenticatedIdentities: true,
108 });
109 const role = yield* Role("CognitoAuthRole", {
110 assumeRolePolicyDocument: {
111 Version: "2012-10-17",
112 Statement: [
113 {
114 Effect: "Allow",
115 Principal: { Federated: "cognito-identity.amazonaws.com" },
116 Action: ["sts:AssumeRoleWithWebIdentity"],
117 Condition: {
118 StringEquals: {
119 "cognito-identity.amazonaws.com:aud": identities.identityPoolId,
120 },
121 },
122 },
123 ],
124 },
125 });
126 const attachment = yield* IdentityPoolRoleAttachment("Roles", {
127 identityPoolId: identities.identityPoolId,
128 roles: { authenticated: role.roleArn },
129 });
130 return { identities, role, attachment };
131 }),
132 );
133
134 expect(outputs.attachment.roles.authenticated).toBe(outputs.role.roleArn);
135
136 // out-of-band verification via distilled
137 const observed = yield* ci.getIdentityPoolRoles({
138 IdentityPoolId: outputs.identities.identityPoolId,
139 });
140 expect(observed.Roles?.authenticated).toBe(outputs.role.roleArn);
141
142 yield* stack.destroy();
143 yield* assertIdentityPoolDeleted(outputs.identities.identityPoolId);
144 }),
145 {
146 tags: ["provider:aws", "provider:aws:cognito", "provider:aws:iam", "live"],
147 timeout: 120_000,
148 },
149 );