예제 #1
0
    def fetch(self, command, **params):
        """
        @return object
        """
        # Perform HTTP request
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if {} != params :
            for key, value in params.items():
                request_body[key] = value

        file = open(self.key_path)
        key = binascii.a2b_base64(file.read())
        file.close()

        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        try:
            # Work over [Errno -3] Temporary failure in name resolution
            # http://bugs.centos.org/view.php?id=4814
            os.chmod('/etc/resolv.conf', 0755)
        except OSError:
            self._logger.debug('Cant chmod /etc/resolv.conf: %s', sys.exc_info()[1])

        post_data = urllib.urlencode(request_body)
        headers = {
                "Date": timestamp,
                "X-Signature": signature,
                "X-Server-Id": self.server_id
        }
        response = None
        wait_seconds = 30
        msg_wait = 'Waiting %d seconds before the next try' % wait_seconds
        while True:
            try:
                self._logger.debug("QueryEnv request: %s", post_data)
                opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
                req = urllib2.Request(url, post_data, headers)
                response = opener.open(req)
                break
            except:
            	e = sys.exc_info()[1]
                if isinstance(e, urllib2.HTTPError):
                    resp_body = e.read() if e.fp is not None else ""
                    self._logger.warn('QueryEnv failed. HTTP %s. %s. %s', e.code, resp_body or e.msg, msg_wait)
                else:
                    self._logger.warn('QueryEnv failed. %s. %s', e, msg_wait)
                self._logger.warn('Sleep %s seconds before next attempt...', wait_seconds)
                time.sleep(wait_seconds)

        resp_body = response.read()
        resp_body = self.htmlparser.unescape(resp_body)
        resp_body = resp_body.encode('utf-8')

        self._logger.debug("QueryEnv response: %s", resp_body)
        return resp_body
예제 #2
0
    def fetch(self, command, **params):
        """
        @return object
        """
        # Perform HTTP request
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if {} != params:
            for key, value in params.items():
                request_body[key] = value

        file = open(self.key_path)
        key = binascii.a2b_base64(file.read())
        file.close()

        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        try:
            # Work over [Errno -3] Temporary failure in name resolution
            # http://bugs.centos.org/view.php?id=4814
            os.chmod('/etc/resolv.conf', 0755)
        except OSError:
            self._logger.debug('Cant chmod /etc/resolv.conf: %s',
                               sys.exc_info()[1])

        post_data = urllib.urlencode(request_body)
        headers = {
            "Date": timestamp,
            "X-Signature": signature,
            "X-Server-Id": self.server_id
        }
        response = None
        wait_seconds = 30
        msg_wait = 'Waiting %d seconds before the next try' % wait_seconds
        while True:
            try:
                self._logger.debug("QueryEnv request: %s", post_data)
                opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
                req = urllib2.Request(url, post_data, headers)
                response = opener.open(req)
                break
            except:
                e = sys.exc_info()[1]
                if isinstance(e, urllib2.HTTPError):
                    resp_body = e.read() if e.fp is not None else ""
                    self._logger.warn('QueryEnv failed. HTTP %s. %s. %s',
                                      e.code, resp_body or e.msg, msg_wait)
                else:
                    self._logger.warn('QueryEnv failed. %s. %s', e, msg_wait)
                self._logger.warn('Sleep %s seconds before next attempt...',
                                  wait_seconds)
                time.sleep(wait_seconds)

        resp_body = response.read()
        self._logger.debug("QueryEnv response: %s", resp_body)
        return resp_body
예제 #3
0
    def fetch(self, command, params=None, log_response=True):
        """
        @return object
        """
        # Perform HTTP request
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        self._logger.debug('Call QueryEnv: %s', url)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if params:
            for key, value in params.items():
                request_body[key] = value

        file = open(self.key_path)
        key = binascii.a2b_base64(file.read())
        file.close()

        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        post_data = urllib.urlencode(request_body)
        headers = {
            "Date": timestamp,
            "X-Signature": signature,
            "X-Server-Id": self.server_id
        }
        response = None
        wait_seconds = 30
        msg_wait = 'Waiting %d seconds before the next try' % wait_seconds
        while True:
            try:
                self._logger.debug("QueryEnv request: %s", post_data)
                opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
                req = urllib2.Request(url, post_data, headers)
                response = opener.open(req)
                break
            except:
                e = sys.exc_info()[1]
                if isinstance(e, urllib2.HTTPError):
                    resp_body = e.read() if e.fp is not None else ""
                    self._logger.warn('QueryEnv failed. HTTP %s. %s. %s',
                                      e.code, resp_body or e.msg, msg_wait)
                else:
                    self._logger.warn('QueryEnv failed. %s. %s', e, msg_wait)
                self._logger.warn('Sleep %s seconds before next attempt...',
                                  wait_seconds)
                time.sleep(wait_seconds)

        resp_body = response.read()
        resp_body = self.htmlparser.unescape(resp_body)
        resp_body = resp_body.encode('utf-8')

        if log_response:
            self._logger.debug("QueryEnv response: %s", resp_body)
        return resp_body
예제 #4
0
	def fetch(self, command, **params):
		"""
		@return object
		"""
		# Perform HTTP request
		url = "%s/%s/%s" % (self.url, self.api_version, command)
		request_body = {}
		request_body["operation"] = command
		request_body["version"] = self.api_version
		if {} != params :
			for key, value in params.items():
				request_body[key] = value
		
		file = open(self.key_path)
		key = binascii.a2b_base64(file.read())
		file.close()

		signature, timestamp = cryptotool.sign_http_request(request_body, key)		
		
		post_data = urllib.urlencode(request_body)
		headers = {
			"Date": timestamp, 
			"X-Signature": signature, 
			"X-Server-Id": self.server_id
		}
		response = None
		max_attempts = 5
		for i in range(1, max_attempts + 1):
			try:
				try:
					self._logger.debug("QueryEnv request: %s", post_data)
					opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
					req = urllib2.Request(url, post_data, headers)
					response = opener.open(req)
					break
				except urllib2.URLError, e:
					if isinstance(e, urllib2.HTTPError):
						resp_body = e.read() if e.fp is not None else ""
						raise QueryEnvError, "Request failed. %s. URL: %s. Service message: %s" % (e, self.url, resp_body), sys.exc_traceback
					else:
						raise QueryEnvError, "Cannot connect to QueryEnv server on %s. %s" % (url, str(e)), sys.exc_traceback
			except:
				if 'not supported' in str(sys.exc_info()[1]):
					raise
				self._logger.debug('QueryEnv failed. %s', sys.exc_info()[1])
				if i < max_attempts:
					self._logger.debug('Waiting %d seconds before the next try', 10)
					time.sleep(10)
				else:
					raise

		resp_body = response.read()
		self._logger.debug("QueryEnv response: %s", resp_body)
		return resp_body
예제 #5
0
    def out_protocol_filter(self, producer, queue, message, headers):
        try:
            # Encrypt message
            cnf = bus.cnf
            self._logger.debug('Encrypting message')
            crypto_key = binascii.a2b_base64(cnf.read_key(self.crypto_key_path))
            data = cryptotool.encrypt(message, crypto_key)

            # Generate signature
            signature, timestamp = cryptotool.sign_http_request(data, crypto_key)

            # Add extra headers
            headers['Date'] = timestamp
            headers['X-Signature'] = signature
            headers['X-Server-Id'] = self.server_id

            return data
        except (BaseException, Exception), e:
            raise MessagingError('Cannot encrypt message. error: %s' % (e))
예제 #6
0
    def out_protocol_filter(self, producer, queue, message, headers):
        try:
            # Encrypt message
            cnf = bus.cnf
            self._logger.debug('Encrypting message')
            crypto_key = binascii.a2b_base64(cnf.read_key(self.crypto_key_path))
            data = cryptotool.encrypt(message, crypto_key)

            # Generate signature
            signature, timestamp = cryptotool.sign_http_request(data, crypto_key)

            # Add extra headers
            headers['Date'] = timestamp
            headers['X-Signature'] = signature
            headers['X-Server-Id'] = self.server_id

            return data
        except (BaseException, Exception), e:
            raise MessagingError('Cannot encrypt message. error: %s' % (e))
예제 #7
0
    def _prepare_request(self, command, params=None):
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if params:
            for key, value in list(params.items()):
                request_body[key] = value

        with open(self.key_path, 'r') as fp:
            key = binascii.a2b_base64(fp.read())
        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        headers = {
            "Date": timestamp,
            "X-Signature": signature,
            "X-Server-Id": self.server_id,
            "X-Scalr-Agent-Version": self.agent_version
        }

        return (url, request_body, headers)
