Skip to content

Commit

Permalink
Move the uaa related operation scripts into a sperate package named '…
Browse files Browse the repository at this point in the history
…uaa' for reusing
  • Loading branch information
steven-zou committed Jan 23, 2018
1 parent 4a90d5b commit 0ae6c3e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 21 deletions.
31 changes: 10 additions & 21 deletions jobs/harbor/templates/bin/uaa.sh.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#Import UAA util functions
source /var/vcap/packages/uaa/uaa.sh

#Function is used to register UAA client for harbor
register_harbor_uaa_client() {
####Register Harbor UAA client
Expand All @@ -14,41 +17,27 @@ register_harbor_uaa_client() {

#Get OAuth admin token
log "Getting access token from UAA server..."
access_token=$($CURL_CMD "${UAA_SERVER_ADDRESS}/oauth/token" -X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d "client_id=$UAA_ADMIN&client_secret=$UAA_ADMIN_SECRET&grant_type=client_credentials&token_format=opaque&response_type=token" | \
readJson "access_token")

ACCESS_TOKEN=$(get_uaa_access_token "$CURL_CMD" $UAA_SERVER_ADDRESS $UAA_ADMIN $UAA_ADMIN_SECRET)
log "Got access token:"
echo "$access_token"
echo "$ACCESS_TOKEN"

#Try to get and check if the specified harbor uaa client existing
log "Checking if Harbor UAA client id '$HARBOR_UAA_CLIENT_ID' existing"
harbor_uaa_client_curled=$($CURL_CMD "${UAA_SERVER_ADDRESS}/oauth/clients/$HARBOR_UAA_CLIENT_ID" \
-H "Authorization: Bearer $access_token" \
-H 'Accept: application/json' | \
readJson "client_id")
HARBOR_UAA_CLIENT_CURLED=$(get_uaa_client_info "$CURL_CMD" $UAA_SERVER_ADDRESS $HARBOR_UAA_CLIENT_ID $ACCESS_TOKEN)

#Existing
if [ $harbor_uaa_client_curled = $HARBOR_UAA_CLIENT_ID ]; then
if [ $HARBOR_UAA_CLIENT_CURLED = $HARBOR_UAA_CLIENT_ID ]; then
log "Harbor UAA client id '$HARBOR_UAA_CLIENT_ID' existing, trying to clean"
#Try to delete
$CURL_CMD "${UAA_SERVER_ADDRESS}/oauth/clients/$HARBOR_UAA_CLIENT_ID" -X DELETE \
-H "Authorization: Bearer $access_token" \
-H 'Accept: application/json' | readJson "client_id" >/dev/null
delete_uaa_client "$CURL_CMD" $UAA_SERVER_ADDRESS $HARBOR_UAA_CLIENT_ID $ACCESS_TOKEN
fi


#Create harbor UAA client
uaa_properties=$(cat $UAA_JSON_FILE)
UAA_PROPERTIES=$(cat $UAA_JSON_FILE)

log "Registering Harbor UAA client '$HARBOR_UAA_CLIENT_ID'..."
$CURL_CMD "${UAA_SERVER_ADDRESS}/oauth/clients" -X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $access_token" \
-H 'Accept: application/json' \
-d "$uaa_properties" | readJson "client_id" >/dev/null
register_uaa_client "$CURL_CMD" $UAA_SERVER_ADDRESS $ACCESS_TOKEN "$UAA_PROPERTIES"

log "Harbor UAA client '$HARBOR_UAA_CLIENT_ID' is successfully registered!"
###Register end
Expand Down
4 changes: 4 additions & 0 deletions packages/uaa/packaging
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e

cp -a uaa/* ${BOSH_INSTALL_TARGET}
chmod +x ${BOSH_INSTALL_TARGET}/*.sh
7 changes: 7 additions & 0 deletions packages/uaa/spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: uaa

dependencies: []

files:
- uaa/uaa.sh
95 changes: 95 additions & 0 deletions src/uaa/uaa.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#readJson is util function in common package

#Get access token of UAA
#Access token will be returned if succeed
#e.g: get_uaa_access_token 'curl -k' 'http://localhost/uaa' 'admin' 'secret'
get_uaa_access_token() {
#Four parameters
#curl command, UAA address, UAA admin client and admin client secret are requried
if [ $# -lt 4 ]; then
return 1
fi

curl_command=$1
uaa_address=$2
uaa_admin=$3
uaa_admin_secret=$4

access_token=$($curl_command "$uaa_address/oauth/token" -X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d "client_id=$uaa_admin&client_secret=$uaa_admin_secret&grant_type=client_credentials&token_format=opaque&response_type=token" | \
readJson "access_token")

ret=$?
echo $access_token
return $ret
}

#Get info of specified client ID
#Client ID or empty string will be returned
#e.g: get_uaa_client_info 'curl -k' 'http://localhost/uaa' 'harbor_uaa_client_id' '5166910e847b4401a91f88e65e76c366'
get_uaa_client_info() {
#Four parameters
#curl command, UAA address, client ID and bearer token are requried
if [ $# -lt 4 ]; then
return 1
fi

curl_command=$1
uaa_address=$2
client_id=$3
bearer_token=$4

client_id_curled=$($curl_command "$uaa_address/oauth/clients/$client_id" \
-H "Authorization: Bearer $bearer_token" \
-H 'Accept: application/json' | \
readJson "client_id")

echo $client_id_curled
}

#Delete the specified client from UAA server
#e.g: delete_uaa_client 'curl -k' 'http://localhost/uaa' 'harbor_uaa_client_id' '5166910e847b4401a91f88e65e76c366'
delete_uaa_client() {
#Four parameters
#curl command, UAA address, client ID and bearer token are requried
if [ $# -lt 4 ]; then
return 1
fi

curl_command=$1
uaa_address=$2
client_id=$3
bearer_token=$4

$curl_command "$uaa_address/oauth/clients/$client_id" -X DELETE \
-H "Authorization: Bearer $bearer_token" \
-H 'Accept: application/json' | readJson "client_id" >/dev/null

return $?
}

#Register the UAA client to UAA server
#e.g: register_uaa_client 'curl -k' 'http://localhost/uaa' '5166910e847b4401a91f88e65e76c366' '{}'
register_uaa_client() {
#Four parameters
#curl command, UAA address, bearer token and uaa properties json are requried
if [ $# -lt 4 ]; then
return 1
fi

curl_command=$1
uaa_address=$2
bearer_token=$3
uaa_properties=$4

$curl_command "$uaa_address/oauth/clients" -X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $bearer_token" \
-H 'Accept: application/json' \
-d "$uaa_properties" | readJson "client_id" >/dev/null

return $?
}

0 comments on commit 0ae6c3e

Please sign in to comment.