Skip to content

Instantly share code, notes, and snippets.

@InfoSec812
Created October 3, 2017 19:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InfoSec812/aa246a7ac80d30093fcb660244420901 to your computer and use it in GitHub Desktop.
Save InfoSec812/aa246a7ac80d30093fcb660244420901 to your computer and use it in GitHub Desktop.
Programmatically create login token for SonarQube
#!/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())
}
@InfoSec812
Copy link
Author

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

@rajeshpodipati
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment