Skip to content

Commit

Permalink
added a python script to validate the sonar token before it is generated
Browse files Browse the repository at this point in the history
  • Loading branch information
RobeDevOps authored and rc941t committed Aug 17, 2018
1 parent 20d9627 commit 8643442
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions resources/scripts/generate_sonar_auth_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 8643442

Please sign in to comment.