Merge pull request #2606 from levinine/api_key_fix

api gateway handle duplicate api key values
This commit is contained in:
Mike Grima 2019-12-12 18:21:51 -08:00 committed by GitHub
commit 8399bc4df5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 17 deletions

View file

@ -101,3 +101,12 @@ class ApiKeyNotFoundException(RESTError):
super(ApiKeyNotFoundException, self).__init__(
"NotFoundException", "Invalid API Key identifier specified"
)
class ApiKeyAlreadyExists(RESTError):
code = 409
def __init__(self):
super(ApiKeyAlreadyExists, self).__init__(
"ConflictException", "API Key already exists"
)

View file

@ -32,6 +32,7 @@ from .exceptions import (
RoleNotSpecified,
NoIntegrationDefined,
NoMethodDefined,
ApiKeyAlreadyExists,
)
STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}"
@ -759,6 +760,10 @@ class APIGatewayBackend(BaseBackend):
return api.delete_deployment(deployment_id)
def create_apikey(self, payload):
if payload.get("value") is not None:
for api_key in self.get_apikeys():
if api_key.get("value") == payload["value"]:
raise ApiKeyAlreadyExists()
key = ApiKey(**payload)
self.keys[key["id"]] = key
return key

View file

@ -9,6 +9,7 @@ from .exceptions import (
BadRequestException,
CrossAccountNotAllowed,
StageNotFoundException,
ApiKeyAlreadyExists,
)
@ -302,7 +303,17 @@ class APIGatewayResponse(BaseResponse):
self.setup_class(request, full_url, headers)
if self.method == "POST":
apikey_response = self.backend.create_apikey(json.loads(self.body))
try:
apikey_response = self.backend.create_apikey(json.loads(self.body))
except ApiKeyAlreadyExists as error:
return (
error.code,
self.headers,
'{{"message":"{0}","code":"{1}"}}'.format(
error.message, error.error_type
),
)
elif self.method == "GET":
apikeys_response = self.backend.get_apikeys()
return 200, {}, json.dumps({"item": apikeys_response})