From 36004ef3a99ddc781b96ec1e8a09f314238610fe Mon Sep 17 00:00:00 2001 From: litoral05 Date: Tue, 5 May 2026 11:34:08 +0100 Subject: [PATCH] Configure VPS SSH connection from environment variables --- pom.xml | 2 +- .../openvpn/LrOpenvpnBackendApplication.java | 6 ++- .../openvpn/router/RouterController.java | 33 +++++++------- .../litoralregas/openvpn/ssh/SshService.java | 14 ++++++ .../openvpn/ssh/SshTestController.java | 10 +---- .../openvpn/ssh/VpsSshProperties.java | 44 +++++++++++++++++++ src/main/resources/application.yaml | 10 ++++- 7 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/litoralregas/openvpn/ssh/VpsSshProperties.java diff --git a/pom.xml b/pom.xml index 7e55c70..17643c2 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ - 21 + 17 diff --git a/src/main/java/com/litoralregas/openvpn/LrOpenvpnBackendApplication.java b/src/main/java/com/litoralregas/openvpn/LrOpenvpnBackendApplication.java index 8e345bb..277f0d7 100644 --- a/src/main/java/com/litoralregas/openvpn/LrOpenvpnBackendApplication.java +++ b/src/main/java/com/litoralregas/openvpn/LrOpenvpnBackendApplication.java @@ -1,13 +1,15 @@ package com.litoralregas.openvpn; +import com.litoralregas.openvpn.ssh.VpsSshProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication +@EnableConfigurationProperties(VpsSshProperties.class) public class LrOpenvpnBackendApplication { public static void main(String[] args) { SpringApplication.run(LrOpenvpnBackendApplication.class, args); } - -} +} \ No newline at end of file diff --git a/src/main/java/com/litoralregas/openvpn/router/RouterController.java b/src/main/java/com/litoralregas/openvpn/router/RouterController.java index 66a4d2a..e6dedba 100644 --- a/src/main/java/com/litoralregas/openvpn/router/RouterController.java +++ b/src/main/java/com/litoralregas/openvpn/router/RouterController.java @@ -3,6 +3,7 @@ package com.litoralregas.openvpn.router; import com.litoralregas.openvpn.deployment.DeploymentAction; import com.litoralregas.openvpn.deployment.DeploymentResponse; import com.litoralregas.openvpn.deployment.DeploymentService; +import com.litoralregas.openvpn.ssh.SshService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -15,10 +16,12 @@ public class RouterController { private final RouterService service; private final DeploymentService deploymentService; + private final SshService sshService; - public RouterController(RouterService service, DeploymentService deploymentService) { + public RouterController(RouterService service, DeploymentService deploymentService, SshService sshService) { this.service = service; this.deploymentService = deploymentService; + this.sshService = sshService; } @GetMapping @@ -58,24 +61,22 @@ public class RouterController { try { service.forceStatus(id, RouterStatus.PROVISIONING); - // Simulated provisioning for now. - var finishedDeployment = deploymentService.finishSuccess( - deployment, - "Provision completed successfully" - ); + var result = sshService.executeOnConfiguredVps("whoami && hostname"); - service.forceStatus(id, RouterStatus.PROVISIONED); - - return DeploymentResponse.from(finishedDeployment); - } catch (Exception exception) { - var failedDeployment = deploymentService.finishFailure( - deployment, - exception.getMessage() - ); + if (result.exitCode() == 0) { + var finished = deploymentService.finishSuccess(deployment, result.stdout()); + service.forceStatus(id, RouterStatus.PROVISIONED); + return DeploymentResponse.from(finished); + } else { + var failed = deploymentService.finishFailure(deployment, result.stderr()); + service.forceStatus(id, RouterStatus.FAILED); + return DeploymentResponse.from(failed); + } + } catch (Exception e) { + var failed = deploymentService.finishFailure(deployment, e.getMessage()); service.forceStatus(id, RouterStatus.FAILED); - - return DeploymentResponse.from(failedDeployment); + return DeploymentResponse.from(failed); } } diff --git a/src/main/java/com/litoralregas/openvpn/ssh/SshService.java b/src/main/java/com/litoralregas/openvpn/ssh/SshService.java index 9414d2e..31151e2 100644 --- a/src/main/java/com/litoralregas/openvpn/ssh/SshService.java +++ b/src/main/java/com/litoralregas/openvpn/ssh/SshService.java @@ -8,6 +8,20 @@ import java.nio.charset.StandardCharsets; @Service public class SshService { + private final VpsSshProperties properties; + public SshService(VpsSshProperties properties) { + this.properties = properties; + } + + public SshCommandResult executeOnConfiguredVps(String command) { + return execute( + properties.getHost(), + properties.getPort(), + properties.getUsername(), + properties.getPassword(), + command + ); + } public SshCommandResult execute( String host, diff --git a/src/main/java/com/litoralregas/openvpn/ssh/SshTestController.java b/src/main/java/com/litoralregas/openvpn/ssh/SshTestController.java index e619bff..c459e06 100644 --- a/src/main/java/com/litoralregas/openvpn/ssh/SshTestController.java +++ b/src/main/java/com/litoralregas/openvpn/ssh/SshTestController.java @@ -13,13 +13,7 @@ public class SshTestController { } @PostMapping - public SshCommandResult test(@RequestBody SshTestRequest request) { - return sshService.execute( - request.host(), - request.port(), - request.username(), - request.password(), - "whoami && hostname" - ); + public SshCommandResult test() { + return sshService.executeOnConfiguredVps("whoami && hostname"); } } \ No newline at end of file diff --git a/src/main/java/com/litoralregas/openvpn/ssh/VpsSshProperties.java b/src/main/java/com/litoralregas/openvpn/ssh/VpsSshProperties.java new file mode 100644 index 0000000..4d1d000 --- /dev/null +++ b/src/main/java/com/litoralregas/openvpn/ssh/VpsSshProperties.java @@ -0,0 +1,44 @@ +package com.litoralregas.openvpn.ssh; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "lr.vps.ssh") +public class VpsSshProperties { + + private String host; + private int port = 22; + private String username; + private String password; + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 7652351..3352c58 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -22,4 +22,12 @@ management: endpoints: web: exposure: - include: health,info \ No newline at end of file + include: health,info + +lr: + vps: + ssh: + host: ${LR_VPS_SSH_HOST} + port: ${LR_VPS_SSH_PORT:22} + username: ${LR_VPS_SSH_USER} + password: ${LR_VPS_SSH_PASSWORD} \ No newline at end of file