Fixed error in python 2 and did some refactoring.
This commit is contained in:
parent
140f4110ac
commit
f3f47d44ac
@ -127,7 +127,7 @@ class AssumedRoleAccessKey(object):
|
|||||||
class CreateAccessKeyFailure(Exception):
|
class CreateAccessKeyFailure(Exception):
|
||||||
|
|
||||||
def __init__(self, reason, *args):
|
def __init__(self, reason, *args):
|
||||||
super().__init__(*args)
|
super(CreateAccessKeyFailure, self).__init__(*args)
|
||||||
self.reason = reason
|
self.reason = reason
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@ import io
|
|||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from moto.core.authentication import IAMRequest, S3IAMRequest
|
from moto.core.access_control import IAMRequest, S3IAMRequest
|
||||||
from moto.core.exceptions import DryRunClientError
|
from moto.core.exceptions import DryRunClientError
|
||||||
|
|
||||||
from jinja2 import Environment, DictLoader, TemplateNotFound
|
from jinja2 import Environment, DictLoader, TemplateNotFound
|
||||||
@ -110,7 +110,7 @@ class ActionAuthenticatorMixin(object):
|
|||||||
|
|
||||||
request_count = 0
|
request_count = 0
|
||||||
|
|
||||||
def _authenticate_action(self, iam_request_cls):
|
def _authenticate_and_authorize_action(self, iam_request_cls):
|
||||||
if ActionAuthenticatorMixin.request_count >= settings.INITIAL_NO_AUTH_ACTION_COUNT:
|
if ActionAuthenticatorMixin.request_count >= settings.INITIAL_NO_AUTH_ACTION_COUNT:
|
||||||
iam_request = iam_request_cls(method=self.method, path=self.path, data=self.data, headers=self.headers)
|
iam_request = iam_request_cls(method=self.method, path=self.path, data=self.data, headers=self.headers)
|
||||||
iam_request.check_signature()
|
iam_request.check_signature()
|
||||||
@ -118,11 +118,11 @@ class ActionAuthenticatorMixin(object):
|
|||||||
else:
|
else:
|
||||||
ActionAuthenticatorMixin.request_count += 1
|
ActionAuthenticatorMixin.request_count += 1
|
||||||
|
|
||||||
def _authenticate_normal_action(self):
|
def _authenticate_and_authorize_normal_action(self):
|
||||||
self._authenticate_action(IAMRequest)
|
self._authenticate_and_authorize_action(IAMRequest)
|
||||||
|
|
||||||
def _authenticate_s3_action(self):
|
def _authenticate_and_authorize_s3_action(self):
|
||||||
self._authenticate_action(S3IAMRequest)
|
self._authenticate_and_authorize_action(S3IAMRequest)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_initial_no_auth_action_count(initial_no_auth_action_count):
|
def set_initial_no_auth_action_count(initial_no_auth_action_count):
|
||||||
@ -319,7 +319,7 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
headers = self.response_headers
|
headers = self.response_headers
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._authenticate_normal_action()
|
self._authenticate_and_authorize_normal_action()
|
||||||
except HTTPException as http_error:
|
except HTTPException as http_error:
|
||||||
response = http_error.description, dict(status=http_error.code)
|
response = http_error.description, dict(status=http_error.code)
|
||||||
return self._send_response(headers, response)
|
return self._send_response(headers, response)
|
||||||
|
@ -120,7 +120,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def all_buckets(self):
|
def all_buckets(self):
|
||||||
self.data["Action"] = "ListAllMyBuckets"
|
self.data["Action"] = "ListAllMyBuckets"
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
# No bucket specified. Listing all buckets
|
# No bucket specified. Listing all buckets
|
||||||
all_buckets = self.backend.get_all_buckets()
|
all_buckets = self.backend.get_all_buckets()
|
||||||
@ -266,7 +266,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _bucket_response_get(self, bucket_name, querystring):
|
def _bucket_response_get(self, bucket_name, querystring):
|
||||||
self._set_action("BUCKET", "GET", querystring)
|
self._set_action("BUCKET", "GET", querystring)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
if 'uploads' in querystring:
|
if 'uploads' in querystring:
|
||||||
for unsup in ('delimiter', 'max-uploads'):
|
for unsup in ('delimiter', 'max-uploads'):
|
||||||
@ -500,7 +500,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
return 411, {}, "Content-Length required"
|
return 411, {}, "Content-Length required"
|
||||||
|
|
||||||
self._set_action("BUCKET", "PUT", querystring)
|
self._set_action("BUCKET", "PUT", querystring)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
if 'versioning' in querystring:
|
if 'versioning' in querystring:
|
||||||
ver = re.search('<Status>([A-Za-z]+)</Status>', body.decode())
|
ver = re.search('<Status>([A-Za-z]+)</Status>', body.decode())
|
||||||
@ -602,7 +602,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _bucket_response_delete(self, body, bucket_name, querystring):
|
def _bucket_response_delete(self, body, bucket_name, querystring):
|
||||||
self._set_action("BUCKET", "DELETE", querystring)
|
self._set_action("BUCKET", "DELETE", querystring)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
if 'policy' in querystring:
|
if 'policy' in querystring:
|
||||||
self.backend.delete_bucket_policy(bucket_name, body)
|
self.backend.delete_bucket_policy(bucket_name, body)
|
||||||
@ -638,12 +638,12 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
if self.is_delete_keys(request, path, bucket_name):
|
if self.is_delete_keys(request, path, bucket_name):
|
||||||
self.data["Action"] = "DeleteObject"
|
self.data["Action"] = "DeleteObject"
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
return self._bucket_response_delete_keys(request, body, bucket_name)
|
return self._bucket_response_delete_keys(request, body, bucket_name)
|
||||||
|
|
||||||
self.data["Action"] = "PutObject"
|
self.data["Action"] = "PutObject"
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
# POST to bucket-url should create file from form
|
# POST to bucket-url should create file from form
|
||||||
if hasattr(request, 'form'):
|
if hasattr(request, 'form'):
|
||||||
@ -797,7 +797,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _key_response_get(self, bucket_name, query, key_name, headers):
|
def _key_response_get(self, bucket_name, query, key_name, headers):
|
||||||
self._set_action("KEY", "GET", query)
|
self._set_action("KEY", "GET", query)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
response_headers = {}
|
response_headers = {}
|
||||||
if query.get('uploadId'):
|
if query.get('uploadId'):
|
||||||
@ -834,7 +834,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _key_response_put(self, request, body, bucket_name, query, key_name, headers):
|
def _key_response_put(self, request, body, bucket_name, query, key_name, headers):
|
||||||
self._set_action("KEY", "PUT", query)
|
self._set_action("KEY", "PUT", query)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
response_headers = {}
|
response_headers = {}
|
||||||
if query.get('uploadId') and query.get('partNumber'):
|
if query.get('uploadId') and query.get('partNumber'):
|
||||||
@ -1204,7 +1204,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _key_response_delete(self, bucket_name, query, key_name):
|
def _key_response_delete(self, bucket_name, query, key_name):
|
||||||
self._set_action("KEY", "DELETE", query)
|
self._set_action("KEY", "DELETE", query)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
if query.get('uploadId'):
|
if query.get('uploadId'):
|
||||||
upload_id = query['uploadId'][0]
|
upload_id = query['uploadId'][0]
|
||||||
@ -1227,7 +1227,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
|
|||||||
|
|
||||||
def _key_response_post(self, request, body, bucket_name, query, key_name):
|
def _key_response_post(self, request, body, bucket_name, query, key_name):
|
||||||
self._set_action("KEY", "POST", query)
|
self._set_action("KEY", "POST", query)
|
||||||
self._authenticate_s3_action()
|
self._authenticate_and_authorize_s3_action()
|
||||||
|
|
||||||
if body == b'' and 'uploads' in query:
|
if body == b'' and 'uploads' in query:
|
||||||
metadata = metadata_from_headers(request.headers)
|
metadata = metadata_from_headers(request.headers)
|
||||||
|
Loading…
Reference in New Issue
Block a user