Beispiel #1
0
 def test_splunkd_rest(self):
     splunkd = "https://localhost:8089"
     eid = "{}/services/auth/login".format(splunkd)
     response, content = rest.splunkd_request(eid, None, method="POST",
                                              data=self.splunk_user_pass)
     self.assertIsNotNone(response)
     self.assertIsNotNone(content)
     eid = "{}/servicesNS/nobody/-/configs/conf-inputs".format(splunkd)
     response, content = rest.splunkd_request(eid, self.session_key,
                                              method="GET")
     self.assertIsNotNone(response)
     self.assertIsNotNone(content)
Beispiel #2
0
    def get_session_key(username,
                        password,
                        splunkd_uri="https://localhost:8089"):
        """
        Get session key by using login username and passwrod
        @return: session_key if successful, None if failed
        """

        eid = "".join((splunkd_uri, "/services/auth/login"))
        postargs = {
            "username": username,
            "password": password,
        }

        response, content = rest.splunkd_request(eid,
                                                 None,
                                                 method="POST",
                                                 data=postargs)

        if response is None and content is None:
            return None

        xml_obj = xdm.parseString(content)
        session_nodes = xml_obj.getElementsByTagName("sessionKey")
        return session_nodes[0].firstChild.nodeValue
Beispiel #3
0
    def _do_request(self, uri, method, payload, err_msg):
        resp, content = rest.splunkd_request(uri, self.session_key, method,
                                             data=payload, retry=3)
        if resp is None and content is None:
            return None

        if resp.status in (200, 201):
            return content
        else:
            _LOGGER.error("%s, reason=%s", err_msg, resp.reason)
        return None
    def delete(self, realm, user, app, owner="nobody"):
        """
        Delete the encrypted entry
        @return: True for success, False for failure
        """

        endpoint = self._get_endpoint(realm, user, app, owner)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="DELETE")
        if response and response.status in (200, 201):
            return True
        return False
    def delete(self, realm, user, app, owner="nobody"):
        """
        Delete the encrypted entry
        @return: True for success, False for failure
        """

        endpoint = self._get_endpoint(realm, user, app, owner)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="DELETE")
        if response and response.status in (200, 201):
            return True
        return False
    def _get_credentials(self, realm, user, app, owner, prop):
        """
        @return: clear or encrypted password for specified realm, user
        """

        endpoint = self._get_endpoint(realm, user, app, owner)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="GET")
        if response and response.status in (200, 201) and content:
            password = conf.parse_conf_xml_dom(content)[0]
            return password[prop]
        return None
    def _get_credentials(self, realm, user, app, owner, prop):
        """
        @return: clear or encrypted password for specified realm, user
        """

        endpoint = self._get_endpoint(realm, user, app, owner)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="GET")
        if response and response.status in (200, 201) and content:
            password = conf.parse_conf_xml_dom(content)[0]
            return password[prop]
        return None
Beispiel #8
0
def restart_splunkd(username, password, host_path="https://localhost:8089"):
    session_key = cred.CredentialManager.get_session_key(
        username,
        password)

    path = "".join((host_path, "/services/server/control/restart"))
    response, content = rest.splunkd_request(path, session_key, method="POST")
    if response.status == 200:
        time.sleep(30)
        poll_until_splunk_is_alive(username, password, host_path)
    else:
        raise Exception("Failed to restart splunk. %d : %s",
                        response.status, response.reason)
    def _do_request(self, uri, method, payload, err_msg):
        resp, content = rest.splunkd_request(uri,
                                             self.session_key,
                                             method,
                                             data=payload,
                                             retry=3)
        if resp is None and content is None:
            return None

        if resp.status in (200, 201):
            return content
        else:
            _LOGGER.error("%s, reason=%s", err_msg, resp.reason)
        return None
    def get_all_passwords(self):
        """
        @return: a list of dict when successful, None when failed.
        the dict at least contains
        {
            "realm": xxx,
            "username": yyy,
            "clear_password": zzz,
        }
        """

        endpoint = "{}/services/storage/passwords".format(self._splunkd_uri)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="GET")
        if response and response.status in (200, 201) and content:
            return conf.parse_conf_xml_dom(content)
Beispiel #11
0
    def get_all_passwords(self):
        """
        @return: a list of dict when successful, None when failed.
        the dict at least contains
        {
            "realm": xxx,
            "username": yyy,
            "clear_password": zzz,
        }
        """

        endpoint = "{}/services/storage/passwords".format(self._splunkd_uri)
        response, content = rest.splunkd_request(
            endpoint, self._session_key, method="GET")
        if response and response.status in (200, 201) and content:
            return conf.parse_conf_xml_dom(content)
