Ejemplo n.º 1
0
 def _is_invalid_json(self, response):
     try:
         json.loads(response.text)
         return False
     except ValueError:
         self._log_imds_response(response, 'invalid json')
         return True
Ejemplo n.º 2
0
def _get_body_as_dict(request):
    # For query services, request.data is form-encoded and is already a
    # dict, but for other services such as rest-json it could be a json
    # string or bytes. In those cases we attempt to load the data as a
    # dict.
    data = request.data
    if isinstance(data, six.binary_type):
        data = json.loads(data.decode('utf-8'))
    elif isinstance(data, six.string_types):
        data = json.loads(data)
    return data
Ejemplo n.º 3
0
def json_decode_template_body(parsed, **kwargs):
    if 'TemplateBody' in parsed:
        try:
            value = json.loads(parsed['TemplateBody'])
            parsed['TemplateBody'] = value
        except (ValueError, TypeError):
            logger.debug('error loading JSON', exc_info=True)
Ejemplo n.º 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')
Ejemplo n.º 5
0
 def retrieve_iam_role_credentials(self):
     data = {}
     url = self._url
     timeout = self._timeout
     num_attempts = self._num_attempts
     try:
         r = self._get_request(url, timeout, num_attempts)
         if r.content:
             fields = r.content.decode('utf-8').split('\n')
             for field in fields:
                 if field.endswith('/'):
                     data[field[0:-1]] = self.retrieve_iam_role_credentials(
                         url + field, timeout, num_attempts)
                 else:
                     val = self._get_request(
                         url + field,
                         timeout=timeout,
                         num_attempts=num_attempts).content.decode('utf-8')
                     if val[0] == '{':
                         val = json.loads(val)
                     data[field] = val
         else:
             logger.debug(
                 "Metadata service returned non 200 status code "
                 "of %s for url: %s, content body: %s", r.status_code, url,
                 r.content)
     except _RetriesExceededError:
         logger.debug(
             "Max number of attempts exceeded (%s) when "
             "attempting to retrieve data from metadata service.",
             num_attempts)
     # We sort for stable ordering. In practice, this should only consist
     # of one role, but may need revisiting if this expands in the future.
     final_data = {}
     for role_name in sorted(data):
         final_data = {
             'role_name': role_name,
             'access_key': data[role_name]['AccessKeyId'],
             'secret_key': data[role_name]['SecretAccessKey'],
             'token': data[role_name]['Token'],
             'expiry_time': data[role_name]['Expiration'],
         }
     return final_data
Ejemplo n.º 6
0
    def load_file(self, file_path):
        """Attempt to load the file path.

        :type file_path: str
        :param file_path: The full path to the file to load without
            the '.json' extension.

        :return: The loaded data if it exists, otherwise None.

        """
        full_path = file_path + '.json'
        if not os.path.isfile(full_path):
            return

        # By default the file will be opened with locale encoding on Python 3.
        # We specify "utf8" here to ensure the correct behavior.
        with open(full_path, 'rb') as fp:
            payload = fp.read().decode('utf-8')
            logger.debug("Loading JSON file: %s", full_path)
            return json.loads(payload, object_pairs_hook=OrderedDict)
Ejemplo n.º 7
0
 def _get_response(self, full_url, headers, timeout):
     try:
         response = self._session.get(full_url,
                                      headers=headers,
                                      timeout=timeout)
         if response.status_code != 200:
             raise MetadataRetrievalError(
                 error_msg=
                 "Received non 200 response (%s) from ECS metadata: %s" %
                 (response.status_code, response.text))
         try:
             return json.loads(response.text)
         except ValueError:
             raise MetadataRetrievalError(
                 error_msg=("Unable to parse JSON returned from "
                            "ECS metadata: %s" % response.text))
     except RETRYABLE_HTTP_ERRORS as e:
         error_msg = ("Received error when attempting to retrieve "
                      "ECS metadata: %s" % e)
         raise MetadataRetrievalError(error_msg=error_msg)
Ejemplo n.º 8
0
 def _get_response(self, full_url, headers, timeout):
     try:
         AWSRequest = ibm_botocore.awsrequest.AWSRequest
         request = AWSRequest(method='GET', url=full_url, headers=headers)
         response = self._session.send(request.prepare())
         response_text = response.content.decode('utf-8')
         if response.status_code != 200:
             raise MetadataRetrievalError(
                 error_msg=(
                     "Received non 200 response (%s) from ECS metadata: %s"
                 ) % (response.status_code, response_text))
         try:
             return json.loads(response_text)
         except ValueError:
             raise MetadataRetrievalError(
                 error_msg=("Unable to parse JSON returned from "
                            "ECS metadata: %s" % response_text))
     except RETRYABLE_HTTP_ERRORS as e:
         error_msg = ("Received error when attempting to retrieve "
                      "ECS metadata: %s" % e)
         raise MetadataRetrievalError(error_msg=error_msg)
def decode_quoted_jsondoc(value):
    try:
        value = json.loads(unquote(value))
    except (ValueError, TypeError):
        logger.debug('Error loading quoted JSON', exc_info=True)
    return value
Ejemplo n.º 10
0
def switch_host_with_param(request, param_name):
    """Switches the host using a parameter value from a JSON request body"""
    request_json = json.loads(request.data.decode('utf-8'))
    if request_json.get(param_name):
        new_endpoint = request_json[param_name]
        _switch_hosts(request, new_endpoint)
Ejemplo n.º 11
0
 def _get_credentials(self, role_name):
     r = self._get_request(
         url_path=self._URL_PATH + role_name,
         retry_func=self._needs_retry_for_credentials
     )
     return json.loads(r.text)