Esempio n. 1
0
def test_server_info():
    session_key = get_session_key(
        context.username,
        context.password,
        scheme=context.scheme,
        host=context.host,
        port=context.port,
    )

    si = server_info.ServerInfo(session_key,
                                scheme=context.scheme,
                                host=context.host,
                                port=context.port)
    print("Local splunk server info")

    print("    -name: ", si.server_name)

    print("    -version: ", si.version)

    print("    -is a cluster captain: ", si.is_captain())

    print("    -is a clound instance: ", si.is_cloud_instance())

    print("    -is a search head: ", si.is_search_head())

    print("    -is a SHC member: ", si.is_shc_member())

    try:
        shc_members = si.get_shc_members()
    except server_info.ServerInfoException as e:
        print(e)
    else:
        print("    -SHC members are: ", shc_members)
Esempio n. 2
0
def is_user_allow_to_create_ta(username, service):
    '''
    return the error code and error params object
    otherwise return None
    '''
    # check if free license is enabled
    if _is_free_license_active(service):
        _logger.info("Add-on builder is not locked in splunk free license")
        return None

    # check the role of current user, only admin can run code
    kwargs = {"sort_key": "realname", "sort_dir": "asc"}
    usrs = service.users.list(count=-1, **kwargs)
    is_admin = False
    for u in usrs:
        if u.name == username:
            roles = u.role_entities
            role_names = [r.name for r in roles]
            for r in roles:
                role_names.extend(r.content.get('imported_roles', []))
                _logger.debug("extend role_names with %s",
                              r.content.get('imported_roles'))
            is_admin = any([n == 'admin' for n in role_names])
        if is_admin:
            break

    if is_admin is False:
        msg = 'Can not use TAB. current user is not admin. current user:{0}'.format(
            username)
        _logger.error(msg)
        return {'err_code': 3122, 'err_args': {'user': username}}
    mode = _get_cluster_server_mode(service)
    if mode == 'slave' or mode == 'master':
        _logger.error('cluster mode is %s', mode)
        return {'err_code': 3135}
    elif mode == 'searchhead':
        shc_disable = _get_shc_disable_flag(service)
        if not shc_disable:
            _logger.error('this search head is in a SHC.')
            return {'err_code': 3123}
        else:
            _logger.debug('this is a stand alone search head.')
    # check if it is shc env
    si = server_info.ServerInfo(service.token, service.scheme, service.host,
                                service.port)
    _logger.debug(
        'user allow. is-search-head:%s, is-search-head-member:%s, server-info:%s',
        si.is_search_head(), si.is_shc_member(), si._server_info())
    if si.is_shc_member():
        msg = 'Can not use TAB. current server is in a search head cluster.'
        _logger.error(msg)
        return {'err_code': 3123}
    return None
Esempio n. 3
0
    def test_get_shc_members(self, monkeypatch):
        def _mock_get(self,
                      path_segment,
                      owner=None,
                      app=None,
                      sharing=None,
                      **query):
            return common.make_response_record(
                '{"entry": [{"name": "5B4A53C7-B824-4103-B8CC-C22E1EC6480F", "content": {"peer_scheme_host_port": "https://192.168.1.85:8089", "label": "SHC01_SearchHead02_1_85"}}, {"name": "D7E3BA03-85CE-449A-9736-38F2DA58236B", "content": {"peer_scheme_host_port": "https://192.168.1.86:8089", "label": "SHC01_SearchHead03_1_86"}}, {"name": "DA72938A-72C4-46F3-86BE-2E200EC56C76", "content": {"peer_scheme_host_port": "https://192.168.1.84:8089", "label": "SHC01_SearchHead01_1_84"}}]}'
            )

        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)
        monkeypatch.setattr(binding.Context, "get", _mock_get)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert si.get_shc_members() == [
            ("SHC01_SearchHead02_1_85", "https://192.168.1.85:8089"),
            ("SHC01_SearchHead03_1_86", "https://192.168.1.86:8089"),
            ("SHC01_SearchHead01_1_84", u"https://192.168.1.84:8089"),
        ]
Esempio n. 4
0
    def test_is_captain_ready(self, monkeypatch):
        def _mock_get(self,
                      path_segment,
                      owner=None,
                      app=None,
                      sharing=None,
                      **query):
            msg = (
                '{"entry": [{"content": {"eai:acl": null,"elected_captain": 1463195590,'
                '"id": "9CA04BAD-0C24-4703-8A88-E20345833508","initialized_flag": true,'
                '"label": "ta-shc04-sh2","maintenance_mode": false,"mgmt_uri": "https://ta-shc04-sh2:8089",'
                '"min_peers_joined_flag": true,"peer_scheme_host_port": "https://ta-shc04-sh2:8089",'
                '"rolling_restart_flag": false,"service_ready_flag": true,"start_time": 1463195526}}]}'
            )
            return common.make_response_record(msg)

        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)
        monkeypatch.setattr(binding.Context, "get", _mock_get)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert si.is_captain_ready()
Esempio n. 5
0
    def test_is_shc_member(self, monkeypatch):
        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert si.is_shc_member()
Esempio n. 6
0
    def test_is_cloud_instance(self, monkeypatch):
        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert not si.is_cloud_instance()
Esempio n. 7
0
    def test_version(self, monkeypatch):
        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert si.version == "6.3.1511.2"
Esempio n. 8
0
    def test_server_name(self, monkeypatch):
        common.mock_splunkhome(monkeypatch)
        common.mock_serverinfo(monkeypatch)

        si = server_info.ServerInfo(common.SESSION_KEY)
        assert si.server_name == "unittestServer"