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()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rajeshpodipati Very useful, thank you.