예제 #8
0
    def fetch(self, command, params=None, log_response=True):
        """
        @return object
        """
        # Perform HTTP request
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        self._logger.debug('Call QueryEnv: %s', url)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if params:
            for key, value in params.items():
                request_body[key] = value

        file = open(self.key_path)
        key = binascii.a2b_base64(file.read())
        file.close()

        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        post_data = urllib.urlencode(request_body)
        headers = {
            "Date": timestamp,
            "X-Signature": signature,
            "X-Server-Id": self.server_id
        }
        response = None
        wait_seconds = 30
        msg_wait = 'Waiting %d seconds before the next try' % wait_seconds
        while True:
            try:
                self._logger.debug("QueryEnv request: %s", post_data)
                opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
                req = urllib2.Request(url, post_data, headers)
                response = opener.open(req)
                break
            except:
                e = sys.exc_info()[1]
                if isinstance(e, urllib2.HTTPError):
                    resp_body = e.read() if e.fp is not None else ""
                    msg = resp_body or e.msg
                    if "Signature doesn't match" in msg:
                        raise InvalidSignatureError(msg)
                    if "not supported" in msg:
                        raise
                    if e.code in (509, 400, 403) or not self.autoretry:
                        raise QueryEnvError('QueryEnv failed: %s' % msg)
                    self._logger.warn('QueryEnv failed. HTTP %s. %s. %s',
                                      e.code, msg, msg_wait)
                else:
                    self._logger.warn('QueryEnv failed. %s. %s', e, msg_wait)
                self._logger.warn('Sleep %s seconds before next attempt...',
                                  wait_seconds)
                time.sleep(wait_seconds)

        resp_body = response.read()
        resp_body = self.htmlparser.unescape(resp_body)
        resp_body = resp_body.encode('utf-8')

        if log_response:
            log_body = resp_body
            if command == 'list-global-variables':
                try:
                    xml = ET.XML(resp_body)
                    glob_vars = xml[0]
                    i = 0
                    for _ in xrange(len(glob_vars)):
                        var = glob_vars[i]
                        if int(var.attrib.get('private', 0)) == 1:
                            glob_vars.remove(var)
                            continue
                        i += 1
                    log_body = ET.tostring(xml)
                except (BaseException, Exception), e:
                    self._logger.debug(
                        "Exception occured while parsing list-global-variables response: %s"
                        % e.message)
                    if isinstance(e, ET.ParseError):
                        raise
            self._logger.debug("QueryEnv response: %s", log_body)
예제 #9
0
    def fetch(self, command, params=None, log_response=True):
        """
        @return object
        """
        # Perform HTTP request
        url = "%s/%s/%s" % (self.url, self.api_version, command)
        self._logger.debug("Call QueryEnv: %s", url)
        request_body = {}
        request_body["operation"] = command
        request_body["version"] = self.api_version
        if params:
            for key, value in params.items():
                request_body[key] = value

        file = open(self.key_path)
        key = binascii.a2b_base64(file.read())
        file.close()

        signature, timestamp = cryptotool.sign_http_request(request_body, key)

        post_data = urllib.urlencode(request_body)
        headers = {"Date": timestamp, "X-Signature": signature, "X-Server-Id": self.server_id}
        response = None
        wait_seconds = 30
        msg_wait = "Waiting %d seconds before the next try" % wait_seconds
        while True:
            try:
                self._logger.debug("QueryEnv request: %s", post_data)
                opener = urllib2.build_opener(urltool.HTTPRedirectHandler)
                req = urllib2.Request(url, post_data, headers)
                response = opener.open(req)
                break
            except:
                e = sys.exc_info()[1]
                if isinstance(e, urllib2.HTTPError):
                    resp_body = e.read() if e.fp is not None else ""
                    msg = resp_body or e.msg
                    if "Signature doesn't match" in msg:
                        raise InvalidSignatureError(msg)
                    if "not supported" in msg:
                        raise
                    if e.code in (509, 400, 403) or not self.autoretry:
                        raise QueryEnvError("QueryEnv failed: %s" % msg)
                    self._logger.warn("QueryEnv failed. HTTP %s. %s. %s", e.code, msg, msg_wait)
                else:
                    self._logger.warn("QueryEnv failed. %s. %s", e, msg_wait)
                self._logger.warn("Sleep %s seconds before next attempt...", wait_seconds)
                time.sleep(wait_seconds)

        resp_body = response.read()
        resp_body = self.htmlparser.unescape(resp_body)
        resp_body = resp_body.encode("utf-8")

        if log_response:
            log_body = resp_body
            if command == "list-global-variables":
                try:
                    xml = ET.XML(resp_body)
                    glob_vars = xml[0]
                    i = 0
                    for _ in xrange(len(glob_vars)):
                        var = glob_vars[i]
                        if int(var.attrib.get("private", 0)) == 1:
                            glob_vars.remove(var)
                            continue
                        i += 1
                    log_body = ET.tostring(xml)
                except (BaseException, Exception), e:
                    self._logger.debug("Exception occured while parsing list-global-variables response: %s" % e.message)
                    if isinstance(e, ET.ParseError):
                        raise
            self._logger.debug("QueryEnv response: %s", log_body)