Skip to content

Commit 40a2ac6

Browse files
authored
Merge pull request #104 from lalithkota/main
Misc scripts added
2 parents af6d32c + a630e4d commit 40a2ac6

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

misc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Miscellaneous Scripts

misc/remove_email_mappers.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
# --- Env Vars ---
4+
# KEYCLOAK_URL=
5+
# REALM=
6+
# ADMIN_USER=
7+
# ADMIN_PASSWORD=
8+
# MASTER_REALM=
9+
10+
# --- Authentication: Get Admin Access Token ---
11+
TOKEN=$(curl -s -X POST "$KEYCLOAK_URL/realms/$MASTER_REALM/protocol/openid-connect/token" \
12+
-H 'Content-Type: application/x-www-form-urlencoded' \
13+
-d "username=$ADMIN_USER" \
14+
-d "password=$ADMIN_PASSWORD" \
15+
-d 'grant_type=password' \
16+
-d 'client_id=admin-cli')
17+
echo $TOKEN
18+
TOKEN=$(echo $TOKEN | jq -r '.access_token')
19+
20+
if [ -z "$TOKEN" ]; then
21+
echo "Error: Failed to obtain access token."
22+
exit 1
23+
fi
24+
25+
echo "Successfully obtained access token."
26+
27+
# --- Get All Client IDs ---
28+
CLIENT_IDS=$(curl -s -X GET "$KEYCLOAK_URL/admin/realms/$REALM/clients" \
29+
-H "Authorization: Bearer $TOKEN")
30+
echo $CLIENT_IDS
31+
CLIENT_IDS=$(echo $CLIENT_IDS | jq -r '.[].id')
32+
33+
if [ -z "$CLIENT_IDS" ]; then
34+
echo "No clients found in realm '$REALM'."
35+
exit 0
36+
fi
37+
38+
echo "Processing $(echo "$CLIENT_IDS" | wc -l) clients."
39+
40+
# --- Iterate and Delete Mappers ---
41+
for CLIENT_ID in $CLIENT_IDS; do
42+
echo "Checking client ID: $CLIENT_ID"
43+
44+
# Get all mappers for the current client
45+
MAPPERS=$(curl -s -X GET "$KEYCLOAK_URL/admin/realms/$REALM/clients/$CLIENT_ID/protocol-mappers/models" \
46+
-H "Authorization: Bearer $TOKEN" | jq -r '.[] | select(.name | contains("email")) | .id')
47+
48+
if [ -z "$MAPPERS" ]; then
49+
echo " No email mappers found."
50+
continue
51+
fi
52+
53+
# Delete each identified mapper
54+
for MAPPER_ID in $MAPPERS; do
55+
echo " Deleting mapper ID: $MAPPER_ID"
56+
curl -s -X DELETE "$KEYCLOAK_URL/admin/realms/$REALM/clients/$CLIENT_ID/protocol-mappers/models/$MAPPER_ID" \
57+
-H "Authorization: Bearer $TOKEN"
58+
59+
if [ $? -eq 0 ]; then
60+
echo " Mapper deleted successfully."
61+
else
62+
echo " Error deleting mapper."
63+
fi
64+
done
65+
done
66+
67+
echo "Script execution complete."

0 commit comments

Comments
 (0)