2022-11-12 22:57:42 +00:00
|
|
|
import kopf
|
|
|
|
import kubernetes
|
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
|
2022-11-26 20:33:31 +00:00
|
|
|
from utils.utils import unlock_bw, get_secret_from_bitwarden, parse_login_scope, parse_fields_scope
|
2022-11-12 22:57:42 +00:00
|
|
|
|
2022-12-26 15:29:14 +00:00
|
|
|
|
2022-11-12 22:57:42 +00:00
|
|
|
def create_kv(secret, secret_json, content_def):
|
|
|
|
secret.type = "Opaque"
|
|
|
|
secret.data = {}
|
|
|
|
for eleml in content_def:
|
|
|
|
for k, elem in eleml.items():
|
2022-12-26 15:29:14 +00:00
|
|
|
for key, value in elem.items():
|
2022-11-12 22:57:42 +00:00
|
|
|
if key == "secretName":
|
|
|
|
_secret_key = value
|
|
|
|
if key == "secretRef":
|
|
|
|
_secret_ref = value
|
2022-11-26 20:33:31 +00:00
|
|
|
if key == "secretScope":
|
|
|
|
_secret_scope = value
|
|
|
|
if _secret_scope == "login":
|
2023-01-20 01:17:53 +00:00
|
|
|
value = parse_login_scope(secret_json, _secret_key)
|
|
|
|
if value is None:
|
2023-04-21 12:39:06 +00:00
|
|
|
raise Exception(
|
|
|
|
f"Field {_secret_key} has no value in bitwarden secret")
|
2022-12-26 15:29:14 +00:00
|
|
|
secret.data[_secret_ref] = str(base64.b64encode(
|
2023-01-20 01:17:53 +00:00
|
|
|
value.encode("utf-8")), "utf-8")
|
2022-11-26 20:33:31 +00:00
|
|
|
if _secret_scope == "fields":
|
2023-01-20 01:17:53 +00:00
|
|
|
value = parse_fields_scope(secret_json, _secret_key)
|
|
|
|
if value is None:
|
2023-04-21 12:39:06 +00:00
|
|
|
raise Exception(
|
|
|
|
f"Field {_secret_key} has no value in bitwarden secret")
|
2022-12-26 15:29:14 +00:00
|
|
|
secret.data[_secret_ref] = str(base64.b64encode(
|
2023-01-20 01:17:53 +00:00
|
|
|
value.encode("utf-8")), "utf-8")
|
2022-11-12 22:57:42 +00:00
|
|
|
return secret
|
|
|
|
|
2022-12-26 15:29:14 +00:00
|
|
|
|
2022-11-26 20:33:31 +00:00
|
|
|
@kopf.on.create('bitwarden-secret.lerentis.uploadfilter24.eu')
|
2022-11-12 22:57:42 +00:00
|
|
|
def create_managed_secret(spec, name, namespace, logger, body, **kwargs):
|
|
|
|
|
|
|
|
content_def = body['spec']['content']
|
|
|
|
id = spec.get('id')
|
|
|
|
secret_name = spec.get('name')
|
|
|
|
secret_namespace = spec.get('namespace')
|
|
|
|
|
|
|
|
unlock_bw(logger)
|
2022-11-26 17:55:42 +00:00
|
|
|
logger.info(f"Locking up secret with ID: {id}")
|
|
|
|
secret_json_object = json.loads(get_secret_from_bitwarden(id))
|
2022-11-12 22:57:42 +00:00
|
|
|
|
|
|
|
api = kubernetes.client.CoreV1Api()
|
|
|
|
|
|
|
|
annotations = {
|
2022-11-26 20:33:31 +00:00
|
|
|
"managed": "bitwarden-secret.lerentis.uploadfilter24.eu",
|
2022-11-12 22:57:42 +00:00
|
|
|
"managedObject": f"{namespace}/{name}"
|
|
|
|
}
|
|
|
|
secret = kubernetes.client.V1Secret()
|
2022-12-26 15:29:14 +00:00
|
|
|
secret.metadata = kubernetes.client.V1ObjectMeta(
|
|
|
|
name=secret_name, annotations=annotations)
|
|
|
|
secret = create_kv(secret, secret_json_object, content_def)
|
2022-11-12 22:57:42 +00:00
|
|
|
|
|
|
|
obj = api.create_namespaced_secret(
|
2022-12-26 15:29:14 +00:00
|
|
|
namespace="{}".format(secret_namespace),
|
|
|
|
body=secret
|
2022-11-12 22:57:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
logger.info(f"Secret {secret_namespace}/{secret_name} has been created")
|
|
|
|
|
2022-12-26 15:29:14 +00:00
|
|
|
|
2022-11-26 20:33:31 +00:00
|
|
|
@kopf.on.update('bitwarden-secret.lerentis.uploadfilter24.eu')
|
2022-12-26 15:29:14 +00:00
|
|
|
@kopf.timer('bitwarden-secret.lerentis.uploadfilter24.eu', interval=900)
|
2023-04-21 12:39:06 +00:00
|
|
|
def update_managed_secret(
|
|
|
|
spec,
|
|
|
|
status,
|
|
|
|
name,
|
|
|
|
namespace,
|
|
|
|
logger,
|
|
|
|
body,
|
|
|
|
**kwargs):
|
2022-12-26 15:29:14 +00:00
|
|
|
|
|
|
|
content_def = body['spec']['content']
|
|
|
|
id = spec.get('id')
|
2023-01-20 01:43:25 +00:00
|
|
|
old_config = None
|
|
|
|
old_secret_name = None
|
|
|
|
old_secret_namespace = None
|
|
|
|
if 'kopf.zalando.org/last-handled-configuration' in body.metadata.annotations:
|
2023-04-21 12:39:06 +00:00
|
|
|
old_config = json.loads(
|
|
|
|
body.metadata.annotations['kopf.zalando.org/last-handled-configuration'])
|
2023-01-20 01:43:25 +00:00
|
|
|
old_secret_name = old_config['spec'].get('name')
|
|
|
|
old_secret_namespace = old_config['spec'].get('namespace')
|
2022-12-26 15:29:14 +00:00
|
|
|
secret_name = spec.get('name')
|
|
|
|
secret_namespace = spec.get('namespace')
|
|
|
|
|
2023-04-21 12:39:06 +00:00
|
|
|
if old_config is not None and (
|
|
|
|
old_secret_name != secret_name or old_secret_namespace != secret_namespace):
|
2023-01-19 02:12:13 +00:00
|
|
|
# If the name of the secret or the namespace of the secret is different
|
|
|
|
# We have to delete the secret an recreate it
|
|
|
|
logger.info("Secret name or namespace changed, let's recreate it")
|
2023-04-21 12:39:06 +00:00
|
|
|
delete_managed_secret(
|
|
|
|
old_config['spec'],
|
|
|
|
name,
|
|
|
|
namespace,
|
|
|
|
logger,
|
|
|
|
**kwargs)
|
2023-01-19 02:12:13 +00:00
|
|
|
create_managed_secret(spec, name, namespace, logger, body, **kwargs)
|
|
|
|
return
|
|
|
|
|
2022-12-26 15:29:14 +00:00
|
|
|
unlock_bw(logger)
|
|
|
|
logger.info(f"Locking up secret with ID: {id}")
|
|
|
|
secret_json_object = json.loads(get_secret_from_bitwarden(id))
|
|
|
|
|
|
|
|
api = kubernetes.client.CoreV1Api()
|
|
|
|
|
|
|
|
annotations = {
|
|
|
|
"managed": "bitwarden-secret.lerentis.uploadfilter24.eu",
|
|
|
|
"managedObject": f"{namespace}/{name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
secret = kubernetes.client.V1Secret()
|
|
|
|
secret.metadata = kubernetes.client.V1ObjectMeta(
|
|
|
|
name=secret_name, annotations=annotations)
|
|
|
|
secret = create_kv(secret, secret_json_object, content_def)
|
|
|
|
|
|
|
|
try:
|
|
|
|
obj = api.replace_namespaced_secret(
|
|
|
|
name=secret_name,
|
|
|
|
body=secret,
|
|
|
|
namespace="{}".format(secret_namespace))
|
2023-04-21 12:39:06 +00:00
|
|
|
logger.info(
|
|
|
|
f"Secret {secret_namespace}/{secret_name} has been updated")
|
|
|
|
except BaseException:
|
2022-12-26 15:29:14 +00:00
|
|
|
logger.warn(
|
|
|
|
f"Could not update secret {secret_namespace}/{secret_name}!")
|
|
|
|
|
2022-11-12 22:57:42 +00:00
|
|
|
|
2022-11-26 20:33:31 +00:00
|
|
|
@kopf.on.delete('bitwarden-secret.lerentis.uploadfilter24.eu')
|
2022-11-12 22:57:42 +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)
|
2022-12-26 15:29:14 +00:00
|
|
|
logger.info(
|
|
|
|
f"Secret {secret_namespace}/{secret_name} has been deleted")
|
2023-04-21 12:39:06 +00:00
|
|
|
except BaseException:
|
2022-12-26 15:29:14 +00:00
|
|
|
logger.warn(
|
|
|
|
f"Could not delete secret {secret_namespace}/{secret_name}!")
|