-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
85 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Convince ChatGPT to do bash scripting for me | ||
|
||
## The problem | ||
I freely admit it: **I don't like bash scripting.** Yes, it's out, chat scripting is my nemesis, i find these scripts are verbose and ugly for me, no types, just... go away. | ||
|
||
Don't get me wrong, I usualy _**can**_ script in bash or any shell, but not without constant googling, headaches and urges to throw my laptop out of the window. | ||
|
||
So... long story short, i thought, why not let ChatGPT, the newest hype in town, do that for me? | ||
Well, here's how it went: | ||
|
||
## Prompts: | ||
I created a very quick example from the existing [initialize-poc.sh](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/77875514d23956e5b689a4da87d4c4c8b438194f/initialize-poc.sh#L45-L46) at that state - initially i wanted ChatGPT to "just" generate example commands, because I am lazy. Here's the prompt I initially used: | ||
``` | ||
chatgpt data generator prompt kcadm: | ||
I want you to create randomized test data for kcadm.sh, an administration CLI tool for an IAM open source software named keycloak. | ||
To add a user to keycloak using kcadm.sh, the following prompt is needed: | ||
/opt/keycloak/bin/kcadm.sh create users -r <realm> -s username=<name> -s firstName=<firstname> -s lastName=<lastname> -s enabled=true -s email=<email> -s emailVerified=true -s "attributes.org_id=<orgid>" | ||
an example is | ||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=paula -s firstName=Paula -s lastName=Von -s enabled=true -s [email protected] -s emailVerified=true -s "attributes.org_id=12345" | ||
after these users are created, a password is set using the following prompt for every user: | ||
/opt/keycloak/bin/kcadm.sh set-password -r <realm> --username <username> --new-password <newpassword> | ||
so the full example would be | ||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=paula -s firstName=Paula -s lastName=Von -s enabled=true -s [email protected] -s emailVerified=true -s "attributes.org_id=12345" | ||
/opt/keycloak/bin/kcadm.sh set-password -r master --username paula --new-password demo1234! | ||
Create example kcadm commands for creating users and password for 10 users for me with random values for email, firstName, username, lastName, attributes.org_id and new-password | ||
``` | ||
|
||
 | ||
|
||
That answer contained only 7 answers, so... i "decently" said: hey ChatGPT, y u no 10?! | ||
|
||
``` | ||
these ar eonly seven | ||
``` | ||
Also it was saturday morning, so i couldn't even type right. Well, things happen. Luckily ChatGPT had enough to do with us dumb humans so far, so it understood me and created the other 3 examples: | ||
 | ||
|
||
Ok... I was interested, to be fair. Now it's still saturday, and I wanted to go bouldering and later there's this poetry slam, and generating example prompts took time, and so... could we make this even more generic? I asked simply: | ||
``` | ||
can you create a script that allows to add as much users as i want? | ||
``` | ||
 | ||
|
||
and the answer was: yes. yes, sure! And I thought: Good machine! | ||
 | ||
|
||
And it even explained me the script: | ||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,14 +40,8 @@ echo "Success! Now adding test-events to master-realm and initializing scheme... | |
|
||
sleep 5 | ||
echo "Success! Also waited 5 seconds to allow initializing the spiceDB schema." | ||
echo "Now adding users to master-realm including org_id field..." | ||
# Users | ||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=paula -s firstName=Paula -s lastName=Von -s enabled=true -s [email protected] -s "attributes.org_id=12345" | ||
/opt/keycloak/bin/kcadm.sh set-password -r master --username paula --new-password demo1234! | ||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=peter -s firstName=Peter -s lastName=Anderson -s enabled=true -s [email protected] -s "attributes.org_id=12345" | ||
/opt/keycloak/bin/kcadm.sh set-password -r master --username peter --new-password demo1234! | ||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=richard -s firstName=Richard -s lastName=Miles -s enabled=true -s [email protected] -s "attributes.org_id=23456" | ||
/opt/keycloak/bin/kcadm.sh set-password -r master --username richard --new-password demo1234! | ||
# echo "Now adding users to master-realm including org_id field. Thank you, ChatGPT... :-D" | ||
/opt/keycloak/bin/usergenerator.sh 10 | ||
|
||
echo "Now adding an org/ tenant id to the admin, needed for creating groups that are tied to an org (we need the orgid of the creating person there)" | ||
|
||
|
@@ -60,7 +54,7 @@ echo "Now that we simulate an orgs admin, we can let them create a group that sh | |
/opt/keycloak/bin/kcadm.sh create groups -r master -s name=MyGroup | ||
|
||
echo "ok now lets add a user to the previously created group... in order to do that we need the userId and the groupId." | ||
paulaUid=$(/opt/keycloak/bin//kcadm.sh get users -r master -q username=paula --fields=id | awk -F':' '{print $2}' | grep . | tr -d "\"" | sed -e 's/^[[:space:]]*//') | ||
user1Uid=$(/opt/keycloak/bin//kcadm.sh get users -r master -q username=$username1 --fields=id | awk -F':' '{print $2}' | grep . | tr -d "\"" | sed -e 's/^[[:space:]]*//') | ||
myGroupUid=$(/opt/keycloak/bin//kcadm.sh get groups -r master -q name=MyGroup --fields=id | awk -F':' '{print $2}' | grep . | tr -d "\"" | sed -e 's/^[[:space:]]*//') | ||
/opt/keycloak/bin/kcadm.sh update users/$paulaUid/groups/$myGroupUid -r master -s realm=master -s userId=$paulaUid -s groupId=$myGroupUid -n | ||
/opt/keycloak/bin/kcadm.sh update users/$user1Uid/groups/$myGroupUid -r master -s realm=master -s userId=$user1Uid -s groupId=$myGroupUid -n | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash | ||
|
||
num_users=$1 | ||
org_ids=("$(cat /dev/urandom | tr -dc '0-9' | fold -w 8 | head -n 1)" "$(cat /dev/urandom | tr -dc '0-9' | fold -w 8 | head -n 1)" "$(cat /dev/urandom | tr -dc '0-9' | fold -w 8 | head -n 1)") | ||
|
||
for i in $(seq 1 $num_users) | ||
do | ||
username="user$i" | ||
firstName="First$i" | ||
lastName="Last$i" | ||
email="$username@demo.com" | ||
org_id=${org_ids[$((RANDOM % 3))]} | ||
password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1) | ||
|
||
/opt/keycloak/bin/kcadm.sh create users -r master -s username=$username -s firstName=$firstName -s lastName=$lastName -s enabled=true -s email=$email -s emailVerified=true -s "attributes.org_id=$org_id" | ||
/opt/keycloak/bin/kcadm.sh set-password -r master --username $username --new-password $password | ||
eval "username$i=$username" | ||
done | ||
|
||
echo "Added $num_users users." |