Skip to content

Commit 212c6dc

Browse files
authored
Add support for multi-resource statements (#52)
1 parent 2c04633 commit 212c6dc

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

Diff for: Sources/AWSLambdaEvents/APIGatewayLambdaAuthorizers.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,19 @@ public struct APIGatewayLambdaAuthorizerPolicyResponse: Codable, Sendable {
8181
case deny = "Deny"
8282
}
8383

84-
public let action: String
84+
public let action: [String]
8585
public let effect: Effect
86-
public let resource: String
86+
public let resource: [String]
8787

8888
public init(action: String, effect: Effect, resource: String) {
89+
self.init(
90+
action: [action],
91+
effect: effect,
92+
resource: [resource]
93+
)
94+
}
95+
96+
public init(action: [String], effect: Effect, resource: [String]) {
8997
self.action = action
9098
self.effect = effect
9199
self.resource = resource

Diff for: Tests/AWSLambdaEventsTests/APIGatewayLambdaAuthorizerTest.swift

+35-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {
171171
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))
172172

173173
var stringData: String?
174-
XCTAssertNoThrow(stringData = String(data: try XCTUnwrap(data), encoding: .utf8))
174+
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
175175

176176
data = stringData?.data(using: .utf8)
177177
XCTAssertNoThrow(resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerSimpleResponse.self, from: XCTUnwrap(data)))
@@ -194,14 +194,46 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {
194194
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))
195195

196196
var stringData: String?
197-
XCTAssertNoThrow(stringData = String(data: try XCTUnwrap(data), encoding: .utf8))
197+
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
198198

199199
data = stringData?.data(using: .utf8)
200200
XCTAssertNoThrow(resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: XCTUnwrap(data)))
201201

202202
XCTAssertEqual(resp.principalId, "John Appleseed")
203203
XCTAssertEqual(resp.policyDocument.statement.count, 1)
204-
XCTAssertEqual(resp.policyDocument.statement[0].action, "s3:getObject")
204+
XCTAssertEqual(resp.policyDocument.statement[0].action, ["s3:getObject"])
205+
XCTAssertEqual(resp.context?.count, 2)
206+
XCTAssertEqual(resp.context?["abc1"], "xyz1")
207+
}
208+
209+
func testDecodingLambdaAuthorizerPolicyResponseWithMultipleResources() {
210+
let statement = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument.Statement(action: ["execute-api:Invoke"],
211+
effect: .allow,
212+
resource: [
213+
"arn:aws:execute-api:*:*:*/*/GET/v1/user/0123",
214+
"arn:aws:execute-api:*:*:*/*/POST/v1/user",
215+
])
216+
let policy = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument(statement: [statement])
217+
var resp = APIGatewayLambdaAuthorizerPolicyResponse(principalId: "John Appleseed",
218+
policyDocument: policy,
219+
context: ["abc1": "xyz1", "abc2": "xyz2"])
220+
221+
var data: Data?
222+
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))
223+
224+
var stringData: String?
225+
XCTAssertNoThrow(stringData = try String(data: XCTUnwrap(data), encoding: .utf8))
226+
227+
data = stringData?.data(using: .utf8)
228+
XCTAssertNoThrow(resp = try JSONDecoder().decode(APIGatewayLambdaAuthorizerPolicyResponse.self, from: XCTUnwrap(data)))
229+
230+
XCTAssertEqual(resp.principalId, "John Appleseed")
231+
XCTAssertEqual(resp.policyDocument.statement.count, 1)
232+
XCTAssertEqual(resp.policyDocument.statement[0].action, ["execute-api:Invoke"])
233+
XCTAssertEqual(resp.policyDocument.statement[0].resource, [
234+
"arn:aws:execute-api:*:*:*/*/GET/v1/user/0123",
235+
"arn:aws:execute-api:*:*:*/*/POST/v1/user",
236+
])
205237
XCTAssertEqual(resp.context?.count, 2)
206238
XCTAssertEqual(resp.context?["abc1"], "xyz1")
207239
}

0 commit comments

Comments
 (0)