0

with following script I'm able to add provider to my keycloak instance.

embed-server --server-config=standalone.xml
/subsystem=keycloak-server:list-add(name=providers, value=module:com.example.InviteUserRequiredAction)
stop-embedded-server

However when the application is restarted it's added twice (or that many times the server is restarted). I'd like to add the provider only conditionally, but I can't find the way how to query existence of provider that I'm adding.

so finally it could look like:

if (outcome != success) of /subsystem=keycloak-server:here-should-be-the-query
   ...
end-if

Can somebody please tell me how to query the providers with name module:com.example.InviteUserRequiredAction?

Thanks

bilak
  • 4,526
  • 3
  • 35
  • 75
  • What about the list.get operation ? – ehsavoie Jun 30 '21 at 09:15
  • and what is the exact syntax? I was trying `/subsystem=keycloak-server:list-get(name=providers)` and there seems to be some issue. – bilak Jun 30 '21 at 09:23
  • I don't have keycloak installed but if you install your provider at a specific index you could read that index instead – ehsavoie Jun 30 '21 at 09:55
  • Hmm but I don't know the index when installing. And there could be potentially some change. Therefore I'm trying to find out if it's possible to somehow query providers and find if specified name already exists. – bilak Jun 30 '21 at 10:25
  • you could /subsystem=keycloak-server:list-add(name=providers, value=module:com.example.InviteUserRequiredAction, index=0) – ehsavoie Jun 30 '21 at 11:22
  • 1
    I've ended up with sh script...it's hard to work with just cli when there's no supporting method. – bilak Jun 30 '21 at 16:35

1 Answers1

1

In the end I've finished with custom script as I wasn't able to find the way with cli only.

#!/bin/bash
REQUIREDACTION=module:com.example.InviteUserRequiredAction
ADDREQUIREDACTION=$(cat <<EOF
embed-server --server-config=standalone.xml
/subsystem=keycloak-server:list-add(name=providers, value=$REQUIREDACTION)
stop-embedded-server
EOF
);

cat <<EOF > script.cli
embed-server --server-config=standalone.xml
/subsystem=keycloak-server:read-attribute(name=providers)
stop-embedded-server
EOF

OUTPUT=`/opt/jboss/keycloak/bin/jboss-cli.sh --file=script.cli`
echo $OUTPUT | grep -q "$REQUIREDACTION"
if [ $? -ne 0 ] ; then
  echo going to add provider $REQUIREDACTION
  echo "$ADDREQUIREDACTION" > script.cli
  /opt/jboss/keycloak/bin/jboss-cli.sh --file=script.cli
else
  echo provider $REQUIREDACTION already exists
fi
bilak
  • 4,526
  • 3
  • 35
  • 75