Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-10661] Permissions didn't work with project based matr…
…ix security.

Jenkins core made an incompatible breaking change :-(
Keep compatability with other Jenkins/Hudson versions and the new versions with some ugly reflection.
  • Loading branch information
jtnord committed Oct 7, 2011
1 parent 9c8a83c commit 63f9f3b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
Expand Up @@ -33,32 +33,37 @@
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Run;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import hudson.util.RunList;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletException;

import net.sf.json.JSONObject;

import org.apache.maven.scm.ScmTag;
import org.jvnet.hudson.plugins.m2release.nexus.Stage;
import org.jvnet.hudson.plugins.m2release.nexus.StageClient;
import org.jvnet.hudson.plugins.m2release.nexus.StageException;
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -76,12 +81,13 @@
*/
public class M2ReleaseBuildWrapper extends BuildWrapper {


private transient Logger log = LoggerFactory.getLogger(M2ReleaseBuildWrapper.class);

private transient boolean doRelease = false;
private transient boolean closeNexusStage = true;

private transient String releaseVersion;
private transient String releaseVersion;
private transient String developmentVersion;

private transient boolean appendHudsonBuildNumber;
Expand Down Expand Up @@ -347,13 +353,72 @@ public DescriptorImpl getDescriptor() {

@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {

//public static final PermissionGroup PERMISSIONS = new PermissionGroup(M2ReleaseBuildWrapper.class, Messages._PermissionGroup_Title());
public static final Permission CREATE_RELEASE;

static {
Permission tmpPerm = null;
try {
// Jenkins change the security model in a non backward compatible way :-(
// JENKINS-10661
Class<?> permissionScopeClass = Class.forName("hudson.security.PermissionScope");
Object psArr = Array.newInstance(permissionScopeClass, 2);
Field f;
f = permissionScopeClass.getDeclaredField("JENKINS");
Array.set(psArr, 0, f.get(null));
f = permissionScopeClass.getDeclaredField("ITEM");
Array.set(psArr, 1, f.get(null));

Object[] permissionScopes = (Object[]) psArr;

Constructor<Permission> ctor = Permission.class.getConstructor(PermissionGroup.class,
String.class,
Localizable.class,
Permission.class,
// boolean.class,
permissionScopeClass);
//permissionScopes.getClass());
tmpPerm = ctor.newInstance(Item.PERMISSIONS,
"Release",
Messages._CreateReleasePermission_Description(),
Hudson.ADMINISTER,
// true,
f.get(null));
}
// all these exceptions are Jenkins < 1.421 or Hudson
// wouldn#t multicatch be nice!
catch (NoSuchMethodException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (InvocationTargetException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (IllegalArgumentException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (IllegalAccessException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (InstantiationException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (NoSuchFieldException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
catch (ClassNotFoundException ex) {
LoggerFactory.getLogger(M2ReleaseBuildWrapper.class).warn("Using Legacy Permission as new PermissionScope not detected. {}", ex.getMessage());
}
if (tmpPerm == null) {
tmpPerm = new Permission(Item.PERMISSIONS,
"Release", //$NON-NLS-1$
Messages._CreateReleasePermission_Description(),
Hudson.ADMINISTER);
}
CREATE_RELEASE = tmpPerm;
}

public static final String DEFAULT_RELEASE_GOALS = "-Dresume=false release:prepare release:perform"; //$NON-NLS-1$
public static final Permission CREATE_RELEASE = new Permission(Item.PERMISSIONS,
"Release", //$NON-NLS-1$
Messages._CreateReleasePermission_Description(),
Hudson.ADMINISTER);


public static final boolean DEFAULT_SELECT_CUSTOM_SCM_COMMENT_PREFIX = false;
public static final boolean DEFAULT_SELECT_APPEND_HUDSON_USERNAME = false;
Expand Down
@@ -1,3 +1,4 @@
PermissionGroup.Title=M2Release
CreateReleasePermission.Description=This permission allows users to create releases using the M2Release plugin.
Wrapper.DisplayName=Maven release build
ReleaseAction.perform.release.name=Perform Maven Release
Expand Down

0 comments on commit 63f9f3b

Please sign in to comment.