8000 APIGW: fix importing API with Cognito Authorizer by bentsku · Pull Request #11783 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

APIGW: fix importing API with Cognito Authorizer #11783

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 1 commit into from
Nov 5, 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
4 changes: 2 additions & 2 deletions localstack-core/localstack/services/apigateway/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def create_authorizers(security_schemes: dict) -> None:
name=security_scheme_name,
type=authorizer_type,
authorizerResultTtlInSeconds=aws_apigateway_authorizer.get(
"authorizerResultTtlInSeconds", 300
"authorizerResultTtlInSeconds", None
),
)
if provider_arns := aws_apigateway_authorizer.get("providerARNs"):
Expand All @@ -548,7 +548,7 @@ def create_authorizers(security_schemes: dict) -> None:
authorizer["authorizerUri"] = authorizer_uri
if authorizer_credentials := aws_apigateway_authorizer.get("authorizerCredentials"):
authorizer["authorizerCredentials"] = authorizer_credentials
if authorizer_type == "TOKEN":
if authorizer_type in ("TOKEN", "COGNITO_USER_POOLS"):
header_name = security_config.get("name")
authorizer["identitySource"] = f"method.request.header.{header_name}"
elif identity_source := aws_apigateway_authorizer.get("identitySource"):
Expand Down
97 changes: 97 additions & 0 deletions tests/aws/files/openapi.cognito-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"openapi": "3.0.1",
"info": {
"title": "Example Pet Store",
"description": "A Pet Store API.",
"version": "1.0"
},
"paths": {
"/pets": {
"get": {
"operationId": "GET HTTP",
"parameters": [
{
"name": "type",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "page",
"in": "query",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "200 response",
"headers": {
"Access-Control-Allow-Origin": {
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "HTTP_PROXY",
"httpMethod": "GET",
"uri": "http://petstore.execute-api.us-west-1.amazonaws.com/petstore/pets",
"payloadFormatVersion": 1.0
}
}
}
},
"components": {
"securitySchemes": {
"cognito-test-identity-source": {
"type": "apiKey",
"name": "TestHeaderAuth",
"in": "header",
"x-amazon-apigateway-authtype": "cognito_user_pools",
"x-amazon-apigateway-authorizer": {
"type": "cognito_user_pools",
"providerARNs": [
"${cognito_pool_arn}"
]
}
}
},
"schemas": {
"Pets": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Empty": {
"type": "object"
},
"Pet": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"price": {
"type": "number"
}
}
}
}
}
}
38 changes: 38 additions & 0 deletions tests/aws/services/apigateway/test_apigateway_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
SWAGGER_MOCK_CORS_JSON = os.path.join(PARENT_DIR, "../../files/swagger-mock-cors.json")
PETSTORE_SWAGGER_JSON = os.path.join(PARENT_DIR, "../../files/petstore-authorizer.swagger.json")
TEST_SWAGGER_FILE_JSON = os.path.join(PARENT_DIR, "../../files/swagger.json")
TEST_OPENAPI_COGNITO_AUTH = os.path.join(PARENT_DIR, "../../files/openapi.cognito-auth.json")
TEST_OAS30_BASE_PATH_SERVER_VAR_FILE_YAML = os.path.join(
PARENT_DIR, "../../files/openapi-basepath-server-variable.yaml"
)
Expand Down Expand Up @@ -839,3 +840,40 @@ def test_import_with_http_method_integration(
# this fixture will iterate over every resource and match its method, methodResponse, integration and
# integrationResponse
apigw_snapshot_imported_resources(rest_api_id=rest_api_id, resources=response)

@pytest.mark.no_apigw_snap_transformers
@markers.aws.validated
def test_import_with_cognito_auth_identity_source(
self,
region_name,
account_id,
import_apigw,
snapshot,
aws_client,
apigw_snapshot_imported_resources,
):
snapshot.add_transformers_list(
[
snapshot.transform.jsonpath("$.import-swagger.id", value_replacement="rest-id"),
snapshot.transform.jsonpath(
"$.import-swagger.rootResourceId", value_replacement="root-resource-id"
),
snapshot.transform.jsonpath(
"$.get-authorizers.items..id", value_replacement="authorizer-id"
),
]
)
spec_file = load_file(TEST_OPENAPI_COGNITO_AUTH)
# the authorizer does not need to exist in AWS
spec_file = spec_file.replace(
"${cognito_pool_arn}",
f"arn:aws:cognito-idp:{region_name}:{account_id}:userpool/{region_name}_ABC123",
)
response, root_id = import_apigw(body=spec_file, failOnWarnings=True)
snapshot.match("import-swagger", response)

rest_api_id = response["id"]

# assert that are no multiple authorizers
authorizers = aws_client.apigateway.get_authorizers(restApiId=rest_api_id)
snapshot.match("get-authorizers", authorizers)
42 changes: 42 additions & 0 deletions tests/aws/services/apigateway/test_apigateway_import.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -4808,5 +4808,47 @@
"message": "Internal server error"
}
}
},
"tests/aws/services/apigateway/test_apigateway_import.py::TestApiGatewayImportRestApi::test_import_with_cognito_auth_identity_source": {
"recorded-date": "05-11-2024, 11:37:35",
"recorded-content": {
"import-swagger": {
"apiKeySource": "HEADER",
"createdDate": "datetime",
"description": "A Pet Store API.",
"disableExecuteApiEndpoint": false,
"endpointConfiguration": {
"types": [
"EDGE"
]
},
"id": "<rest-id:1>",
"name": "Example Pet Store",
"rootResourceId": "<root-resource-id:1>",
"version": "1.0",
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 201
}
},
"get-authorizers": {
"items": [
{
"authType": "cognito_user_pools",
"id": "<authorizer-id:1>",
"identitySource": "method.request.header.TestHeaderAuth",
"name": "cognito-test-identity-source",
"providerARNs": [
"arn:<partition>:cognito-idp:<region>:111111111111:userpool/<region>_ABC123"
],
"type": "COGNITO_USER_POOLS"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"tests/aws/services/apigateway/test_apigateway_import.py::TestApiGatewayImportRestApi::test_import_with_circular_models_and_request_validation": {
"last_validated_date": "2024-04-15T21:37:44+00:00"
},
"tests/aws/services/apigateway/test_apigateway_import.py::TestApiGatewayImportRestApi::test_import_with_cognito_auth_identity_source": {
"last_validated_date": "2024-11-05T11:37:34+00:00"
},
"tests/aws/services/apigateway/test_apigateway_import.py::TestApiGatewayImportRestApi::test_import_with_global_api_key_authorizer": {
"last_validated_date": "2024-04-15T21:36:29+00:00"
},
Expand Down
Loading
0