Skip to content

Commit

Permalink
adventures in ChatGPT land: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DGuhr committed Jan 28, 2023
1 parent 7787551 commit 11491fe
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 11 deletions.
57 changes: 57 additions & 0 deletions CHATGPT_GENERATOR.md
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
```

![prompt1 and answer_part_1](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/assets/answer1_p1.png?raw=true)

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:
![answer1_part2_and question2_answer2_part1](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/assets/answer1_p2_and_q2_a2_p1.png?raw=true)

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?
```
![answer2_part1_and_question3](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/assets/answer2_p2_question3.png?raw=true)

and the answer was: yes. yes, sure! And I thought: Good machine!
![question3_p1](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/assets/answer3_p1.png?raw=true)

And it even explained me the script:
![question3_p2_command4](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/assets/question3_p2_command4.png?raw=true)
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ COPY --from=builder /opt/keycloak/lib/quarkus/ /opt/keycloak/lib/quarkus/
COPY --from=builder /opt/keycloak/providers/ /opt/keycloak/providers/
COPY ./initialize-poc.sh /opt/keycloak/bin
COPY ./init.sh /opt/keycloak/bin
COPY ./usergenerator.sh /opt/keycloak/bin

USER root
RUN chmod -R 554 /opt/keycloak/bin/initialize-poc.sh
RUN chmod -R 554 /opt/keycloak/bin/init.sh
RUN chmod -R 554 /opt/keycloak/bin/usergenerator.sh

USER 1000
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ much more. as said highly experimental, dunno where to go with this, but it's fu

# Done
1) extended to (experimentally) handle "add group" events
2) extended to (experimentally) handle "group membership" events
2) extended to (experimentally) handle "group membership" events
3) using ChatGPT to create myself a user generation script for keycloak. More about that [here](https://github.com/DGuhr/keycloak-spicedb-eventlistener/blob/chatgpt_generator/CHATGPT_GENERATOR.md))
Binary file added assets/answer1_p1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer1_p2_and_q2_a2_p1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer2_p2_question3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer3_p1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer3_p2_command4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer4_p1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer4_p2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer5_p2_and_command6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/answer6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/command_5_and_a5_p1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/initial_prompt_w_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 4 additions & 10 deletions initialize-poc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand All @@ -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

20 changes: 20 additions & 0 deletions usergenerator.sh
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."

0 comments on commit 11491fe

Please sign in to comment.