Skip to content

Commit ab2f477

Browse files
author
jantje
committed
#645 password set/delete now available in project properties->arduino
Also implemented the other way of upload but didn't test so I doubt this would work.
1 parent 042f4e7 commit ab2f477

File tree

10 files changed

+258
-239
lines changed

10 files changed

+258
-239
lines changed

io.sloeber.core/src/cc/arduino/packages/ssh/SSH.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
import java.io.IOException;
3333
import java.io.InputStream;
34-
import java.io.PrintStream;
34+
35+
import org.eclipse.ui.console.MessageConsoleStream;
3536

3637
import com.jcraft.jsch.Channel;
3738
import com.jcraft.jsch.ChannelExec;
@@ -52,8 +53,8 @@ public boolean execSyncCommand(String command) throws JSchException, IOException
5253
}
5354

5455
@SuppressWarnings("resource")
55-
public boolean execSyncCommand(String command, PrintStream stdoutConsumer, PrintStream stderrConsumer)
56-
throws JSchException, IOException {
56+
public boolean execSyncCommand(String command, MessageConsoleStream stdoutConsumer,
57+
MessageConsoleStream stderrConsumer) throws JSchException, IOException {
5758
InputStream stdout = null;
5859
InputStream stderr = null;
5960
Channel channel = null;
@@ -68,7 +69,7 @@ public boolean execSyncCommand(String command, PrintStream stdoutConsumer, Print
6869

6970
channel.connect();
7071

71-
int exitCode = consumeOutputSyncAndReturnExitCode(channel, stdout, stdoutConsumer, stderr, stderrConsumer);
72+
int exitCode = consumeOutputSyncAndReturnExitCode(channel);
7273

7374
return exitCode == 0;
7475

@@ -90,12 +91,8 @@ public boolean execSyncCommand(String command, PrintStream stdoutConsumer, Print
9091
}
9192
}
9293

93-
private int consumeOutputSyncAndReturnExitCode(Channel channel, InputStream stdout, PrintStream stdoutConsumer,
94-
InputStream stderr, PrintStream stderrConsumer) throws IOException {
95-
byte[] tmp = new byte[102400];
94+
private static int consumeOutputSyncAndReturnExitCode(Channel channel) {
9695
while (true) {
97-
consumeStream(tmp, stdout, stdoutConsumer);
98-
consumeStream(tmp, stderr, stderrConsumer);
9996

10097
if (channel.isClosed()) {
10198
return channel.getExitStatus();
@@ -108,17 +105,18 @@ private int consumeOutputSyncAndReturnExitCode(Channel channel, InputStream stdo
108105
}
109106
}
110107

111-
@SuppressWarnings("static-method")
112-
private void consumeStream(byte[] buffer, InputStream in, PrintStream out) throws IOException {
113-
while (in.available() > 0) {
114-
int length = in.read(buffer, 0, buffer.length);
115-
if (length < 0) {
116-
break;
117-
}
118-
if (out != null) {
119-
out.print(new String(buffer, 0, length));
120-
}
121-
}
122-
}
108+
// @SuppressWarnings("static-method")
109+
// private void consumeStream(byte[] buffer, InputStream in, PrintStream
110+
// out) throws IOException {
111+
// while (in.available() > 0) {
112+
// int length = in.read(buffer, 0, buffer.length);
113+
// if (length < 0) {
114+
// break;
115+
// }
116+
// if (out != null) {
117+
// out.print(new String(buffer, 0, length));
118+
// }
119+
// }
120+
// }
123121

124122
}

io.sloeber.core/src/cc/arduino/packages/ssh/SSHPwdSetup.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,44 @@
2929

3030
package cc.arduino.packages.ssh;
3131

32+
import org.eclipse.core.runtime.IStatus;
33+
import org.eclipse.core.runtime.Status;
34+
3235
import com.jcraft.jsch.JSch;
3336
import com.jcraft.jsch.JSchException;
3437
import com.jcraft.jsch.Session;
3538

3639
import cc.arduino.packages.BoardPort;
40+
import io.sloeber.core.api.PasswordManager;
41+
import io.sloeber.core.common.Common;
42+
import io.sloeber.core.common.Const;
43+
import io.sloeber.core.tools.uploaders.Messages;
3744

38-
@SuppressWarnings({ "nls" })
45+
@SuppressWarnings({})
3946
public class SSHPwdSetup implements SSHClientSetupChainRing {
4047

4148
@Override
4249
public Session setup(BoardPort port, JSch jSch) throws JSchException {
43-
String ipAddress = port.getAddress();
50+
String hostLogin = new String();
51+
String hostPwd = new String();
52+
String host = port.getBoardName();
53+
PasswordManager pwdManager = new PasswordManager();
54+
if (pwdManager.setHost(port.getBoardName())) {
55+
hostLogin = pwdManager.getLogin();
56+
hostPwd = pwdManager.getPassword();
57+
58+
} else {
59+
// The user should set the password in the project
60+
// properties->arduino
61+
Common.log(
62+
new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, Messages.Upload_login_credentials_missing + host));
63+
64+
return null;
65+
}
66+
67+
Session session = jSch.getSession(hostLogin, host, 22);
4468

45-
Session session = jSch.getSession("root", ipAddress, 22);
46-
// session.setPassword(PreferencesData.get("runtime.pwd." + ipAddress));
69+
session.setUserInfo(new NoInteractionUserInfo(hostPwd));
4770

4871
return session;
4972
}

io.sloeber.core/src/io/sloeber/core/api/PasswordManager.java

Lines changed: 95 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -11,127 +11,110 @@
1111
import io.sloeber.core.tools.Messages;
1212

1313
public class PasswordManager {
14-
private String myPassword;
15-
private String myLogin = null;
16-
private String myhost = null;
17-
boolean ret = false;
18-
19-
public PasswordManager() {
20-
// no constructor needed
21-
}
22-
23-
public String getPassword() {
24-
return this.myPassword;
25-
}
26-
27-
public String getLogin() {
28-
return this.myLogin;
29-
}
30-
31-
public String getHost() {
32-
return this.myhost;
33-
}
34-
35-
/**
36-
* Sets the host. If there is no Login,password for this host the method
37-
* returns false. If a login/password for the host is found the method
38-
* returns true
39-
*
40-
* @param host
41-
* @return
42-
*/
43-
public boolean setHost(String host) {
44-
this.myhost = host;
45-
this.myPassword = null;
46-
this.myLogin = null;
47-
48-
String nodename = ConvertHostToNodeName(this.myhost);
49-
ISecurePreferences root = SecurePreferencesFactory.getDefault();
50-
ISecurePreferences node = root.node(nodename);
51-
52-
try {
53-
if (root.nodeExists(nodename)) {
54-
this.myPassword = node.get(Messages.security_password, null);
55-
this.myLogin = node.get(Messages.security_login, null);
56-
return true;
57-
}
58-
59-
// PasswordDialog dialog = new PasswordDialog(
60-
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
61-
// if (this.myLogin != null)
62-
// dialog.setUser(this.myLogin);
63-
// dialog.sethost(host);
64-
// // get the new values from the dialog
65-
// if (dialog.open() == Window.OK) {
66-
// this.myLogin = dialog.getUser();
67-
// this.myPassword = dialog.getPassword();
68-
// node.put(Messages.security_login, this.myLogin, false);
69-
// node.put(Messages.security_password, this.myPassword, true);
70-
// } else {
71-
// return false;
72-
// }
73-
74-
} catch (StorageException e) {
75-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$
14+
private String myPassword;
15+
private String myLogin = null;
16+
private String myhost = null;
17+
boolean ret = false;
18+
19+
public PasswordManager() {
20+
// no constructor needed
21+
}
22+
23+
public String getPassword() {
24+
return this.myPassword;
7625
}
7726

78-
return false;
79-
}
80-
81-
/**
82-
* Sets the login and password for a host
83-
*
84-
* @param host
85-
* @param login
86-
* @param password
87-
* @throws Exception
88-
*/
89-
public void setLoginData(String host, String login, String password) throws Exception {
90-
this.myhost = host;
91-
this.myLogin = login;
92-
this.myPassword = password;
93-
String nodename = ConvertHostToNodeName(this.myhost);
94-
ISecurePreferences root = SecurePreferencesFactory.getDefault();
95-
ISecurePreferences node = root.node(nodename);
96-
97-
node.put(Messages.security_login, this.myLogin, false);
98-
node.put(Messages.security_password, this.myPassword, true);
99-
}
100-
101-
static public void setPwd(String host, String login, String pwd) {
102-
103-
String nodename = ConvertHostToNodeName(host);
104-
ISecurePreferences root = SecurePreferencesFactory.getDefault();
105-
ISecurePreferences node = root.node(nodename);
106-
107-
try {
108-
node.put(Messages.security_login, login, false);
109-
node.put(Messages.security_password, pwd, false);
110-
} catch (StorageException e) {
111-
112-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$
27+
public String getLogin() {
28+
return this.myLogin;
11329
}
11430

115-
}
31+
public String getHost() {
32+
return this.myhost;
33+
}
11634

117-
public static void ErasePassword(String host) {
118-
String nodename = ConvertHostToNodeName(host);
119-
ISecurePreferences root = SecurePreferencesFactory.getDefault();
120-
ISecurePreferences node = root.node(nodename);
121-
try {
122-
if (root.nodeExists(nodename)) {
123-
node.put(Messages.security_password, null, true);
124-
}
35+
/**
36+
* Sets the host. If there is no Login,password for this host the method
37+
* returns false. If a login/password for the host is found the method
38+
* returns true
39+
*
40+
* @param host
41+
* @return
42+
*/
43+
public boolean setHost(String host) {
44+
this.myhost = host;
45+
this.myPassword = null;
46+
this.myLogin = null;
47+
48+
String nodename = ConvertHostToNodeName(this.myhost);
49+
ISecurePreferences root = SecurePreferencesFactory.getDefault();
50+
51+
try {
52+
if (root.nodeExists(nodename)) {
53+
54+
ISecurePreferences node = root.node(nodename);
55+
this.myPassword = node.get(Messages.security_password, null);
56+
this.myLogin = node.get(Messages.security_login, null);
57+
return true;
58+
}
59+
60+
} catch (StorageException e) {
61+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID,
62+
"Set a password on the project properties->arduino page", e)); //$NON-NLS-1$
63+
}
64+
65+
return false;
66+
}
12567

126-
} catch (StorageException e) {
127-
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to erase login info", e)); //$NON-NLS-1$
68+
/**
69+
* Sets the login and password for a host
70+
*
71+
* @param host
72+
* @param login
73+
* @param password
74+
* @throws Exception
75+
*/
76+
public void setLoginData(String host, String login, String password) throws Exception {
77+
this.myhost = host;
78+
this.myLogin = login;
79+
this.myPassword = password;
80+
String nodename = ConvertHostToNodeName(this.myhost);
81+
ISecurePreferences root = SecurePreferencesFactory.getDefault();
82+
ISecurePreferences node = root.node(nodename);
83+
84+
node.put(Messages.security_login, this.myLogin, false);
85+
node.put(Messages.security_password, this.myPassword, true);
12886
}
12987

130-
}
88+
static public void setPwd(String host, String login, String pwd) {
89+
90+
String nodename = ConvertHostToNodeName(host);
91+
ISecurePreferences root = SecurePreferencesFactory.getDefault();
92+
ISecurePreferences node = root.node(nodename);
13193

132-
private static String ConvertHostToNodeName(String host) {
94+
try {
95+
node.put(Messages.security_login, login, false);
96+
node.put(Messages.security_password, pwd, false);
97+
} catch (StorageException e) {
13398

134-
return "ssh/" + host.replace(Const.DOT, Const.SLACH); //$NON-NLS-1$
135-
}
99+
Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, "failed to set login info", e)); //$NON-NLS-1$
100+
}
101+
102+
}
103+
104+
public static void ErasePassword(String host) {
105+
String nodename = ConvertHostToNodeName(host);
106+
ISecurePreferences root = SecurePreferencesFactory.getDefault();
107+
108+
if (root.nodeExists(nodename)) {
109+
ISecurePreferences node = root.node(nodename);
110+
node.removeNode();// (Messages.security_password, null, true);
111+
}
112+
113+
}
114+
115+
private static String ConvertHostToNodeName(String host) {
116+
117+
return "ssh/" + host.replace(Const.DOT, Const.SLACH); //$NON-NLS-1$
118+
}
136119

137120
}

0 commit comments

Comments
 (0)