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-12 21:03:28 +00:00
|
|
|
import json
|
2022-09-10 12:55:53 +00:00
|
|
|
|
2022-09-12 21:03:28 +00:00
|
|
|
def get_secret_from_bitwarden(id):
|
|
|
|
return command_wrapper(f" item {id}")
|
|
|
|
|
|
|
|
def command_wrapper(command):
|
|
|
|
output = os.os.popen(f"bw {command}")
|
|
|
|
return output
|
2022-09-10 18:16:55 +00:00
|
|
|
|
|
|
|
@kopf.on.startup()
|
|
|
|
def bitwarden_signin(logger, **kwargs):
|
|
|
|
if 'BW_HOST' in os.environ:
|
|
|
|
output = os.popen(f"bw config server {os.getenv('BW_HOST')}")
|
|
|
|
else:
|
|
|
|
logger.info(f"BW_HOST not set. Assuming SaaS installation")
|
2022-09-12 21:03:28 +00:00
|
|
|
command_wrapper("login --apikey")
|
|
|
|
token_output = command_wrapper("unlock --passwordenv BW_PASSWORD")
|
|
|
|
for line in token_output:
|
|
|
|
if "export BW_SESSION" in line:
|
|
|
|
os.popen(line.replace("$", ""))
|
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-09-10 12:55:53 +00:00
|
|
|
def create_fn(spec, name, namespace, logger, **kwargs):
|
|
|
|
|
|
|
|
type = spec.get('type')
|
|
|
|
id = spec.get('id')
|
|
|
|
secret_name = spec.get('name')
|
|
|
|
secret_namespace = spec.get('namespace')
|
|
|
|
|
2022-09-12 21:03:28 +00:00
|
|
|
secret_json_object = json.loads(get_secret_from_bitwarden(id))
|
|
|
|
|
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",
|
|
|
|
"managedObject": name
|
|
|
|
}
|
|
|
|
secret = kubernetes.client.V1Secret()
|
|
|
|
secret.metadata = kubernetes.client.V1ObjectMeta(name=secret_name, annotations=annotations)
|
|
|
|
secret.type = "Opaque"
|
|
|
|
secret.data = {
|
2022-09-12 21:03:28 +00:00
|
|
|
'username': str(base64.b64encode(secret_json_object["login.username"].encode("utf-8")), "utf-8"),
|
|
|
|
'password': str(base64.b64encode(secret_json_object["login.password"].encode("utf-8")), "utf-8")
|
2022-09-10 17:34:07 +00:00
|
|
|
}
|
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-09-10 18:16:55 +00:00
|
|
|
def my_handler(spec, name, namespace, logger, **kwargs):
|
2022-09-10 12:55:53 +00:00
|
|
|
pass
|