@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 204 lines 7.69 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { OpenIDConnectProvider, Role, SAMLProvider } from "@alchemy.run/aws/IAM";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as IAM from "@distilled.cloud/aws/iam";
6 import * as Effect from "effect/Effect";
7 import {
8 testOidcGithubHost,
9 testOidcGithubUrl,
10 testOidcThumbprintA,
11 testOidcThumbprintB,
12 testOidcUrl,
13 testSamlMetadataDocument,
14 testSamlMetadataDocumentUpdated,
15 } from "./fixtures.ts";
16
17 const { test } = Test.make({ providers: AWS.providers() });
18
19 describe(
20 "AWS.IAM federation resources",
21 { tags: ["provider:aws", "provider:aws:iam", "live"] },
22 () => {
23 test.provider("create, update, and delete an OpenID Connect provider", (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 const provider = yield* stack.deploy(
28 Effect.gen(function* () {
29 return yield* OpenIDConnectProvider("OidcProvider", {
30 url: testOidcUrl,
31 clientIDList: ["sts.amazonaws.com"],
32 thumbprintList: [testOidcThumbprintA],
33 tags: { env: "test" },
34 });
35 }),
36 );
37
38 const created = yield* IAM.getOpenIDConnectProvider({
39 OpenIDConnectProviderArn: provider.openIDConnectProviderArn,
40 });
41 expect(created.Url).toBe(testOidcUrl.replace(/^https?:\/\//, ""));
42 expect(created.ClientIDList ?? []).toContain("sts.amazonaws.com");
43
44 yield* stack.deploy(
45 Effect.gen(function* () {
46 return yield* OpenIDConnectProvider("OidcProvider", {
47 url: testOidcUrl,
48 clientIDList: ["sts.amazonaws.com", "alchemy-client"],
49 thumbprintList: [testOidcThumbprintB],
50 tags: { env: "prod" },
51 });
52 }),
53 );
54
55 const updated = yield* IAM.getOpenIDConnectProvider({
56 OpenIDConnectProviderArn: provider.openIDConnectProviderArn,
57 });
58 expect(updated.ClientIDList ?? []).toContain("alchemy-client");
59 expect(updated.ThumbprintList).toEqual([testOidcThumbprintB]);
60
61 const tags = yield* IAM.listOpenIDConnectProviderTags({
62 OpenIDConnectProviderArn: provider.openIDConnectProviderArn,
63 });
64 expect(
65 Object.fromEntries((tags.Tags ?? []).map((tag) => [tag.Key, tag.Value])),
66 ).toMatchObject({ env: "prod" });
67
68 yield* stack.destroy();
69
70 const deleted = yield* IAM.getOpenIDConnectProvider({
71 OpenIDConnectProviderArn: provider.openIDConnectProviderArn,
72 }).pipe(Effect.option);
73 expect(deleted._tag).toBe("None");
74 }),
75 );
76
77 // The GitHub Actions federation flagship: an OIDC provider plus a Role
78 // whose trust policy federates to it with a repo-scoped `sub` condition.
79 // The issuer is a stand-in (see fixtures.ts) — the account's real
80 // token.actions.githubusercontent.com provider belongs to CI and IAM
81 // allows only one provider per issuer URL — but the trust mechanics
82 // (Federated principal + AssumeRoleWithWebIdentity + aud/sub conditions)
83 // are identical.
84 test.provider(
85 "federates a role to a GitHub-Actions-style OIDC provider",
86 (stack) =>
87 Effect.gen(function* () {
88 yield* stack.destroy();
89
90 const outputs = yield* stack.deploy(
91 Effect.gen(function* () {
92 const oidc = yield* OpenIDConnectProvider("GithubStyleOidc", {
93 url: testOidcGithubUrl,
94 clientIDList: ["sts.amazonaws.com"],
95 thumbprintList: [testOidcThumbprintA],
96 });
97 const role = yield* Role("GithubDeployRole", {
98 assumeRolePolicyDocument: {
99 Version: "2012-10-17",
100 Statement: [
101 {
102 Effect: "Allow",
103 Principal: { Federated: oidc.openIDConnectProviderArn },
104 Action: ["sts:AssumeRoleWithWebIdentity"],
105 Condition: {
106 StringEquals: { [`${testOidcGithubHost}:aud`]: "sts.amazonaws.com" },
107 StringLike: { [`${testOidcGithubHost}:sub`]: "repo:sam-goodwin/alchemy:*" },
108 },
109 },
110 ],
111 },
112 });
113 return {
114 providerArn: oidc.openIDConnectProviderArn,
115 roleName: role.roleName,
116 roleArn: role.roleArn,
117 };
118 }),
119 );
120
121 // Assert the trust document out-of-band from the live role.
122 const live = yield* IAM.getRole({ RoleName: outputs.roleName });
123 const trust = JSON.parse(
124 decodeURIComponent(live.Role?.AssumeRolePolicyDocument ?? ""),
125 ) as {
126 Statement: [
127 {
128 Effect: string;
129 Principal: { Federated: string };
130 // IAM normalizes a single-element array to a bare string in
131 // the stored document.
132 Action: string | string[];
133 Condition: Record<string, Record<string, string>>;
134 },
135 ];
136 };
137 expect(trust.Statement).toHaveLength(1);
138 expect(trust.Statement[0].Effect).toBe("Allow");
139 expect(trust.Statement[0].Principal.Federated).toBe(outputs.providerArn);
140 expect([trust.Statement[0].Action].flat()).toEqual(["sts:AssumeRoleWithWebIdentity"]);
141 expect(trust.Statement[0].Condition.StringEquals[`${testOidcGithubHost}:aud`]).toBe(
142 "sts.amazonaws.com",
143 );
144 expect(trust.Statement[0].Condition.StringLike[`${testOidcGithubHost}:sub`]).toBe(
145 "repo:sam-goodwin/alchemy:*",
146 );
147
148 yield* stack.destroy();
149
150 const deletedRole = yield* IAM.getRole({ RoleName: outputs.roleName }).pipe(
151 Effect.option,
152 );
153 expect(deletedRole._tag).toBe("None");
154 const deletedProvider = yield* IAM.getOpenIDConnectProvider({
155 OpenIDConnectProviderArn: outputs.providerArn,
156 }).pipe(Effect.option);
157 expect(deletedProvider._tag).toBe("None");
158 }),
159 { timeout: 120_000 },
160 );
161
162 test.provider("create, update, and delete a SAML provider", (stack) =>
163 Effect.gen(function* () {
164 yield* stack.destroy();
165
166 const provider = yield* stack.deploy(
167 Effect.gen(function* () {
168 return yield* SAMLProvider("SamlProvider", {
169 samlMetadataDocument: testSamlMetadataDocument,
170 tags: { env: "test" },
171 });
172 }),
173 );
174
175 const created = yield* IAM.getSAMLProvider({ SAMLProviderArn: provider.samlProviderArn });
176 expect(created.SAMLMetadataDocument).toContain("urn:alchemy:test:idp");
177
178 yield* stack.deploy(
179 Effect.gen(function* () {
180 return yield* SAMLProvider("SamlProvider", {
181 samlMetadataDocument: testSamlMetadataDocumentUpdated,
182 tags: { env: "prod" },
183 });
184 }),
185 );
186
187 const updated = yield* IAM.getSAMLProvider({ SAMLProviderArn: provider.samlProviderArn });
188 expect(updated.SAMLMetadataDocument).toContain("urn:alchemy:test:idp:updated");
189
190 const tags = yield* IAM.listSAMLProviderTags({ SAMLProviderArn: provider.samlProviderArn });
191 expect(
192 Object.fromEntries((tags.Tags ?? []).map((tag) => [tag.Key, tag.Value])),
193 ).toMatchObject({ env: "prod" });
194
195 yield* stack.destroy();
196
197 const deleted = yield* IAM.getSAMLProvider({
198 SAMLProviderArn: provider.samlProviderArn,
199 }).pipe(Effect.option);
200 expect(deleted._tag).toBe("None");
201 }),
202 );
203 },
204 );