Skip to content

Commit

Permalink
Merge pull request #8 from jafferli/patch-1
Browse files Browse the repository at this point in the history
add support for token based access
  • Loading branch information
zmallen committed Apr 20, 2017
2 parents 8b9019f + ae67a1a commit 22c8cd3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Then, make an object with your base Graylog URL and username and password for au

`api = pygraylog.GraylogAPI('https://localhost:12900', username='foo', password='foo')`

For good security practics, use token based access as following

`api = pygraylog.GraylogAPI('https://localhost:12900', api_key='xopqoiwepmd1o23m9awuaspofp')`

Then, check out `pygraylog/endpoints.py` to see which endpoints are implemented/enforced by this library and make your API call.

`api.search.universal.absolute.get(fields="src_ip", query="*", from_='1970-01-01 00:00:00', to='1970-01-01 23:59:59')`
Expand Down
13 changes: 12 additions & 1 deletion pygraylog/graylogapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from urlparse import urlparse
class GraylogAPI(object):
def __init__(self, url, username=None, password=None, api_key=None):

# check if the username / password vs. api_key cannot be provided at same time
if username is not None and api_key is not None:
raise ValueError('username / api_key cannot be provided at same time')

self.url = url
self._path = urlparse(self.url).path
self.username = username
Expand Down Expand Up @@ -31,7 +36,13 @@ def method(**kwargs):
return self._(name)

def build_auth_header(self):
payload = self.username + ':' + self.password

# use the api_key if it is been provided
if self.api_key is not None:
payload = self.api_key + ':token'
else:
payload = self.username + ':' + self.password

header = {
'Authorization' : 'Basic ' + base64.b64encode(payload),
'Accept' : 'application/json'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name='pygraylog',
version='0.3.1',
version='0.3.2',
description='Graylog API wrapper for python',
author='Zack Allen',
author_email='zma4580@gmail.com',
Expand Down
29 changes: 27 additions & 2 deletions test/unit/test_graylogapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
from pygraylog.pygraylog import graylogapi

def test_obj_creation():
# test based on username / password API end-point access level
api = graylogapi.GraylogAPI('https://test.com/foo/bar/',
username = 'Zack',
password = 'Zack',
api_key = 'ABCDEFG')
api_key = None)
assert api._path == '/foo/bar/'
assert api.username == 'Zack'
assert api.password == 'Zack'
assert api.api_key == 'ABCDEFG'
assert api.api_key is None

# test based on api-key token access level
api = graylogapi.GraylogAPI('https://test.com/foo/bar/',
username = None,
password = None,
api_key = 'ABCDEFG')
assert api._path == '/foo/bar/'
assert api.username is None
assert api.password is None
assert api.api_key == 'ABCDEFG'

def test_obj_path_build():
api = graylogapi.GraylogAPI('https://test.com')
Expand All @@ -23,6 +34,7 @@ def test_obj_path_build():
assert api._path == '/foo/bar/baz/from'

def test_auth_header():
# test based on username / password API end-point access level
api = graylogapi.GraylogAPI('/', username='Zack', password='Zack')
header = api.build_auth_header()
payload = 'Zack' + ':' + 'Zack'
Expand All @@ -31,3 +43,16 @@ def test_auth_header():
'Accept' : 'application/json'
}
assert header == header_test

# test based on api-key token access level
api = graylogapi.GraylogAPI('/',
username = None,
password = None,
api_key = 'ABCDEFG')
header = api.build_auth_header()
payload = 'ABCDEFG' + ':' + 'token'
header_test = {
'Authorization' : 'Basic ' + base64.b64encode(payload),
'Accept' : 'application/json'
}
assert header == header_test

0 comments on commit 22c8cd3

Please sign in to comment.