From 0ba8d922ef486854da01603410952506a7755635 Mon Sep 17 00:00:00 2001 From: "Michael N. Lipp" Date: Thu, 14 Nov 2024 18:46:19 +0100 Subject: [PATCH] Add used by information. --- deploy/crds/vms-crd.yaml | 6 ++++++ .../manager/events/GetDisplayPassword.java | 16 ++++++++++++++-- .../manager/DisplaySecretMonitor.java | 17 ++++++++++++++++- .../jdrupes/vmoperator/vmconlet/l10n.properties | 1 + .../vmoperator/vmconlet/l10n_de.properties | 1 + .../vmconlet/browser/VmConlet-functions.ts | 4 +++- .../jdrupes/vmoperator/vmviewer/VmViewer.java | 9 ++++++--- 7 files changed, 47 insertions(+), 7 deletions(-) diff --git a/deploy/crds/vms-crd.yaml b/deploy/crds/vms-crd.yaml index f1bbaf2..93c70cc 100644 --- a/deploy/crds/vms-crd.yaml +++ b/deploy/crds/vms-crd.yaml @@ -1467,6 +1467,12 @@ spec: The hostname of the currently connected client. type: string default: "" + consoleUser: + description: >- + The id of the user who has last requested a console + connection. + type: string + default: "" displayPasswordSerial: description: >- Counts changes of the display password. Set to -1 diff --git a/org.jdrupes.vmoperator.manager.events/src/org/jdrupes/vmoperator/manager/events/GetDisplayPassword.java b/org.jdrupes.vmoperator.manager.events/src/org/jdrupes/vmoperator/manager/events/GetDisplayPassword.java index 3322f1a..f6fa555 100644 --- a/org.jdrupes.vmoperator.manager.events/src/org/jdrupes/vmoperator/manager/events/GetDisplayPassword.java +++ b/org.jdrupes.vmoperator.manager.events/src/org/jdrupes/vmoperator/manager/events/GetDisplayPassword.java @@ -29,14 +29,17 @@ import org.jgrapes.core.Event; public class GetDisplayPassword extends Event { private final VmDefinition vmDef; + private final String user; /** - * Instantiates a new returns the display secret. + * Instantiates a new request for the display secret. * * @param vmDef the vm name + * @param user the requesting user */ - public GetDisplayPassword(VmDefinition vmDef) { + public GetDisplayPassword(VmDefinition vmDef, String user) { this.vmDef = vmDef; + this.user = user; } /** @@ -48,6 +51,15 @@ public class GetDisplayPassword extends Event { return vmDef; } + /** + * Return the id of the user who has requested the password. + * + * @return the string + */ + public String user() { + return user; + } + /** * Return the password. May only be called when the event is completed. * diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/DisplaySecretMonitor.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/DisplaySecretMonitor.java index 69d4058..2f480a3 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/DisplaySecretMonitor.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/DisplaySecretMonitor.java @@ -18,6 +18,8 @@ package org.jdrupes.vmoperator.manager; +import com.google.gson.JsonObject; +import io.kubernetes.client.apimachinery.GroupVersionKind; import io.kubernetes.client.custom.V1Patch; import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.models.V1Secret; @@ -37,10 +39,13 @@ import java.util.Optional; import java.util.Scanner; import java.util.logging.Level; import static org.jdrupes.vmoperator.common.Constants.APP_NAME; +import static org.jdrupes.vmoperator.common.Constants.VM_OP_GROUP; +import static org.jdrupes.vmoperator.common.Constants.VM_OP_KIND_VM; import static org.jdrupes.vmoperator.common.Constants.VM_OP_NAME; import org.jdrupes.vmoperator.common.K8sClient; import org.jdrupes.vmoperator.common.K8sV1PodStub; import org.jdrupes.vmoperator.common.K8sV1SecretStub; +import org.jdrupes.vmoperator.common.VmDefinitionStub; import static org.jdrupes.vmoperator.manager.Constants.COMP_DISPLAY_SECRET; import static org.jdrupes.vmoperator.manager.Constants.DATA_DISPLAY_PASSWORD; import static org.jdrupes.vmoperator.manager.Constants.DATA_PASSWORD_EXPIRY; @@ -181,12 +186,22 @@ public class DisplaySecretMonitor + "app.kubernetes.io/instance=" + event.vmDefinition().metadata().getName()); var stubs = K8sV1SecretStub.list(client(), - event.vmDefinition().metadata().getNamespace(), options); + event.vmDefinition().namespace(), options); if (stubs.isEmpty()) { return; } var stub = stubs.iterator().next(); + // Valid request, update console user in status + var vmStub = VmDefinitionStub.get(client(), + new GroupVersionKind(VM_OP_GROUP, "", VM_OP_KIND_VM), + event.vmDefinition().namespace(), event.vmDefinition().name()); + vmStub.updateStatus(from -> { + JsonObject status = from.status(); + status.addProperty("consoleUser", event.user()); + return status; + }); + // Check validity var model = stub.model().get(); @SuppressWarnings("PMD.StringInstantiation") diff --git a/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n.properties b/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n.properties index f4165c4..4ab3a3f 100644 --- a/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n.properties +++ b/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n.properties @@ -11,6 +11,7 @@ nodeName = Node requestedCpus = Requested CPUs requestedRam = Requested RAM running = Running +usedBy = Used by usedFrom = Used from vmActions = Actions vmname = Name diff --git a/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n_de.properties b/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n_de.properties index 29239ed..15a8b68 100644 --- a/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n_de.properties +++ b/org.jdrupes.vmoperator.vmconlet/resources/org/jdrupes/vmoperator/vmconlet/l10n_de.properties @@ -15,6 +15,7 @@ maximumRam = Maximales RAM nodeName = Knoten requestedCpus = Angeforderte CPUs requestedRam = Angefordertes RAM +usedBy = Benutzt durch usedFrom = Benutzt von vmActions = Aktionen vmname = Name diff --git a/org.jdrupes.vmoperator.vmconlet/src/org/jdrupes/vmoperator/vmconlet/browser/VmConlet-functions.ts b/org.jdrupes.vmoperator.vmconlet/src/org/jdrupes/vmoperator/vmconlet/browser/VmConlet-functions.ts index b171569..cfda2de 100644 --- a/org.jdrupes.vmoperator.vmconlet/src/org/jdrupes/vmoperator/vmconlet/browser/VmConlet-functions.ts +++ b/org.jdrupes.vmoperator.vmconlet/src/org/jdrupes/vmoperator/vmconlet/browser/VmConlet-functions.ts @@ -112,7 +112,8 @@ window.orgJDrupesVmOperatorVmConlet.initView = (viewDom: HTMLElement, ["currentCpus", "currentCpus"], ["currentRam", "currentRam"], ["nodeName", "nodeName"], - ["usedFrom", "usedFrom"] + ["usedFrom", "usedFrom"], + ["usedBy", "usedBy"] ], { sortKey: "name", sortOrder: "up" @@ -181,6 +182,7 @@ JGConsole.registerConletFunction("org.jdrupes.vmoperator.vmconlet.VmConlet", vmDefinition.currentCpus = vmDefinition.status.cpus; vmDefinition.currentRam = Number(vmDefinition.status.ram); vmDefinition.usedFrom = vmDefinition.status.consoleClient || ""; + vmDefinition.usedBy = vmDefinition.status.consoleUser || ""; for (const condition of vmDefinition.status.conditions) { if (condition.type === "Running") { vmDefinition.running = condition.status === "True"; diff --git a/org.jdrupes.vmoperator.vmviewer/src/org/jdrupes/vmoperator/vmviewer/VmViewer.java b/org.jdrupes.vmoperator.vmviewer/src/org/jdrupes/vmoperator/vmviewer/VmViewer.java index a21c420..f87b341 100644 --- a/org.jdrupes.vmoperator.vmviewer/src/org/jdrupes/vmoperator/vmviewer/VmViewer.java +++ b/org.jdrupes.vmoperator.vmviewer/src/org/jdrupes/vmoperator/vmviewer/VmViewer.java @@ -527,9 +527,12 @@ public class VmViewer extends FreeMarkerConlet { break; case "openConsole": if (perms.contains(Permission.ACCESS_CONSOLE)) { - var pwQuery = Event.onCompletion(new GetDisplayPassword(vmDef), - e -> openConsole(vmName, channel, model, - e.password().orElse(null))); + var user = WebConsoleUtils.userFromSession(channel.session()) + .map(ConsoleUser::getName).orElse(""); + var pwQuery + = Event.onCompletion(new GetDisplayPassword(vmDef, user), + e -> openConsole(vmName, channel, model, + e.password().orElse(null))); fire(pwQuery, vmChannel); } break;