Beispiel #12
0
    def create(self, realm, user, password, app, owner="nobody"):
        """
        Create a new stored credential.
        """

        payload = {
            "name": user,
            "password": password,
            "realm": realm,
        }

        endpoint = self._get_endpoint(realm, user, app, owner)
        resp, content = rest.splunkd_request(endpoint, self._session_key,
                                             method="POST", data=payload)
        if resp and resp.status in (200, 201):
            return True
        return False
    def create(self, realm, user, password, app, owner="nobody"):
        """
        Create a new stored credential.
        """

        payload = {
            "name": user,
            "password": password,
            "realm": realm,
        }

        endpoint = self._get_endpoint(realm, user, app, owner)
        resp, content = rest.splunkd_request(endpoint, self._session_key,
                                             method="POST", data=payload)
        if resp and resp.status in (200, 201):
            return True
        return False
Beispiel #14
0
    def _do_request(self, uri, method, data=None,
                    content_type="application/x-www-form-urlencoded"):
        headers = {"Content-Type": content_type}

        resp, content = rest.splunkd_request(uri, self._session_key,
                                             method, headers, data)
        if resp is None and content is None:
            raise KVException("Failed uri={0}, data={1}".format(uri, data))

        if resp.status in (200, 201):
            return content
        elif resp.status == 409:
            raise KVAlreadyExists("{0}-{1} already exists".format(uri, data))
        elif resp.status in (500, 404):
            raise KVNotExists("{0}-{1} not exists".format(uri, data))
        else:
            raise KVException("Failed to {0} {1}, reason={2}".format(
                method, uri, resp.reason))
Beispiel #15
0
def reload_confs(confs, session_key, splunkd_uri="https://localhost:8089",
                 appname="-"):
    new_confs = []
    for conf in confs:
        conf = op.basename(conf)
        if conf.endswith(".conf"):
            conf = conf[:-5]
            new_confs.append(conf)
        else:
            new_confs.append(conf)

    endpoint_template = "{0}/servicesNS/-/{1}/configs/conf-{2}/_reload"
    for conf in new_confs:
        endpoint = endpoint_template.format(splunkd_uri, appname, conf)
        resp, content = rest.splunkd_request(endpoint, session_key)
        if not resp or resp.status not in (200, 201):
            _LOGGER.error("Failed to refresh %s, reason=%s",
                          endpoint, resp.reason if resp else "")
Beispiel #16
0
def reload_confs(confs,
                 session_key,
                 splunkd_uri="https://localhost:8089",
                 appname="-"):
    new_confs = []
    for conf in confs:
        conf = op.basename(conf)
        if conf.endswith(".conf"):
            conf = conf[:-5]
            new_confs.append(conf)
        else:
            new_confs.append(conf)

    endpoint_template = "{0}/servicesNS/-/{1}/configs/conf-{2}/_reload"
    for conf in new_confs:
        endpoint = endpoint_template.format(splunkd_uri, appname, conf)
        resp, content = rest.splunkd_request(endpoint, session_key)
        if not resp or resp.status not in (200, 201):
            _LOGGER.error("Failed to refresh %s, reason=%s", endpoint,
                          resp.reason if resp else "")
    def _do_request(self,
                    uri,
                    method,
                    data=None,
                    content_type="application/x-www-form-urlencoded"):
        headers = {"Content-Type": content_type}

        resp, content = rest.splunkd_request(uri, self._session_key, method,
                                             headers, data)
        if resp is None and content is None:
            raise KVException("Failed uri={0}, data={1}".format(uri, data))

        if resp.status in (200, 201):
            return content
        elif resp.status == 409:
            raise KVAlreadyExists("{0}-{1} already exists".format(uri, data))
        elif resp.status in (500, 404):
            raise KVNotExists("{0}-{1} not exists".format(uri, data))
        else:
            raise KVException("Failed to {0} {1}, reason={2}".format(
                method, uri, resp.reason))
Beispiel #18
0
    def get_session_key(username, password,
                        splunkd_uri="https://localhost:8089"):
        """
        Get session key by using login username and passwrod
        @return: session_key if successful, None if failed
        """

        eid = "".join((splunkd_uri, "/services/auth/login"))
        postargs = {
            "username": username,
            "password": password,
        }

        response, content = rest.splunkd_request(
            eid, None, method="POST", data=postargs)

        if response is None and content is None:
            return None

        xml_obj = xdm.parseString(content)
        session_nodes = xml_obj.getElementsByTagName("sessionKey")
        return session_nodes[0].firstChild.nodeValue