From 4ac5d9db61c38d8a658ee019b25e46ed4c9611fb Mon Sep 17 00:00:00 2001 From: bvanroll Date: Tue, 23 Mar 2021 12:58:18 +0100 Subject: [PATCH] we're getting somewhere --- setup.py | 2 +- src/python-vault-db/__init__.py | 30 ------------------ vaultdatabaseengine/__init__.py | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 31 deletions(-) delete mode 100644 src/python-vault-db/__init__.py create mode 100644 vaultdatabaseengine/__init__.py diff --git a/setup.py b/setup.py index 11026fe..e2294e1 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ with open("README.md", "r", encoding="utf-8") as fh: long_description = fh.read() setuptools.setup( - name="python-vault-db-bvanroll", # Replace with your own username + name="vaultdatabaseengine-bvanroll", # Replace with your own username version="0.0.1", author="Beppe Vanrolleghem", author_email="beppe.vanrolleghem@gmail.com", diff --git a/src/python-vault-db/__init__.py b/src/python-vault-db/__init__.py deleted file mode 100644 index 3cae3e8..0000000 --- a/src/python-vault-db/__init__.py +++ /dev/null @@ -1,30 +0,0 @@ -import requests as rq - -class Database: - def __init__(self, dbname, vault_url, token): - self.dbname = dbname - self.url = vault_url - self.valid = False - self.lastReq = None #TODO some datetime in here - self.ttl = None - self.token = token - self.username = "" - self.password = "" - self.get_creds() - - def check_valid(self): - #TODO check datetime for current datetime - if self.ttl == None: - return False - return True - - def get_creds(self): - self.check_valid() - if (self.valid): - return {"username": self.username, "password": self.password} - else: - rq.get(url=self.url, headers={"X-Vault-Token":self.token}) - #TODO set datetime for current datetime - #TODO set these to the correct response values - self.username = None - self.password = None \ No newline at end of file diff --git a/vaultdatabaseengine/__init__.py b/vaultdatabaseengine/__init__.py new file mode 100644 index 0000000..fbf6d10 --- /dev/null +++ b/vaultdatabaseengine/__init__.py @@ -0,0 +1,55 @@ +import requests as rq +import datetime as dt + + +class Database: + def __init__(self, dbname, vault_url, token, vault_port=8200): + self.dbname = dbname + self.url = vault_url + ":" + str(vault_port) + "/v1/database/creds/" + dbname + self.lastReq = None #TODO some datetime in here + self.ttl = None + self.token = token + self.username = "" + self.password = "" + self.get_creds() + + def check_valid(self): + #TODO check datetime for current datetime + if self.ttl == None: + return False + return True + + def update_creds(self): + r = rq.get(url=self.url, headers={"X-Vault-Token": self.token, "Content-Type": "application/json"}) + # print(r.status_code) + # print(r.text) + if r.status_code != 200: + raise Exception("status code was nog 200") + data = r.json() + # TODO set datetime for current datetime + # TODO set these to the correct response values + self.username = data["data"]["username"] + self.password = data["data"]["password"] + self.lease_id = data["lease_id"] + self.request_id = data["request_id"] + self.wrap_info = data["wrap_info"] + self.warnings = data["warnings"] + self.auth = data["auth"] + self.ttl = dt.datetime.now() + dt.timedelta(seconds=float(data["lease_duration"])) + + + def get_creds(self): + if not (self.check_valid()): + self.update_creds() + return {"username": self.username, "password": self.password} + + def get_username(self): + if not (self.check_valid()): + self.update_creds() + return self.username + + def get_password(self): + if not (self.check_valid()): + self.update_creds() + return self.password +