This commit is contained in:
35
src/kv.py
35
src/kv.py
@ -21,13 +21,15 @@ def create_kv(secret, secret_json, content_def):
|
||||
if _secret_scope == "login":
|
||||
value = parse_login_scope(secret_json, _secret_key)
|
||||
if value is None:
|
||||
raise Exception(f"Field {_secret_key} has no value in bitwarden secret")
|
||||
raise Exception(
|
||||
f"Field {_secret_key} has no value in bitwarden secret")
|
||||
secret.data[_secret_ref] = str(base64.b64encode(
|
||||
value.encode("utf-8")), "utf-8")
|
||||
if _secret_scope == "fields":
|
||||
value = parse_fields_scope(secret_json, _secret_key)
|
||||
if value is None:
|
||||
raise Exception(f"Field {_secret_key} has no value in bitwarden secret")
|
||||
raise Exception(
|
||||
f"Field {_secret_key} has no value in bitwarden secret")
|
||||
secret.data[_secret_ref] = str(base64.b64encode(
|
||||
value.encode("utf-8")), "utf-8")
|
||||
return secret
|
||||
@ -66,7 +68,14 @@ def create_managed_secret(spec, name, namespace, logger, body, **kwargs):
|
||||
|
||||
@kopf.on.update('bitwarden-secret.lerentis.uploadfilter24.eu')
|
||||
@kopf.timer('bitwarden-secret.lerentis.uploadfilter24.eu', interval=900)
|
||||
def update_managed_secret(spec, status, name, namespace, logger, body, **kwargs):
|
||||
def update_managed_secret(
|
||||
spec,
|
||||
status,
|
||||
name,
|
||||
namespace,
|
||||
logger,
|
||||
body,
|
||||
**kwargs):
|
||||
|
||||
content_def = body['spec']['content']
|
||||
id = spec.get('id')
|
||||
@ -74,17 +83,24 @@ def update_managed_secret(spec, status, name, namespace, logger, body, **kwargs)
|
||||
old_secret_name = None
|
||||
old_secret_namespace = None
|
||||
if 'kopf.zalando.org/last-handled-configuration' in body.metadata.annotations:
|
||||
old_config = json.loads(body.metadata.annotations['kopf.zalando.org/last-handled-configuration'])
|
||||
old_config = json.loads(
|
||||
body.metadata.annotations['kopf.zalando.org/last-handled-configuration'])
|
||||
old_secret_name = old_config['spec'].get('name')
|
||||
old_secret_namespace = old_config['spec'].get('namespace')
|
||||
secret_name = spec.get('name')
|
||||
secret_namespace = spec.get('namespace')
|
||||
|
||||
if old_config is not None and (old_secret_name != secret_name or old_secret_namespace != secret_namespace):
|
||||
if old_config is not None and (
|
||||
old_secret_name != secret_name or old_secret_namespace != secret_namespace):
|
||||
# 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")
|
||||
delete_managed_secret(old_config['spec'], name, namespace, logger, **kwargs)
|
||||
delete_managed_secret(
|
||||
old_config['spec'],
|
||||
name,
|
||||
namespace,
|
||||
logger,
|
||||
**kwargs)
|
||||
create_managed_secret(spec, name, namespace, logger, body, **kwargs)
|
||||
return
|
||||
|
||||
@ -109,8 +125,9 @@ def update_managed_secret(spec, status, name, namespace, logger, body, **kwargs)
|
||||
name=secret_name,
|
||||
body=secret,
|
||||
namespace="{}".format(secret_namespace))
|
||||
logger.info(f"Secret {secret_namespace}/{secret_name} has been updated")
|
||||
except:
|
||||
logger.info(
|
||||
f"Secret {secret_namespace}/{secret_name} has been updated")
|
||||
except BaseException:
|
||||
logger.warn(
|
||||
f"Could not update secret {secret_namespace}/{secret_name}!")
|
||||
|
||||
@ -125,6 +142,6 @@ def delete_managed_secret(spec, name, namespace, logger, **kwargs):
|
||||
api.delete_namespaced_secret(secret_name, secret_namespace)
|
||||
logger.info(
|
||||
f"Secret {secret_namespace}/{secret_name} has been deleted")
|
||||
except:
|
||||
except BaseException:
|
||||
logger.warn(
|
||||
f"Could not delete secret {secret_namespace}/{secret_name}!")
|
||||
|
Reference in New Issue
Block a user