Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary use of reflection #206

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.security.Permission;
import hudson.util.ReflectionUtils;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.lang.reflect.InvocationTargetException;

/**
* Supports a global default durability level for users
Expand Down Expand Up @@ -75,20 +73,9 @@ public static FlowDurabilityHint[] getDurabilityHintValues() {
}

@NonNull
// TODO: Add @Override when Jenkins core baseline is 2.222+
@Override
public Permission getRequiredGlobalConfigPagePermission() {
return getJenkinsManageOrAdmin();
}

// TODO: remove when Jenkins core baseline is 2.222+
Permission getJenkinsManageOrAdmin() {
Permission manage;
try { // Manage is available starting from Jenkins 2.222 (https://jenkins.io/changelog/#v2.222). See JEP-223 for more info
manage = (Permission) ReflectionUtils.getPublicProperty(Jenkins.get(), "MANAGE");
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
manage = Jenkins.ADMINISTER;
}
return manage;
return Jenkins.MANAGE;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.Permission;
import hudson.util.ReflectionUtils;
import jenkins.model.Jenkins;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.JenkinsSessionRule;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -66,13 +61,6 @@ public void defaultHandling() throws Throwable {
@Test
public void managePermissionShouldAccessGlobalConfig() throws Throwable {
sessions.then(j -> {
Permission jenkinsManage;
try {
jenkinsManage = getJenkinsManage();
} catch (Exception e) {
Assume.assumeTrue("Jenkins baseline is too old for this test (requires Jenkins.MANAGE)", false);
return;
}
final String USER = "user";
final String MANAGER = "manager";
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
Expand All @@ -82,7 +70,7 @@ public void managePermissionShouldAccessGlobalConfig() throws Throwable {

// Read and Manage
.grant(Jenkins.READ).everywhere().to(MANAGER)
.grant(jenkinsManage).everywhere().to(MANAGER)
.grant(Jenkins.MANAGE).everywhere().to(MANAGER)
);

try (ACLContext c = ACL.as(User.getById(USER, true))) {
Expand All @@ -96,10 +84,4 @@ public void managePermissionShouldAccessGlobalConfig() throws Throwable {
}
});
}

// TODO: remove when Jenkins core baseline is 2.222+
private Permission getJenkinsManage() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Jenkins.MANAGE is available starting from Jenkins 2.222 (https://jenkins.io/changelog/#v2.222). See JEP-223 for more info
return (Permission) ReflectionUtils.getPublicProperty(Jenkins.get(), "MANAGE");
}
}