CognitoIdentity:create_identity_pool: Add validation for name (#4415)

This commit is contained in:
Bert Blommers 2021-10-14 22:20:56 +00:00 committed by GitHub
parent 1f13d6c406
commit d72c6b7baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 1 deletions

View File

@ -11,3 +11,17 @@ class ResourceNotFoundError(BadRequest):
self.description = json.dumps( self.description = json.dumps(
{"message": message, "__type": "ResourceNotFoundException"} {"message": message, "__type": "ResourceNotFoundException"}
) )
class InvalidNameException(BadRequest):
message = "1 validation error detected: Value '{}' at 'identityPoolName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w\\s+=,.@-]+"
def __init__(self, name):
super(InvalidNameException, self).__init__()
self.description = json.dumps(
{
"message": InvalidNameException.message.format(name),
"__type": "ValidationException",
}
)

View File

@ -2,19 +2,24 @@ from __future__ import unicode_literals
import datetime import datetime
import json import json
import re
from boto3 import Session from boto3 import Session
from collections import OrderedDict from collections import OrderedDict
from moto.core import BaseBackend, BaseModel from moto.core import BaseBackend, BaseModel
from moto.core.utils import iso_8601_datetime_with_milliseconds from moto.core.utils import iso_8601_datetime_with_milliseconds
from .exceptions import ResourceNotFoundError from .exceptions import InvalidNameException, ResourceNotFoundError
from .utils import get_random_identity_id from .utils import get_random_identity_id
class CognitoIdentity(BaseModel): class CognitoIdentity(BaseModel):
def __init__(self, region, identity_pool_name, **kwargs): def __init__(self, region, identity_pool_name, **kwargs):
self.identity_pool_name = identity_pool_name self.identity_pool_name = identity_pool_name
if not re.fullmatch(r"[\w\s+=,.@-]+", identity_pool_name):
raise InvalidNameException(identity_pool_name)
self.allow_unauthenticated_identities = kwargs.get( self.allow_unauthenticated_identities = kwargs.get(
"allow_unauthenticated_identities", "" "allow_unauthenticated_identities", ""
) )

View File

@ -11,6 +11,32 @@ from moto.core import ACCOUNT_ID
from uuid import UUID from uuid import UUID
@mock_cognitoidentity
@pytest.mark.parametrize("name", ["pool#name", "with!excl", "with?quest"])
def test_create_identity_pool_invalid_name(name):
conn = boto3.client("cognito-identity", "us-west-2")
with pytest.raises(ClientError) as exc:
conn.create_identity_pool(
IdentityPoolName=name, AllowUnauthenticatedIdentities=False
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationException")
err["Message"].should.equal(
f"1 validation error detected: Value '{name}' at 'identityPoolName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\w\\s+=,.@-]+"
)
@mock_cognitoidentity
@pytest.mark.parametrize("name", ["x", "pool-", "pool_name", "with space"])
def test_create_identity_pool_valid_name(name):
conn = boto3.client("cognito-identity", "us-west-2")
conn.create_identity_pool(
IdentityPoolName=name, AllowUnauthenticatedIdentities=False
)
@mock_cognitoidentity @mock_cognitoidentity
def test_create_identity_pool(): def test_create_identity_pool():
conn = boto3.client("cognito-identity", "us-west-2") conn = boto3.client("cognito-identity", "us-west-2")