From 587a8500ba3c3bac38fd2864a2eed1f41ed7b6bb Mon Sep 17 00:00:00 2001 From: Luca Oonk Date: Mon, 10 Jan 2022 22:12:19 +0100 Subject: [PATCH] Added support for HTTP authentication for remote virt-server-conections --- .DS_Store | Bin 6148 -> 8196 bytes .gitignore | 1 + .../Controllers/RemoteVMController.java | 11 +++---- .../Backend/Controllers/VMController.java | 4 +-- .../Backend/Objects/Context.java | 4 ++- .../Backend/Objects/RemoteConnection.java | 1 + .../Remote/RemoteVMDetailProcessor.java | 3 +- .../Remote/RemoteVMListProcessor.java | 7 +++-- .../Backend/RemoteConnector.java | 29 ++++++++++++++++-- 9 files changed, 44 insertions(+), 16 deletions(-) diff --git a/.DS_Store b/.DS_Store index feca8613504bcd7430524202412f605a970dd548..c96c6939b462174cb71781f3dc94b51176a39bf0 100644 GIT binary patch literal 8196 zcmeHMU2GIp6h3EK%G^NZg7jw=fejmetc$djmY=FD-IgDP@U#5~sk1vnJ7GFgc4l|M zTCK+T1Q>tb42iy&Xq4!K0ev(2Vq%OEf+8;_`r?xh8sNbP&%Jk+K%s9$Ntm0={pOr= z&)j>?d}n6wEC674$!Y^=1^}H7fqVgVcWJ^djyswbY330k`9s0Y`@So*b&)BCI0JD8 z;ta$Yh%*pp;6BR$y|YEr=XmccYuv{fh%@j&Wc1fFcyE;MVkIVfa^rp_7n)%y=r)Uogw#hL0`k>!Ge6+il~KMl>wBT;cy>`bIF?OA4i7q> zl4k|dH>V6bV{DWsrl!`nZ*1>OC!grpG@VXPZE0^!CpUI(oStTSLuy0U!O>&xgy)}9 zv0?Olfcjano+h~tZi&hb%2^dvjaH57=W0}pEin2A28TvDF9r5N+ciU{Z16QMX83H)N1l>>(y@)f#K zzC`%%n+zS_3kK|BK*-p+g$>JBt$D1qW9zo;&OQ4t)jza|FJ8jf2<62KouiIj?5{Xh zI6iC!1>3V6Z)~va1Ww+y`#f{P=KnBusj;cK-bfgaR4ubTbe__ON_ppa$q7%&DORjB z*kOy-zSpB4*xW2I0O*+mn;a z*j5@lbbVOZwp*5MU8-+tZrruoFh+uLvuN+D5X<;Qu~O3SCB3|FWD!^DwVco|M#clu z$Q`fj5QXUuC?*rLQ<-&&MKZ0aOskT(cv%^chNoc%j6eZAsK6;W4QJpid;p)pdH4#x zh40}9_z^C`Rrn45fWP2xxCYk|kYOV>qk#!b;RbBQHr$Haa65Km7Wd!*%wZq);}Nv* zD30Mc9>WFGy8@^k0Bzkx3?D$`*2yUOy1xxvSH9E;w=|pfo zPl>D9TAirP>y+4%Nb6KgT$3W(kZ5C6GQ38KEr|}Mb5z1+wv|!w@LDB4#kMgjXAV+K zv?Q{+&gUfNoV0u&K7vo-0(?WXyiA1r75;=9M8!JfM8+nf;xb&0Yj7<-iY=JN4(!Cu zxCOhg2YYcR?!spzEeCKAhcS-@NlqUpv4{bd@Fjd1U%^-LH9U=Hh@@{4P2a`$6uoYe zZda9V3h_h2+elZ=6+PGY#zg_JEbT2bdqb}Ag_W!+efB(OiQ^wbdGZ1IsKFa{s z_T+lHX;E`Mbj4mfNyqbah$hC(D3mVL$?Z6y+>R68{KJsWlQdNLWFnkVNE&MY`GXcB?8<)+7*aZHx_ response = Unirest.post(remoteAddress+"/vm/") .header("Content-Type", "application/json") + .header("Authentication", httpAuth) .body("{\"name\":\""+vm.getDomain()+"\",\"action\":\"start\"}") .asString(); Gson g = new Gson(); VM remote = g.fromJson(response.getBody(), VM.class); - - } catch (Exception e) { JDialog dialog = new JDialog(); @@ -47,19 +47,18 @@ public static void startVm(VM vm, String remoteAddress){ } - public static void stopVM(VM vm, String remoteAddress){ + public static void stopVM(VM vm, String remoteAddress, String httpAuth){ try { HttpResponse response = Unirest.post(remoteAddress+"/vm/") .header("Content-Type", "application/json") + .header("Authentication", httpAuth) .body("{\"name\":\""+vm.getDomain()+"\",\"action\":\"stop\"}") .asString(); Gson g = new Gson(); VM remote = g.fromJson(response.getBody(), VM.class); - - } catch (Exception e) { JDialog dialog = new JDialog(); diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Controllers/VMController.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Controllers/VMController.java index 48ff4e8..3863938 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Controllers/VMController.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Controllers/VMController.java @@ -32,7 +32,7 @@ public void startVM(VM vm){ VMController.startVMthread(vmToStart, context); }else{ - RemoteVMController.startVm(vm, context.remoteAddress); + RemoteVMController.startVm(vm, context.remoteAddress, context.httpAuth); } @@ -45,7 +45,7 @@ public void stopVM(VM vm){ if(context.local){ VMController.stopVMthread(vmToStop, context); }else{ - RemoteVMController.stopVM(vm, context.remoteAddress); + RemoteVMController.stopVM(vm, context.remoteAddress, context.httpAuth); } diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/Context.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/Context.java index fd808b5..7ba527e 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/Context.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/Context.java @@ -35,7 +35,7 @@ public class Context { public MainContent mainContent; public JFrame mainJFrame; public String defaultSaveLocation; - private static final String versionString = "0.5.3"; + private static final String versionString = "0.5.4"; public Boolean checkForUpdates; private String applicationDefaultSaveLocation; public Integer windowHeight; @@ -45,6 +45,7 @@ public class Context { public long autoRefreshRate; public boolean local; public String remoteAddress; + public String httpAuth; private String currentSelectedUUID; public ArrayList remoteConnections; @@ -64,6 +65,7 @@ public Context(){ private void initDefaults(){ this.checkForUpdates = true; this.local = true; + this.httpAuth = ""; this.remoteAddress = ""; this.remoteConnections = new ArrayList(); diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/RemoteConnection.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/RemoteConnection.java index f855a7f..c2854b1 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/RemoteConnection.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/RemoteConnection.java @@ -5,5 +5,6 @@ public class RemoteConnection { public String name; public String address; public Boolean favorite; + public String httpAuthentication; } diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMDetailProcessor.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMDetailProcessor.java index 120ca21..f4fd59d 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMDetailProcessor.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMDetailProcessor.java @@ -8,11 +8,12 @@ public class RemoteVMDetailProcessor { - public static VM getDetails(VM vm, String remoteAddress) { + public static VM getDetails(VM vm, String remoteAddress, String httpAuth) { try { HttpResponse response = Unirest.post(remoteAddress+"/vm/details/") .header("Content-Type", "application/json") + .header("Authentication", httpAuth) .body("{\"name\":\""+vm.getDomain()+"\"}") .asString(); diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMListProcessor.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMListProcessor.java index 574d03b..c1a05f2 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMListProcessor.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/Processors/Remote/RemoteVMListProcessor.java @@ -27,16 +27,17 @@ public RemoteVMListProcessor(Context context){ } public void runCommand() throws IOException{ - this.vmList = getVMdomainList(context.remoteAddress, context); + this.vmList = getVMdomainList(context.remoteAddress, context, context.httpAuth); context.updateVMList(vmList); } - public static ArrayList getVMdomainList(String remoteAddress, Context context) throws IOException { + public static ArrayList getVMdomainList(String remoteAddress, Context context, String httpAuth) throws IOException { try { HttpResponse response = Unirest.get(remoteAddress+"/list") .header("Content-Type", "application/json") + .header("Authentication", httpAuth) .asString(); Gson g = new Gson(); @@ -46,7 +47,7 @@ public static ArrayList getVMdomainList(String remoteAddress, Context contex ArrayList actualVMs = new ArrayList(); for (VM vm : list.vms) { - actualVMs.add(RemoteVMDetailProcessor.getDetails(vm, remoteAddress)); + actualVMs.add(RemoteVMDetailProcessor.getDetails(vm, remoteAddress, httpAuth)); } return actualVMs; diff --git a/src/main/java/com/lucaoonk/Virt_Commander/Backend/RemoteConnector.java b/src/main/java/com/lucaoonk/Virt_Commander/Backend/RemoteConnector.java index 95b4589..f197b73 100644 --- a/src/main/java/com/lucaoonk/Virt_Commander/Backend/RemoteConnector.java +++ b/src/main/java/com/lucaoonk/Virt_Commander/Backend/RemoteConnector.java @@ -23,6 +23,8 @@ public class RemoteConnector implements ActionListener{ private Context context; private JComboBox combobox; private JTextArea remoteAddressText; + private JTextArea httpAuthenticationText; + private JButton removeButton; private JTextArea remoteNameText; private JButton addButton; @@ -83,6 +85,13 @@ public void actionPerformed(ActionEvent e) { RemoteConnection connection = ((RemoteConnectionComboItem)item).getValue(); context.remoteAddress = connection.address; + + if(connection.httpAuthentication != ""){ + context.httpAuth = connection.httpAuthentication; + }else{ + context.httpAuth = ""; + } + context.local = false; remoteDialog.dispose(); context.refresh(); @@ -118,7 +127,8 @@ public void actionPerformed(ActionEvent e) { RemoteConnection connection = new RemoteConnection(); connection.address = remoteAddressText.getText(); connection.name = remoteNameText.getText(); - + connection.httpAuthentication = httpAuthenticationText.getText(); + System.out.println(connection.name+" " + connection.address); context.remoteConnections.add(connection); context.writeConnections(); @@ -146,9 +156,13 @@ private void showConnectionEditor(){ this.remoteNameText = remoteNameText; remoteNameText.setSize(80, 20); + JTextArea httpAuthText = new JTextArea(); + this.httpAuthenticationText = httpAuthText; + httpAuthenticationText.setSize(80, 20); + // remoteAddressText.setSize(100, 20); panel.setBorder(new EmptyBorder(10,10,10,10)); - panel.setLayout(new GridLayout(5,2)); + panel.setLayout(new GridLayout(7,2)); panel.add(new JLabel("Select a connection and click remove to remove it")); panel.add(new JLabel("New Connection Name")); @@ -172,6 +186,15 @@ private void showConnectionEditor(){ panel.add(new JLabel()); + panel.add(new JLabel("HTTP authentication (leave empty if none should be used)")); + + panel.add(new JLabel()); + + panel.add(httpAuthenticationText); + + panel.add(new JLabel()); + + JButton addButton = new JButton("Add new Connection"); this.addButton = addButton; addButton.addActionListener(this); @@ -180,7 +203,7 @@ private void showConnectionEditor(){ // panel.add(switchToLocalButton); dialog.add(panel); - dialog.setSize(500, 200); + dialog.setSize(500, 250); dialog.setLocationRelativeTo(null); dialog.setVisible(true); dialog.setResizable(false);