diff options
author | Georg Pfuetzenreuter | 2022-05-08 04:16:19 +0200 |
---|---|---|
committer | Georg Pfuetzenreuter | 2022-05-08 04:16:19 +0200 |
commit | f105d0e1ab01fca7a04f54a8cacf6372a5b2d6c0 (patch) | |
tree | 3aa606b8f504acfece71f3cc2476b32afac0d42d /src/main/java/net | |
parent | 67ead5b62d0b1291bde0b49ac602be7cdcf3d536 (diff) | |
download | pubsh-web-f105d0e1ab01fca7a04f54a8cacf6372a5b2d6c0.tar.gz pubsh-web-f105d0e1ab01fca7a04f54a8cacf6372a5b2d6c0.tar.bz2 pubsh-web-f105d0e1ab01fca7a04f54a8cacf6372a5b2d6c0.zip |
User/Admin logic
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
Diffstat (limited to 'src/main/java/net')
-rw-r--r-- | src/main/java/net/libertacasa/pubsh/web/Docker.java | 42 | ||||
-rw-r--r-- | src/main/java/net/libertacasa/pubsh/web/WebApplication.java | 78 |
2 files changed, 105 insertions, 15 deletions
diff --git a/src/main/java/net/libertacasa/pubsh/web/Docker.java b/src/main/java/net/libertacasa/pubsh/web/Docker.java index ccac8bf..00341a6 100644 --- a/src/main/java/net/libertacasa/pubsh/web/Docker.java +++ b/src/main/java/net/libertacasa/pubsh/web/Docker.java @@ -6,11 +6,14 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Map; 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.CreateContainerResponse; +import com.github.dockerjava.api.command.ListContainersCmd; +import com.github.dockerjava.api.command.ListImagesCmd; import com.github.dockerjava.api.command.WaitContainerResultCallback; import com.github.dockerjava.api.model.Image; import com.github.dockerjava.core.DefaultDockerClientConfig; @@ -44,15 +47,28 @@ public class Docker { return imageList; } - public static List<Image> getImages() { + public static List<Image> getImages(String name) { List<Image> imageQuery; - imageQuery = dockerClient.listImagesCmd().exec(); + ListImagesCmd imageQueryCmd = dockerClient.listImagesCmd(); + + if (name != null) { + imageQueryCmd.getFilters().put("reference", Arrays.asList(name + "*")); //to-do: filter by nameshX instead + } + + imageQuery = imageQueryCmd.exec(); return imageQuery; - } + } - public static List<com.github.dockerjava.api.model.Container> getContainers() { - List<com.github.dockerjava.api.model.Container> containerQuery; - containerQuery = dockerClient.listContainersCmd().withShowAll(true).exec(); + public static List<com.github.dockerjava.api.model.Container> getContainers(String name) { + List<com.github.dockerjava.api.model.Container> containerQuery; + ListContainersCmd containerQueryCmd = dockerClient.listContainersCmd(); + + if (name != null) { + containerQueryCmd.getFilters().put("name", Arrays.asList(name + "*")); + } + + containerQuery = containerQueryCmd.withShowAll(true).exec(); + return containerQuery; } @@ -99,11 +115,19 @@ public class Docker { } public static void deleteImage(String username, String id) { - List<Image> imagequery = Docker.getImages(); + List<Image> imagequery = Docker.getImages(null); if (imagequery.toString().contains(id)) { - System.out.printf("Found image ID %s, deleting ...\n", id); - dockerClient.removeImageCmd(id).withImageId(id).exec(); + try { + System.out.printf("Found image ID %s, deleting ...\n", id); + dockerClient.removeImageCmd(id).withImageId(id).exec(); + } catch (com.github.dockerjava.api.exception.ConflictException exception) { + System.out.println("Image is still being used by a container."); + throw exception; + } + //needs to wait for callback, but there is none + //System.out.printf("%s", removeQuery); + } if (! imagequery.toString().contains(id)) { System.out.printf("Image ID %s not found.\n", id); diff --git a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java index ec3fbc8..d4094f6 100644 --- a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java +++ b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java @@ -33,6 +33,8 @@ import com.github.dockerjava.api.model.Image; @Controller public class WebApplication { + static ArrayList<String> availableOs = new ArrayList<String>(); + public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @@ -44,7 +46,7 @@ public class WebApplication { } @GetMapping("/portal") - public String portal(Model model) { + public String portal(Model model) throws pubshError { KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); @@ -52,14 +54,21 @@ public class WebApplication { String username=""; String email=""; String attribute01=""; - + + if (! (principal instanceof KeycloakPrincipal)) { + System.out.println("Fatal: received logon without Keycloak principal."); + throw new pubshError("Invalid request."); + } if (principal instanceof KeycloakPrincipal) { KeycloakPrincipal<?> kPrincipal = (KeycloakPrincipal<?>) principal; IDToken token = kPrincipal.getKeycloakSecurityContext().getIdToken(); //System.out.println("Token: " + token); Map<String, Object> customClaims = token.getOtherClaims(); - + if (! customClaims.containsKey("username")) { + System.out.println("Fatal: received logon without username."); + throw new pubshError("Invalid request."); + } if (customClaims.containsKey("email")) { email = String.valueOf(customClaims.get("email")); } @@ -75,9 +84,10 @@ public class WebApplication { model.addAttribute("attribute01", attribute01); } - List<Image> images = Docker.getImages(); + List<Image> images = Docker.getImages(null); + //System.out.println(images); - List<Container> containers = Docker.getContainers(); + List<Container> containers = Docker.getContainers(null); //System.out.println(containers); model.addAttribute("docker_images", images); @@ -94,6 +104,51 @@ public class WebApplication { return("portal"); } + @GetMapping("/user") + public String user(Model model) throws pubshError { + KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) + SecurityContextHolder.getContext().getAuthentication(); + Principal principal = (Principal) authentication.getPrincipal(); + String username=""; + String email=""; + String attribute01=""; + if (! (principal instanceof KeycloakPrincipal)) { + System.out.println("Fatal: received logon without Keycloak principal."); + throw new pubshError("Invalid request."); + } + if (principal instanceof KeycloakPrincipal) { + KeycloakPrincipal<?> kPrincipal = (KeycloakPrincipal<?>) principal; + IDToken token = kPrincipal.getKeycloakSecurityContext().getIdToken(); + Map<String, Object> customClaims = token.getOtherClaims(); + if (! customClaims.containsKey("username")) { + System.out.println("Fatal: received logon without username."); + throw new pubshError("Invalid request."); + } + if (customClaims.containsKey("email")) { + email = String.valueOf(customClaims.get("email")); + } + if (customClaims.containsKey("username")) { + username = String.valueOf(customClaims.get("username")); + } + if (customClaims.containsKey("attribute01")) { + attribute01 = String.valueOf(customClaims.get("attribute01")); + } + model.addAttribute("username", username); + model.addAttribute("principalid", principal.getName()); + model.addAttribute("email", email); + model.addAttribute("attribute01", attribute01); + } + + List<Image> images = Docker.getImages(username); + List<Container> containers = Docker.getContainers(username); + + model.addAttribute("docker_images", images); + model.addAttribute("docker_containers", containers); + model.addAttribute("availableOs", availableOs); + + return("user"); + } + @DeleteMapping("/frontend/container/delete/{id}") public static String deleteContainer(@PathVariable String id, HttpServletRequest request, RedirectAttributes redirectAttributes) { // [Start] This block should move to a logging method. It's only job is to print user details to the console. @@ -184,5 +239,16 @@ public class WebApplication { private RefreshableKeycloakSecurityContext getKeycloakSecurityContext(HttpServletRequest request){ return (RefreshableKeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName()); - } + } + + public class pubshError extends Exception + { + private static final long serialVersionUID = 1L; + + public pubshError(String message) + { + super(message); + } + } + } |