def assert_equal(first, second, prefix):
    # A better assert equals.  It allows you to just provide
    # prefix instead of the entire message.
    try:
        _assert_equal(first, second)
    except Exception:
        try:
            better = "%s (actual != expected)\n%s !=\n%s" % (
                prefix, json.dumps(first,
                                   indent=2), json.dumps(second, indent=2))
        except (ValueError, TypeError):
            better = "%s (actual != expected)\n%s !=\n%s" % (prefix, first,
                                                             second)
        raise AssertionError(better)
示例#2
0
    def add_auth(self, request):
        fields = {}
        if request.context.get('s3-presign-post-fields', None) is not None:
            fields = request.context['s3-presign-post-fields']

        policy = {}
        conditions = []
        if request.context.get('s3-presign-post-policy', None) is not None:
            policy = request.context['s3-presign-post-policy']
            if policy.get('conditions', None) is not None:
                conditions = policy['conditions']

        policy['conditions'] = conditions

        fields['AWSAccessKeyId'] = self.credentials.access_key

        if self.credentials.token is not None:
            fields['x-amz-security-token'] = self.credentials.token
            conditions.append({'x-amz-security-token': self.credentials.token})

        # Dump the base64 encoded policy into the fields dictionary.
        fields['policy'] = base64.b64encode(
            json.dumps(policy).encode('utf-8')).decode('utf-8')

        fields['signature'] = self.sign_string(fields['policy'])

        request.context['s3-presign-post-fields'] = fields
        request.context['s3-presign-post-policy'] = policy
示例#3
0
 def _convert_header_value(self, shape, value):
     if shape.type_name == 'timestamp':
         datetime_obj = parse_to_aware_datetime(value)
         timestamp = calendar.timegm(datetime_obj.utctimetuple())
         timestamp_format = shape.serialization.get(
             'timestampFormat', self.HEADER_TIMESTAMP_FORMAT)
         return self._convert_timestamp_to_str(timestamp, timestamp_format)
     elif is_json_value_header(shape):
         # Serialize with no spaces after separators to save space in
         # the header.
         return self._get_base64(json.dumps(value, separators=(',', ':')))
     else:
         return value
示例#4
0
 def test_can_always_json_serialize_headers(self):
     parser = self.create_parser()
     original_headers = {
         'x-amzn-requestid': 'request-id',
         'Header1': 'foo',
     }
     headers = CustomHeaderDict(original_headers)
     output_shape = self.create_arbitary_output_shape()
     parsed = parser.parse(
         {'body': b'{}', 'headers': headers,
          'status_code': 200}, output_shape)
     metadata = parsed['ResponseMetadata']
     # We've had the contract that you can json serialize a
     # response.  So we want to ensure that despite using a CustomHeaderDict
     # we can always JSON dumps the response metadata.
     self.assertEqual(
         json.loads(json.dumps(metadata))['HTTPHeaders']['header1'], 'foo')
 def serialize_to_request(self, parameters, operation_model):
     target = '%s.%s' % (operation_model.metadata['targetPrefix'],
                         operation_model.name)
     json_version = operation_model.metadata['jsonVersion']
     serialized = self._create_default_request()
     serialized['method'] = operation_model.http.get(
         'method', self.DEFAULT_METHOD)
     serialized['headers'] = {
         'X-Amz-Target': target,
         'Content-Type': 'application/x-amz-json-%s' % json_version,
     }
     body = {}
     input_shape = operation_model.input_shape
     if input_shape is not None:
         self._serialize(body, parameters, input_shape)
     serialized['body'] = json.dumps(body).encode(self.DEFAULT_ENCODING)
     return serialized
示例#6
0
    def add_auth(self, request):
        datetime_now = datetime.datetime.utcnow()
        request.context['timestamp'] = datetime_now.strftime(SIGV4_TIMESTAMP)

        fields = {}
        if request.context.get('s3-presign-post-fields', None) is not None:
            fields = request.context['s3-presign-post-fields']

        policy = {}
        conditions = []
        if request.context.get('s3-presign-post-policy', None) is not None:
            policy = request.context['s3-presign-post-policy']
            if policy.get('conditions', None) is not None:
                conditions = policy['conditions']

        policy['conditions'] = conditions

        fields['x-amz-algorithm'] = 'AWS4-HMAC-SHA256'
        fields['x-amz-credential'] = self.scope(request)
        fields['x-amz-date'] = request.context['timestamp']

        conditions.append({'x-amz-algorithm': 'AWS4-HMAC-SHA256'})
        conditions.append({'x-amz-credential': self.scope(request)})
        conditions.append({'x-amz-date': request.context['timestamp']})

        if self.credentials.token is not None:
            fields['x-amz-security-token'] = self.credentials.token
            conditions.append({'x-amz-security-token': self.credentials.token})

        # Dump the base64 encoded policy into the fields dictionary.
        fields['policy'] = base64.b64encode(
            json.dumps(policy).encode('utf-8')).decode('utf-8')

        fields['x-amz-signature'] = self.signature(fields['policy'], request)

        request.context['s3-presign-post-fields'] = fields
        request.context['s3-presign-post-policy'] = policy
示例#7
0
 def _serialize_body_params(self, params, shape):
     serialized_body = self.MAP_TYPE()
     self._serialize(serialized_body, params, shape)
     return json.dumps(serialized_body).encode(self.DEFAULT_ENCODING)
def _try_json_dump(obj):
    try:
        return json.dumps(obj)
    except (ValueError, TypeError):
        return str(obj)