5050import static org .jdrupes .vmoperator .manager .Constants .DATA_DISPLAY_PASSWORD ;
5151import static org .jdrupes .vmoperator .manager .Constants .DATA_PASSWORD_EXPIRY ;
5252import org .jdrupes .vmoperator .manager .events .ChannelDictionary ;
53- import org .jdrupes .vmoperator .manager .events .GetDisplayPassword ;
53+ import org .jdrupes .vmoperator .manager .events .PrepareConsole ;
5454import org .jdrupes .vmoperator .manager .events .VmChannel ;
5555import org .jdrupes .vmoperator .manager .events .VmDefChanged ;
5656import org .jgrapes .core .Channel ;
@@ -72,7 +72,7 @@ public class DisplaySecretMonitor
7272 extends AbstractMonitor <V1Secret , V1SecretList , VmChannel > {
7373
7474 private int passwordValidity = 10 ;
75- private final List <PendingGet > pendingGets
75+ private final List <PendingGet > pendingPrepares
7676 = Collections .synchronizedList (new LinkedList <>());
7777 private final ChannelDictionary <String , VmChannel , ?> channelDictionary ;
7878
@@ -178,49 +178,59 @@ private void patchPod(K8sClient client, Response<V1Secret> change)
178178 */
179179 @ Handler
180180 @ SuppressWarnings ("PMD.StringInstantiation" )
181- public void onGetDisplaySecrets ( GetDisplayPassword event , VmChannel channel )
181+ public void onPrepareConsole ( PrepareConsole event , VmChannel channel )
182182 throws ApiException {
183183 // Update console user in status
184184 var vmStub = VmDefinitionStub .get (client (),
185185 new GroupVersionKind (VM_OP_GROUP , "" , VM_OP_KIND_VM ),
186186 event .vmDefinition ().namespace (), event .vmDefinition ().name ());
187- vmStub .updateStatus (from -> {
187+ var optVmDef = vmStub .updateStatus (from -> {
188188 JsonObject status = from .statusJson ();
189189 status .addProperty ("consoleUser" , event .user ());
190190 return status ;
191191 });
192+ if (optVmDef .isEmpty ()) {
193+ return ;
194+ }
195+ var vmDef = optVmDef .get ();
196+
197+ // Check if access is possible
198+ if (event .loginUser ()
199+ ? !vmDef .conditionStatus ("Booted" ).orElse (false )
200+ : !vmDef .conditionStatus ("Running" ).orElse (false )) {
201+ return ;
202+ }
192203
193204 // Look for secret
194205 ListOptions options = new ListOptions ();
195206 options .setLabelSelector ("app.kubernetes.io/name=" + APP_NAME + ","
196207 + "app.kubernetes.io/component=" + COMP_DISPLAY_SECRET + ","
197- + "app.kubernetes.io/instance="
198- + event .vmDefinition ().metadata ().getName ());
199- var stubs = K8sV1SecretStub .list (client (),
200- event .vmDefinition ().namespace (), options );
208+ + "app.kubernetes.io/instance=" + vmDef .name ());
209+ var stubs = K8sV1SecretStub .list (client (), vmDef .namespace (), options );
201210 if (stubs .isEmpty ()) {
202211 // No secret means no password for this VM wanted
212+ event .setResult (null );
203213 return ;
204214 }
205215 var stub = stubs .iterator ().next ();
206216
207217 // Check validity
208- var model = stub .model ().get ();
218+ var secret = stub .model ().get ();
209219 @ SuppressWarnings ("PMD.StringInstantiation" )
210- var expiry = Optional .ofNullable (model .getData ()
220+ var expiry = Optional .ofNullable (secret .getData ()
211221 .get (DATA_PASSWORD_EXPIRY )).map (b -> new String (b )).orElse (null );
212- if (model .getData ().get (DATA_DISPLAY_PASSWORD ) != null
222+ if (secret .getData ().get (DATA_DISPLAY_PASSWORD ) != null
213223 && stillValid (expiry )) {
214224 // Fixed secret, don't touch
215225 event .setResult (
216- new String (model .getData ().get (DATA_DISPLAY_PASSWORD )));
226+ new String (secret .getData ().get (DATA_DISPLAY_PASSWORD )));
217227 return ;
218228 }
219229 updatePassword (stub , event );
220230 }
221231
222232 @ SuppressWarnings ("PMD.StringInstantiation" )
223- private void updatePassword (K8sV1SecretStub stub , GetDisplayPassword event )
233+ private void updatePassword (K8sV1SecretStub stub , PrepareConsole event )
224234 throws ApiException {
225235 SecureRandom random = null ;
226236 try {
@@ -242,9 +252,9 @@ private void updatePassword(K8sV1SecretStub stub, GetDisplayPassword event)
242252 var pending = new PendingGet (event ,
243253 event .vmDefinition ().displayPasswordSerial ().orElse (0L ) + 1 ,
244254 new CompletionLock (event , 1500 ));
245- pendingGets .add (pending );
255+ pendingPrepares .add (pending );
246256 Event .onCompletion (event , e -> {
247- pendingGets .remove (pending );
257+ pendingPrepares .remove (pending );
248258 });
249259
250260 // Update, will (eventually) trigger confirmation
@@ -273,9 +283,9 @@ private boolean stillValid(String expiry) {
273283 @ Handler
274284 @ SuppressWarnings ("PMD.AvoidSynchronizedStatement" )
275285 public void onVmDefChanged (VmDefChanged event , Channel channel ) {
276- synchronized (pendingGets ) {
286+ synchronized (pendingPrepares ) {
277287 String vmName = event .vmDefinition ().name ();
278- for (var pending : pendingGets ) {
288+ for (var pending : pendingPrepares ) {
279289 if (pending .event .vmDefinition ().name ().equals (vmName )
280290 && event .vmDefinition ().displayPasswordSerial ()
281291 .map (s -> s >= pending .expectedSerial ).orElse (false )) {
@@ -293,7 +303,7 @@ public void onVmDefChanged(VmDefChanged event, Channel channel) {
293303 */
294304 @ SuppressWarnings ("PMD.DataClass" )
295305 private static class PendingGet {
296- public final GetDisplayPassword event ;
306+ public final PrepareConsole event ;
297307 public final long expectedSerial ;
298308 public final CompletionLock lock ;
299309
@@ -303,7 +313,7 @@ private static class PendingGet {
303313 * @param event the event
304314 * @param expectedSerial the expected serial
305315 */
306- public PendingGet (GetDisplayPassword event , long expectedSerial ,
316+ public PendingGet (PrepareConsole event , long expectedSerial ,
307317 CompletionLock lock ) {
308318 super ();
309319 this .event = event ;
0 commit comments