2022-09-04 21:21:33 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import kopf
|
2022-09-10 12:55:53 +00:00
|
|
|
import kubernetes
|
2022-09-10 13:23:53 +00:00
|
|
|
import base64
|
2022-09-10 18:16:55 +00:00
|
|
|
import os
|
2022-09-13 21:18:27 +00:00
|
|
|
import subprocess
|
2022-09-12 21:03:28 +00:00
|
|
|
import json
|
2022-09-10 12:55:53 +00:00
|
|
|
|
2022-10-03 14:00:11 +00:00
|
|
|
from pprint import pprint
|
|
|
|
|
2022-09-13 21:18:27 +00:00
|
|
|
def get_secret_from_bitwarden(logger, id):
|
|
|
|
return command_wrapper(logger, f"get item {id}")
|
2022-09-12 21:03:28 +00:00
|
|
|
|
2022-09-13 21:18:27 +00:00
|
|
|
def unlock_bw(logger):
|
|
|
|
token_output = command_wrapper(logger, "unlock --passwordenv BW_PASSWORD")
|
|
|
|
tokens = token_output.split('"')[1::2]
|
|
|
|
os.environ["BW_SESSION"] = tokens[1]
|
|
|
|
logger.info("Signin successful. Session exported")
|
|
|
|
|
|
|
|
def command_wrapper(logger, command):
|
|
|
|
system_env = dict(os.environ)
|
|
|
|
sp = subprocess.Popen([f"bw {command}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True, env=system_env)
|
|
|
|
out, err = sp.communicate()
|
|
|
|
if err:
|
|
|
|
logger.warn(f"Error during bw cli invokement: {err}")
|
2022-09-14 20:18:48 +00:00
|
|
|
return out.decode(encoding='UTF-8')
|
2022-09-10 18:16:55 +00:00
|
|
|
|
|
|
|
@kopf.on.startup()
|
|
|
|
def bitwarden_signin(logger, **kwargs):
|
|
|
|
if 'BW_HOST' in os.environ:
|
2022-09-13 21:18:27 +00:00
|
|
|
command_wrapper(logger, f"config server {os.getenv('BW_HOST')}")
|
2022-09-10 18:16:55 +00:00
|
|
|
else:
|
|
|
|
logger.info(f"BW_HOST not set. Assuming SaaS installation")
|
2022-09-13 21:18:27 +00:00
|
|
|
command_wrapper(logger, "login --apikey")
|
|
|
|
unlock_bw(logger)
|
2022-09-04 21:21:33 +00:00
|
|
|
|
2022-09-09 21:41:06 +00:00
|
|
|
@kopf.on.create('bitwarden-secrets.lerentis.uploadfilter24.eu')
|
2022-10-03 14:00:11 +00:00
|
|
|
def create_managed_secret(spec, name, namespace, logger, body, **kwargs):
|
2022-09-10 12:55:53 +00:00
|
|
|
|
2022-10-03 14:00:11 +00:00
|
|
|
content_def = body['spec']['content']
|
2022-09-10 12:55:53 +00:00
|
|
|
id = spec.get('id')
|
|
|
|
secret_name = spec.get('name')
|
|
|
|
secret_namespace = spec.get('namespace')
|
|
|
|
|
2022-09-13 21:18:27 +00:00
|
|
|
unlock_bw(logger)
|
|
|
|
|
2022-09-14 20:18:48 +00:00
|
|
|
secret_json_object = json.loads(get_secret_from_bitwarden(logger, id))
|
2022-09-12 21:03:28 +00:00
|
|
|
|
2022-09-10 12:55:53 +00:00
|
|
|
api = kubernetes.client.CoreV1Api()
|
|
|
|
|
2022-09-10 18:16:55 +00:00
|
|
|
annotations = {
|
|
|
|
"managed": "bitwarden-secrets.lerentis.uploadfilter24.eu",
|
2022-09-14 20:18:48 +00:00
|
|
|
"managedObject": f"{namespace}/{name}"
|
2022-09-10 18:16:55 +00:00
|
|
|
}
|
|
|
|
secret = kubernetes.client.V1Secret()
|
|
|
|
secret.metadata = kubernetes.client.V1ObjectMeta(name=secret_name, annotations=annotations)
|
|
|
|
secret.type = "Opaque"
|
2022-10-03 14:00:11 +00:00
|
|
|
secret.data = {}
|
|
|
|
for eleml in content_def:
|
|
|
|
for k, elem in eleml.items():
|
|
|
|
for key,value in elem.items():
|
|
|
|
if key == "secretName":
|
|
|
|
_secret_key = value
|
|
|
|
if key == "secretRef":
|
|
|
|
_secret_ref = value
|
|
|
|
|
|
|
|
secret.data[_secret_ref] = str(base64.b64encode(secret_json_object["login"][_secret_key].encode("utf-8")), "utf-8")
|
2022-09-10 12:55:53 +00:00
|
|
|
|
|
|
|
obj = api.create_namespaced_secret(
|
2022-09-10 18:16:55 +00:00
|
|
|
secret_namespace, secret
|
2022-09-10 12:55:53 +00:00
|
|
|
)
|
|
|
|
|
2022-09-10 18:16:55 +00:00
|
|
|
logger.info(f"Secret {secret_namespace}/{secret_name} is created")
|
2022-09-04 21:21:33 +00:00
|
|
|
|
|
|
|
|
2022-09-10 12:55:53 +00:00
|
|
|
@kopf.on.update('bitwarden-secrets.lerentis.uploadfilter24.eu')
|
|
|
|
def my_handler(spec, old, new, diff, **_):
|
|
|
|
pass
|
2022-09-04 21:21:33 +00:00
|
|
|
|
2022-09-10 12:55:53 +00:00
|
|
|
@kopf.on.delete('bitwarden-secrets.lerentis.uploadfilter24.eu')
|
2022-10-03 14:00:11 +00:00
|
|
|
def delete_managed_secret(spec, name, namespace, logger, **kwargs):
|
|
|
|
secret_name = spec.get('name')
|
|
|
|
secret_namespace = spec.get('namespace')
|
|
|
|
api = kubernetes.client.CoreV1Api()
|
|
|
|
|
|
|
|
try:
|
|
|
|
api.delete_namespaced_secret(secret_name, secret_namespace)
|
|
|
|
logger.info(f"Secret {secret_namespace}/{secret_name} has been deleted")
|
|
|
|
except:
|
|
|
|
logger.warn(f"Could not delete secret {secret_namespace}/{secret_name}!")
|