Skip to content

Commit 4e31346

Browse files
author
Frederick Ross
committed
Replace None with NoAuthenticationToken for Contexts.
Part of a pass to remove all use of None. Remaining Nones come from three sources that I won't or can't fix: 1. The standard library. 2. Default argument values for functions (which is a good semantics for None) 3. Namespaces, which need to be rearchitected entirely to remove them.
1 parent f389e49 commit 4e31346

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

‎splunklib/binding.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ def new_f(*args, **kwargs):
6464
return val
6565
return new_f
6666

67+
# Custom exceptions
6768
class AuthenticationError(Exception):
6869
pass
6970

71+
# Singleton values to eschew None
72+
class NoAuthenticationToken(object):
73+
pass
74+
7075
class UrlEncoded(str):
7176
"""String subclass to represent URL encoded strings.
7277
@@ -207,7 +212,7 @@ def f():
207212
"""
208213
@functools.wraps(request_fun)
209214
def wrapper(self, *args, **kwargs):
210-
if self.token is None:
215+
if self.token is NoAuthenticationToken:
211216
# Not yet logged in.
212217
if self.autologin and self.username and self.password:
213218
# This will throw an uncaught
@@ -379,7 +384,7 @@ class Context(object):
379384
"""
380385
def __init__(self, handler=None, **kwargs):
381386
self.http = HttpLib(handler)
382-
self.token = kwargs.get("token", None)
387+
self.token = kwargs.get("token", NoAuthenticationToken)
383388
self.scheme = kwargs.get("scheme", DEFAULT_SCHEME)
384389
self.host = kwargs.get("host", DEFAULT_HOST)
385390
self.port = int(kwargs.get("port", DEFAULT_PORT))
@@ -400,7 +405,12 @@ def _auth_headers(self):
400405
401406
:returns: A list of 2-tuples containing key and value
402407
"""
403-
return [("Authorization", self.token)]
408+
# Ensure the token is properly formatted
409+
if self.token.startswith('Splunk'):
410+
token = self.token
411+
else:
412+
token = 'Splunk %s' % self.token
413+
return [("Authorization", token)]
404414

405415
def connect(self):
406416
"""Returns an open connection (socket) to the Splunk instance.
@@ -671,7 +681,7 @@ def login(self):
671681
c = binding.Context(...).login()
672682
# Then issue requests...
673683
"""
674-
if self.token is not None and \
684+
if self.token is not NoAuthenticationToken and \
675685
(self.username == "" and self.password == ""):
676686
# If we were passed a session token, but no username or
677687
# password, then login is a nop, since we're automatically
@@ -694,7 +704,7 @@ def login(self):
694704

695705
def logout(self):
696706
"""Forget the current session token."""
697-
self.token = None
707+
self.token = NoAuthenticationToken
698708
return self
699709

700710
def _abspath(self, path_segment,

‎tests/test_binding.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,35 @@ def test_preexisting_token(self):
510510
socket.write("\r\n")
511511
socket.close()
512512

513-
def test_connect_with_preexisting_token_sans_user_and_pass(self):
513+
def test_preexisting_token_sans_splunk(self):
514+
token = self.context.token
515+
if token.startswith('Splunk'):
516+
token = token.split(' ', 1)[1]
517+
else:
518+
self.fail("Token did not start with Splunk.")
519+
opts = self.opts.kwargs.copy()
520+
opts["token"] = token
521+
opts["username"] = "boris the mad baboon"
522+
opts["password"] = "nothing real"
523+
524+
newContext = binding.Context(**opts)
525+
response = newContext.get("/services")
526+
self.assertEqual(response.status, 200)
527+
528+
socket = newContext.connect()
529+
socket.write("POST %s HTTP/1.1\r\n" %\
530+
self.context._abspath("some/path/to/post/to"))
531+
socket.write("Host: %s:%s\r\n" %\
532+
(self.context.host, self.context.port))
533+
socket.write("Accept-Encoding: identity\r\n")
534+
socket.write("Authorization: %s\r\n" %\
535+
self.context.token)
536+
socket.write("X-Splunk-Input-Mode: Streaming\r\n")
537+
socket.write("\r\n")
538+
socket.close()
539+
540+
541+
def test_connect_with_preexisting_token_sans_user_and_pass(self):
514542
token = self.context.token
515543
opts = self.opts.kwargs.copy()
516544
del opts['username']

0 commit comments

Comments
 (0)