Checkpoint

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2022-05-06 18:25:22 +02:00
parent 65688954c9
commit 20d049ea10
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
4 changed files with 81 additions and 25 deletions

View File

@ -14,13 +14,16 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
public class API {
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
Random rand = new Random();
Integer randomInt = rand.nextInt(100-10);
return String.format("Hello %s! Your random number is %s!", name, randomInt);
}
@DeleteMapping("/backend/container/delete/{id}")

View File

@ -1,11 +1,15 @@
package net.libertacasa.pubsh.web;
import java.io.File;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.BuildImageResultCallback;
import com.github.dockerjava.api.command.InspectExecResponse.Container;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.model.Image;
@ -77,4 +81,17 @@ public class Docker {
dockerClient.removeContainerCmd(id).exec();
}
public static String buildImage(String targetUser, String osChoice, Integer count) {
String dockerfile = "classpath:docker/Dockerfile-" + osChoice;
String tag = targetUser + ":sh" + count;
Set<String> tags = new HashSet<String>();
tags.add(tag);
String imgid = dockerClient.buildImageCmd()
.withDockerfile(new File(dockerfile))
.withPull(false).withNoCache(false).withTags(tags)
.exec(new BuildImageResultCallback()).awaitImageId();
return(imgid);
}
}

View File

@ -1,6 +1,7 @@
package net.libertacasa.pubsh.web;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -21,12 +22,15 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
//import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Image;
import java.util.Random;
@SpringBootApplication
@ -81,7 +85,17 @@ public class WebApplication {
//System.out.println(containers);
model.addAttribute("docker_images", images);
model.addAttribute("docker_containers", containers);
model.addAttribute("docker_containers", containers);
ArrayList<String> availableOs = new ArrayList<String>();
availableOs.add("archlinux");
availableOs.add("opensuse-leap");
availableOs.add("opensuse-tumbleweed");
availableOs.add("ubuntu");
model.addAttribute("availableOs", availableOs);
model.addAttribute("osChoice", new String());
return("portal");
}
@ -104,6 +118,30 @@ public class WebApplication {
return("redirect:/portal");
}
@PostMapping("/frontend/container/add")
public static String addContainer(@PathVariable String osChoice, HttpServletRequest request, Model model) {
KeycloakAuthenticationToken principal = (KeycloakAuthenticationToken) request.getUserPrincipal();
String username= null;
String userid = principal.getName();
IDToken token = principal.getAccount().getKeycloakSecurityContext().getIdToken();
Map<String, Object> customClaims = token.getOtherClaims();
username = String.valueOf(customClaims.get("username"));
model.addAttribute("osChoice", osChoice);
System.out.printf("New container with OS %s requested by %s (%s)", osChoice, userid, username);
Random rand = new Random();
Integer randomInt = rand.nextInt(9999999-1111);
Integer count = randomInt;
//Docker.buildImage(username, osChoice, count);
return("redirect:/portal");
}
@GetMapping(path = "/logout")
public String logout(HttpServletRequest request) throws ServletException {
request.logout();

View File

@ -8,14 +8,30 @@
Hello, <span th:text="${username}"></span>.
</h1>
<div th:if="${attribute01 != null}" th:text="${attribute01}"></div>
<h2>Docker Images:</h2>
<h2>Available Docker Images:</h2>
<table>
<tr th:each="image: ${docker_images}" th:if="${image.repoTags[0] != '<none>:<none>'}">
<td th:text="${image.repoTags[0]}" />
<td th:text="${image.created}" />
</tr>
</table>
<h2>Docker Containers:</h2>
<h2>Generate new throw-away shell:</h2>
<select th:field="*{availableOs}" class="form-control" id="osChoice" name="osChoice">
<option value="">Select operating system ...</option>
<option
th:each="osoption : ${availableOs}"
th:value="${osoption}"
th:text="${osoption}"></option>
</select>
<form th:object="${osChoice}" id="request_pseudoform" action="#" th:action="@{'/frontend/container/add}" th:method="post" th:os="${osChoice}" th:onsubmit="return confirm('You are about to generate a shell with the OS ' + this.getAttribute('os') + ' - please be patient after you confirm, as the generation may take a short while.');">
<button class="btn btn-primary" id="request_submission" type="submit">Generate</button>
</form>
<h2>Existing Containers:</h2>
<table>
<tr th:each="container: ${docker_containers}">
<td th:text="${container.names[0]}" />
@ -44,27 +60,9 @@
</tr>
</table>
<!--h4>We are currently running <span th:text="${docker_containercount}"></span> containers.</h4-->
<!--table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Service Rendered</th>
</tr>
</thead>
<tbody>
<tr th:each="customer : ${customers}">
<td th:text="${customer.id}">Text ...</td>
<td th:text="${customer.name}">Text ...</td>
<td th:text="${customer.address}">Text ...</td>
<td th:text="${customer.serviceRendered}">Text...</td>
</tr>
</tbody>
</table-->
<!--div id="pagefoot" th:include="layout :: footerFragment">Footer</div-->
<p></p>
<a href="/logout">Logout</a>