monitoring: use python dedupe job
This commit is contained in:
parent
a0caeb407c
commit
08716c6be6
@ -2,7 +2,7 @@
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: grafana-user-dedupe-api
|
||||
name: grafana-user-dedupe-api-v2
|
||||
namespace: monitoring
|
||||
annotations:
|
||||
vault.hashicorp.com/agent-inject: "true"
|
||||
@ -20,14 +20,13 @@ spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: dedupe
|
||||
image: alpine:3.20
|
||||
image: python:3.12-slim
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
args:
|
||||
- |
|
||||
set -euo pipefail
|
||||
apk add --no-cache curl jq
|
||||
. /vault/secrets/grafana-env.sh
|
||||
grafana_url="${GRAFANA_URL}"
|
||||
if [ -z "${grafana_url}" ]; then
|
||||
@ -42,17 +41,41 @@ spec:
|
||||
echo "GRAFANA_DEDUPE_EMAILS is required"
|
||||
exit 1
|
||||
fi
|
||||
for email in $(echo "${GRAFANA_DEDUPE_EMAILS}" | tr ',' ' '); do
|
||||
user_id="$(curl -sf -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" \
|
||||
"${grafana_url}/api/users/lookup?loginOrEmail=${email}" | jq -r '.id // empty')"
|
||||
if [ -z "$user_id" ]; then
|
||||
echo "no grafana user found for ${email}"
|
||||
continue
|
||||
fi
|
||||
echo "deleting grafana user ${user_id} (${email})"
|
||||
curl -sf -X DELETE -u "${GRAFANA_USER}:${GRAFANA_PASSWORD}" \
|
||||
"${grafana_url}/api/admin/users/${user_id}"
|
||||
done
|
||||
python - <<'PY'
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
grafana_url = os.environ["GRAFANA_URL"].rstrip("/")
|
||||
user = os.environ["GRAFANA_USER"]
|
||||
password = os.environ["GRAFANA_PASSWORD"]
|
||||
emails = [e.strip() for e in os.environ["GRAFANA_DEDUPE_EMAILS"].split(",") if e.strip()]
|
||||
|
||||
token = base64.b64encode(f"{user}:{password}".encode("utf-8")).decode("utf-8")
|
||||
headers = {"Authorization": f"Basic {token}"}
|
||||
|
||||
def request(method: str, url: str):
|
||||
req = urllib.request.Request(url, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
return resp.read()
|
||||
|
||||
for email in emails:
|
||||
lookup_url = f"{grafana_url}/api/users/lookup?loginOrEmail={urllib.parse.quote(email)}"
|
||||
try:
|
||||
payload = json.loads(request("GET", lookup_url))
|
||||
except Exception:
|
||||
print(f"no grafana user found for {email}")
|
||||
continue
|
||||
user_id = payload.get("id")
|
||||
if not user_id:
|
||||
print(f"no grafana user found for {email}")
|
||||
continue
|
||||
print(f"deleting grafana user {user_id} ({email})")
|
||||
delete_url = f"{grafana_url}/api/admin/users/{user_id}"
|
||||
request("DELETE", delete_url)
|
||||
PY
|
||||
echo "done"
|
||||
env:
|
||||
- name: GRAFANA_URL
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user