From b2c7772ca180ba561018c182eda0240a8b43758b Mon Sep 17 00:00:00 2001 From: bvanroll Date: Wed, 24 Mar 2021 09:40:44 +0100 Subject: [PATCH] imports werken niet voor een of andere reden :( --- setup.py | 2 +- vaultdatabaseengine/__main__.py | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 vaultdatabaseengine/__main__.py diff --git a/setup.py b/setup.py index 8c21cca..b902ba7 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ setuptools.setup( "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], - package_dir={"": "vaultdatabaseengine"}, + package_dir={"": "src"}, packages=setuptools.find_packages(where="src"), python_requires=">=3.6", ) diff --git a/vaultdatabaseengine/__main__.py b/vaultdatabaseengine/__main__.py new file mode 100644 index 0000000..079a489 --- /dev/null +++ b/vaultdatabaseengine/__main__.py @@ -0,0 +1,48 @@ +import requests as rq +import datetime as dt + +class Database2: + 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