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
| 1 | import type { IamAction } from "@alchemy.run/aws/IAM/actions.generated"; |
| 2 | import { |
| 3 | normalizePolicyDocument, |
| 4 | stringifyPolicyDocument, |
| 5 | type PolicyDocument, |
| 6 | type ServiceControlPolicyDocument, |
| 7 | } from "@alchemy.run/aws/IAM/Policy"; |
| 8 | import { describe, expect, it } from "@alchemy.run/test"; |
| 9 | |
| 10 | const document: PolicyDocument = { |
| 11 | Version: "2012-10-17", |
| 12 | Statement: [ |
| 13 | { |
| 14 | Sid: "ReadObjects", |
| 15 | Effect: "Allow", |
| 16 | Action: ["s3:GetObject", "s3:ListBucket"], |
| 17 | Resource: ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-bucket/*"], |
| 18 | Condition: { StringEquals: { "aws:PrincipalOrgID": "o-example" } }, |
| 19 | }, |
| 20 | ], |
| 21 | }; |
| 22 | |
| 23 | describe( |
| 24 | "stringifyPolicyDocument", |
| 25 | { tags: ["unit", "provider:aws", "provider:aws:iam", "local"] }, |
| 26 | () => { |
| 27 | it("round-trips through JSON.parse", () => { |
| 28 | expect(JSON.parse(stringifyPolicyDocument(document))).toEqual(document); |
| 29 | }); |
| 30 | }, |
| 31 | ); |
| 32 | |
| 33 | describe( |
| 34 | "normalizePolicyDocument", |
| 35 | { tags: ["unit", "provider:aws", "provider:aws:iam", "local"] }, |
| 36 | () => { |
| 37 | it("makes two equivalent documents with different key order equal", () => { |
| 38 | const reordered = { |
| 39 | Statement: [ |
| 40 | { |
| 41 | Condition: { StringEquals: { "aws:PrincipalOrgID": "o-example" } }, |
| 42 | Resource: ["arn:aws:s3:::my-bucket", "arn:aws:s3:::my-bucket/*"], |
| 43 | Action: ["s3:GetObject", "s3:ListBucket"], |
| 44 | Effect: "Allow", |
| 45 | Sid: "ReadObjects", |
| 46 | }, |
| 47 | ], |
| 48 | Version: "2012-10-17", |
| 49 | }; |
| 50 | expect(normalizePolicyDocument(reordered)).toBe(normalizePolicyDocument(document)); |
| 51 | }); |
| 52 | |
| 53 | it("treats a JSON string and the equivalent object identically", () => { |
| 54 | const json = JSON.stringify(document, null, 2); |
| 55 | expect(normalizePolicyDocument(json)).toBe(normalizePolicyDocument(document)); |
| 56 | }); |
| 57 | |
| 58 | it("handles the URL-encoded documents IAM returns", () => { |
| 59 | const encoded = encodeURIComponent(JSON.stringify(document)); |
| 60 | expect(normalizePolicyDocument(encoded)).toBe(normalizePolicyDocument(document)); |
| 61 | }); |
| 62 | |
| 63 | it("is deterministic for the same document", () => { |
| 64 | expect(normalizePolicyDocument(document)).toBe( |
| 65 | normalizePolicyDocument(JSON.parse(stringifyPolicyDocument(document))), |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | it("returns unparseable strings unchanged so drift diffs still fire", () => { |
| 70 | expect(normalizePolicyDocument("not-json{")).toBe("not-json{"); |
| 71 | }); |
| 72 | |
| 73 | it("treats singleton arrays and scalars as equivalent (AWS collapses them)", () => { |
| 74 | // The IAM grammar accepts `"x"` wherever `["x"]` is legal, and several |
| 75 | // services (e.g. Secrets Manager `GetResourcePolicy`) return the scalar |
| 76 | // form for a stored singleton array — the two must canonicalize equal or |
| 77 | // every re-deploy spuriously re-puts the policy. |
| 78 | const arrays: PolicyDocument = { |
| 79 | Version: "2012-10-17", |
| 80 | Statement: [ |
| 81 | { |
| 82 | Effect: "Allow", |
| 83 | Principal: { AWS: ["arn:aws:iam::123456789012:root"] }, |
| 84 | Action: ["secretsmanager:GetSecretValue"], |
| 85 | Resource: "*", |
| 86 | }, |
| 87 | ], |
| 88 | }; |
| 89 | const scalars = { |
| 90 | Version: "2012-10-17", |
| 91 | Statement: { |
| 92 | Effect: "Allow", |
| 93 | Principal: { AWS: "arn:aws:iam::123456789012:root" }, |
| 94 | Action: "secretsmanager:GetSecretValue", |
| 95 | Resource: "*", |
| 96 | }, |
| 97 | }; |
| 98 | expect(normalizePolicyDocument(arrays)).toBe(normalizePolicyDocument(scalars)); |
| 99 | // Multi-element arrays are untouched (order still significant). |
| 100 | expect(normalizePolicyDocument(document)).toContain( |
| 101 | '"Action":["s3:GetObject","s3:ListBucket"]', |
| 102 | ); |
| 103 | }); |
| 104 | }, |
| 105 | ); |
| 106 | |
| 107 | describe("IamAction", { tags: ["unit", "provider:aws", "provider:aws:iam", "local"] }, () => { |
| 108 | it("accepts generated literals, wildcards, and arbitrary strings", () => { |
| 109 | // Compile-time assertions: generated ops autocomplete, `service:*` |
| 110 | // wildcards exist, and the `(string & {})` escape hatch never hard-breaks. |
| 111 | const actions: IamAction[] = [ |
| 112 | "s3:GetObject", |
| 113 | "iam:CreateRole", |
| 114 | "states:StartExecution", |
| 115 | "cloudwatch:PutMetricData", |
| 116 | "dynamodb:*", |
| 117 | "some-future-service:SomeUnknownAction", |
| 118 | ]; |
| 119 | expect(actions).toHaveLength(6); |
| 120 | |
| 121 | // PolicyStatement.Action accepts both typed and plain-string arrays. |
| 122 | const typed: PolicyDocument = { |
| 123 | Version: "2012-10-17", |
| 124 | Statement: [{ Effect: "Allow", Action: ["lambda:InvokeFunction"], Resource: "*" }], |
| 125 | }; |
| 126 | const plain: string[] = ["s3:GetObject"]; |
| 127 | const fromStrings: PolicyDocument = { |
| 128 | Version: "2012-10-17", |
| 129 | Statement: [{ Effect: "Allow", Action: plain, Resource: "*" }], |
| 130 | }; |
| 131 | expect(typed.Statement).toHaveLength(1); |
| 132 | expect(fromStrings.Statement).toHaveLength(1); |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | describe( |
| 137 | "ServiceControlPolicyDocument", |
| 138 | { tags: ["unit", "provider:aws", "provider:aws:iam", "local"] }, |
| 139 | () => { |
| 140 | it("types the SCP-legal subset (no Principal) and normalizes like any doc", () => { |
| 141 | const scp: ServiceControlPolicyDocument = { |
| 142 | Version: "2012-10-17", |
| 143 | Statement: [ |
| 144 | { Sid: "AllowAll", Effect: "Allow", Action: ["*"], Resource: "*" }, |
| 145 | { |
| 146 | Sid: "DenyLeaveOrg", |
| 147 | Effect: "Deny", |
| 148 | Action: ["organizations:LeaveOrganization"], |
| 149 | Resource: "*", |
| 150 | Condition: { StringNotEquals: { "aws:PrincipalOrgID": "o-example" } }, |
| 151 | }, |
| 152 | ], |
| 153 | }; |
| 154 | const illegal: ServiceControlPolicyDocument = { |
| 155 | Version: "2012-10-17", |
| 156 | Statement: [ |
| 157 | { |
| 158 | Effect: "Deny", |
| 159 | Action: ["s3:*"], |
| 160 | Resource: "*", |
| 161 | // @ts-expect-error — SCP statements never carry a Principal |
| 162 | Principal: { AWS: "*" }, |
| 163 | }, |
| 164 | ], |
| 165 | }; |
| 166 | expect(illegal).toBeDefined(); |
| 167 | expect(normalizePolicyDocument(scp)).toBe( |
| 168 | normalizePolicyDocument(JSON.parse(JSON.stringify(scp))), |
| 169 | ); |
| 170 | }); |
| 171 | }, |
| 172 | ); |