show message if private key not found

allow private key passphrase
private key class
This commit is contained in:
Niklas 2017-10-25 15:27:17 +02:00
parent aeac202b32
commit a8a961c5b9
8 changed files with 79 additions and 46 deletions

View File

@ -37,6 +37,7 @@ public class Config extends JSONObject {
this.put("debug", true); this.put("debug", true);
this.put("sqlite", Variables.DATABASE_NAME); this.put("sqlite", Variables.DATABASE_NAME);
this.put("private_key", Variables.PRIVATE_KEY); this.put("private_key", Variables.PRIVATE_KEY);
this.put("private_key_pass", "");
JSONObject custom_commands = new JSONObject(); JSONObject custom_commands = new JSONObject();
custom_commands.put("Screen installieren", "apt-get install screen"); custom_commands.put("Screen installieren", "apt-get install screen");

View File

@ -88,6 +88,13 @@ public class GUI {
MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE)); MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
if (!StartUp.getPrivateKey().exists()) {
Logger.error(StartUp.getPrivateKey().getPath() + " wurde nicht gefunden!");
MessageDialog.showMessageDialog(gui, "ERROR!", StartUp.getPrivateKey().getPath() + " wurde nicht gefunden!", MessageDialogButton.Cancel);
gui.addWindowAndWait(window);
return;
}
CheckBoxList<String> checkBoxList = new CheckBoxList<String>().addTo(leftPanel); CheckBoxList<String> checkBoxList = new CheckBoxList<String>().addTo(leftPanel);
updateList(checkBoxList, serverlist); updateList(checkBoxList, serverlist);
@ -145,7 +152,7 @@ public class GUI {
checkBoxList.addItem(servername); checkBoxList.addItem(servername);
Logger.info("Added: " + ip + " " + port); Logger.info("Added: " + ip + " " + port);
MessageDialog.showMessageDialog(gui, "INFO", "Server hinzugefügt.\n" + MessageDialog.showMessageDialog(gui, "INFO", "Server hinzugefügt.\n" +
"Gehe sicher das der Public Key von " + StartUp.getPrivate_key() + " auf dem Server abgelegt wurde.", MessageDialogButton.Continue); "Gehe sicher das der Public Key von " + StartUp.getPrivateKey().getPath() + " auf dem Server abgelegt wurde.", MessageDialogButton.Continue);
} catch (SQLException e) { } catch (SQLException e) {
Logger.error(e); Logger.error(e);
} }
@ -190,15 +197,15 @@ public class GUI {
gui.addWindow(windowStatus); gui.addWindow(windowStatus);
gui.setActiveWindow(windowStatus); gui.setActiveWindow(windowStatus);
}) })
.addAction("Updaten", () -> MassCommand.run("apt-get update && apt-get upgrade -y", gui, serverarray, StartUp.getPrivate_key())) .addAction("Updaten", () -> MassCommand.run("apt-get update && apt-get upgrade -y", gui, serverarray, StartUp.getPrivateKey()))
.addAction("Herunterfahren", () -> MassCommand.run("shutdown -h now", gui, serverarray, StartUp.getPrivate_key())) .addAction("Herunterfahren", () -> MassCommand.run("shutdown -h now", gui, serverarray, StartUp.getPrivateKey()))
.addAction("Neustart", () -> MassCommand.run("reboot", gui, serverarray, StartUp.getPrivate_key())); .addAction("Neustart", () -> MassCommand.run("reboot", gui, serverarray, StartUp.getPrivateKey()));
JSONObject customCommands = StartUp.getConfig().getCustomCommands(); JSONObject customCommands = StartUp.getConfig().getCustomCommands();
for (int i = 0; i < customCommands.names().length(); i++) { for (int i = 0; i < customCommands.names().length(); i++) {
String key = customCommands.names().getString(i); String key = customCommands.names().getString(i);
String value = customCommands.getString(key); String value = customCommands.getString(key);
pre.addAction(key, () -> MassCommand.run(value, gui, serverarray, StartUp.getPrivate_key())); pre.addAction(key, () -> MassCommand.run(value, gui, serverarray, StartUp.getPrivateKey()));
} }
pre.build().showDialog(gui); pre.build().showDialog(gui);

View File

@ -24,6 +24,10 @@ public class Poller {
public Poller() { public Poller() {
scheduler = Executors.newScheduledThreadPool(1); scheduler = Executors.newScheduledThreadPool(1);
if (!StartUp.getPrivateKey().exists()) {
Logger.error(StartUp.getPrivateKey().getPath() + " wurde nicht gefunden!");
return;
}
scheduler.scheduleAtFixedRate(() -> { scheduler.scheduleAtFixedRate(() -> {
HashMap<String, Server> serverlist = Server.getServerList(StartUp.getDb()); HashMap<String, Server> serverlist = Server.getServerList(StartUp.getDb());
@ -34,7 +38,7 @@ public class Poller {
Logger.info("Paused!"); Logger.info("Paused!");
return; return;
} }
SSHManager manager = server.getSSHSession(StartUp.getPrivate_key()); SSHManager manager = server.getSSHSession(StartUp.getPrivateKey());
if (manager.hasError()) { if (manager.hasError()) {
status.setState(State.OFFLINE); status.setState(State.OFFLINE);
Logger.error("Offline!"); Logger.error("Offline!");

View File

@ -1,6 +1,7 @@
package de.gurkengewuerz.monitoring; package de.gurkengewuerz.monitoring;
import de.gurkengewuerz.monitoring.object.Database; import de.gurkengewuerz.monitoring.object.Database;
import de.gurkengewuerz.monitoring.object.PrivateKey;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
@ -14,7 +15,7 @@ import java.sql.SQLException;
public class StartUp { public class StartUp {
private static Database db; private static Database db;
private static Config conf; private static Config conf;
private static String private_key; private static PrivateKey private_key;
public static void main(String... args) throws IOException { public static void main(String... args) throws IOException {
// START-ARGUMENTS // START-ARGUMENTS
@ -75,11 +76,7 @@ public class StartUp {
// END-Datenbank // END-Datenbank
// START-PRIV KEY // START-PRIV KEY
private_key = conf.getString("private_key"); private_key = new PrivateKey(conf.getString("private_key"), conf.getString("private_key_pass"));
if (!new File(private_key).exists()) {
Logger.error(private_key + " wurde nicht gefunden!");
System.exit(1);
}
// END-PRIV KEY // END-PRIV KEY
if (cmd.hasOption("p")) { if (cmd.hasOption("p")) {
@ -93,7 +90,7 @@ public class StartUp {
return db; return db;
} }
public static String getPrivate_key() { public static PrivateKey getPrivateKey() {
return private_key; return private_key;
} }

View File

@ -18,13 +18,13 @@ import java.util.List;
public class MassCommand { public class MassCommand {
private String command; private String command;
private String private_key; private PrivateKey private_key;
private List<Server> serverList = new ArrayList<>(); private List<Server> serverList = new ArrayList<>();
private StatusReply answer; private StatusReply answer;
private boolean stop = false; private boolean stop = false;
private Thread t; private Thread t;
public MassCommand(String command, String private_key, List<Server> serverList, StatusReply answer) { public MassCommand(String command, PrivateKey private_key, List<Server> serverList, StatusReply answer) {
this.command = command; this.command = command;
this.private_key = private_key; this.private_key = private_key;
this.serverList = serverList; this.serverList = serverList;
@ -32,10 +32,10 @@ public class MassCommand {
} }
public MassCommand(String command, List<Server> serverList, StatusReply answer) { public MassCommand(String command, List<Server> serverList, StatusReply answer) {
this(command, "", serverList, answer); this(command, new PrivateKey(""), serverList, answer);
} }
public static void run(String command, MultiWindowTextGUI gui, List<Server> serverarray, String ssh_private) { public static void run(String command, MultiWindowTextGUI gui, List<Server> serverarray, PrivateKey ssh_private) {
Label label = new Label("[ ] 00%"); Label label = new Label("[ ] 00%");
Button button = new Button("Abbruch"); Button button = new Button("Abbruch");
BasicWindow windowStatus = GUI.getCallbackWindow(label, button); BasicWindow windowStatus = GUI.getCallbackWindow(label, button);
@ -86,7 +86,7 @@ public class MassCommand {
button.takeFocus(); button.takeFocus();
} }
public MassCommand setPrivateKey(String private_key) { public MassCommand setPrivateKey(PrivateKey private_key) {
this.private_key = private_key; this.private_key = private_key;
return this; return this;
} }
@ -98,7 +98,7 @@ public class MassCommand {
answer.onStart(); answer.onStart();
serverList.forEach(server -> { serverList.forEach(server -> {
if (stop) return; if (stop) return;
if (failed[0]) return; failed[0] = false;
SSHManager manager = server.getSSHSession(private_key); SSHManager manager = server.getSSHSession(private_key);
if (manager.hasError()) { if (manager.hasError()) {
failed[0] = true; failed[0] = true;

View File

@ -0,0 +1,40 @@
package de.gurkengewuerz.monitoring.object;
import java.io.File;
/**
* Created by gurkengewuerz.de on 25.10.2017.
*/
public class PrivateKey {
private String path;
private String pass;
private boolean exists = false;
public PrivateKey(String path, String pass) {
this.path = path;
this.pass = pass;
exists = new File(path).exists();
}
public PrivateKey(String path) {
this(path, "");
}
public boolean hasPassword() {
return !pass.isEmpty();
}
public boolean exists() {
return exists;
}
public String getPath() {
return path;
}
public String getPassword() {
return pass;
}
}

View File

@ -21,48 +21,32 @@ public class SSHManager {
private String strUserName; private String strUserName;
private String strConnectionIP; private String strConnectionIP;
private int intConnectionPort; private int intConnectionPort;
private String strPassword;
private Session sesConnection; private Session sesConnection;
private String errorMessage = null; private String errorMessage = null;
private int intTimeOut; private int intTimeOut;
public SSHManager(String userName, String password, public SSHManager(String userName, String privateKey, String privateKey_pass, String connectionIP, String knownHostsFileName, int connectionPort) {
String connectionIP, String knownHostsFileName) { doCommonConstructorActions(userName, privateKey, privateKey_pass, connectionIP, knownHostsFileName);
doCommonConstructorActions(userName, password,
connectionIP, knownHostsFileName);
intConnectionPort = 22;
intTimeOut = 60000;
}
public SSHManager(String userName, String password, String connectionIP,
String knownHostsFileName, int connectionPort) {
doCommonConstructorActions(userName, password, connectionIP,
knownHostsFileName);
intConnectionPort = connectionPort; intConnectionPort = connectionPort;
intTimeOut = 60000; intTimeOut = 60000;
} }
public SSHManager(String userName, String password, String connectionIP, private void doCommonConstructorActions(String userName, String privateKey, String privateKey_pass, String connectionIP, String knownHostsFileName) {
String knownHostsFileName, int connectionPort, int timeOutMilliseconds) {
doCommonConstructorActions(userName, password, connectionIP,
knownHostsFileName);
intConnectionPort = connectionPort;
intTimeOut = timeOutMilliseconds;
}
private void doCommonConstructorActions(String userName,
String password, String connectionIP, String knownHostsFileName) {
jschSSHChannel = new JSch(); jschSSHChannel = new JSch();
try { try {
jschSSHChannel.addIdentity(password); if (privateKey_pass.isEmpty()) {
jschSSHChannel.addIdentity(privateKey);
} else {
jschSSHChannel.addIdentity(privateKey, privateKey_pass);
}
jschSSHChannel.setKnownHosts(knownHostsFileName); jschSSHChannel.setKnownHosts(knownHostsFileName);
} catch (JSchException jschX) { } catch (JSchException jschX) {
logError(jschX.getMessage()); logError(jschX.getMessage());
} }
strUserName = userName; strUserName = userName;
strPassword = password;
strConnectionIP = connectionIP; strConnectionIP = connectionIP;
} }

View File

@ -98,8 +98,8 @@ public class Server {
"Status: " + status.getState().toString(); "Status: " + status.getState().toString();
} }
public SSHManager getSSHSession(String ssh_private) { public SSHManager getSSHSession(PrivateKey privateKey) {
SSHManager instance = new SSHManager(getUsername(), ssh_private, ip, "", port); SSHManager instance = new SSHManager(getUsername(), privateKey.getPath(), privateKey.getPassword(), ip, "", port);
String errorMessage = instance.connect(); String errorMessage = instance.connect();
if (errorMessage != null) { if (errorMessage != null) {