示例#1
0
def streamingRequest(path, sessionKey=None, getargs=None, postargs=None, method='GET', timeout=SPLUNKD_CONNECTION_TIMEOUT):
    """
    A streaming counterpart to simpleRequest
    Returns an instance of StreamingResponse which has a readall() method
    that will return a generator to stream a response from splundk rather than buffering
    it in memory
    """
    # if absolute URI, pass along as-is
    if path.startswith('http'):
        uri = path
        pasrsedUri = urlsplit(uri)
        host = parsedUri.hostname
        path = parsedUri.path
        port = parsedUri.port

    else:
        # prepend convenience root path
        if not path.startswith(REST_ROOT_PATH): path = REST_ROOT_PATH + '/' + path.strip('/')
        
        # setup args
        host = splunk.getDefault('host')
        port = splunk.getDefault('port')
        urihost = '[%s]' % host if ':' in host else host
            
        uri = '%s://%s:%s/%s' % \
            (splunk.getDefault('protocol'), urihost, port, path.strip('/'))

    if getargs:
        getargs = dict([(k,v) for (k,v) in getargs.items() if v != None])
        querystring = '?' + util.urlencodeDict(getargs)
        uri += querystring
        path += querystring

    isssl = uri.startswith('https:')
    
    headers = {}
    sessionSource = 'direct'
    # get session key from known places: first the appserver session, then
    # the default instance cache
    if not sessionKey:
        sessionKey, sessionSource = splunk.getSessionKey(return_source=True)
    headers['Authorization'] = 'Splunk %s' % sessionKey

    payload = ''
    if postargs and method in ('GET', 'POST', 'PUT'):
        if method == 'GET':
            method = 'POST'
        payload = util.urlencodeDict(postargs)

    #
    # make request
    #
    if logger.level <= logging.DEBUG:
        if uri.lower().find('login') > -1:
            logpayload = '[REDACTED]'
        else:
            logpayload = payload
        logger.debug('streamingRequest > %s %s [%s] sessionSource=%s' % (method, uri, logpayload, sessionSource))
        t1 = time.time()

    logger.debug('streamingRequest opening connection to host=%s path=%s method=%s postargs=%s payload=%s' % (host, path, method, postargs, payload))

    try:
        conn = httplib.HTTPSConnection(host, port, WEB_KEYFILE, WEB_CERTFILE, False, timeout) if isssl else httplib.HTTPConnection(host, port, False, timeout)
        conn.connect()
        conn.putrequest(method, path)
        for key, val in headers.items():
            conn.putheader(key, val)
        if payload:
            conn.putheader('Content-Type', 'application/x-www-form-urlencoded')
            conn.putheader('Content-Length', str(len(payload)))
            conn.endheaders()
            conn.send(payload)
        else:
            conn.endheaders()
        
        response = conn.getresponse()
    except socket.error, e:
        raise splunk.SplunkdConnectionException, str(e)
示例#2
0
def streamingRequest(path, sessionKey=None, getargs=None, postargs=None, method='GET', timeout=None):
    """
    A streaming counterpart to simpleRequest
    Returns an instance of StreamingResponse which has a readall() method
    that will return a generator to stream a response from splundk rather than buffering
    it in memory
    """
    if timeout is None:
        timeout = SPLUNKD_CONNECTION_TIMEOUT

    # if absolute URI, pass along as-is
    if path.startswith('http'):
        uri = path
        parsedUri = urlsplit(uri)
        host = parsedUri.hostname
        path = parsedUri.path
        port = parsedUri.port

    else:
        # prepend convenience root path
        if not path.startswith(REST_ROOT_PATH): path = REST_ROOT_PATH + '/' + path.strip('/')
        
        # setup args
        host = splunk.getDefault('host')
        port = splunk.getDefault('port')
        urihost = '[%s]' % host if ':' in host else host
            
        uri = '%s://%s:%s/%s' % \
            (splunk.getDefault('protocol'), urihost, port, path.strip('/'))

    if getargs:
        getargs = dict([(k,v) for (k,v) in getargs.items() if v != None])
        querystring = '?' + util.urlencodeDict(getargs)
        uri += querystring
        path += querystring

    isssl = uri.startswith('https:')
    
    headers = {}
    sessionSource = 'direct'
    # get session key from known places: first the appserver session, then
    # the default instance cache
    if not sessionKey:
        sessionKey, sessionSource = splunk.getSessionKey(return_source=True)
    headers['Authorization'] = 'Splunk %s' % sessionKey

    payload = ''
    if postargs and method in ('GET', 'POST', 'PUT'):
        if method == 'GET':
            method = 'POST'
        payload = util.urlencodeDict(postargs)

    #
    # make request
    #
    if logger.level <= logging.DEBUG:
        if uri.lower().find('login') > -1:
            logpayload = '[REDACTED]'
        else:
            logpayload = payload
        logger.debug('streamingRequest > %s %s [%s] sessionSource=%s' % (method, uri, logpayload, sessionSource))
        t1 = time.time()

    logger.debug('streamingRequest opening connection to host=%s path=%s method=%s postargs=%s payload=%s' % (host, path, method, postargs, payload))

    try:
        conn = httplib.HTTPSConnection(host, port, getWebKeyFile(), getWebCertFile(), False, timeout) if isssl else httplib.HTTPConnection(host, port, False, timeout)
        conn.connect()
        conn.putrequest(method, path)
        for key, val in headers.items():
            conn.putheader(key, val)
        if payload:
            conn.putheader('Content-Type', 'application/x-www-form-urlencoded')
            conn.putheader('Content-Length', str(len(payload)))
            conn.endheaders()
            conn.send(payload)
        else:
            conn.endheaders()
        
        response = conn.getresponse()
    except socket.error, e:
        logger.error('Socket error communicating with splunkd (error=%s), path = %s' % (str(e), path))
        raise splunk.SplunkdConnectionException, 'Error connecting to %s: %s' % (path, str(e))
