From 4ac9e91dfdada6dae5ba873cf011612856bdb228 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Fri, 9 Dec 2022 11:47:09 -0100 Subject: [PATCH] Core: Detect unknown regions as part of the URL (#5745) --- moto/core/responses.py | 3 +- .../test_cognitoidentity.py | 39 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index 114cae96e..bb43a83ab 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -204,7 +204,8 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): default_region = "us-east-1" # to extract region, use [^.] - region_regex = re.compile(r"\.(?P[a-z]{2}-[a-z]+-\d{1})\.amazonaws\.com") + # Note that the URL region can be anything, thanks to our MOTO_ALLOW_NONEXISTENT_REGION-config - so we can't have a very specific regex + region_regex = re.compile(r"\.(?P[^.]+)\.amazonaws\.com") region_from_useragent_regex = re.compile( r"region/(?P[a-z]{2}-[a-z]+-\d{1})" ) diff --git a/tests/test_cognitoidentity/test_cognitoidentity.py b/tests/test_cognitoidentity/test_cognitoidentity.py index 5e9d1e424..2b81e553e 100644 --- a/tests/test_cognitoidentity/test_cognitoidentity.py +++ b/tests/test_cognitoidentity/test_cognitoidentity.py @@ -1,11 +1,15 @@ import boto3 +import mock import sure # noqa # pylint: disable=unused-import from botocore.exceptions import ClientError +from datetime import datetime import pytest +import os -from moto import mock_cognitoidentity +from moto import mock_cognitoidentity, settings from moto.cognitoidentity.utils import get_random_identity_id from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID +from unittest import SkipTest from uuid import UUID @@ -150,7 +154,6 @@ def test_get_random_identity_id(): @mock_cognitoidentity def test_get_id(): - # These two do NOT work in server mode. They just don't return the data from the model. conn = boto3.client("cognito-identity", "us-west-2") identity_pool_data = conn.create_identity_pool( IdentityPoolName="test_identity_pool", AllowUnauthenticatedIdentities=True @@ -160,26 +163,34 @@ def test_get_id(): IdentityPoolId=identity_pool_data["IdentityPoolId"], Logins={"someurl": "12345"}, ) - assert ( - result.get("IdentityId", "").startswith("us-west-2") - or result.get("ResponseMetadata").get("HTTPStatusCode") == 200 + assert result.get("IdentityId").startswith("us-west-2") + + +@mock_cognitoidentity +@mock.patch.dict(os.environ, {"AWS_DEFAULT_REGION": "any-region"}) +@mock.patch.dict(os.environ, {"MOTO_ALLOW_NONEXISTENT_REGION": "trUe"}) +def test_get_id__unknown_region(): + if settings.TEST_SERVER_MODE: + raise SkipTest("Cannot set environemnt variables in ServerMode") + conn = boto3.client("cognito-identity") + identity_pool_data = conn.create_identity_pool( + IdentityPoolName="test_identity_pool", AllowUnauthenticatedIdentities=True ) + result = conn.get_id( + AccountId="someaccount", + IdentityPoolId=identity_pool_data["IdentityPoolId"], + Logins={"someurl": "12345"}, + ) + assert result.get("IdentityId").startswith("any-region") @mock_cognitoidentity def test_get_credentials_for_identity(): - # These two do NOT work in server mode. They just don't return the data from the model. conn = boto3.client("cognito-identity", "us-west-2") result = conn.get_credentials_for_identity(IdentityId="12345") - assert ( - result.get("Expiration", 0) > 0 - or result.get("ResponseMetadata").get("HTTPStatusCode") == 200 - ) - assert ( - result.get("IdentityId") == "12345" - or result.get("ResponseMetadata").get("HTTPStatusCode") == 200 - ) + result["Credentials"].get("Expiration").should.be.a(datetime) + result.get("IdentityId").should.equal("12345") @mock_cognitoidentity