Skip to content

Commit

Permalink
Moved gentoken into pkilib.utils
Browse files Browse the repository at this point in the history
  • Loading branch information
r3boot committed Jul 16, 2015
1 parent 68d8b24 commit 539fc04
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkilib/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions pkilib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
.. moduleauthor:: Lex van Roon <[email protected]>
"""

import hashlib
import os
import random
import shlex
import subprocess
import time
Expand Down Expand Up @@ -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()
15 changes: 15 additions & 0 deletions templates/client.template
Original file line number Diff line number Diff line change
@@ -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']}

0 comments on commit 539fc04

Please sign in to comment.