Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit a411d16

Browse files
authored
Avoid validation of options.password when using private key file. (#84)
1 parent 915a9fe commit a411d16

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

GeneXusSftp/src/main/java/com/genexus/sftp/SftpClient.java

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@ public SftpClient() {
2626

2727
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
2828
public boolean connect(SftpOptions options) {
29-
if(options.hasError())
30-
{
29+
if (options.hasError()) {
3130
this.error = options.getError();
3231
return false;
3332
}
3433
boolean useKey = false;
35-
if (SecurityUtils.compareStrings("", options.getKeyPath()) || SecurityUtils.compareStrings("", options.getUser()) || SecurityUtils.compareStrings("", options.getKeyPassword())) {
36-
useKey = false;
34+
if (!SecurityUtils.compareStrings("", options.getKeyPath())) {
35+
useKey = true;
36+
} else {
3737
if (SecurityUtils.compareStrings("", options.getUser())
3838
|| SecurityUtils.compareStrings("", options.getPassword())) {
39-
40-
this.error.setError("SF001", "Authentication misconfiguration");
39+
40+
this.error.setError("SF001", "Authentication misconfiguration. Missing user or password");
4141
return false;
42-
}else {
43-
useKey = false;
4442
}
45-
}else {
46-
useKey=true;
4743
}
4844
if (SecurityUtils.compareStrings("", options.getHost())) {
4945
this.error.setError("SF003", "Empty host");
@@ -72,19 +68,18 @@ public boolean put(String localPath, String remoteDir) {
7268
this.error.setError("SF005", "The channel is invalid, reconect");
7369
return false;
7470
}
75-
76-
if(remoteDir.length() > 1) {
77-
if(remoteDir.startsWith("\\") || remoteDir.startsWith("/"))
78-
{
71+
72+
if (remoteDir.length() > 1) {
73+
if (remoteDir.startsWith("\\") || remoteDir.startsWith("/")) {
7974
remoteDir = remoteDir.substring(1, remoteDir.length());
8075
}
81-
}
76+
}
8277
try {
8378
this.channel.put(localPath, remoteDir);
8479
} catch (SftpException e) {
8580
if (SecurityUtils.compareStrings(rDir, "/") || SecurityUtils.compareStrings(rDir, "\\")) {
8681
try {
87-
this.channel.put(localPath, getFileName(localPath));
82+
this.channel.put(localPath, getFileName(localPath));
8883
} catch (SftpException s) {
8984
this.error.setError("SF006", s.getMessage());
9085
return false;
@@ -123,16 +118,13 @@ public void disconnect() {
123118
if (this.channel != null) {
124119
this.channel.disconnect();
125120
}
126-
if(this.session != null)
127-
{
121+
if (this.session != null) {
128122
this.session.disconnect();
129123
}
130124
}
131-
132-
public String getWorkingDirectory()
133-
{
134-
if (this.channel != null)
135-
{
125+
126+
public String getWorkingDirectory() {
127+
if (this.channel != null) {
136128
try {
137129
return this.channel.pwd();
138130
} catch (SftpException e) {
@@ -147,22 +139,21 @@ public String getWorkingDirectory()
147139

148140
private ChannelSftp setupJsch(SftpOptions options, boolean useKey) throws JSchException {
149141
JSch jsch = new JSch();
150-
151-
152-
if (useKey) {
142+
143+
if (useKey) {
153144
jsch.addIdentity(options.getKeyPath(), options.getKeyPassword());
154-
155-
this.session = jsch.getSession(options.getUser(),options.getHost());
156-
if(options.getAllowHostKeyChecking()) {
157-
if(SecurityUtils.compareStrings("", options.getKnownHostsPath()))
158-
{
159-
this.error.setError("SF009", "Options misconfiguration, known_hosts path is empty but host key checking is true");
145+
146+
this.session = jsch.getSession(options.getUser(), options.getHost());
147+
if (options.getAllowHostKeyChecking()) {
148+
if (SecurityUtils.compareStrings("", options.getKnownHostsPath())) {
149+
this.error.setError("SF009",
150+
"Options misconfiguration, known_hosts path is empty but host key checking is true");
160151
}
161152
jsch.setKnownHosts(options.getKnownHostsPath());
162-
}else {
153+
} else {
163154
this.session.setConfig("StrictHostKeyChecking", "no");
164155
}
165-
156+
166157
} else {
167158
this.session = jsch.getSession(options.getUser(), options.getHost(), options.getPort());
168159
this.session.setPassword(options.getPassword());
@@ -171,9 +162,8 @@ private ChannelSftp setupJsch(SftpOptions options, boolean useKey) throws JSchEx
171162
this.session.connect();
172163
return (ChannelSftp) this.session.openChannel("sftp");
173164
}
174-
175-
private String getFileName(String path)
176-
{
165+
166+
private String getFileName(String path) {
177167
Path p = Paths.get(path);
178168
return p.getFileName().toString();
179169
}

0 commit comments

Comments
 (0)