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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/AWSLambdaEvents/APIGatewayLambdaAuthorizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,19 @@ public struct APIGatewayLambdaAuthorizerPolicyResponse: Codable, Sendable {
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.init(
action: [action],
effect: effect,
resource: [resource]
)
}

public init(action: [String], effect: Effect, resource: [String]) {
self.action = action
self.effect = effect
self.resource = resource
Expand Down
38 changes: 35 additions & 3 deletions Tests/AWSLambdaEventsTests/APIGatewayLambdaAuthorizerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class APIGatewayLambdaAuthorizerTests: XCTestCase {
XCTAssertNoThrow(data = try JSONEncoder().encode(resp))

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

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

var stringData: String?
XCTAssertNoThrow(stringData = String(data: try XCTUnwrap(data), encoding: .utf8))
XCTAssertNoThrow(stringData = try String(data: 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, "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 = try String(data: 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