use of ch.ethz.ssh2.Connection in project warn-report by saaavsaaa.
the class LinuxExecUtil method connect.
public Connection connect(final String host, final int port, final String user, final String password) throws IOException {
conn = new Connection(host, port);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(user, password);
if (isAuthenticated == false) {
throw new IOException("Authentication failed.");
}
return conn;
}
use of ch.ethz.ssh2.Connection in project Payara by payara.
the class SSHLauncher method openConnection.
/**
* Opens the connection to the host and authenticates with public
* key.
*/
private void openConnection() throws IOException {
boolean isAuthenticated = false;
String message = "";
connection = new Connection(host, port);
connection.connect(new HostVerifier(knownHostsDatabase));
if (SSHUtil.checkString(keyFile) == null && SSHUtil.checkString(password) == null && privateKey == null) {
message += "No key or password specified - trying default keys \n";
if (logger.isLoggable(Level.FINE)) {
logger.fine("keyfile and password are null. Will try to authenticate with default key file if available");
}
// check the default key locations if no authentication
// method is explicitly configured.
File home = new File(System.getProperty("user.home"));
for (String keyName : Arrays.asList("id_rsa", "id_dsa", "identity")) {
message += "Tried to authenticate using " + keyName + "\n";
File key = new File(home, ".ssh/" + keyName);
if (key.exists()) {
isAuthenticated = connection.authenticateWithPublicKey(userName, key, null);
}
if (isAuthenticated) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Authentication successful using key " + keyName);
}
message = null;
break;
}
}
}
if (!isAuthenticated && SSHUtil.checkString(password) != null) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Authenticating with password " + getPrintablePassword(password));
}
try {
isAuthenticated = connection.authenticateWithPassword(userName, password);
} catch (IOException iex) {
message = "SSH authentication with password failed: " + ExceptionUtil.getRootCause(iex).getMessage();
logger.log(Level.WARNING, message, iex);
}
}
if (!isAuthenticated && privateKey != null) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("Authenticating with privateKey");
}
try {
isAuthenticated = connection.authenticateWithPublicKey(userName, privateKey, keyPassPhrase);
} catch (IOException iex) {
message = "SSH authentication with private key failed: " + ExceptionUtil.getRootCause(iex).getMessage();
logger.log(Level.WARNING, message, iex);
}
}
if (!isAuthenticated && SSHUtil.checkString(keyFile) != null) {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Specified key file is " + keyFile);
}
File key = new File(keyFile);
if (key.exists()) {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Specified key file exists at " + key);
}
try {
isAuthenticated = connection.authenticateWithPublicKey(userName, key, keyPassPhrase);
} catch (IOException iex) {
message = "SSH authentication with key file " + key + " failed: " + ExceptionUtil.getRootCause(iex).getMessage();
logger.log(Level.WARNING, message, iex);
}
}
}
if (!isAuthenticated && !connection.isAuthenticationComplete()) {
connection.close();
connection = null;
if (logger.isLoggable(Level.FINE)) {
logger.fine("Could not authenticate");
}
throw new IOException("Could not authenticate. " + message);
}
message = null;
SSHUtil.register(connection);
}
use of ch.ethz.ssh2.Connection in project DevRing by LJYcoder.
the class HttpLoggingInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Level level = this.level;
Request request = chain.request();
if (level == Level.NONE) {
return chain.proceed(request);
}
boolean logBody = level == Level.BODY;
boolean logHeaders = logBody || level == Level.HEADERS;
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
logger.log(requestStartMessage);
if (logHeaders) {
if (hasRequestBody) {
// them to be included (when available) so there values are known.
if (requestBody.contentType() != null) {
logger.log("Content-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
logger.log("Content-Length: " + requestBody.contentLength());
}
}
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
// Skip headers from the request body as they are explicitly logged above.
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
logger.log(name + ": " + headers.value(i));
}
}
if (!logBody || !hasRequestBody) {
logger.log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
logger.log("--> END " + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
logger.log("");
if (isPlaintext(buffer)) {
logger.log(buffer.readString(charset));
logger.log("--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)");
} else {
logger.log("--> END " + request.method() + " (binary " + requestBody.contentLength() + "-byte body omitted)");
}
}
}
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
logger.log("<-- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
logger.log("<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", " + bodySize + " body" : "") + ')');
if (logHeaders) {
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
logger.log(headers.name(i) + ": " + headers.value(i));
}
if (!logBody || !HttpHeaders.hasBody(response)) {
logger.log("<-- END HTTP");
} else if (bodyEncoded(response.headers())) {
logger.log("<-- END HTTP (encoded body omitted)");
} else {
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
if (!isPlaintext(buffer)) {
logger.log("");
logger.log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
return response;
}
if (contentLength != 0) {
logger.log("");
logger.log(buffer.clone().readString(charset));
}
logger.log("<-- END HTTP (" + buffer.size() + "-byte body)");
}
}
return response;
}
Aggregations