+ Returns a list of all global roles of the user that started the build. This includes roles assigned via groups.
+ When the run is triggered by an SCM event or by the timer, the build usually runs as the
System user. This user is
+ considered as having all roles.
+ You can use the
Authorize Project plugin
+ to run the builds as a different user. When running as
anonymous, an empty list is returned.
+
diff --git a/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/config.jelly b/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/config.jelly
new file mode 100644
index 00000000..7d788e67
--- /dev/null
+++ b/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/config.jelly
@@ -0,0 +1,6 @@
+
+
+ If checked, all item roles of the user are returned. Otherwise only roles matching the pipeline job are returned.
+
diff --git a/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/help.html b/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/help.html
new file mode 100644
index 00000000..342eba77
--- /dev/null
+++ b/src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/help.html
@@ -0,0 +1,7 @@
+
+ Returns a list of all item roles of the user that started the build. This includes roles assigned via groups.
+ When the run is triggered by an SCM event or by the timer, the build usually runs as the
System user. This user is
+ considered as having all roles.
+ You can use the
Authorize Project plugin
+ to run the builds as a different user. When running as
anonymous, an empty list is returned.
+
diff --git a/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserGlobalRolesTest.java b/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserGlobalRolesTest.java
new file mode 100644
index 00000000..dc9f43a2
--- /dev/null
+++ b/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserGlobalRolesTest.java
@@ -0,0 +1,115 @@
+package org.jenkinsci.plugins.rolestrategy.pipeline;
+
+import hudson.model.Cause;
+import hudson.model.User;
+import hudson.security.ACL;
+import hudson.security.ACLContext;
+import hudson.triggers.TimerTrigger;
+import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
+import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
+import java.io.IOException;
+import jenkins.security.QueueItemAuthenticatorConfiguration;
+import org.jenkinsci.plugins.authorizeproject.GlobalQueueItemAuthenticator;
+import org.jenkinsci.plugins.authorizeproject.strategy.AnonymousAuthorizationStrategy;
+import org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategy;
+import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
+import org.jenkinsci.plugins.workflow.job.WorkflowJob;
+import org.jenkinsci.plugins.workflow.job.WorkflowRun;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
+
+public class UserGlobalRolesTest {
+ @Rule
+ public JenkinsConfiguredWithCodeRule jenkinsRule = new JenkinsConfiguredWithCodeRule();
+
+ private WorkflowJob pipeline;
+
+ @Before
+ public void setup() throws IOException {
+ DummySecurityRealm securityRealm = jenkinsRule.createDummySecurityRealm();
+ jenkinsRule.jenkins.setSecurityRealm(securityRealm);
+ securityRealm.addGroups("builder1", "readers");
+ securityRealm.addGroups("builder2", "readers");
+ User.getById("builder1", true);
+ User.getById("builder2", true);
+ pipeline = jenkinsRule.createProject(WorkflowJob.class, "pipeline");
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void systemUserHasAllGlobalRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserGlobalRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Global Role: \" + r)\n"
+ + "}", true));
+ WorkflowRun run = jenkinsRule.buildAndAssertSuccess(pipeline);
+ jenkinsRule.assertLogContains("adminRole", run);
+ jenkinsRule.assertLogContains("readonlyRole", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void builderUserHasGlobalRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserGlobalRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Global Role: \" + r)\n"
+ + "}", true));
+ try (ACLContext c = ACL.as(User.getById("builder1", true))) {
+ pipeline.scheduleBuild(0, new Cause.UserIdCause());
+ }
+ jenkinsRule.waitUntilNoActivity();
+ WorkflowRun run = pipeline.getLastBuild();
+ jenkinsRule.assertLogContains("readonlyRole", run);
+ jenkinsRule.assertLogNotContains("adminRole", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void anonymousUserHasNoRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserGlobalRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Global Role: \" + r)\n"
+ + "}\n"
+ + "roles = currentUserItemRoles showAllRoles: true\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ QueueItemAuthenticatorConfiguration.get().getAuthenticators()
+ .add(new GlobalQueueItemAuthenticator(new AnonymousAuthorizationStrategy()));
+ pipeline.scheduleBuild(0, new Cause.UserIdCause());
+ jenkinsRule.waitUntilNoActivity();
+ WorkflowRun run = pipeline.getLastBuild();
+ jenkinsRule.assertLogNotContains("readonlyRole", run);
+ jenkinsRule.assertLogNotContains("adminRole", run);
+ jenkinsRule.assertLogNotContains("builder1Role", run);
+ jenkinsRule.assertLogNotContains("builder2Role", run);
+ jenkinsRule.assertLogNotContains("reader1Role", run);
+ jenkinsRule.assertLogNotContains("reader2Role", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void builderUserHasRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserGlobalRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Global Role: \" + r)\n"
+ + "}\n"
+ + "roles = currentUserItemRoles showAllRoles: true\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ QueueItemAuthenticatorConfiguration.get().getAuthenticators()
+ .add(new GlobalQueueItemAuthenticator(new SpecificUsersAuthorizationStrategy("builder1")));
+ pipeline.scheduleBuild(0, new TimerTrigger.TimerTriggerCause());
+ jenkinsRule.waitUntilNoActivity();
+ WorkflowRun run = pipeline.getLastBuild();
+ jenkinsRule.assertLogContains("readonlyRole", run);
+ jenkinsRule.assertLogNotContains("adminRole", run);
+ jenkinsRule.assertLogContains("builder1Role", run);
+ jenkinsRule.assertLogNotContains("builder2Role", run);
+ jenkinsRule.assertLogContains("reader1Role", run);
+ jenkinsRule.assertLogContains("reader2Role", run);
+ }
+}
diff --git a/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRolesTest.java b/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRolesTest.java
new file mode 100644
index 00000000..98639b12
--- /dev/null
+++ b/src/test/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRolesTest.java
@@ -0,0 +1,103 @@
+package org.jenkinsci.plugins.rolestrategy.pipeline;
+
+import hudson.model.Cause;
+import hudson.model.User;
+import hudson.security.ACL;
+import hudson.security.ACLContext;
+import hudson.triggers.TimerTrigger;
+import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
+import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
+import java.io.IOException;
+import jenkins.security.QueueItemAuthenticatorConfiguration;
+import org.jenkinsci.plugins.authorizeproject.GlobalQueueItemAuthenticator;
+import org.jenkinsci.plugins.authorizeproject.strategy.AnonymousAuthorizationStrategy;
+import org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategy;
+import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
+import org.jenkinsci.plugins.workflow.job.WorkflowJob;
+import org.jenkinsci.plugins.workflow.job.WorkflowRun;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
+
+public class UserItemRolesTest {
+ @Rule
+ public JenkinsConfiguredWithCodeRule jenkinsRule = new JenkinsConfiguredWithCodeRule();
+
+ private WorkflowJob pipeline;
+
+ @Before
+ public void setup() throws IOException {
+ DummySecurityRealm securityRealm = jenkinsRule.createDummySecurityRealm();
+ jenkinsRule.jenkins.setSecurityRealm(securityRealm);
+ securityRealm.addGroups("builder1", "readers");
+ securityRealm.addGroups("builder2", "readers");
+ User.getById("builder1", true);
+ User.getById("builder2", true);
+ pipeline = jenkinsRule.createProject(WorkflowJob.class, "pipeline");
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void systemUserHasAllItemRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserItemRoles showAllRoles: true\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ WorkflowRun run = jenkinsRule.buildAndAssertSuccess(pipeline);
+ jenkinsRule.assertLogContains("builder1Role", run);
+ jenkinsRule.assertLogContains("builder2Role", run);
+ jenkinsRule.assertLogContains("reader1Role", run);
+ jenkinsRule.assertLogContains("reader2Role", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void systemUserHasAllMatchingItemRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserItemRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ WorkflowRun run = jenkinsRule.buildAndAssertSuccess(pipeline);
+ jenkinsRule.assertLogContains("builder1Role", run);
+ jenkinsRule.assertLogNotContains("builder2Role", run);
+ jenkinsRule.assertLogContains("reader1Role", run);
+ jenkinsRule.assertLogNotContains("reader2Role", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void builderUserHasItemRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserItemRoles showAllRoles: true\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ try (ACLContext c = ACL.as(User.getById("builder1", true))) {
+ pipeline.scheduleBuild(0, new Cause.UserIdCause());
+ }
+ jenkinsRule.waitUntilNoActivity();
+ WorkflowRun run = pipeline.getLastBuild();
+ jenkinsRule.assertLogContains("builder1Role", run);
+ jenkinsRule.assertLogNotContains("builder2Role", run);
+ jenkinsRule.assertLogContains("reader1Role", run);
+ jenkinsRule.assertLogContains("reader2Role", run);
+ }
+
+ @Test
+ @ConfiguredWithCode("Configuration-as-Code-pipeline.yml")
+ public void builderUserHasMatchingItemRoles() throws Exception {
+ pipeline.setDefinition(new CpsFlowDefinition("roles = currentUserItemRoles()\n"
+ + "for (r in roles) {\n"
+ + " echo(\"Item Role: \" + r)\n"
+ + "}", true));
+ try (ACLContext c = ACL.as(User.getById("builder1", true))) {
+ pipeline.scheduleBuild(0, new Cause.UserIdCause());
+ }
+ jenkinsRule.waitUntilNoActivity();
+ WorkflowRun run = pipeline.getLastBuild();
+ jenkinsRule.assertLogContains("builder1Role", run);
+ jenkinsRule.assertLogNotContains("builder2Role", run);
+ jenkinsRule.assertLogContains("reader1Role", run);
+ jenkinsRule.assertLogNotContains("reader2Role", run);
+ }
+}
diff --git a/src/test/resources/org/jenkinsci/plugins/rolestrategy/pipeline/Configuration-as-Code-pipeline.yml b/src/test/resources/org/jenkinsci/plugins/rolestrategy/pipeline/Configuration-as-Code-pipeline.yml
new file mode 100644
index 00000000..32e0dd35
--- /dev/null
+++ b/src/test/resources/org/jenkinsci/plugins/rolestrategy/pipeline/Configuration-as-Code-pipeline.yml
@@ -0,0 +1,67 @@
+jenkins:
+ authorizationStrategy:
+ roleBased:
+ roles:
+ global:
+ - name: "adminRole"
+ description: "Jenkins administrators"
+ permissions:
+ - "Overall/Administer"
+ entries:
+ - user: "admin"
+ - name: "readonlyRole"
+ description: "Read-only users"
+ permissions:
+ - "Overall/Read"
+ entries:
+ - group: "readers"
+ items:
+ - name: "builder1Role"
+ description: "build job pipeline"
+ pattern: "^pipeline$"
+ permissions:
+ - "Job/Configure"
+ - "Job/Workspace"
+ - "Job/Read"
+ - "Job/Build"
+ - "Job/Delete"
+ - "Job/Cancel"
+ entries:
+ - user: "builder1"
+ - name: "reader1Role"
+ description: "read job pipeline"
+ pattern: "^pipeline$"
+ permissions:
+ - "Job/Read"
+ entries:
+ - group: "readers"
+ - name: "builder2Role"
+ description: "build job pipeline 2"
+ pattern: "^job-pipeline$"
+ permissions:
+ - "Job/Configure"
+ - "Job/Read"
+ - "Job/Build"
+ - "Job/Delete"
+ entries:
+ - user: "builder2"
+ - name: "reader2Role"
+ description: "read job pipeline 2"
+ pattern: "^job-pipeline$"
+ permissions:
+ - "Job/Read"
+ entries:
+ - group: "readers"
+
+ # System for test
+ securityRealm:
+ local:
+ allowsSignup: false
+ users:
+ - id: "admin"
+ password: "1234"
+ - id: "builder1"
+ password: "builder1"
+ - id: "builder2"
+ password: "builder2"
+