Skip to content

Add support for multi-resource statements #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 29, 2024
10 changes: 8 additions & 2 deletions Sources/AWSLambdaEvents/APIGatewayLambdaAuthorizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ public struct APIGatewayLambdaAuthorizerPolicyResponse: Codable {
case deny = "Deny"
}

public let action: String
public let action: [String]
public let effect: Effect
public let resource: String
public let resource: [String]

public init(action: String, effect: Effect, resource: String) {
self.action = [action]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call the other init() method to avoid duplicating code

self.effect = effect
self.resource = [resource]
}

public init(action: [String], effect: Effect, resource: [String]) {
self.action = action
self.effect = effect
self.resource = resource
Expand Down
34 changes: 33 additions & 1 deletion Tests/AWSLambdaEventsTests/APIGatewayLambdaAuthorizerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,39 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {

XCTAssertEqual(resp.principalId, "John Appleseed")
XCTAssertEqual(resp.policyDocument.statement.count, 1)
XCTAssertEqual(resp.policyDocument.statement[0].action, "s3:getObject")
XCTAssertEqual(resp.policyDocument.statement[0].action, ["s3:getObject"])
XCTAssertEqual(resp.context?.count, 2)
XCTAssertEqual(resp.context?["abc1"], "xyz1")
}

func testDecodingLambdaAuthorizerPolicyResponseWithMultipleResources() {
let statement = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument.Statement(action: ["execute-api:Invoke"],
effect: .allow,
resource: [
"arn:aws:execute-api:*:*:*/*/GET/v1/user/0123",
"arn:aws:execute-api:*:*:*/*/POST/v1/user",
])
let policy = APIGatewayLambdaAuthorizerPolicyResponse.PolicyDocument(statement: [statement])
var resp = APIGatewayLambdaAuthorizerPolicyResponse(principalId: "John Appleseed",
policyDocument: policy,
context: ["abc1": "xyz1", "abc2": "xyz2"])

var data: Data?
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))

var stringData: String?
XCTAssertNoThrow(stringData = String(data: try XCTUnwrap(data), encoding: .utf8))

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

XCTAssertEqual(resp.principalId, "John Appleseed")
XCTAssertEqual(resp.policyDocument.statement.count, 1)
XCTAssertEqual(resp.policyDocument.statement[0].action, ["execute-api:Invoke"])
XCTAssertEqual(resp.policyDocument.statement[0].resource, [
"arn:aws:execute-api:*:*:*/*/GET/v1/user/0123",
"arn:aws:execute-api:*:*:*/*/POST/v1/user",
])
XCTAssertEqual(resp.context?.count, 2)
XCTAssertEqual(resp.context?["abc1"], "xyz1")
}
Expand Down