diff --git a/pkilib/tests/utils.py b/pkilib/tests/utils.py index fd758ca..5af4fc5 100644 --- a/pkilib/tests/utils.py +++ b/pkilib/tests/utils.py @@ -121,3 +121,8 @@ def test_uname(self): def test_list_command(self): assert utils.run(shlex.split('uname -s')).strip() == os.uname()[0] + + +class test_gentoken: + def test_generates_token(self): + assert len(utils.gentoken()) == 64 diff --git a/pkilib/utils.py b/pkilib/utils.py index e7106dc..bcd64ae 100644 --- a/pkilib/utils.py +++ b/pkilib/utils.py @@ -6,7 +6,9 @@ .. moduleauthor:: Lex van Roon """ +import hashlib import os +import random import shlex import subprocess import time @@ -157,3 +159,18 @@ def gen_enddate(days): days_sec = days * (60*60*24) future_date = time.localtime(time.time() + days_sec) return time.strftime('%Y%m%d%H%M%SZ', future_date) + + +def gentoken(): + """Utility function which generates a token based on a sha256 hash of + a random value. + + >>> gentoken() + '3b2b469df99db2e207cd6232124816caaee8e28401e495627e8209f08426f8d2' + + :returns: Random token + :rtype: str + """ + sha = hashlib.sha256() + sha.update(str(random.random()).encode('utf-8')) + return sha.hexdigest() diff --git a/templates/client.template b/templates/client.template new file mode 100644 index 0000000..4d3dd91 --- /dev/null +++ b/templates/client.template @@ -0,0 +1,15 @@ +--- +# This file contains the configuration for a certificate client + +api: + url: http://${server_host}:${server_port} + token: ${client_token} + +certs: + bits: ${crypto['bits']} + hash: ${crypto['hash']} + country: ${ca['country']} + province: ${ca['province']} + city: ${ca['city']} + organization: ${ca['organization']} + unit: ${ca['unit']}