From dfa58b2cb51b0e7ebfb8e1e3875f64fb1ce73295 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Wed, 11 May 2022 11:03:16 +0200 Subject: [PATCH] Shell expiry logic Signed-off-by: Georg Pfuetzenreuter --- .gitignore | 3 ++ .../libertacasa/pubsh/web/SchedulerBean.java | 34 +++++++++++++------ .../libertacasa/pubsh/web/WebApplication.java | 17 +++++++++- src/main/resources/templates/portal.html | 5 +++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 549e00a..37352b5 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ build/ ### VS Code ### .vscode/ + +### Vim ### +.metals/ diff --git a/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java b/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java index e0bfd24..b68a14a 100644 --- a/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java +++ b/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java @@ -1,6 +1,5 @@ package net.libertacasa.pubsh.web; -import java.time.Duration; import java.time.Instant; import org.springframework.boot.CommandLineRunner; @@ -10,20 +9,32 @@ import org.springframework.context.annotation.Configuration; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.Task; import com.github.kagkarlsson.scheduler.task.helper.Tasks; -import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; +//import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; @Configuration public class SchedulerBean { - @Bean - Task recurringSampleTask(CounterService counter) { - return Tasks - .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) - .execute((instance, ctx) -> { - System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); - CounterService.increase(); - }); - } +// @Bean +// Task recurringSampleTask(CounterService counter) { +// return Tasks +// .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) +// .execute((instance, ctx) -> { +// System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); +// CounterService.increase(); +// }); +// } + + @Bean + public static Task shellRemovalTask() { + + return Tasks.oneTime("shell-removal") + .execute((instance, ctx) -> { + System.out.printf("Running container removal task - Instance: %s, ctx: %s\n", instance, ctx); + String username = instance.getId().split("&")[0]; + String containerid = instance.getId().split("&")[1]; + Docker.deleteShell(username, containerid); + }); + } @Bean Task sampleOneTimeTask() { @@ -33,6 +44,7 @@ public class SchedulerBean { }); } + // keeping this as a quick way to check if the scheduler booted up after an application restart @Bean CommandLineRunner executeOnStartup(Scheduler scheduler, Task sampleOneTimeTask) { System.out.println("Scheduling one-shot testing task to execute now."); diff --git a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java index e04aa48..5407256 100644 --- a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java +++ b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java @@ -1,6 +1,7 @@ package net.libertacasa.pubsh.web; import java.security.Principal; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -13,6 +14,7 @@ import org.keycloak.adapters.KeycloakDeployment; import org.keycloak.adapters.RefreshableKeycloakSecurityContext; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.representations.IDToken; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.MediaType; @@ -28,10 +30,19 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.Image; +import com.github.kagkarlsson.scheduler.Scheduler; @SpringBootApplication @Controller public class WebApplication { + + //private final CounterService counter; + private static Scheduler scheduler; + + @Autowired + public void Scheduler(Scheduler scheduler) { + WebApplication.scheduler = scheduler; + } static ArrayList availableOs = new ArrayList(); @@ -228,15 +239,18 @@ public class WebApplication { Map customClaims = token.getOtherClaims(); username = String.valueOf(customClaims.get("username")); String os = (String) body.getFirst("osChoice"); + Integer expiry = (Integer) Integer.parseInt(body.getFirst("expiry")); System.out.printf("New shell with OS %s requested by %s (%s)\n", os, userid, username); Random rand = new Random(); Integer randomInt = rand.nextInt(9999999-1111); Integer count = randomInt; - + try { String containerid = Docker.createShell(username, os, count); + String instanceid = username + "&" + containerid; + scheduler.schedule(SchedulerBean.shellRemovalTask().instance(instanceid), Instant.now().plusSeconds(expiry)); String returnmessage = "Success - spawned shell " + username + "_" + os + "_" + count + " - internal ID: " + containerid; redirectAttributes.addFlashAttribute("message", returnmessage); } catch (Exception exception) { @@ -244,6 +258,7 @@ public class WebApplication { String returnmessage = "Error - failed to build image :-("; redirectAttributes.addFlashAttribute("message", returnmessage); System.out.println(exception); + //exception.printStackTrace(); } return("redirect:/portal"); diff --git a/src/main/resources/templates/portal.html b/src/main/resources/templates/portal.html index a9319a4..524f878 100644 --- a/src/main/resources/templates/portal.html +++ b/src/main/resources/templates/portal.html @@ -18,6 +18,11 @@ th:attr="value=${osoption}" th:text="${osoption}"> +