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
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
def json_decode_template_body(parsed, **kwargs): try: value = json.loads(parsed['TemplateBody']) parsed['TemplateBody'] = value except (ValueError, TypeError): logger.debug('error loading JSON', exc_info=True)
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