def test_tomcat_wget_no_bytestring(self): responses = { "string": StringIO("Best response ever\r\nAnd you know it!"), "bytes": BytesIO(b"Best response ever\r\nAnd you know it!"), } string_mock = MagicMock(return_value=responses["string"]) bytes_mock = MagicMock(return_value=responses["bytes"]) with patch( "salt.modules.tomcat._auth", MagicMock(return_value=_build_opener( _HTTPBasicAuthHandler(), _HTTPDigestAuthHandler())), ): with patch("salt.modules.tomcat._urlopen", string_mock): response = tomcat._wget("tomcat.wait", url="http://localhost:8080/nofail") for line in response["msg"]: self.assertIsInstance(line, string_types) with patch("salt.modules.tomcat._urlopen", bytes_mock): try: response = tomcat._wget("tomcat.wait", url="http://localhost:8080/nofail") except TypeError as type_error: if (type_error.args[0] == "startswith first arg must be bytes or a tuple of bytes, not str" ): self.fail( "Got back a byte string, should've been a string") else: raise type_error for line in response["msg"]: self.assertIsInstance(line, string_types)
def _auth(url, user, passwd, realm): ''' returns a authentication handler. ''' basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=passwd) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=passwd) return _build_opener(basic, digest)
def _auth(url): """ Install an auth handler for urllib2 """ user = __salt__["config.get"]("solr.user", False) password = __salt__["config.get"]("solr.passwd", False) realm = __salt__["config.get"]("solr.auth_realm", "Solr") if user and password: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=password) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=password) _install_opener(_build_opener(basic, digest))
def _auth(url): ''' Install an auth handler for urllib2 ''' user = __salt__['config.get']('solr.user', False) password = __salt__['config.get']('solr.passwd', False) realm = __salt__['config.get']('solr.auth_realm', 'Solr') if user and password: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=password) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=password) _install_opener(_build_opener(basic, digest))
def _auth(uri): ''' returns a authentication handler. Get user & password from grains, if are not set default to modules.config.option If user & pass are missing return False ''' user, password = _get_credentials() if user is False or password is False: return False basic = _HTTPBasicAuthHandler() basic.add_password(realm='Tomcat Manager Application', uri=uri, user=user, passwd=password) digest = _HTTPDigestAuthHandler() digest.add_password(realm='Tomcat Manager Application', uri=uri, user=user, passwd=password) return _build_opener(basic, digest)
def _auth(url): ''' Install an auth handler for urllib2 ''' user = __salt__['config.get']('solr.user', False) password = __salt__['config.get']('solr.passwd', False) realm = __salt__['config.get']('solr.auth_realm', 'Solr') if user and password: basic = _HTTPBasicAuthHandler() basic.add_password( realm=realm, uri=url, user=user, passwd=password ) digest = _HTTPDigestAuthHandler() digest.add_password( realm=realm, uri=url, user=user, passwd=password ) _install_opener( _build_opener(basic, digest) )
def server_status(profile='default'): ''' Get Information from the Apache server-status handler .. note:: The server-status handler is disabled by default. In order for this function to work it needs to be enabled. See http://httpd.apache.org/docs/2.2/mod/mod_status.html The following configuration needs to exists in pillar/grains. Each entry nested in ``apache.server-status`` is a profile of a vhost/server. This would give support for multiple apache servers/vhosts. .. code-block:: yaml apache.server-status: default: url: http://localhost/server-status user: someuser pass: password realm: 'authentication realm for digest passwords' timeout: 5 CLI Examples: .. code-block:: bash salt '*' apache.server_status salt '*' apache.server_status other-profile ''' ret = { 'Scoreboard': { '_': 0, 'S': 0, 'R': 0, 'W': 0, 'K': 0, 'D': 0, 'C': 0, 'L': 0, 'G': 0, 'I': 0, '.': 0, }, } # Get configuration from pillar url = __salt__['config.get']( 'apache.server-status:{0}:url'.format(profile), 'http://localhost/server-status' ) user = __salt__['config.get']( 'apache.server-status:{0}:user'.format(profile), '' ) passwd = __salt__['config.get']( 'apache.server-status:{0}:pass'.format(profile), '' ) realm = __salt__['config.get']( 'apache.server-status:{0}:realm'.format(profile), '' ) timeout = __salt__['config.get']( 'apache.server-status:{0}:timeout'.format(profile), 5 ) # create authentication handler if configuration exists if user and passwd: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=passwd) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=passwd) _install_opener(_build_opener(basic, digest)) # get http data url += '?auto' try: response = _urlopen(url, timeout=timeout).read().splitlines() except URLError: return 'error' # parse the data for line in response: splt = line.split(':', 1) splt[0] = splt[0].strip() splt[1] = splt[1].strip() if splt[0] == 'Scoreboard': for c in splt[1]: ret['Scoreboard'][c] += 1 else: if splt[1].isdigit(): ret[splt[0]] = int(splt[1]) else: ret[splt[0]] = float(splt[1]) # return the good stuff return ret
def query(action=None, command=None, args=None, method="GET", data=None): """ Make a web call to a Parallels provider """ path = config.get_cloud_config_value( "url", get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm="Parallels Instance Manager", uri=path, user=config.get_cloud_config_value( "user", get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( "password", get_configured_provider(), __opts__, search_global=False ), ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += "/{0}".format(command) if not type(args, dict): args = {} kwargs = {"data": data} if isinstance(data, six.string_types) and "<?xml" in data: kwargs["headers"] = { "Content-type": "application/xml", } if args: params = _urlencode(args) req = _Request(url="{0}?{1}".format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug("%s %s", method, req.get_full_url()) if data: log.debug(data) try: result = _urlopen(req) log.debug("PARALLELS Response Status Code: %s", result.getcode()) if "content-length" in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error("PARALLELS Response Status Code: %s %s", exc.code, exc.msg) root = ET.fromstring(exc.read()) log.error(root) return {"error": root}
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value('url', get_configured_provider(), __opts__, search_global=False) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value('user', get_configured_provider(), __opts__, search_global=False), passwd=config.get_cloud_config_value('password', get_configured_provider(), __opts__, search_global=False)) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, six.string_types) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug('PARALLELS Response Status Code: {0}'.format( result.getcode())) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error('PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg)) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value( 'url', get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value( 'user', get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( 'password', get_configured_provider(), __opts__, search_global=False ) ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, str) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug( 'PARALLELS Response Status Code: {0}'.format( result.getcode() ) ) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error( 'PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg ) ) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def server_status(profile="default"): """ Get Information from the Apache server-status handler .. note:: The server-status handler is disabled by default. In order for this function to work it needs to be enabled. See http://httpd.apache.org/docs/2.2/mod/mod_status.html The following configuration needs to exists in pillar/grains. Each entry nested in ``apache.server-status`` is a profile of a vhost/server. This would give support for multiple apache servers/vhosts. .. code-block:: yaml apache.server-status: default: url: http://localhost/server-status user: someuser pass: password realm: 'authentication realm for digest passwords' timeout: 5 CLI Examples: .. code-block:: bash salt '*' apache.server_status salt '*' apache.server_status other-profile """ ret = { "Scoreboard": { "_": 0, "S": 0, "R": 0, "W": 0, "K": 0, "D": 0, "C": 0, "L": 0, "G": 0, "I": 0, ".": 0, }, } # Get configuration from pillar url = __salt__["config.get"]( "apache.server-status:{0}:url".format(profile), "http://localhost/server-status") user = __salt__["config.get"]( "apache.server-status:{0}:user".format(profile), "") passwd = __salt__["config.get"]( "apache.server-status:{0}:pass".format(profile), "") realm = __salt__["config.get"]( "apache.server-status:{0}:realm".format(profile), "") timeout = __salt__["config.get"]( "apache.server-status:{0}:timeout".format(profile), 5) # create authentication handler if configuration exists if user and passwd: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=passwd) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=passwd) _install_opener(_build_opener(basic, digest)) # get http data url += "?auto" try: response = _urlopen(url, timeout=timeout).read().splitlines() except URLError: return "error" # parse the data for line in response: splt = line.split(":", 1) splt[0] = splt[0].strip() splt[1] = splt[1].strip() if splt[0] == "Scoreboard": for c in splt[1]: ret["Scoreboard"][c] += 1 else: if splt[1].isdigit(): ret[splt[0]] = int(splt[1]) else: ret[splt[0]] = float(splt[1]) # return the good stuff return ret