Core: Detect unknown regions as part of the URL (#5745)

This commit is contained in:
Bert Blommers 2022-12-09 11:47:09 -01:00 committed by GitHub
parent 08ed9038e7
commit 4ac9e91dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 15 deletions

View File

@ -204,7 +204,8 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
default_region = "us-east-1"
# to extract region, use [^.]
region_regex = re.compile(r"\.(?P<region>[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<region>[^.]+)\.amazonaws\.com")
region_from_useragent_regex = re.compile(
r"region/(?P<region>[a-z]{2}-[a-z]+-\d{1})"
)

View File

@ -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