mirror of
https://github.com/bvanroll/python-vault-db.git
synced 2025-08-29 20:12:43 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
3cf1fa2dbd | |||
f960d43543 | |||
35f3e33cfd | |||
afd725d5b4 | |||
b2c7772ca1 | |||
5fd9987486 | |||
ed19d7468f | |||
7e6cfab28e | |||
d8886090f6 |
3
TODO.md
Normal file
3
TODO.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# TODO
|
||||
|
||||
1: add proper versioning (usage of tags during publish pipeline)
|
8
setup.py
8
setup.py
@@ -4,8 +4,10 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setuptools.setup(
|
||||
name="vault-db-bvanroll", # Replace with your own username
|
||||
name="VaultDb",
|
||||
version="0.0.1",
|
||||
version_config=True,
|
||||
setup_requires=['setuptools-git-versioning'],
|
||||
author="Beppe Vanrolleghem",
|
||||
author_email="beppe.vanrolleghem@gmail.com",
|
||||
description="A vault creds reader for the vault database engine",
|
||||
@@ -20,7 +22,7 @@ setuptools.setup(
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
package_dir={"": "vaultdatabaseengine"},
|
||||
packages=setuptools.find_packages(where="src"),
|
||||
package_dir={"": "src"},
|
||||
packages=setuptools.find_packages(where="VaultDb"),
|
||||
python_requires=">=3.6",
|
||||
)
|
||||
|
48
src/VaultDb/__main__.py
Normal file
48
src/VaultDb/__main__.py
Normal file
@@ -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
|
@@ -2,32 +2,32 @@ import unittest
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
import vaultdatabaseengine
|
||||
import src
|
||||
|
||||
class MyTestCase(unittest.TestCase):
|
||||
def test_psql(self):
|
||||
#TODO exec psql connect
|
||||
print("hi")
|
||||
test = vaultdatabaseengine.Database()
|
||||
test = src.Database()
|
||||
test.check_valid()
|
||||
self.assertEqual(test.valid, False)
|
||||
|
||||
def test_psql_invalid_vault_url(self):
|
||||
#TODO figure out how to fail unit test check
|
||||
try:
|
||||
vaultdatabaseengine.Database(dbname="sdfk", vault_url="localhost", vault_port=8200, token="bla")
|
||||
src.Database(dbname="sdfk", vault_url="localhost", vault_port=8200, token="bla")
|
||||
except:
|
||||
print("did it")
|
||||
return
|
||||
|
||||
def test_psql_invalid_vault_port(self):
|
||||
try:
|
||||
vaultdatabaseengine.Database(dbname="sdfk", vault_url="localhost", vault_port="a", token="bla")
|
||||
src.Database(dbname="sdfk", vault_url="localhost", vault_port="a", token="bla")
|
||||
except:
|
||||
return
|
||||
|
||||
def test_psql_valid(self):
|
||||
print(vaultdatabaseengine.Database(dbname="psql", vault_url="http://localhost", token="s.GoR2nisHPeKU1vOaw9hZ5L7h").get_creds())
|
||||
print(src.Database(dbname="psql", vault_url="http://localhost", token="s.GoR2nisHPeKU1vOaw9hZ5L7h").get_creds())
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user