Skip to content

Instantly share code, notes, and snippets.

@paladin-dranser
Last active January 14, 2021 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paladin-dranser/5f8ce7e10b48a1b8828ad46841845ef8 to your computer and use it in GitHub Desktop.
Save paladin-dranser/5f8ce7e10b48a1b8828ad46841845ef8 to your computer and use it in GitHub Desktop.
Jenkins: Groovy script to configure Authorize Project settings
/**
* The script removes Build Authorization settings
* (Jenkins - Configure Global Security - Access Control for Builds)
* and creates:
* - Per-project 'Run as Specific User'
* - Default Project 'Run as User Who Triggered Build'
*
* Issue:
* Authorize Project plugin doesn't full support JCasC plugin configuration
* For more details, see: https://github.com/jenkinsci/authorize-project-plugin/pull/44
*/
import jenkins.model.Jenkins
import hudson.util.DescribableList
import jenkins.security.QueueItemAuthenticator
import jenkins.security.QueueItemAuthenticatorDescriptor
import jenkins.security.QueueItemAuthenticatorConfiguration
import org.jenkinsci.plugins.authorizeproject.GlobalQueueItemAuthenticator
import org.jenkinsci.plugins.authorizeproject.ProjectQueueItemAuthenticator
import org.jenkinsci.plugins.authorizeproject.strategy.AnonymousAuthorizationStrategy
import org.jenkinsci.plugins.authorizeproject.strategy.TriggeringUsersAuthorizationStrategy
import org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategy
import org.jenkinsci.plugins.authorizeproject.strategy.SystemAuthorizationStrategy
Jenkins instance = Jenkins.get()
Map<String,Boolean> perProjectStrategyEnabledMap = [
(instance.getDescriptor(AnonymousAuthorizationStrategy.class).getId()): false,
(instance.getDescriptor(TriggeringUsersAuthorizationStrategy.class).getId()): false,
(instance.getDescriptor(SpecificUsersAuthorizationStrategy.class).getId()): true,
(instance.getDescriptor(SystemAuthorizationStrategy.class).getId()): false
]
DescribableList<QueueItemAuthenticator,QueueItemAuthenticatorDescriptor> authenticators = QueueItemAuthenticatorConfiguration.get().getAuthenticators()
authenticators.removeAll { it instanceof QueueItemAuthenticator }
authenticators.add(
new ProjectQueueItemAuthenticator(perProjectStrategyEnabledMap)
)
authenticators.add(
new GlobalQueueItemAuthenticator(
new TriggeringUsersAuthorizationStrategy()
)
)
instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment