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 #157

Merged
merged 3 commits into from
Apr 27, 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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.289.1</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<useBeta>true</useBeta>
basil marked this conversation as resolved.
Show resolved Hide resolved
</properties>

<dependencyManagement>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/cli/MailCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String getShortDescription() {
}

protected int run() throws Exception {
Jenkins.get().checkPermission(Mailer.DescriptorImpl.getJenkinsManageOrAdmin());
Jenkins.get().checkPermission(Jenkins.MANAGE);
Transport.send(new MimeMessage(Mailer.descriptor().createSession(), stdin));
return 0;
}
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/hudson/tasks/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Util;

import hudson.BulkChange;
import hudson.EnvVars;
Expand All @@ -41,7 +40,6 @@
import jenkins.plugins.mailer.tasks.i18n.Messages;
import hudson.security.Permission;
import hudson.util.FormValidation;
import hudson.util.ReflectionUtils;
import hudson.util.Secret;
import hudson.util.XStream2;

Expand All @@ -53,7 +51,6 @@
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
Expand Down Expand Up @@ -325,20 +322,9 @@ public DescriptorImpl() {
}

@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+
public static 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;
}

public String getDisplayName() {
Expand Down Expand Up @@ -670,7 +656,7 @@ public FormValidation doAddressCheck(@QueryParameter String value) {

@RequirePOST
public FormValidation doCheckSmtpHost(@QueryParameter String value) {
Jenkins.get().checkPermission(getJenkinsManageOrAdmin());
Jenkins.get().checkPermission(Jenkins.MANAGE);
try {
if (Util.fixEmptyAndTrim(value)!=null)
InetAddress.getByName(value);
Expand Down Expand Up @@ -709,7 +695,7 @@ public FormValidation doSendTestMail(
@QueryParameter boolean useSsl, @QueryParameter boolean useTls, @QueryParameter String smtpPort, @QueryParameter String charset,
@QueryParameter String sendTestMailTo) throws IOException {
try {
Jenkins.get().checkPermission(DescriptorImpl.getJenkinsManageOrAdmin());
Jenkins.get().checkPermission(Jenkins.MANAGE);
if (!authentication) {
username = null;
password = null;
Expand Down
20 changes: 1 addition & 19 deletions src/test/java/hudson/tasks/MailerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
import hudson.model.*;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.Permission;
import hudson.slaves.DumbSlave;
import hudson.tasks.Mailer.DescriptorImpl;
import hudson.util.ReflectionUtils;
import hudson.util.Secret;
import org.hamcrest.MatcherAssert;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand Down Expand Up @@ -62,8 +60,6 @@
import jenkins.model.JenkinsLocationConfiguration;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -449,13 +445,6 @@ public void testMigrateOldData() {

@Test
public void managePermissionShouldAccessGlobalConfig() {
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";
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
Expand All @@ -465,7 +454,7 @@ public void managePermissionShouldAccessGlobalConfig() {

// 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 Down Expand Up @@ -513,13 +502,6 @@ public void doCheckSmtpServerShouldNotThrowForUserWithManagePermissions() {
}
}

// 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");
}

private final class TestProject {
private final FakeChangeLogSCM scm = new FakeChangeLogSCM();
private final FreeStyleProject project;
Expand Down