示例#3
0
def simpleRequest(path, sessionKey=None, getargs=None, postargs=None, method='GET', raiseAllErrors=False, proxyMode=False, rawResult=False, timeout=SPLUNKD_CONNECTION_TIMEOUT):
    """
    Makes an HTTP call to the main splunk REST endpoint
    
    path: the URI to fetch
        If given a relative URI, then the method will normalize to the splunkd
        default of "/services/...".
        If given an absolute HTTP(S) URI, then the method will use as-is.
        If given a 'file://' URI, then the method will attempt to read the file
        from the local filesystem.  Only files under $SPLUNK_HOME are supported,
        so paths are 'chrooted' from $SPLUNK_HOME.
        
    getargs: dict of k/v pairs that are always appended to the URL
    
    postargs: dict of k/v pairs that get placed into the body of the 
        request. If postargs is provided, then the HTTP method is auto
        assigned to POST.
        
    method: the HTTP verb - [GET | POST | DELETE | PUT]
    
    raiseAllErrors: indicates if the method should raise an exception
        if the server HTTP response code is >= 400

    rawResult: don't raise an exception if a non 200 response is received;
        return the actual response
    
    Return:
    
        This method will return a tuple of (serverResponse, serverContent)
        
        serverResponse: a dict of HTTP status information
        serverContent: the body content
    """
    
    # if absolute URI, pass along as-is
    if path.startswith('http'):
        uri = path
        
    # if file:// protocol, try to read file and return
    # the serverStatus is just an empty dict; file contents are in serverResponse
    # TODO: this probably doesn't work in windows
    elif path.startswith('file://'):
        workingPath = path[7:].strip(os.sep)
        lines = util.readSplunkFile(workingPath)
        return ({}, ''.join(lines))
            
    else:
        # prepend convenience root path
        if not path.startswith(REST_ROOT_PATH): path = REST_ROOT_PATH + '/' + path.strip('/')
        
        # setup args
        host = splunk.getDefault('host')
        if ':' in host:
            host = '[%s]' % host
            
        uri = '%s://%s:%s/%s' % \
            (splunk.getDefault('protocol'), host, splunk.getDefault('port'), path.strip('/'))

    if getargs:
        getargs = dict([(k,v) for (k,v) in getargs.items() if v != None])
        uri += '?' + util.urlencodeDict(getargs)

    
    # proxy mode bypasses all header passing
    headers = {}
    sessionSource = 'direct'
    if not proxyMode:
        
        # get session key from known places: first the appserver session, then
        # the default instance cache
        if not sessionKey:
            sessionKey, sessionSource = splunk.getSessionKey(return_source=True)
        headers['Authorization'] = 'Splunk %s' % sessionKey

    payload = ''
    if postargs and method in ('GET', 'POST', 'PUT'):
        if method == 'GET':
            method = 'POST'
        payload = util.urlencodeDict(postargs)
        
    #
    # make request
    #
    if logger.level <= logging.DEBUG:
        if uri.lower().find('login') > -1:
            logpayload = '[REDACTED]'
        else:
            logpayload = payload
        #logger.debug('simpleRequest >>>\n\tmethod=%s\n\turi=%s\n\tbody=%s' % (method, uri, logpayload))
        logger.debug('simpleRequest > %s %s [%s] sessionSource=%s' % (method, uri, logpayload, sessionSource))
        t1 = time.time()

    # Add wait and tries to check if the HTTP server is up and running
    tries = 4
    wait = 10
    try:
        for aTry in range(tries):
            h = httplib2.Http(timeout=timeout, disable_ssl_certificate_validation=True)
            if WEB_KEYFILE and WEB_CERTFILE:
                h.add_certificate(WEB_KEYFILE, WEB_CERTFILE, '')
            serverResponse, serverContent = h.request(uri, method, headers=headers, body=payload)
            if serverResponse == None:
                if aTry < tries:
                    time.sleep(wait)
            else:
                break
    except socket.error, e:
        raise splunk.SplunkdConnectionException, str(e)
