Skip to content

Commit

Permalink
Added support for HTTP authentication for remote virt-server-conections
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaOonk committed Jan 10, 2022
1 parent 56950c3 commit 587a850
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

target/*
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
public class RemoteVMController {


public static void startVm(VM vm, String remoteAddress){
public static void startVm(VM vm, String remoteAddress, String httpAuth){

try {

HttpResponse<String> 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();
Expand All @@ -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<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,6 +45,7 @@ public class Context {
public long autoRefreshRate;
public boolean local;
public String remoteAddress;
public String httpAuth;
private String currentSelectedUUID;

public ArrayList<RemoteConnection> remoteConnections;
Expand All @@ -64,6 +65,7 @@ public Context(){
private void initDefaults(){
this.checkForUpdates = true;
this.local = true;
this.httpAuth = "";
this.remoteAddress = "";
this.remoteConnections = new ArrayList<RemoteConnection>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class RemoteConnection {
public String name;
public String address;
public Boolean favorite;
public String httpAuthentication;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> response = Unirest.post(remoteAddress+"/vm/details/")
.header("Content-Type", "application/json")
.header("Authentication", httpAuth)
.body("{\"name\":\""+vm.getDomain()+"\"}")
.asString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VM> getVMdomainList(String remoteAddress, Context context) throws IOException {
public static ArrayList<VM> getVMdomainList(String remoteAddress, Context context, String httpAuth) throws IOException {

try {
HttpResponse<String> response = Unirest.get(remoteAddress+"/list")
.header("Content-Type", "application/json")
.header("Authentication", httpAuth)
.asString();

Gson g = new Gson();
Expand All @@ -46,7 +47,7 @@ public static ArrayList<VM> getVMdomainList(String remoteAddress, Context contex
ArrayList<VM> actualVMs = new ArrayList<VM>();

for (VM vm : list.vms) {
actualVMs.add(RemoteVMDetailProcessor.getDetails(vm, remoteAddress));
actualVMs.add(RemoteVMDetailProcessor.getDetails(vm, remoteAddress, httpAuth));
}

return actualVMs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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("<html><b>Select a connection and click remove to remove it</b></html>"));
panel.add(new JLabel("<html><b>New Connection Name</b></html>"));
Expand All @@ -172,6 +186,15 @@ private void showConnectionEditor(){

panel.add(new JLabel());

panel.add(new JLabel("<html><b>HTTP authentication (leave empty if none should be used)</b></html>"));

panel.add(new JLabel());

panel.add(httpAuthenticationText);

panel.add(new JLabel());


JButton addButton = new JButton("Add new Connection");
this.addButton = addButton;
addButton.addActionListener(this);
Expand All @@ -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);
Expand Down

0 comments on commit 587a850

Please sign in to comment.