Skip to content

Instantly share code, notes, and snippets.

@sboardwell
Last active August 4, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sboardwell/1d3a555bd6eaabb49045aaf8ea5a80f4 to your computer and use it in GitHub Desktop.
Save sboardwell/1d3a555bd6eaabb49045aaf8ea5a80f4 to your computer and use it in GitHub Desktop.
// SecurityRealm
import jenkins.model.Jenkins
import hudson.security.SecurityRealm
import org.jenkinsci.plugins.GithubSecurityRealm
clientID = null
clientSecret = null
securityRealm = null
// secret sanity check
def setSecrets(def secretFile) {
def secret = new File(secretFile)
if (secret.exists()) {
def lines = secret.readLines()
if (lines.size() < 2) {
throw new Exception("Secret file doesn't have enough lines. Supposed to have two (clientID and clientSecret).")
}
clientID = lines.get(0)
clientSecret = lines.get(1)
} else {
throw new Exception("Secret file ${secretFileName}")
}
}
setSecrets('/etc/jenkins-secrets/github-client-auth')
securityRealm = new GithubSecurityRealm('https://github.com', 'https://api.github.com', clientID, clientSecret, 'read:org')
//check for equality, no need to modify the runtime if no settings changed
if(!securityRealm.equals(Jenkins.instance.getSecurityRealm())) {
Jenkins.instance.setSecurityRealm(securityRealm)
Jenkins.instance.save()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment