def _unpack_complex_cli_arg(argument_model, value, cli_name): type_name = argument_model.type_name if type_name == OBJECT_TYPE or type_name == MAP_TYPE: if value.lstrip()[0] == '{': try: return json.loads(value, object_pairs_hook=OrderedDict) except ValueError as e: raise ParamError( cli_name, "Invalid JSON: %s\nJSON received: %s" % (e, value)) raise ParamError(cli_name, "Invalid JSON:\n%s" % value) elif type_name == LIST_TYPE: if isinstance(value, six.string_types): if value.lstrip()[0] == '[': return json.loads(value, object_pairs_hook=OrderedDict) elif isinstance(value, list) and len(value) == 1: single_value = value[0].strip() if single_value and single_value[0] == '[': return json.loads(value[0], object_pairs_hook=OrderedDict) try: # There's a couple of cases remaining here. # 1. It's possible that this is just a list of strings, i.e # --security-group-ids sg-1 sg-2 sg-3 => ['sg-1', 'sg-2', 'sg-3'] # 2. It's possible this is a list of json objects: # --filters '{"Name": ..}' '{"Name": ...}' member_shape_model = argument_model.member return [ _unpack_cli_arg(member_shape_model, v, cli_name) for v in value ] except (ValueError, TypeError): # The list params don't have a name/cli_name attached to them # so they will have bad error messages. We're going to # attach the parent parameter to this error message to provide # a more helpful error message. raise ParamError(cli_name, value[0])
def load(self): """ load the credential from the json configuration file. """ if self._conf is None: return None if not os.path.isfile(self._conf): LOG.debug("Conf file at %s does not exist!" % self._conf) raise NoCredentialsError() try: conf = json.loads(open(self._conf).read()) except Exception: LOG.debug("Could not read conf: %s", exc_info=True) return None if ACCESS_KEY_ID not in conf: LOG.debug('Auth config file is missing required key %s', ACCESS_KEY_ID) raise MalformedCredentialsError(provider=self.METHOD, cred_var=ACCESS_KEY_ID) LOG.debug('Found credentials for key: %s in configuration file.', conf[ACCESS_KEY_ID]) access_key_id, private_key = self._extract_creds_from_mapping( conf, ACCESS_KEY_ID, PRIVATE_KEY) return Credentials(access_key_id, private_key, self.METHOD)
def _create_retry_handler(self): original_config = open(os.path.join(CLIENT_DIR, '_retry.json')).read() original_config = json.loads(original_config, object_pairs_hook=OrderedDict) config = build_retry_config(original_config['retry'], original_config.get('definitions', {})) return create_retry_handler(config)
def load_file(self, file_path): if not os.path.isfile(file_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(file_path, 'rb') as fp: payload = fp.read().decode('utf-8') return json.loads(payload, object_pairs_hook=OrderedDict)
def test_auth_header_string(self): http_headers = HTTPHeaders.from_dict({}) split = urlsplit('/foo/bar') sig = self.ed25519v1._get_signature('PUT', split, http_headers) self.assertEqual(EXPECTED_ED25519_SIG, sig) auth_header_string = self.ed25519v1._get_signature_header(sig) expected_metadata = 'eyJhY2Nlc3Nfa2V5X2lkIjogIkFCQ0QtRUZHSC1JSktMLU1OT1' \ 'AtUVJTVCIsICJhdXRoX21ldGhvZCI6ICJlZDI1NTE5djEifQ==' metadata, sig = auth_header_string.split(".") self.assertEqual(expected_metadata, metadata) self.assertEqual(EXPECTED_ED25519_SIG, sig) json_metadata = json.loads( urlsafe_b64decode(metadata.encode('utf-8')).decode('utf-8')) self.assertEqual(self.credentials.access_key_id, json_metadata['access_key_id']) self.assertEqual("ed25519v1", json_metadata['auth_method'])
def load(self): """ load the credential from the json configuration file. """ if self._conf is None: return None if not os.path.isfile(self._conf): raise NoCredentialsError( err_msg="Config file {} not found".format(self._conf)) try: conf = json.loads(open(self._conf).read()) except Exception: LOG.debug("Could not read conf: %s", exc_info=True) return None if ACCESS_KEY_ID in conf or PRIVATE_KEY in conf: LOG.debug('Found credentials for key: %s in configuration file.', conf[ACCESS_KEY_ID]) access_key_id, private_key = self._extract_creds_from_mapping( conf, ACCESS_KEY_ID, PRIVATE_KEY) return Credentials(access_key_id=access_key_id, private_key=private_key, method=self.METHOD) elif ACCESS_TOKEN in conf: LOG.debug('Found access-token in configuration file.') access_token = self._extract_creds_from_mapping(conf, ACCESS_TOKEN) return Credentials(access_token=access_token, method=self.METHOD) else: cred_vars = '%s or %s' % (ACCESS_KEY_ID, ACCESS_TOKEN) LOG.debug('Auth config file is missing required key %s', cred_vars) raise MalformedCredentialsError(provider=self.METHOD, cred_var=cred_vars)
def _validate_error_response(self, response, parsed_response): self._assert_same(json.loads(response['body'].decode(UTF8)), parsed_response['error'])
def _decode_body(self, response): body = response['body'] if not body: return {} return json.loads(body.decode(self.DEFAULT_ENCODING))