2014-08-27 15:17:06 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-09-27 23:04:58 +00:00
|
|
|
|
|
|
|
import argparse
|
2020-04-22 11:18:27 +00:00
|
|
|
import io
|
2015-08-02 22:31:36 +00:00
|
|
|
import json
|
2013-12-27 18:45:53 +00:00
|
|
|
import re
|
2013-03-05 13:14:43 +00:00
|
|
|
import sys
|
2013-12-27 18:45:53 +00:00
|
|
|
from threading import Lock
|
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
import six
|
2013-03-05 13:14:43 +00:00
|
|
|
from flask import Flask
|
2020-09-19 09:07:17 +00:00
|
|
|
from flask_cors import CORS
|
2015-08-02 22:31:36 +00:00
|
|
|
from flask.testing import FlaskClient
|
2017-09-27 23:04:58 +00:00
|
|
|
|
|
|
|
from six.moves.urllib.parse import urlencode
|
2013-03-05 13:14:43 +00:00
|
|
|
from werkzeug.routing import BaseConverter
|
2013-12-27 18:45:53 +00:00
|
|
|
from werkzeug.serving import run_simple
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2020-01-17 01:08:06 +00:00
|
|
|
import moto.backends as backends
|
2013-03-05 13:14:43 +00:00
|
|
|
from moto.core.utils import convert_flask_to_httpretty_response
|
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2016-08-11 06:14:13 +00:00
|
|
|
HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"]
|
2013-03-05 13:14:43 +00:00
|
|
|
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
DEFAULT_SERVICE_REGION = ("s3", "us-east-1")
|
2019-07-16 22:47:26 +00:00
|
|
|
|
|
|
|
# Map of unsigned calls to service-region as per AWS API docs
|
|
|
|
# https://docs.aws.amazon.com/cognito/latest/developerguide/resource-permissions.html#amazon-cognito-signed-versus-unsigned-apis
|
|
|
|
UNSIGNED_REQUESTS = {
|
2019-10-31 15:44:26 +00:00
|
|
|
"AWSCognitoIdentityService": ("cognito-identity", "us-east-1"),
|
|
|
|
"AWSCognitoIdentityProviderService": ("cognito-idp", "us-east-1"),
|
2019-07-16 22:47:26 +00:00
|
|
|
}
|
2020-04-22 11:18:27 +00:00
|
|
|
UNSIGNED_ACTIONS = {"AssumeRoleWithSAML": ("sts", "us-east-1")}
|
2019-07-16 22:47:26 +00:00
|
|
|
|
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
class DomainDispatcherApplication(object):
|
|
|
|
"""
|
|
|
|
Dispatch requests to different applications based on the "Host:" header
|
|
|
|
value. We'll match the host header value with the url_bases of each backend.
|
|
|
|
"""
|
|
|
|
|
2013-12-29 01:15:37 +00:00
|
|
|
def __init__(self, create_app, service=None):
|
2013-12-27 18:45:53 +00:00
|
|
|
self.create_app = create_app
|
|
|
|
self.lock = Lock()
|
|
|
|
self.app_instances = {}
|
2013-12-29 01:15:37 +00:00
|
|
|
self.service = service
|
2013-12-27 18:45:53 +00:00
|
|
|
|
|
|
|
def get_backend_for_host(self, host):
|
2019-10-31 15:44:26 +00:00
|
|
|
if host == "moto_api":
|
2018-09-03 19:34:19 +00:00
|
|
|
return host
|
|
|
|
|
2013-12-29 01:15:37 +00:00
|
|
|
if self.service:
|
2013-12-30 17:33:31 +00:00
|
|
|
return self.service
|
2013-12-29 01:15:37 +00:00
|
|
|
|
2020-01-17 01:08:06 +00:00
|
|
|
if host in backends.BACKENDS:
|
2017-02-20 23:25:10 +00:00
|
|
|
return host
|
|
|
|
|
2020-01-17 01:08:06 +00:00
|
|
|
return backends.search_backend(
|
|
|
|
lambda backend: any(
|
|
|
|
re.match(url_base, "http://%s" % host)
|
|
|
|
for url_base in list(backend.values())[0].url_bases
|
|
|
|
)
|
|
|
|
)
|
2013-12-27 18:45:53 +00:00
|
|
|
|
2019-07-22 03:30:35 +00:00
|
|
|
def infer_service_region_host(self, environ):
|
2019-10-31 15:44:26 +00:00
|
|
|
auth = environ.get("HTTP_AUTHORIZATION")
|
2019-07-16 22:47:26 +00:00
|
|
|
if auth:
|
|
|
|
# Signed request
|
|
|
|
# Parse auth header to find service assuming a SigV4 request
|
|
|
|
# https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
|
|
|
|
# ['Credential=sdffdsa', '20170220', 'us-east-1', 'sns', 'aws4_request']
|
|
|
|
try:
|
|
|
|
credential_scope = auth.split(",")[0].split()[1]
|
|
|
|
_, _, region, service, _ = credential_scope.split("/")
|
|
|
|
except ValueError:
|
|
|
|
# Signature format does not match, this is exceptional and we can't
|
|
|
|
# infer a service-region. A reduced set of services still use
|
|
|
|
# the deprecated SigV2, ergo prefer S3 as most likely default.
|
|
|
|
# https://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
|
2019-07-22 03:30:35 +00:00
|
|
|
service, region = DEFAULT_SERVICE_REGION
|
2019-07-16 22:47:26 +00:00
|
|
|
else:
|
|
|
|
# Unsigned request
|
2019-10-31 15:44:26 +00:00
|
|
|
target = environ.get("HTTP_X_AMZ_TARGET")
|
2020-04-22 11:18:27 +00:00
|
|
|
action = self.get_action_from_body(environ)
|
2019-07-16 22:47:26 +00:00
|
|
|
if target:
|
2019-10-31 15:44:26 +00:00
|
|
|
service, _ = target.split(".", 1)
|
2019-07-22 03:30:35 +00:00
|
|
|
service, region = UNSIGNED_REQUESTS.get(service, DEFAULT_SERVICE_REGION)
|
2020-04-22 11:18:27 +00:00
|
|
|
elif action and action in UNSIGNED_ACTIONS:
|
|
|
|
# See if we can match the Action to a known service
|
|
|
|
service, region = UNSIGNED_ACTIONS.get(action)
|
2019-07-22 04:03:36 +00:00
|
|
|
else:
|
|
|
|
# S3 is the last resort when the target is also unknown
|
|
|
|
service, region = DEFAULT_SERVICE_REGION
|
2019-07-22 03:30:35 +00:00
|
|
|
|
2020-11-26 08:52:58 +00:00
|
|
|
if service == "EventBridge":
|
|
|
|
# Go SDK uses 'EventBridge' in the SigV4 request instead of 'events'
|
|
|
|
# see https://github.com/spulec/moto/issues/3494
|
|
|
|
service = "events"
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
if service == "dynamodb":
|
|
|
|
if environ["HTTP_X_AMZ_TARGET"].startswith("DynamoDBStreams"):
|
|
|
|
host = "dynamodbstreams"
|
2019-07-22 03:30:35 +00:00
|
|
|
else:
|
2019-10-31 15:44:26 +00:00
|
|
|
dynamo_api_version = (
|
|
|
|
environ["HTTP_X_AMZ_TARGET"].split("_")[1].split(".")[0]
|
|
|
|
)
|
2019-07-22 03:30:35 +00:00
|
|
|
# If Newer API version, use dynamodb2
|
|
|
|
if dynamo_api_version > "20111205":
|
|
|
|
host = "dynamodb2"
|
Sagemaker models (#3105)
* First failing test, and enough framework to run it.
* Rudimentary passing test.
* Sagemaker Notebook Support, take-1: create, describe, start, stop, delete.
* Added list_tags.
* Merged in model support from https://github.com/porthunt/moto/tree/sagemaker-support.
* Re-org'd
* Fixed up describe_model exception when no matching model.
* Segregated tests by Sagemaker entity. Model arn check by regex..
* Python2 compabitility changes.
* Added sagemaker to list of known backends. Corrected urls.
* Added sagemaker special case to moto.server.infer_service_region_host due to irregular url format (use of 'api' subdomain) to support server mode.
* Changes for PR 3105 comments of July 10, 2020
* PR3105 July 10, 2020, 8:55 AM EDT comment: dropped unnecessary re-addition of arn when formulating model list response.
* PR 3105 July 15, 2020 9:10 AM EDT Comment: clean-up SageMakerModelBackend.describe_models logic for finding the model in the dict.
* Optimized imports
Co-authored-by: Joseph Weitekamp <jweite@amazon.com>
2020-07-16 12:12:25 +00:00
|
|
|
elif service == "sagemaker":
|
|
|
|
host = "api.sagemaker.{region}.amazonaws.com".format(
|
|
|
|
service=service, region=region
|
|
|
|
)
|
2019-07-22 03:30:35 +00:00
|
|
|
else:
|
|
|
|
host = "{service}.{region}.amazonaws.com".format(
|
2019-10-31 15:44:26 +00:00
|
|
|
service=service, region=region
|
|
|
|
)
|
2019-07-22 03:30:35 +00:00
|
|
|
|
|
|
|
return host
|
2019-07-16 22:47:26 +00:00
|
|
|
|
2017-02-20 19:31:19 +00:00
|
|
|
def get_application(self, environ):
|
2019-10-31 15:44:26 +00:00
|
|
|
path_info = environ.get("PATH_INFO", "")
|
2017-08-05 10:29:40 +00:00
|
|
|
|
|
|
|
# The URL path might contain non-ASCII text, for instance unicode S3 bucket names
|
|
|
|
if six.PY2 and isinstance(path_info, str):
|
|
|
|
path_info = six.u(path_info)
|
|
|
|
if six.PY3 and isinstance(path_info, six.binary_type):
|
2019-10-31 15:44:26 +00:00
|
|
|
path_info = path_info.decode("utf-8")
|
2017-08-05 10:29:40 +00:00
|
|
|
|
2017-03-12 03:45:42 +00:00
|
|
|
if path_info.startswith("/moto-api") or path_info == "/favicon.ico":
|
2017-02-20 23:25:10 +00:00
|
|
|
host = "moto_api"
|
2017-02-24 00:43:48 +00:00
|
|
|
elif path_info.startswith("/latest/meta-data/"):
|
|
|
|
host = "instance_metadata"
|
2017-02-20 23:25:10 +00:00
|
|
|
else:
|
2019-10-31 15:44:26 +00:00
|
|
|
host = environ["HTTP_HOST"].split(":")[0]
|
2017-02-20 19:31:19 +00:00
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
with self.lock:
|
|
|
|
backend = self.get_backend_for_host(host)
|
2019-07-22 03:07:58 +00:00
|
|
|
if not backend:
|
2019-07-22 03:30:35 +00:00
|
|
|
# No regular backend found; try parsing other headers
|
|
|
|
host = self.infer_service_region_host(environ)
|
2019-07-22 03:07:58 +00:00
|
|
|
backend = self.get_backend_for_host(host)
|
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
app = self.app_instances.get(backend, None)
|
|
|
|
if app is None:
|
|
|
|
app = self.create_app(backend)
|
|
|
|
self.app_instances[backend] = app
|
|
|
|
return app
|
|
|
|
|
2020-04-22 11:18:27 +00:00
|
|
|
def get_action_from_body(self, environ):
|
|
|
|
body = None
|
|
|
|
try:
|
2020-04-22 13:07:19 +00:00
|
|
|
# AWS requests use querystrings as the body (Action=x&Data=y&...)
|
|
|
|
simple_form = environ["CONTENT_TYPE"].startswith(
|
|
|
|
"application/x-www-form-urlencoded"
|
|
|
|
)
|
|
|
|
request_body_size = int(environ["CONTENT_LENGTH"])
|
|
|
|
if simple_form and request_body_size:
|
2020-04-22 11:18:27 +00:00
|
|
|
body = environ["wsgi.input"].read(request_body_size).decode("utf-8")
|
2020-04-22 13:07:19 +00:00
|
|
|
body_dict = dict(x.split("=") for x in body.split("&"))
|
2020-04-22 11:18:27 +00:00
|
|
|
return body_dict["Action"]
|
2020-04-22 13:07:19 +00:00
|
|
|
except (KeyError, ValueError):
|
2020-04-22 11:18:27 +00:00
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
if body:
|
|
|
|
# We've consumed the body = need to reset it
|
|
|
|
environ["wsgi.input"] = io.StringIO(body)
|
|
|
|
return None
|
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
def __call__(self, environ, start_response):
|
2017-02-20 19:31:19 +00:00
|
|
|
backend_app = self.get_application(environ)
|
2013-12-27 18:45:53 +00:00
|
|
|
return backend_app(environ, start_response)
|
|
|
|
|
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
class RegexConverter(BaseConverter):
|
|
|
|
# http://werkzeug.pocoo.org/docs/routing/#custom-converters
|
2017-02-24 02:37:43 +00:00
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
def __init__(self, url_map, *items):
|
|
|
|
super(RegexConverter, self).__init__(url_map)
|
|
|
|
self.regex = items[0]
|
|
|
|
|
|
|
|
|
2015-08-02 22:31:36 +00:00
|
|
|
class AWSTestHelper(FlaskClient):
|
|
|
|
def action_data(self, action_name, **kwargs):
|
|
|
|
"""
|
|
|
|
Method calls resource with action_name and returns data of response.
|
|
|
|
"""
|
|
|
|
opts = {"Action": action_name}
|
|
|
|
opts.update(kwargs)
|
2019-10-31 15:44:26 +00:00
|
|
|
res = self.get(
|
|
|
|
"/?{0}".format(urlencode(opts)),
|
|
|
|
headers={
|
|
|
|
"Host": "{0}.us-east-1.amazonaws.com".format(self.application.service)
|
|
|
|
},
|
|
|
|
)
|
2015-08-02 22:31:36 +00:00
|
|
|
return res.data.decode("utf-8")
|
|
|
|
|
|
|
|
def action_json(self, action_name, **kwargs):
|
|
|
|
"""
|
|
|
|
Method calls resource with action_name and returns object obtained via
|
|
|
|
deserialization of output.
|
|
|
|
"""
|
|
|
|
return json.loads(self.action_data(action_name, **kwargs))
|
|
|
|
|
|
|
|
|
2013-12-29 01:15:37 +00:00
|
|
|
def create_backend_app(service):
|
2013-03-05 13:14:43 +00:00
|
|
|
from werkzeug.routing import Map
|
2013-12-27 18:45:53 +00:00
|
|
|
|
|
|
|
# Create the backend_app
|
|
|
|
backend_app = Flask(__name__)
|
|
|
|
backend_app.debug = True
|
2015-08-02 22:31:36 +00:00
|
|
|
backend_app.service = service
|
2020-09-19 09:07:17 +00:00
|
|
|
CORS(backend_app)
|
2013-12-27 18:45:53 +00:00
|
|
|
|
2013-06-25 16:36:21 +00:00
|
|
|
# Reset view functions to reset the app
|
2013-12-27 18:45:53 +00:00
|
|
|
backend_app.view_functions = {}
|
|
|
|
backend_app.url_map = Map()
|
2019-10-31 15:44:26 +00:00
|
|
|
backend_app.url_map.converters["regex"] = RegexConverter
|
2020-01-17 01:08:06 +00:00
|
|
|
backend = list(backends.get_backend(service).values())[0]
|
2014-08-26 17:25:50 +00:00
|
|
|
for url_path, handler in backend.flask_paths.items():
|
2019-08-28 14:17:45 +00:00
|
|
|
view_func = convert_flask_to_httpretty_response(handler)
|
2019-10-31 15:44:26 +00:00
|
|
|
if handler.__name__ == "dispatch":
|
|
|
|
endpoint = "{0}.dispatch".format(handler.__self__.__name__)
|
2015-03-16 12:13:40 +00:00
|
|
|
else:
|
2019-08-28 14:17:45 +00:00
|
|
|
endpoint = view_func.__name__
|
2015-03-16 12:13:40 +00:00
|
|
|
|
2017-09-23 10:02:25 +00:00
|
|
|
original_endpoint = endpoint
|
|
|
|
index = 2
|
|
|
|
while endpoint in backend_app.view_functions:
|
2015-08-02 13:45:40 +00:00
|
|
|
# HACK: Sometimes we map the same view to multiple url_paths. Flask
|
2019-11-16 20:31:45 +00:00
|
|
|
# requires us to have different names.
|
2017-09-23 10:02:25 +00:00
|
|
|
endpoint = original_endpoint + str(index)
|
|
|
|
index += 1
|
2015-08-02 13:45:40 +00:00
|
|
|
|
|
|
|
backend_app.add_url_rule(
|
2015-03-16 12:13:40 +00:00
|
|
|
url_path,
|
|
|
|
endpoint=endpoint,
|
2015-08-02 13:45:40 +00:00
|
|
|
methods=HTTP_METHODS,
|
2019-08-28 14:17:45 +00:00
|
|
|
view_func=view_func,
|
2016-12-03 23:59:28 +00:00
|
|
|
strict_slashes=False,
|
2015-08-02 13:45:40 +00:00
|
|
|
)
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2015-08-02 22:31:36 +00:00
|
|
|
backend_app.test_client_class = AWSTestHelper
|
2013-12-27 18:45:53 +00:00
|
|
|
return backend_app
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2013-07-26 18:42:32 +00:00
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
def main(argv=sys.argv[1:]):
|
2013-07-26 18:42:32 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2013-12-29 01:15:37 +00:00
|
|
|
|
|
|
|
# Keep this for backwards compat
|
2013-12-30 18:29:57 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"service",
|
|
|
|
type=str,
|
2019-10-31 15:44:26 +00:00
|
|
|
nargs="?", # http://stackoverflow.com/a/4480202/731592
|
|
|
|
default=None,
|
|
|
|
)
|
2013-07-26 18:42:32 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-H", "--host", type=str, help="Which host to bind", default="127.0.0.1"
|
|
|
|
)
|
2013-07-26 18:42:32 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-p", "--port", type=int, help="Port number to use for connection", default=5000
|
|
|
|
)
|
2017-02-09 02:22:14 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-r",
|
|
|
|
"--reload",
|
|
|
|
action="store_true",
|
|
|
|
help="Reload server on a file change",
|
|
|
|
default=False,
|
2017-02-09 02:22:14 +00:00
|
|
|
)
|
2017-06-27 07:18:21 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-s",
|
|
|
|
"--ssl",
|
|
|
|
action="store_true",
|
|
|
|
help="Enable SSL encrypted connection with auto-generated certificate (use https://... URL)",
|
|
|
|
default=False,
|
2017-06-27 07:18:21 +00:00
|
|
|
)
|
2018-05-03 19:09:56 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-c", "--ssl-cert", type=str, help="Path to SSL certificate", default=None
|
|
|
|
)
|
2018-05-03 19:09:56 +00:00
|
|
|
parser.add_argument(
|
2019-10-31 15:44:26 +00:00
|
|
|
"-k", "--ssl-key", type=str, help="Path to SSL private key", default=None
|
|
|
|
)
|
2018-05-03 19:09:56 +00:00
|
|
|
|
2013-07-26 18:42:32 +00:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
2013-12-27 18:45:53 +00:00
|
|
|
# Wrap the main application
|
2019-10-31 15:44:26 +00:00
|
|
|
main_app = DomainDispatcherApplication(create_backend_app, service=args.service)
|
2013-12-27 18:45:53 +00:00
|
|
|
main_app.debug = True
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2018-05-03 19:09:56 +00:00
|
|
|
ssl_context = None
|
|
|
|
if args.ssl_key and args.ssl_cert:
|
|
|
|
ssl_context = (args.ssl_cert, args.ssl_key)
|
|
|
|
elif args.ssl:
|
2019-10-31 15:44:26 +00:00
|
|
|
ssl_context = "adhoc"
|
|
|
|
|
|
|
|
run_simple(
|
|
|
|
args.host,
|
|
|
|
args.port,
|
|
|
|
main_app,
|
|
|
|
threaded=True,
|
|
|
|
use_reloader=args.reload,
|
|
|
|
ssl_context=ssl_context,
|
|
|
|
)
|
2017-02-09 02:22:14 +00:00
|
|
|
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
if __name__ == "__main__":
|
2013-03-05 13:14:43 +00:00
|
|
|
main()
|