From 7a09045e82c8fe311d12bc832f9e2c453b4e7d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Gourv=C3=A9nec?= Date: Thu, 27 Feb 2025 16:01:54 +0100 Subject: [PATCH] feat(hydra-cleaner): add component --- .../hydra-cleaner/files/hydra-cleaner.sh | 116 ++++++++++++++++++ components/hydra-cleaner/kustomization.yaml | 17 +++ .../resources/hydra-cleaner-cronjob.yaml | 54 ++++++++ 3 files changed, 187 insertions(+) create mode 100644 components/hydra-cleaner/files/hydra-cleaner.sh create mode 100644 components/hydra-cleaner/kustomization.yaml create mode 100644 components/hydra-cleaner/resources/hydra-cleaner-cronjob.yaml diff --git a/components/hydra-cleaner/files/hydra-cleaner.sh b/components/hydra-cleaner/files/hydra-cleaner.sh new file mode 100644 index 0000000..dadfad3 --- /dev/null +++ b/components/hydra-cleaner/files/hydra-cleaner.sh @@ -0,0 +1,116 @@ +#!/bin/sh + +set -e +set -o nounset + +# 4 tables to empty, at least +# oidc, code, flow, authentication_session + +# \d hydra_oauth2_flow +#Referenced by: +# TABLE "hydra_oauth2_access" CONSTRAINT "hydra_oauth2_access_challenge_id_fk" FOREIGN KEY (challenge_id) REFERENCES hydra_oauth2_flow(consent_challenge_id) ON DELETE CASCADE +# TABLE "hydra_oauth2_code" CONSTRAINT "hydra_oauth2_code_challenge_id_fk" FOREIGN KEY (challenge_id) REFERENCES hydra_oauth2_flow(consent_challenge_id) ON DELETE CASCADE +# TABLE "hydra_oauth2_oidc" CONSTRAINT "hydra_oauth2_oidc_challenge_id_fk" FOREIGN KEY (challenge_id) REFERENCES hydra_oauth2_flow(consent_challenge_id) ON DELETE CASCADE +# TABLE "hydra_oauth2_pkce" CONSTRAINT "hydra_oauth2_pkce_challenge_id_fk" FOREIGN KEY (challenge_id) REFERENCES hydra_oauth2_flow(consent_challenge_id) ON DELETE CASCADE +# TABLE "hydra_oauth2_refresh" CONSTRAINT "hydra_oauth2_refresh_challenge_id_fk" FOREIGN KEY (challenge_id) REFERENCES hydra_oauth2_flow(consent_challenge_id) ON DELETE CASCADE + +# -> delete "cascade" on table "flow" cleans access, code, oidc, pkce and refresh tables. + + +DSN="postgresql://${HYDRA_DATABASE_USER}:${HYDRA_DATABASE_PASSWORD}@${HYDRA_DATABASE_SERVICE_NAME}:5432/hydra?sslmode=disable" +RETENTION_HOURS="${RETENTION_HOURS:-48}" +BATCH_SIZE="${BATCH_SIZE:-50}" +LIMIT="${LIMIT:-1000}" +BEFORE_DATE="$(date +'%Y-%m-%d %H:%M:%S' --date=@$(($(date +%s) - RETENTION_HOURS * 3600)))" + + +log() { + echo "$(date +'%d-%m-%y %H:%M:%S%z')| $1" +} + +perror() { + log "Something went wrong, exiting." + trap - EXIT + exit 1 +} + +trap perror EXIT + +if ! [[ ${RETENTION_HOURS} =~ '^[0-9]+$' ]]; then + log "Error: variable RETENTION_HOURS is not a positive integer." + perror +fi + +if ! [[ ${LIMIT} =~ '^[0-9]+$' ]]; then + log "Error: variable LIMIT is not a positive integer." + perror +fi + +if ! [[ ${BATCH_SIZE} =~ '^[0-9]+$' ]]; then + log "Error: variable BATCH_SIZE is not a positive integer." + perror +fi + +log "Starting hydra cleaner" + +log "Removing up to ${LIMIT} elements before ${BEFORE_DATE} by batch of ${BATCH_SIZE}" + +log "Beginning estimated size:" +psql "${DSN}" <