Created
October 3, 2017 19:47
-
-
Save InfoSec812/aa246a7ac80d30093fcb660244420901 to your computer and use it in GitHub Desktop.
Programmatically create login token for SonarQube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
/* | |
Execute this file by typing `./sonarqube-auth.groovy TokenName` | |
In order to specify the location of the SonarQube server, export to the environment | |
variable `SONARQUBE_URL` which should contain the URL to the ROOT of the SonarQube web application. | |
Example: | |
export SONARQUBE_URL="http://sonarqube.example.com/" | |
*/ | |
import groovy.json.JsonSlurper | |
def tokenName = args[0] | |
if (!(tokenName)) { | |
println("Token name MUST be specified on the command line.") | |
exit(1) | |
} | |
def sonarHost = System.getenv().get("SONARQUBE_URL") | |
if (!(sonarHost ==~ $/https?://.*/$)) { | |
sonarHost = 'http://localhost:9000/' | |
} | |
println("SonarQube Host: ${sonarHost}") | |
def post = new URL("${sonarHost}api/user_tokens/generate").openConnection() | |
def message = "name=${tokenName}&login=admin" | |
post.setRequestMethod("POST") | |
post.setDoOutput(true) | |
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") | |
def authString = "admin:admin".bytes.encodeBase64().toString() | |
post.setRequestProperty("Authorization", "Basic ${authString}") | |
post.getOutputStream().write(message.getBytes("UTF-8")) | |
def rc = post.getResponseCode() | |
if (rc == 200) { | |
def jsonBody = post.getInputStream().getText() | |
def jsonParser = new JsonSlurper() | |
def data = jsonParser.parseText(jsonBody) | |
def token = data.token | |
println("Auth Token: ${token}") | |
} else { | |
println("Request failed") | |
println(post.getErrorStream().getText()) | |
} |
Use the below command from the command line to generate the user token for SonarQube
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=${user_tokenName}" -u ${username}:${user_password} ${SonarQube_Server_URL}:{Port}/api/user_tokens/generate
Use the below command from the command line to Revoke the user token for SonarQube
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=${user_tokenName}" -u ${username}:${user_password} ${SonarQube_Server_URL}:{Port}/api/user_tokens/revoke
Use the below command from the command line to search the specific user tokens for SonarQube
curl -u ${username}:${user_password} ${SonarQube_Server_URL}:{Port}/api/user_tokens/search
@rajeshpodipati Very useful, thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The eventual goal of this is to be integrated into a Jenkins initialization script so that SonarQube deployments in OpenShift can be completely automated and integrated with SonarQube. If you're interested in trying out The Red Hat Open Innovation Labs CI/CD/PaaS environment, look at https://github.com/rht-labs/labs-ci-cd