Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Fix Jenkins on strict mode (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvish authored and takirala committed Mar 13, 2019
1 parent c673080 commit 3320a4f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ COPY conf/nginx/nginx.conf /etc/nginx/nginx.conf
COPY conf/jenkins/config.xml "${JENKINS_STAGING}/config.xml"
COPY conf/jenkins/jenkins.model.JenkinsLocationConfiguration.xml "${JENKINS_STAGING}/jenkins.model.JenkinsLocationConfiguration.xml"
COPY conf/jenkins/nodeMonitors.xml "${JENKINS_STAGING}/nodeMonitors.xml"
COPY scripts/init.groovy.d/mesos-auth.groovy "${JENKINS_STAGING}/init.groovy.d/mesos-auth.groovy"

# add plugins
COPY plugins.conf /tmp/
Expand Down
54 changes: 54 additions & 0 deletions scripts/init.groovy.d/mesos-auth.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.*
import hudson.tasks.*
import jenkins.model.*
import org.jenkinsci.plugins.mesos.MesosCloud

def changePassword = { userName ->
def cloud = MesosCloud.get()
def credentialsId = cloud.getCredentialsId()
def credId = "mesos-${userName}"

if (credentialsId && credentialsId == credId) {
// do nothing if credential already exists
println "--> [mesos] credentials already selected"
} else {
// create a new credential with an expected ID
println "--> [mesos] creating new credentials"
String randomPwd = org.apache.commons.lang.RandomStringUtils.random(9, true, true)

mesosFrameworkCreds = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
"mesos-${userName}",
"mesos authentication",
userName, randomPwd)
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), mesosFrameworkCreds)
cloud.setCredentialsId(mesosFrameworkCreds.getId())
Jenkins.getInstance().save()
cloud.restartMesos()

println "--> [mesos] creating new credentials... done"
}
}

// the env var is set by DCOS when using a service account to run Jenkins
def accountCreds = System.getenv("DCOS_SERVICE_ACCOUNT_CREDENTIAL")
def sleepTimeStr = System.getenv("DCOS_JENKINS_MESOS_PLUGIN_BOOT_TIME")
def sleepTime = sleepTimeStr == null ? 60000 : Integer.parseInt(sleepTimeStr)
if (accountCreds) {
Thread.start {
// wait 60s, this gives the mesos plugin time to start
sleep sleepTime
def credURL = new URL(accountCreds)
def credFile = new File(credURL.toURI())
def credJSON = new groovy.json.JsonSlurper().parseText(credFile.text)
if (credJSON && credJSON.uid) {
changePassword(credJSON.uid)
} else {
println "--> [mesos] Failed to read principal from credentials file"
}
}
} else {
println "--> [mesos] No DC/OS account detected; skipping mesos auth"
}

0 comments on commit 3320a4f

Please sign in to comment.