From 8643442819716f9a6fd5f09af3f6a97400fd9e9f Mon Sep 17 00:00:00 2001 From: RobeDevOps Date: Tue, 14 Aug 2018 16:49:40 -0400 Subject: [PATCH] added a python script to validate the sonar token before it is generated --- Dockerfile | 3 +- .../scripts/generate_sonar_auth_token.sh | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index f8c815a..8cc7e54 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,8 @@ COPY resources/scriptApproval.xml /usr/share/jenkins/ref/ # Reprotect USER root -RUN chmod +x -R /usr/share/jenkins/ref/adop_scripts/ && chmod +x /entrypoint.sh +RUN chmod +x -R /usr/share/jenkins/ref/adop_scripts/ && \ + chmod +x /entrypoint.sh # USER jenkins # Environment variables diff --git a/resources/scripts/generate_sonar_auth_token.sh b/resources/scripts/generate_sonar_auth_token.sh index 634ff78..292080c 100644 --- a/resources/scripts/generate_sonar_auth_token.sh +++ b/resources/scripts/generate_sonar_auth_token.sh @@ -17,9 +17,31 @@ pretty_sleep() { echo "* Waiting for the Sonar user token api to become available - this can take a few minutes" TOOL_SLEEP_TIME=30 -until [[ $(curl -I -s -u "jenkins":"${SONAR_ACCOUNT_PASSWORD}" -X POST ${SONAR_SERVER_URL}api/user_tokens/generate|head -n 1|cut -d$' ' -f2) == 400 ]]; do pretty_sleep ${TOOL_SLEEP_TIME} Sonar; done +until [[ $(curl -I -s -u jenkins:${SONAR_ACCOUNT_PASSWORD} -X POST ${SONAR_SERVER_URL}api/user_tokens/generate|head -n 1|cut -d$' ' -f2) == 400 ]]; do pretty_sleep ${TOOL_SLEEP_TIME} Sonar; done -SONAR_TOKEN=$(curl -u jenkins:${SONAR_ACCOUNT_PASSWORD} -X POST ${SONAR_SERVER_URL}api/user_tokens/generate?name=jenkins | - python -c 'import sys,json; print(json.load(sys.stdin)["token"])') +# Validating if token already exists: +USER_TOKEN=$(curl -u jenkins:${SONAR_ACCOUNT_PASSWORD} -X POST ${SONAR_SERVER_URL}api/user_tokens/search | +python -c " +import sys, json +def tokenExists(userTokens, token): + if len(userTokens) == 0: return '' + for t in userTokens: + if t['name'] == token: + return t['name'] + return '' +userTokens = json.load(sys.stdin)['userTokens'] +print(tokenExists(userTokens, 'jenkins')) +") + +SONAR_TOKEN="" + +if [[ ! -z $USER_TOKEN ]]; then + SONAR_TOKEN=$USER_TOKEN + echo "Sonar Auth Token exists already" +else + SONAR_TOKEN=$(curl -u jenkins:${SONAR_ACCOUNT_PASSWORD} -X POST ${SONAR_SERVER_URL}api/user_tokens/generate?name=jenkins | + python -c 'import sys,json; print(json.load(sys.stdin)["token"])') + echo "Generated Sonar Auth Token" +fi export SONAR_AUTH_TOKEN=${SONAR_TOKEN} \ No newline at end of file