From 277d8122f8fb35194cb26a646c64a28189b6e358 Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Tue, 16 Jul 2024 17:50:03 +0200 Subject: [PATCH] fixed a couple of deprecation warnings (#504) --- .../plugins/sshslaves/SSHConnector.java | 2 +- .../hudson/plugins/sshslaves/SSHLauncher.java | 5 +-- .../plugins/sshslaves/SSHLauncherTest.java | 13 ++++--- .../verifiers/TrustHostKeyActionTest.java | 37 +++++++++---------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/hudson/plugins/sshslaves/SSHConnector.java b/src/main/java/hudson/plugins/sshslaves/SSHConnector.java index 8ed920d7..e98e33d7 100644 --- a/src/main/java/hudson/plugins/sshslaves/SSHConnector.java +++ b/src/main/java/hudson/plugins/sshslaves/SSHConnector.java @@ -301,7 +301,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup context, } return new StandardUsernameListBoxModel() .includeMatchingAs( - ACL.SYSTEM, + ACL.SYSTEM2, context, StandardUsernameCredentials.class, Collections.singletonList(SSHLauncher.SSH_SCHEME), diff --git a/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java b/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java index 6ba1f09e..cf3c3111 100644 --- a/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java +++ b/src/main/java/hudson/plugins/sshslaves/SSHLauncher.java @@ -64,7 +64,6 @@ import hudson.util.FormValidation; import hudson.util.ListBoxModel; import hudson.util.NamingThreadFactory; -import hudson.util.NullStream; import java.util.Collections; import jenkins.model.Jenkins; import org.apache.commons.io.IOUtils; @@ -825,7 +824,7 @@ private void copySlaveJarUsingSCP(TaskListener listener, String workingDirectory } // delete the agent jar as we do with SFTP - connection.exec("rm " + workingDirectory + SLASH_AGENT_JAR, new NullStream()); + connection.exec("rm " + workingDirectory + SLASH_AGENT_JAR, OutputStream.nullOutputStream()); // SCP it to the agent. hudson.Util.ByteArrayOutputStream2 doesn't work for this. It pads the byte array. listener.getLogger().println(Messages.SSHLauncher_CopyingAgentJar(getTimestamp())); @@ -1253,7 +1252,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath AccessControlled co int portValue = Integer.parseInt(port); return new StandardUsernameListBoxModel() .includeMatchingAs( - ACL.SYSTEM, + ACL.SYSTEM2, jenkins, StandardUsernameCredentials.class, Collections.singletonList( diff --git a/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java b/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java index a776fd29..929accae 100644 --- a/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java +++ b/src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java @@ -42,6 +42,7 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.BuildWatcher; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; @@ -78,13 +79,15 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; public class SSHLauncherTest { @ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule public JenkinsRule j = new JenkinsRule(); @@ -167,7 +170,7 @@ private void checkRoundTrip(String host) throws Exception { SSHLauncher launcher = new SSHLauncher(host, 123, "dummyCredentialId"); launcher.setSshHostKeyVerificationStrategy(new KnownHostsFileKeyVerificationStrategy()); assertEquals(host.trim(), launcher.getHost()); - DumbSlave agent = new DumbSlave("agent", j.createTmpDir().getPath(), launcher); + DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher); j.jenkins.addNode(agent); HtmlPage p = j.createWebClient().getPage(agent, "configure"); @@ -251,7 +254,7 @@ public void trackCredentialsWithUsernameAndPassword() throws Exception { launcher.setLaunchTimeoutSeconds(5); launcher.setRetryWaitTime(5); launcher.setMaxNumRetries(2); - DumbSlave agent = new DumbSlave("agent", j.createTmpDir().getPath(), launcher); + DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher); Fingerprint fingerprint = CredentialsProvider.getFingerprintOf(credentials); assertThat("No fingerprint created until use", fingerprint, nullValue()); @@ -279,7 +282,7 @@ public void trackCredentialsWithUsernameAndPrivateKey() throws Exception { launcher.setLaunchTimeoutSeconds(5); launcher.setRetryWaitTime(5); launcher.setMaxNumRetries(2); - DumbSlave agent = new DumbSlave("agent", j.createTmpDir().getPath(), launcher); + DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher); Fingerprint fingerprint = CredentialsProvider.getFingerprintOf(credentials); assertThat("No fingerprint created until use", fingerprint, nullValue()); @@ -456,7 +459,7 @@ private DumbSlave getPermanentAgentHostNotExist() throws Descriptor.FormExceptio launcher.setLaunchTimeoutSeconds(5); launcher.setRetryWaitTime(1); launcher.setMaxNumRetries(3); - return new DumbSlave("agent", j.createTmpDir().getPath(), launcher); + return new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher); } private void fakeCredentials(String id) { diff --git a/src/test/java/hudson/plugins/sshslaves/verifiers/TrustHostKeyActionTest.java b/src/test/java/hudson/plugins/sshslaves/verifiers/TrustHostKeyActionTest.java index b7d90a99..737ea398 100644 --- a/src/test/java/hudson/plugins/sshslaves/verifiers/TrustHostKeyActionTest.java +++ b/src/test/java/hudson/plugins/sshslaves/verifiers/TrustHostKeyActionTest.java @@ -46,7 +46,6 @@ import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; import org.htmlunit.html.HtmlPage; -import hudson.model.Node.Mode; import hudson.plugins.sshslaves.SSHLauncher; import hudson.slaves.DumbSlave; import hudson.slaves.RetentionStrategy; @@ -82,15 +81,15 @@ public void testSubmitNotAuthorised() throws Exception { try { Object server = newSshServer(); assertNotNull(server); - Class keyPairProviderClass = newKeyPairProviderClass(); + Class keyPairProviderClass = newKeyPairProviderClass(); Object provider = newProvider(); assertNotNull(provider); Object factory = newFactory(); assertNotNull(factory); - Class commandFactoryClass = newCommandFactoryClass(); + Class commandFactoryClass = newCommandFactoryClass(); Object commandFactory = newCommandFactory(commandFactoryClass); assertNotNull(commandFactory); - Class commandAuthenticatorClass = newCommandAuthenticatorClass(); + Class commandAuthenticatorClass = newCommandAuthenticatorClass(); Object authenticator = newAuthenticator(commandAuthenticatorClass); assertNotNull(authenticator); @@ -106,9 +105,9 @@ public void testSubmitNotAuthorised() throws Exception { } SSHLauncher launcher = new SSHLauncher("localhost", port, "dummyCredentialId", null, "xyz", null, null, 30, 1, 1, new ManuallyTrustedKeyVerificationStrategy(true)); - DumbSlave agent = new DumbSlave("test-agent", "SSH Test agent", - temporaryFolder.newFolder().getAbsolutePath(), "1", Mode.NORMAL, "", - launcher, RetentionStrategy.NOOP, Collections.emptyList()); + DumbSlave agent = new DumbSlave("test-agent", temporaryFolder.newFolder().getAbsolutePath(), launcher); + agent.setNodeDescription("SSH Test agent"); + agent.setRetentionStrategy(RetentionStrategy.NOOP); jenkins.getInstance().addNode(agent); SlaveComputer computer = (SlaveComputer) jenkins.getInstance().getComputer("test-agent"); @@ -136,7 +135,7 @@ public void testSubmitNotAuthorised() throws Exception { } private Object newSshServer() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Class serverClass; + Class serverClass; try { serverClass = Class.forName("org.apache.sshd.SshServer"); } catch (ClassNotFoundException e) { @@ -146,8 +145,8 @@ private Object newSshServer() throws ClassNotFoundException, NoSuchMethodExcepti return serverClass.getDeclaredMethod("setUpDefaultServer", null).invoke(null); } - private Class newKeyPairProviderClass() throws ClassNotFoundException { - Class keyPairProviderClass; + private Class newKeyPairProviderClass() throws ClassNotFoundException { + Class keyPairProviderClass; try { keyPairProviderClass = Class.forName("org.apache.sshd.common.KeyPairProvider"); } catch (ClassNotFoundException e) { @@ -158,7 +157,7 @@ private Class newKeyPairProviderClass() throws ClassNotFoundException { } private Object newProvider() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Class providerClass; + Class providerClass; try { providerClass = Class.forName("org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider"); } catch (ClassNotFoundException e) { @@ -169,7 +168,7 @@ private Object newProvider() throws ClassNotFoundException, NoSuchMethodExceptio } private Object newFactory() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { - Class factoryClass; + Class factoryClass; try { factoryClass = Class.forName("org.apache.sshd.server.auth.UserAuthPassword$Factory"); } catch (ClassNotFoundException e) { @@ -179,18 +178,18 @@ private Object newFactory() throws ClassNotFoundException, NoSuchMethodException return factoryClass.getConstructor().newInstance(); } - private Class newCommandFactoryClass() throws ClassNotFoundException { + private Class newCommandFactoryClass() throws ClassNotFoundException { return Class.forName("org.apache.sshd.server.command.CommandFactory"); } - private Object newCommandFactory(Class commandFactoryClass) throws ClassNotFoundException, IllegalArgumentException { + private Object newCommandFactory(Class commandFactoryClass) throws ClassNotFoundException, IllegalArgumentException { return java.lang.reflect.Proxy.newProxyInstance( commandFactoryClass.getClassLoader(), new java.lang.Class[]{commandFactoryClass}, (proxy, method, args) -> { if (method.getName().equals("createCommand")) { - Class commandClass; + Class commandClass; try { commandClass = Class.forName("org.apache.sshd.server.command.UnknownCommand"); } catch (ClassNotFoundException e) { @@ -204,8 +203,8 @@ private Object newCommandFactory(Class commandFactoryClass) throws ClassNotFound }); } - private Class newCommandAuthenticatorClass() throws ClassNotFoundException { - Class passwordAuthenticatorClass; + private Class newCommandAuthenticatorClass() throws ClassNotFoundException { + Class passwordAuthenticatorClass; try { passwordAuthenticatorClass = Class.forName("org.apache.sshd.server.PasswordAuthenticator"); } catch(ClassNotFoundException e) { @@ -215,7 +214,7 @@ private Class newCommandAuthenticatorClass() throws ClassNotFoundException { return passwordAuthenticatorClass; } - private Object newAuthenticator(Class passwordAuthenticatorClass) throws ClassNotFoundException, IllegalArgumentException { + private Object newAuthenticator(Class passwordAuthenticatorClass) throws ClassNotFoundException, IllegalArgumentException { return java.lang.reflect.Proxy.newProxyInstance( passwordAuthenticatorClass.getClassLoader(), new java.lang.Class[]{passwordAuthenticatorClass}, @@ -229,7 +228,7 @@ private Object newAuthenticator(Class passwordAuthenticatorClass) throws ClassNo }); } - private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + private Object invoke(Object target, String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { return target.getClass().getMethod(methodName, parameterTypes).invoke(target, args); } }