Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a python script to validate the sonar token before it is generated #65

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}