Skip to content

Commit

Permalink
Make JWT functions more generic
Browse files Browse the repository at this point in the history
Config now looks something like:
```
  apijwt:
    auth:
      type: jwt
      url: https://jwt.example.com
      body: '{"client_id":"myclientid","client_secret":"supersecret"}'
      headers:
        content-type: application/json
      field: jwt
```
  • Loading branch information
rickymoorhouse committed Aug 16, 2018
1 parent 5a17979 commit 8fcdc74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
44 changes: 27 additions & 17 deletions hemApp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
import threading
import jwt

hemStore = {'token':None}
class HemStore:
def __init__(self):
self.data = {}
def set(self, key, value):
self.data[key] = value
def get(self, key):
return self.data.get(key, None)

logging.captureWarnings(True)

Expand Down Expand Up @@ -78,11 +84,12 @@ class Check(object):
expected = None
timeout = 10
metrics = None
storage = None
auth = {}
token = None
certificate = None

def __init__(self, name, test, metrics=None):
def __init__(self, name, test, metrics=None, storage=None):
#path, secure=False, verify=True, metrics=None):
self.logger = logging.getLogger(__name__)
self.name = name
Expand All @@ -104,21 +111,24 @@ def __init__(self, name, test, metrics=None):
self.auth = test['auth']

self.metrics = metrics
self.storage = storage

def get_jwt_token(self, url):
global hemStore
j = requests.post(url)
hemStore['token'] = j.json().get('jwt', None)
self.logger.debug("storing token: {}".format(self.token))
def get_jwt(self, auth):
j = requests.post(auth['url'], data=auth['body'], headers=auth['headers'])
self.logger.debug(j.status_code)
self.logger.debug(j.text)
token = j.json().get(auth['field'], None)
self.storage.set(auth.get('key', 'jwt'), token)
self.logger.debug("storing token: {}".format(token))


def is_token_valid(self, url):
global hemStore
if hemStore['token'] == None:
def is_jwt_valid(self, auth):
token = self.storage.get(auth.get('key', 'jwt'))
if token == None:
self.logger.debug("No token - therefore not valid")
return False
else:
decoded = jwt.decode(hemStore['token'], verify=False)
decoded = jwt.decode(token, verify=False)
if time.time() < decoded['exp']:
self.logger.debug("token still valid")
return True
Expand All @@ -131,18 +141,17 @@ def test(self, param, results):
The core testing -
takes in the parameter to test the check with and returns status and time
"""
global hemStore
elapsed_time = timedelta(seconds=0)

if self.auth.get('type', None) == "jwt":
self.logger.info("Using JWT, checking token")
# If we're using JWT, then we need a token
if self.is_token_valid(self.auth.get('url')) == False:
if self.is_jwt_valid(self.auth) == False:
self.logger.info("JWT: token invalid")

# If there is no token or it's expired, then get one
self.get_jwt_token(self.auth.get('url'))
self.headers = {"Authorization":"Bearer {}".format(hemStore['token'])}
self.get_jwt(self.auth)
self.headers = {"Authorization":"Bearer {}".format(self.storage.get(self.auth.get('key', 'jwt')))}
self.logger.info(self.headers)
try:
http_call=getattr(requests,self.method)
Expand Down Expand Up @@ -253,7 +262,7 @@ def initialise_metrics(metricConfig):
)
return metrics_driver.instance(metricConfig)

def run_tests(config, metrics=None):
def run_tests(config, metrics=None, storage=None):
start = time.time()
logging.info("Started tests at {}".format(start))

Expand All @@ -280,7 +289,8 @@ def run_tests(config, metrics=None):
CHECK = Check(
test_name,
test,
metrics)
metrics,
storage)
# test.get('secure',False),
# test.get('verify',True),
results = CHECK.test_list(hosts)
Expand Down
3 changes: 2 additions & 1 deletion hemApp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def main(**kwargs):
logger.info("Frequency is {}".format(frequency))
logger.info(config)
while True:
duration = hemApp.run_tests(config, metrics)
storage = hemApp.HemStore()
duration = hemApp.run_tests(config, metrics, storage)
try:
if int(frequency - duration) > 0:
time.sleep(int(frequency - duration))
Expand Down

0 comments on commit 8fcdc74

Please sign in to comment.