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

test: replace trilead with connectbot/sshlib #19

Closed
Closed
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
21 changes: 19 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<revision>1.0.14</revision>
<changelist>-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/trilead-api-plugin</gitHubRepo>
<jenkins.version>2.204</jenkins.version>
<jenkins.version>2.204.3</jenkins.version>
<java.level>8</java.level>
</properties>

Expand Down Expand Up @@ -81,11 +81,28 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.connectbot</groupId>
<artifactId>sshlib</artifactId>
<version>2.2.16-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.crypto.tink</groupId>
<artifactId>tink</artifactId>
<version>1.4.0</version>
</dependency>
<!--
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build-217-jenkins-27</version>
</dependency>
</dependency>-->
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>trilead-putty-extension</artifactId>
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/com/trilead/ssh2/ConnectionJenkins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.trilead.ssh2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.trilead.ssh2.Connection;

public class ConnectionJenkins extends Connection{

public ConnectionJenkins(String hostname) {
super(hostname);
}

public ConnectionJenkins(String hostname, int port) {
super(hostname, port);
}

/**
* Executes a process remotely and blocks until its completion.
*
* @param command the command
* @param output The stdout/stderr will be sent to this stream.
* @return the int
* @throws IOException the io exception
* @throws InterruptedException the interrupted exception
*/
public int exec(String command, OutputStream output) throws IOException, InterruptedException {
Session session = openSession();
try {
session.execCommand(command);
PumpThread t1 = new PumpThread(session.getStdout(), output);
t1.start();
PumpThread t2 = new PumpThread(session.getStderr(), output);
t2.start();
session.getStdin().close();
t1.join();
t2.join();

// wait for some time since the delivery of the exit status often gets delayed
session.waitForCondition(ChannelCondition.EXIT_STATUS,3000);
Integer r = session.getExitStatus();
if(r!=null) return r.intValue();
return -1;
} finally {
session.close();
}
}

/**
* Pumps {@link InputStream} to {@link OutputStream}.
*
* @author Kohsuke Kawaguchi
*/
private static final class PumpThread extends Thread {
private final InputStream in;
private final OutputStream out;

/**
* Instantiates a new Pump thread.
*
* @param in the in
* @param out the out
*/
public PumpThread(InputStream in, OutputStream out) {
super("pump thread");
this.in = in;
this.out = out;
}

public void run() {
byte[] buf = new byte[1024];
try {
while(true) {
int len = in.read(buf);
if(len<0) {
in.close();
return;
}
out.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/trilead/ssh2/SessionJenkins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.trilead.ssh2;

import java.io.IOException;
import java.io.OutputStream;
import java.security.SecureRandom;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.channel.ChannelManager;

public class SessionJenkins extends Session{

SessionJenkins(ChannelManager cm, SecureRandom rnd) throws IOException {
super(cm, rnd);
}

/**
* Write stdout received from the other side to the specified {@link OutputStream}.
*
* <p>
* By default, when data arrives from the other side, trilead buffers them and lets
* you read it at your convenience from {@link #getStdout()}. This is normally convenient,
* but if all you are doing is to send the data to another {@link OutputStream} by
* copying a stream, then you'll be end up wasting a thread just for this.
* In such a situation, you can call this method and let the I/O handling thread of trilead
* directly pass the received data to the output stream. This also eliminates the internal
* buffer used for spooling.
*
* <p>
* When you do this, beware of a blocking write! If a write blocks, it'll affect
* all the other channels and sessions that are sharing the same SSH connection,
* as there's only one I/O thread. For example, this can happen if you are writing to
* {@link Socket}.
*
* <p>
* If any data has already been received and spooled before calling this method,
* the data will be sent to the given stream immediately.
*
* <p>
* To signal the end of the stream, when the other side notifies us of EOF or when
* the channel closes, the output stream gets closed. If this is not desirable,
* you must wrap the output stream and ignore the {@link OutputStream#close()} call.
*
* @param os the os
* @throws IOException the io exception
*/
/*
public void pipeStdout(OutputStream os) throws IOException {
cn.pipeStdoutStream(os);
cn.getStdoutStream().
}*/

/**
* The same as {@link #pipeStdout(OutputStream)} except for stderr, not for stdout.
*
* @param os the os
* @throws IOException the io exception
*/
/*
public void pipeStderr(OutputStream os) throws IOException {
cn.pipeStderrStream(os);
}*/
}
20 changes: 20 additions & 0 deletions src/main/java/com/trilead/ssh2/crypto/CertificateDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trilead.ssh2.crypto;

import java.io.IOException;
import java.security.KeyPair;

/**
* @author Michael Clarke
*/
public abstract class CertificateDecoder {

public abstract String getStartLine();

public abstract String getEndLine();

public KeyPair createKeyPair(PEMStructure pemStructure, String password) throws IOException {
return createKeyPair(pemStructure);
}

protected abstract KeyPair createKeyPair(PEMStructure pemStructure) throws IOException;
}
189 changes: 189 additions & 0 deletions src/main/java/com/trilead/ssh2/jenkins/SFTPClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* The MIT License
*
* Copyright (c) 2004-, all the contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.trilead.ssh2.jenkins;

import com.trilead.ssh2.SFTPv3Client;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SFTPv3FileHandle;
import com.trilead.ssh2.SFTPv3FileAttributes;
import com.trilead.ssh2.SFTPException;
import com.trilead.ssh2.sftp.ErrorCodes;

import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;

/**
* This Class adds file manage capabilities to the SFTPv3Client class.
* @author Kohsuke Kawaguchi
*/
public class SFTPClient extends SFTPv3Client {
public SFTPClient(Connection conn) throws IOException {
super(conn);
}

/**
* Checks if the given path exists.
* @param path directory or file path.
* @return true if it exists.
* @throws IOException if it is not possible to access to the directory or file .
*/
public boolean exists(String path) throws IOException {
return _stat(path)!=null;
}

/**
* Graceful {@link #stat(String)} that returns null if the path doesn't exist.
* @param path directory path.
* @throws IOException if it is not possible to access to the directory.
*/
public SFTPv3FileAttributes _stat(String path) throws IOException {
try {
return stat(path);
} catch (SFTPException e) {
int c = e.getServerErrorCode();
if (c==ErrorCodes.SSH_FX_NO_SUCH_FILE || c==ErrorCodes.SSH_FX_NO_SUCH_PATH)
return null;
else
throw e;
}
}

/**
* Makes sure that the directory exists, by creating it if necessary.
* @param path directory path.
* @param posixPermission POSIX permissions.
* @throws IOException if it is not possible to access to the directory.
*/
public void mkdirs(String path, int posixPermission) throws IOException {
SFTPv3FileAttributes atts = _stat(path);
if (atts!=null && atts.isDirectory())
return;

int idx = path.lastIndexOf('/');
if (idx>0)
mkdirs(path.substring(0,idx), posixPermission);

try {
mkdir(path, posixPermission);
} catch (IOException e) {
throw new IOException("Failed to mkdir "+path,e);
}
}

/**
*
* @param path file path.
* @return Creates a new file and return an OutputStream to writes to it.
* @throws IOException if it is not possible to access to the file.
*/
public OutputStream writeToFile(String path) throws IOException {
final SFTPv3FileHandle h = createFile(path);
return new SFTPOutputStream(h);
}

/**
*
* @param file file path.
* @return return an InputStream to the file.
* @throws IOException if it is not possible to access to the file.
*/
public InputStream read(String file) throws IOException {
final SFTPv3FileHandle h = openFileRO(file);
return new SFTPInputStream(h);
}

/**
* Change file or directory permissions.
* @param path file or directory path.
* @param permissions POSIX permissions.
* @throws IOException in case of error.
*/
public void chmod(String path, int permissions) throws IOException {
SFTPv3FileAttributes atts = new SFTPv3FileAttributes();
atts.permissions = permissions;
setstat(path, atts);
}

private class SFTPOutputStream extends OutputStream {
private final SFTPv3FileHandle h;
private long offset;

public SFTPOutputStream(SFTPv3FileHandle h) {
this.h = h;
offset = 0;
}

public void write(int b) throws IOException {
write(new byte[]{(byte)b});
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
SFTPClient.this.write(h,offset,b,off,len);
offset += len;
}

@Override
public void close() throws IOException {
closeFile(h);
}
}

private class SFTPInputStream extends InputStream {
private final SFTPv3FileHandle h;
private long offset;

public SFTPInputStream(SFTPv3FileHandle h) {
this.h = h;
offset = 0;
}

public int read() throws IOException {
byte[] b = new byte[1];
if(read(b)<0)
return -1;
return b[0];
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int r = SFTPClient.this.read(h,offset,b,off,len);
if (r<0) return -1;
offset += r;
return r;
}

@Override
public long skip(long n) throws IOException {
offset += n;
return n;
}

@Override
public void close() throws IOException {
closeFile(h);
}
}
}
Loading