예제 #1
0
    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)
예제 #2
0
파일: modjk.py 프로젝트: MalloZup/salt-2
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)
예제 #3
0
파일: modjk.py 프로젝트: bryson/salt
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)
예제 #4
0
파일: solr.py 프로젝트: DaveQB/salt
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))
예제 #5
0
파일: solr.py 프로젝트: MalloZup/salt-2
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))
예제 #6
0
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))
예제 #7
0
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)
예제 #8
0
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)
예제 #9
0
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)
        )
예제 #10
0
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
예제 #11
0
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
예제 #12
0
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