Issue
Pipeline jobs run as the SYSTEM user by default, and this practice can be problematic when a pipeline is triggered by a specific user. For instance, with the SYSTEM user permissions, Pipeline builds are able to trigger any job in Jenkins, even if the user that triggered the original build does not have access to the downstream (triggered) job (for example if it’s protected using Role-Based Access Control.
Resolution
The best practice would be to use Trigger restrictions to restrict job triggers.
There is also an alternative solution using the plugin Authorize Project plugin, which is a tier 3 community plugin and is subject to the support terms as documented in the CloudBees plugin support policies.
In this example, there is one controller with one admin and two developers, each in different groups, using that controller:
| User | Group(s) |
|---|---|
admin |
admin |
developerA |
groupA shared |
developerB |
groupB shared |
-
Admincan access all folders and jobs on the controller, due to theOverall | Administerpermission:
-
developerAcan only access jobs in thegroupAandsharedfolders:
-
developerBcan only access jobs in thegroupBandsharedfolders:
-
Readaccess is granted to both groups at the top level, but it does not propagate to child folders:
-
Access to each group’s folder is configured by creating a group at the folder level, and granting the
developrole to the correct group. In the following example,groupAis granted thedeveloprole for the foldergroupA(a similar configuration is done for thegroupBandsharedfolders):
-
The unexpected behavior is that a job triggered by
developerAis able to trigger a job ingroupB:
If developerA clicks the link for groupB » jobB #1, they will will be informed they don’t have access because access to that job has been restricted using Role-Based Access Control):
To Restrict jobs to run as a specific user using Role-Based Access Control and the Authorize Project plugin:
-
The example settings for Role-Based Access Control have been demonstrated above. However, if you encounter the message
‘developerA’ lacks permission to run on ‘Jenkins’, ensure you have granted theJob / Buildpermission at the top level of the controller for all users. To grant theJob / Buildpermission at the top level of a controller for all users:-
Grant the
developrole to thesharedgroup. -
Uncheck the
Propagatescheck box when creating the group to ensure the role does not propagate to the child folders. -
Include the
Job / Buildpermission in thedeveloprole. While there are multiple ways to configure this setting, but this is one example:
-
-
If you do not already have the Authorize Project plugin installed, create a backup as per the Backup and Restore guide and install that plugin.
-
Configure the Authorize Project plugin settings at
Manage Jenkins→Configure Global Security. TheProject default Build Authorizationsettings are the settings used for any jobs that are triggered automatically, for example by a cron timer, or via webhooks:
-
For each job, select the appropriate default setting to be used when a user manually starts that build. This example uses
Run as User who Triggered BuildforjobAandshared, andRun as Specific UserforjobB:
After configuring the settings described above, you should achieve the following results:
-
Builds of
jobAandsharedtriggered by a user are run as the user who triggered the build, which means thatdeveloperAcan no longer usejobAto trigger builds ofjobB, since they do not have access to that job. TheJenkinsfilefor this example is just one step:build 'groupB/jobB'
-
developerAcan still trigger thesharedjob fromjobA, as expected sincedeveloperAhas access to both of those jobs. TheJenkinsfilefor this example is just one step:build 'shared/sharedJob'
-
jobBwill always run with the access rights ofdeveloperBsince it was configured toRun as Specific User→developerB. So even if admin triggersjobB, it will run as thedeveloperBuser and cannot triggerjobAsincedeveloperBdoes not have access. TheJenkinsfilefor this example is just one step:build 'groupA/jobA'
Any cron triggers of builds or webhooks will run the build as the developerBuser, so carefully consider the user account assigned. TheJenkinsfilefor this example is just one step:build 'shared/sharedJob'
This example uses the Authorize Project plugin, which is a tier 3 community plugin and is subject to the support terms as documented in the CloudBees plugin support policies.
Tested product/plugin versions
CloudBees CI on Traditional Platforms, version 2.176.4.3 Authorize Project plugin, version 1.3.0