-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the pipeline steps currentUserGlobalRoles currentUserItemRoles
- Loading branch information
1 parent
f4495e3
commit a5f2767
Showing
13 changed files
with
618 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/org/jenkinsci/plugins/rolestrategy/pipeline/AbstractUserRolesStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.jenkinsci.plugins.rolestrategy.pipeline; | ||
|
||
import com.michelin.cio.hudson.plugins.rolestrategy.Role; | ||
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy; | ||
import com.michelin.cio.hudson.plugins.rolestrategy.RoleMap; | ||
import com.synopsys.arc.jenkins.plugins.rolestrategy.RoleType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.model.Cause; | ||
import hudson.model.Run; | ||
import hudson.model.User; | ||
import hudson.security.ACL; | ||
import hudson.security.AuthorizationStrategy; | ||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.workflow.steps.Step; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution; | ||
import org.springframework.security.core.Authentication; | ||
|
||
/** | ||
* Base class for the pipeline steps. | ||
*/ | ||
public abstract class AbstractUserRolesStep extends Step { | ||
|
||
/** | ||
* Step Execution. | ||
*/ | ||
protected static class Execution extends SynchronousNonBlockingStepExecution<Set<String>> { | ||
protected final RoleType roleType; | ||
|
||
public Execution(@NonNull StepContext context, RoleType roleType) { | ||
super(context); | ||
this.roleType = roleType; | ||
} | ||
|
||
protected RoleMap getRoleMap() throws IOException, InterruptedException { | ||
AuthorizationStrategy strategy = Jenkins.get().getAuthorizationStrategy(); | ||
if (strategy instanceof RoleBasedAuthorizationStrategy) { | ||
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) strategy; | ||
return rbas.getRoleMap(roleType); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected Set<String> run() throws Exception { | ||
Set<String> roleSet = new HashSet<>(); | ||
Authentication auth = getAuthentication(); | ||
if (auth == null) { | ||
return roleSet; | ||
} | ||
RoleMap roleMap = getRoleMap(); | ||
if (roleMap != null) { | ||
if (auth == ACL.SYSTEM2) { | ||
return roleMap.getRoles().stream().map(Role::getName).collect(Collectors.toSet()); | ||
} | ||
return roleMap.getRolesForAuth(auth); | ||
} | ||
return roleSet; | ||
} | ||
|
||
|
||
private Authentication getAuthentication() throws IOException, InterruptedException { | ||
final Run<?, ?> run = Objects.requireNonNull(getContext().get(Run.class)); | ||
Cause.UserIdCause cause = run.getCause(Cause.UserIdCause.class); | ||
if (cause != null) { | ||
User causeUser = User.getById(cause.getUserId(), false); | ||
if (causeUser != null) { | ||
return causeUser.impersonate2(); | ||
} | ||
} | ||
Authentication auth = Jenkins.getAuthentication2(); | ||
if (ACL.isAnonymous2(auth)) { | ||
return null; | ||
} | ||
return auth; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserGlobalRoles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jenkinsci.plugins.rolestrategy.pipeline; | ||
|
||
import com.synopsys.arc.jenkins.plugins.rolestrategy.RoleType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.model.Run; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Pipeline step that returns the users global roles. | ||
*/ | ||
public class UserGlobalRoles extends AbstractUserRolesStep { | ||
|
||
@DataBoundConstructor | ||
public UserGlobalRoles() { | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext context) throws Exception { | ||
return new Execution(context, RoleType.Global); | ||
} | ||
|
||
/** | ||
* The descriptor of the step. | ||
*/ | ||
@Extension | ||
public static final class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
return Collections.singleton(Run.class); | ||
} | ||
|
||
@NonNull | ||
@Override public String getDisplayName() { | ||
return "Current Users Global Roles"; | ||
} | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "currentUserGlobalRoles"; | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.jenkinsci.plugins.rolestrategy.pipeline; | ||
|
||
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy; | ||
import com.michelin.cio.hudson.plugins.rolestrategy.RoleMap; | ||
import com.synopsys.arc.jenkins.plugins.rolestrategy.RoleType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.security.AuthorizationStrategy; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
/** | ||
* Pipeline step that returns the users item roles. | ||
*/ | ||
public class UserItemRoles extends AbstractUserRolesStep { | ||
|
||
private boolean showAllRoles; | ||
|
||
@DataBoundConstructor | ||
public UserItemRoles() { | ||
} | ||
|
||
public boolean isShowAllRoles() { | ||
return showAllRoles; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setShowAllRoles(boolean showAllRoles) { | ||
this.showAllRoles = showAllRoles; | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext context) throws Exception { | ||
return new ItemRolesExecution(context, RoleType.Project, showAllRoles); | ||
} | ||
|
||
/** | ||
* Step Execution. | ||
*/ | ||
public static class ItemRolesExecution extends Execution { | ||
|
||
private final boolean showAllRoles; | ||
|
||
public ItemRolesExecution(@NonNull StepContext context, RoleType roleType, boolean showAllRoles) { | ||
super(context, roleType); | ||
this.showAllRoles = showAllRoles; | ||
} | ||
|
||
@Override | ||
protected RoleMap getRoleMap() throws IOException, InterruptedException { | ||
AuthorizationStrategy strategy = Jenkins.get().getAuthorizationStrategy(); | ||
if (strategy instanceof RoleBasedAuthorizationStrategy) { | ||
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) strategy; | ||
RoleMap roleMap = rbas.getRoleMap(roleType); | ||
if (showAllRoles) { | ||
return roleMap; | ||
} else { | ||
final Run<?, ?> run = Objects.requireNonNull(getContext().get(Run.class)); | ||
Job<?, ?> job = run.getParent(); | ||
return roleMap.newMatchingRoleMap(job.getFullName()); | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* The descriptor. | ||
*/ | ||
@Extension | ||
public static final class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
return Collections.singleton(Run.class); | ||
} | ||
|
||
@NonNull | ||
@Override public String getDisplayName() { | ||
return "Current Users Item Roles"; | ||
} | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "currentUserItemRoles"; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserGlobalRoles/help.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div> | ||
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 <em>System</em> user. This user is | ||
considered as having all roles.<br/> | ||
You can use the <a href="https://plugins.jenkins.io/authorize-project/" target="_blank">Authorize Project</a> plugin | ||
to run the builds as a different user. When running as <em>anonymous</em>, an empty list is returned. | ||
</div> |
6 changes: 6 additions & 0 deletions
6
src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:st="jelly:stapler"> | ||
<f:entry title="Show all roles" field="showAllRoles"> | ||
<f:checkbox/> | ||
</f:entry> | ||
</j:jelly> |
3 changes: 3 additions & 0 deletions
3
...esources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/help-showAllRoles.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
If checked, all item roles of the user are returned. Otherwise only roles matching the pipeline job are returned. | ||
</div> |
7 changes: 7 additions & 0 deletions
7
src/main/resources/org/jenkinsci/plugins/rolestrategy/pipeline/UserItemRoles/help.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div> | ||
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 <em>System</em> user. This user is | ||
considered as having all roles.<br/> | ||
You can use the <a href="https://plugins.jenkins.io/authorize-project/" target="_blank">Authorize Project</a> plugin | ||
to run the builds as a different user. When running as <em>anonymous</em>, an empty list is returned. | ||
</div> |
Oops, something went wrong.