示例#4
0
def simpleRequest(path, sessionKey=None, getargs=None, postargs=None, method='GET', raiseAllErrors=False,
                  proxyMode=False, rawResult=False, 
                  timeout=None, jsonargs=None):
    """
    Makes an HTTP call to the main splunk REST endpoint
    
    path: the URI to fetch
        If given a relative URI, then the method will normalize to the splunkd
        default of "/services/...".
        If given an absolute HTTP(S) URI, then the method will use as-is.
        If given a 'file://' URI, then the method will attempt to read the file
        from the local filesystem.  Only files under $SPLUNK_HOME are supported,
        so paths are 'chrooted' from $SPLUNK_HOME.
        
    getargs: dict of k/v pairs that are always appended to the URL
    
    postargs: dict of k/v pairs that get placed into the body of the 
        request. If postargs is provided, then the HTTP method is auto
        assigned to POST.
        
    method: the HTTP verb - [GET | POST | DELETE | PUT]
    
    raiseAllErrors: indicates if the method should raise an exception
        if the server HTTP response code is >= 400

    rawResult: don't raise an exception if a non 200 response is received;
        return the actual response

    timeout: if not set, will default to SPLUNKD_CONNECTION_TIMEOUT

    forceContentType: optionally supply the value for the Content-Type header
        to be set when sending the request to splunkd

    Return:
    
        This method will return a tuple of (serverResponse, serverContent)
        
        serverResponse: a dict of HTTP status information
        serverContent: the body content
    """
  
    if timeout is None:
        timeout = SPLUNKD_CONNECTION_TIMEOUT

    # if absolute URI, pass along as-is
    if path.startswith('http'):
        uri = path
        
    # if file:// protocol, try to read file and return
    # the serverStatus is just an empty dict; file contents are in serverResponse
    # TODO: this probably doesn't work in windows
    elif path.startswith('file://'):
        workingPath = path[7:].strip(os.sep)
        lines = util.readSplunkFile(workingPath)
        return ({}, ''.join(lines))
            
    else:
        # prepend convenience root path
        if not path.startswith(REST_ROOT_PATH): path = REST_ROOT_PATH + '/' + path.strip('/')
        
        # setup args
        host = splunk.getDefault('host')
        if ':' in host:
            host = '[%s]' % host
            
        uri = '%s://%s:%s/%s' % \
            (splunk.getDefault('protocol'), host, splunk.getDefault('port'), path.strip('/'))

    if getargs:
        getargs = dict([(k,v) for (k,v) in getargs.items() if v != None])
        uri += '?' + util.urlencodeDict(getargs)
    
    payload = ''
    if postargs or jsonargs and method in ('GET', 'POST', 'PUT'):
        if method == 'GET':
            method = 'POST'
        if jsonargs:
            # if a JSON body was given, use it for the payload and ignore the postargs
            payload = jsonargs
        else:
            payload = util.urlencodeDict(postargs)

    # proxy mode bypasses all header passing
    headers = {}
    sessionSource = 'direct'
    if not proxyMode:
        headers['Content-Type'] = 'application/x-www-form-urlencoded'
        headers['Content-Length'] = str(len(payload))
        
        # get session key from known places: first the appserver session, then
        # the default instance cache
        if not sessionKey:
            sessionKey, sessionSource = splunk.getSessionKey(return_source=True)
        headers['Authorization'] = 'Splunk %s' % sessionKey

    #
    # make request
    #
    if logger.level <= logging.DEBUG:
        if uri.lower().find('login') > -1:
            logpayload = '[REDACTED]'
        else:
            logpayload = payload
        #logger.debug('simpleRequest >>>\n\tmethod=%s\n\turi=%s\n\tbody=%s' % (method, uri, logpayload))
        logger.debug('simpleRequest > %s %s [%s] sessionSource=%s timeout=%s' % (method, uri, logpayload, sessionSource, timeout))
        t1 = time.time()

    # Add wait and tries to check if the HTTP server is up and running
    tries = 4
    wait = 10
    try:
        import httplib2
        for aTry in range(tries):
            h = httplib2.Http(timeout=timeout, disable_ssl_certificate_validation=True)
            if getWebKeyFile() and getWebCertFile():
                h.add_certificate(getWebKeyFile(), getWebCertFile(), '')
            serverResponse, serverContent = h.request(uri, method, headers=headers, body=payload)
            if serverResponse == None:
                if aTry < tries:
                    time.sleep(wait)
            else:
                break
    except socket.error, e:
        logger.error('Socket error communicating with splunkd (error=%s), path = %s' % (str(e), path))
        raise splunk.SplunkdConnectionException, 'Error connecting to %s: %s' % (path, str(e))