diff --git a/splunklib/client.py b/splunklib/client.py index b5dd96561..73f101cc8 100644 --- a/splunklib/client.py +++ b/splunklib/client.py @@ -67,7 +67,7 @@ import socket import contextlib -from binding import Context, HTTPError, AuthenticationError, namespace, UrlEncoded, _encode, _make_cookie_header +from binding import Context, HTTPError, AuthenticationError, namespace, UrlEncoded, _encode, _make_cookie_header, _NoAuthenticationToken from data import record import data @@ -1937,7 +1937,9 @@ def attach(self, host=None, source=None, sourcetype=None): if sourcetype is not None: args['sourcetype'] = sourcetype path = UrlEncoded(PATH_RECEIVERS_STREAM + "?" + urllib.urlencode(args), skip_encode=True) - cookie_or_auth_header = "Authorization: %s\r\n" % self.service.token + cookie_or_auth_header = "Authorization: Splunk %s\r\n" % \ + (self.service.token if self.service.token is _NoAuthenticationToken + else self.service.token.replace("Splunk ", "")) # If we have cookie(s), use them instead of "Authorization: ..." if self.service.has_cookies(): diff --git a/tests/test_index.py b/tests/test_index.py index 442621470..d6da80b74 100755 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -101,6 +101,18 @@ def test_submit_via_attach(self): cn.close() self.assertEventuallyTrue(lambda: self.totalEventCount() == event_count+1, timeout=60) + def test_submit_via_attach_using_token_header(self): + # Remove the prefix from the token + s = client.connect(**{'token': self.service.token.replace("Splunk ", "")}) + i = s.indexes[self.index_name] + event_count = int(i['totalEventCount']) + if s.has_cookies(): + del s.http._cookies + cn = i.attach() + cn.send("Hello Boris 5!\r\n") + cn.close() + self.assertEventuallyTrue(lambda: self.totalEventCount() == event_count+1, timeout=60) + def test_submit_via_attached_socket(self): event_count = int(self.index['totalEventCount']) f = self.index.attached_socket