Skip to content

Instantly share code, notes, and snippets.

@sboardwell
Created August 3, 2019 19:48
Show Gist options
  • Save sboardwell/ed5b0b77370ff132b2b49d355c517faa to your computer and use it in GitHub Desktop.
Save sboardwell/ed5b0b77370ff132b2b49d355c517faa to your computer and use it in GitHub Desktop.
// SecurityRealm
import jenkins.model.Jenkins
import hudson.security.SecurityRealm
import org.jenkinsci.plugins.googlelogin.GoogleOAuth2SecurityRealm
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/google-client-auth')
securityRealm = new GoogleOAuth2SecurityRealm(clientID, clientSecret, "")
//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