Skip to content

Instantly share code, notes, and snippets.

@jworkmanjc
Last active May 18, 2022 19:36
Show Gist options
  • Save jworkmanjc/dfadfec2c1db7d68bde22037435271b1 to your computer and use it in GitHub Desktop.
Save jworkmanjc/dfadfec2c1db7d68bde22037435271b1 to your computer and use it in GitHub Desktop.
Get userID by searching JC by username and add that user to current system
#!/bin/bash
# example script to check existence of a user
## This value should be encrypted but test with an API Key
APIKEY="YOURAPIKEY"
id_type='"username"'
## This value would have to be scripted
# something like this: stat -f%Su /dev/console
# should get the current logged in user if someone is indeed logged in
username="CurrentLoggedInUserName"
userSearch=$(
curl -s \
-X 'POST' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H "x-api-key: ${APIKEY}" \
-d '{"filter":[{"activated":true, '${id_type}':"'${username}'"}],"fields":["username"]}' \
"https://console.jumpcloud.com/api/search/systemusers"
)
echo $userSearch
regex='[a-zA-Z0-9]{24}'
if [[ $userSearch =~ $regex ]]; then
userID="${BASH_REMATCH[@]}"
fi
## UserID
echo $userID
## Get the JumpCloud SystemID
conf="$(cat /opt/jc/jcagent.conf)"
regex='\"systemKey\":\"[a-zA-Z0-9]{24}\"'
if [[ $conf =~ $regex ]]; then
systemKey="${BASH_REMATCH[@]}"
fi
regex='[a-zA-Z0-9]{24}'
if [[ $systemKey =~ $regex ]]; then
systemID="${BASH_REMATCH[@]}"
echo "$(date "+%Y-%m-%d %H:%M:%S"): JumpCloud systemID found: "$systemID >>"$DEP_N_DEBUG"
else
echo "$(date "+%Y-%m-%d %H:%M:%S"): No systemID found" >>"$DEP_N_DEBUG"
exit 1
fi
## System ID
echo $systemID
## Capture current logFile
logLinesRaw=$(wc -l /var/log/jcagent.log)
logLines=$(echo $logLinesRaw | head -n1 | awk '{print $1;}')
## Bind JumpCloud user to JumpCloud system
userBind=$(
curl -s \
-X 'POST' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'x-api-key: '${APIKEY}'' \
-d '{ "attributes": { "sudo": { "enabled": 'true',"withoutPassword": false}} , "op": "add", "type": "user","id": "'${userID}'"}' \
"https://console.jumpcloud.com/api/v2/systems/${systemID}/associations"
)
# Make this script delete itself
# rm -- "$0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment