Example #1
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
Example #2
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.debug("%s, reason=%s", err_msg, resp.reason)
        return None
Example #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 xdp.parse_conf_xml_dom(content)
        else:
            _LOGGER.error("%s, reason=%s", err_msg, resp.reason)
        return None
Example #4
0
    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, _ = rest.splunkd_request(
            endpoint, self._session_key, method="DELETE")
        if response and response.status in (200, 201):
            return True
        return False
Example #5
0
    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 = xdp.parse_conf_xml_dom(content)[0]
            return password[prop]
        return None
Example #6
0
    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, _ = rest.splunkd_request(endpoint,
                                           self._session_key,
                                           method="DELETE")
        if response and response.status in (200, 201):
            return True
        return False
Example #7
0
    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 = xdp.parse_conf_xml_dom(content)[0]
            return password[prop]
        return None
Example #8
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?count=0&offset=0".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 xdp.parse_conf_xml_dom(content)
Example #9
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, _ = rest.splunkd_request(endpoint, self._session_key,
                                       method="POST", data=payload)
        if resp and resp.status in (200, 201):
            return True
        return False
Example #10
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?count=0&offset=0".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 xdp.parse_conf_xml_dom(content)
Example #11
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))
Example #12
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, _ = 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 "")
Example #13
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, _ = 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 "")
Example #14
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, _ = rest.splunkd_request(endpoint,
                                       self._session_key,
                                       method="POST",
                                       data=payload)
        if resp and resp.status in (200, 201):
            return True
        return False
Example #15
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))
Example #16
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