Skip to content

Commit 8a006ca

Browse files
committed
refactor(cli): /simplify pass on lambda policies
Adds a typed TrustPolicyDocument / TrustPolicyStatement pair so buildRoleTrustPolicy can return a real type instead of unknown. The trust-policy shape has a Principal field that the generic PolicyStatement doesn't model, but it was previously punted via a return unknown rather than a parallel type. Test cleanup: drop the `as {...}` casts that the previous return- unknown signature forced.
1 parent a9f8a7a commit 8a006ca

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

packages/cli/src/commands/lambda/policies.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,10 @@ describe("policies — buildPolicyDocument", () => {
5555

5656
describe("policies — buildRoleTrustPolicy", () => {
5757
it("returns a sts:AssumeRole statement scoped to the requested service", () => {
58-
const trust = buildRoleTrustPolicy("cloudformation") as {
59-
Statement: { Action: string; Principal: { Service: string } }[];
60-
};
58+
const trust = buildRoleTrustPolicy("cloudformation");
6159
expect(trust.Statement[0]!.Action).toBe("sts:AssumeRole");
6260
expect(trust.Statement[0]!.Principal.Service).toBe("cloudformation.amazonaws.com");
63-
const lambdaTrust = buildRoleTrustPolicy("lambda") as {
64-
Statement: { Principal: { Service: string } }[];
65-
};
61+
const lambdaTrust = buildRoleTrustPolicy("lambda");
6662
expect(lambdaTrust.Statement[0]!.Principal.Service).toBe("lambda.amazonaws.com");
6763
});
6864
});

packages/cli/src/commands/lambda/policies.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ interface PolicyDocument {
3636
Statement: PolicyStatement[];
3737
}
3838

39+
/**
40+
* Trust-policy shape consumed by `policies role`. Has a `Principal`
41+
* field (which generic `PolicyStatement` does not model) — keep it as
42+
* a separate type rather than polluting the action-policy shape.
43+
*/
44+
interface TrustPolicyStatement {
45+
Effect: "Allow";
46+
Principal: { Service: string };
47+
Action: "sts:AssumeRole";
48+
}
49+
50+
interface TrustPolicyDocument {
51+
Version: "2012-10-17";
52+
Statement: TrustPolicyStatement[];
53+
}
54+
3955
/**
4056
* Actions the CLI needs to deploy/invoke/destroy the stack. Keep this
4157
* sorted alphabetically inside each service so diffs stay readable.
@@ -166,13 +182,8 @@ export function buildPolicyDocument(): PolicyDocument {
166182
};
167183
}
168184

169-
/**
170-
* Trust policy a service-linked IAM role consumes (used by `policies role`).
171-
* Returned as a structurally-correct `Allow sts:AssumeRole` with the named
172-
* service principal attached — the `Principal` field is an IAM-specific
173-
* extension not modelled by our generic `PolicyStatement` type.
174-
*/
175-
export function buildRoleTrustPolicy(principal: "lambda" | "cloudformation"): unknown {
185+
/** Trust policy a service-linked IAM role consumes (used by `policies role`). */
186+
export function buildRoleTrustPolicy(principal: "lambda" | "cloudformation"): TrustPolicyDocument {
176187
const Service = principal === "lambda" ? "lambda.amazonaws.com" : "cloudformation.amazonaws.com";
177188
return {
178189
Version: "2012-10-17",

0 commit comments

Comments
 (0)