I'm trying to connect to Google Cloud VM from Spring Boot app. This is the first time I'm doing something like that, but I get an Auth fail exception no matter what I'm trying.
This is my code:
@Component
@AllArgsConstructor
@NoArgsConstructor
public class SftpDownloader {
String SFTPHOST = "MY-SERVER-IP";
int SFTPPORT = 22;
String SFTPUSER = "yarin";
String privateKey = "/Users/yarin/.ssh/gcpdash";
String SFTPWORKINGDIR = "/";
JSch jSch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
public void downloadFromSftp (){
try {
jSch.addIdentity(privateKey);
System.out.println("Private Key Added.");
session = jSch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword("");
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
System.out.println("shell channel connected....");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
System.out.println("Changed the directory...");
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (channelSftp != null) {
channelSftp.disconnect();
channelSftp.exit();
}
if (channel != null)
channel.disconnect();
if (session != null)
session.disconnect();
}
}
}
I am able to connect to the GCP VM with the same key from "FileZilla", So I think everything is ok from GCP side.