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 |
-
Admin
can access all folders and jobs on the controller, due to theOverall | Administer
permission:
-
developerA
can only access jobs in thegroupA
andshared
folders:
-
developerB
can only access jobs in thegroupB
andshared
folders:
-
Read
access 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
develop
role to the correct group. In the following example,groupA
is granted thedevelop
role for the foldergroupA
(a similar configuration is done for thegroupB
andshared
folders):
-
The unexpected behavior is that a job triggered by
developerA
is 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 / Build
permission at the top level of the controller for all users. To grant theJob / Build
permission at the top level of a controller for all users:-
Grant the
develop
role to theshared
group. -
Uncheck the
Propagates
check box when creating the group to ensure the role does not propagate to the child folders. -
Include the
Job / Build
permission in thedevelop
role. 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 Authorization
settings 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 Build
forjobA
andshared
, andRun as Specific User
forjobB
:
After configuring the settings described above, you should achieve the following results:
-
Builds of
jobA
andshared
triggered by a user are run as the user who triggered the build, which means thatdeveloperA
can no longer usejobA
to trigger builds ofjobB
, since they do not have access to that job. TheJenkinsfile
for this example is just one step:build 'groupB/jobB'
-
developerA
can still trigger theshared
job fromjobA
, as expected sincedeveloperA
has access to both of those jobs. TheJenkinsfile
for this example is just one step:build 'shared/sharedJob'
-
jobB
will always run with the access rights ofdeveloperB
since it was configured toRun as Specific User
→developerB
. So even if admin triggersjobB
, it will run as thedeveloperB
user and cannot triggerjobA
sincedeveloperB
does not have access. TheJenkinsfile
for this example is just one step:build 'groupA/jobA'
Any cron triggers of builds or webhooks will run the build as the developerB
user, so carefully consider the user account assigned. TheJenkinsfile
for 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