From c6ca8b44f29e565066642bfe2cf4084640d5d42e Mon Sep 17 00:00:00 2001 From: Oleksii Kurinnyi Date: Sat, 21 Dec 2019 12:02:10 +0200 Subject: [PATCH] Fix restarting a workspace from iframe in single-user mode (#15549) * Fix restarting a workspace from iframe Signed-off-by: Oleksii Kurinnyi * Check keycloak presence before updating keycloak token Signed-off-by: Oleksii Kurinnyi --- .../app/ide/ide-iframe/ide-iframe.service.ts | 49 +++++++++++-------- .../api/workspace/che-workspace.factory.ts | 8 +-- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/dashboard/src/app/ide/ide-iframe/ide-iframe.service.ts b/dashboard/src/app/ide/ide-iframe/ide-iframe.service.ts index 9ee46a0a9f..c691e6e979 100644 --- a/dashboard/src/app/ide/ide-iframe/ide-iframe.service.ts +++ b/dashboard/src/app/ide/ide-iframe/ide-iframe.service.ts @@ -31,6 +31,7 @@ interface IIdeIFrameRootScope extends ng.IRootScopeService { class IdeIFrameSvc { static $inject = [ + '$q', '$window', '$location', '$rootScope', @@ -40,6 +41,7 @@ class IdeIFrameSvc { 'cheKeycloak' ]; + private $q: ng.IQService; private $location: ng.ILocationService; private $rootScope: IIdeIFrameRootScope; private $mdSidenav: ng.material.ISidenavService; @@ -51,6 +53,7 @@ class IdeIFrameSvc { * Default constructor that is using resource */ constructor( + $q: ng.IQService, $window: ng.IWindowService, $location: ng.ILocationService, $rootScope: IIdeIFrameRootScope, @@ -59,6 +62,7 @@ class IdeIFrameSvc { ideSvc: IdeSvc, cheKeycloak: CheKeycloak ) { + this.$q = $q; this.$location = $location; this.$rootScope = $rootScope; this.$mdSidenav = $mdSidenav; @@ -140,30 +144,33 @@ class IdeIFrameSvc { * restart-workspace:${workspaceId}:${token} * Where * 'restart-workspace' - action name - * ${workspaceId} - workpsace ID + * ${workspaceId} - workspace ID * ${token} - Che machine token to validate */ private restartWorkspace(message: string): void { - // cut action name - message = message.substring(message.indexOf(':') + 1); + const [actionName, workspaceId, token] = message.split(':'); - // get workpsace ID - const workspaceId = message.substring(0, message.indexOf(':')); - - // get Che machine token - const token = message.substring(message.indexOf(':') + 1); - - this.cheWorkspace.validateMachineToken(workspaceId, token).then(() => { + // validate machine token if it's necessary + const machineTokenValidationDefer = this.$q.defer(); + if (this.cheKeycloak.isPresent()) { + this.cheWorkspace.validateMachineToken(workspaceId, token).then(() => { + machineTokenValidationDefer.resolve(); + }, () => { + machineTokenValidationDefer.reject('Machine token is not valid.'); + }); + } else { + machineTokenValidationDefer.resolve(); + } + // restart the workspace + machineTokenValidationDefer.promise.then(() => { this.cheWorkspace.fetchStatusChange(workspaceId, WorkspaceStatus[WorkspaceStatus.STOPPING]).then(() => { this.ideSvc.reloadIdeFrame(); }); - this.cheWorkspace.stopWorkspace(workspaceId).catch((error) => { - console.error('Unable to stop workspace. ', error); - }); - }).catch(() => { - console.error('Unable to stop workspace: token is not valid.'); + return this.cheWorkspace.stopWorkspace(workspaceId); + }).catch((err: any) => { + console.error('Unable to stop workspace:', err); }); } @@ -173,11 +180,13 @@ class IdeIFrameSvc { private updateToken(msg: string): void { const [actionName, validityTimeStr] = msg.split(':'); - const validityTimeMs = parseInt(validityTimeStr, 10); - const validityTimeSec = Number.isNaN(validityTimeMs) ? 5 : Math.ceil(validityTimeMs / 1000); - this.cheKeycloak.updateToken(validityTimeSec).catch(() => { - console.warn('Cannot refresh keycloak token'); - }); + if (this.cheKeycloak.isPresent()) { + const validityTimeMs = parseInt(validityTimeStr, 10); + const validityTimeSec = Number.isNaN(validityTimeMs) ? 5 : Math.ceil(validityTimeMs / 1000); + this.cheKeycloak.updateToken(validityTimeSec).catch(() => { + console.warn('Cannot refresh keycloak token'); + }); + } } /** diff --git a/dashboard/src/components/api/workspace/che-workspace.factory.ts b/dashboard/src/components/api/workspace/che-workspace.factory.ts index cd42c5b412..2807507060 100644 --- a/dashboard/src/components/api/workspace/che-workspace.factory.ts +++ b/dashboard/src/components/api/workspace/che-workspace.factory.ts @@ -409,14 +409,14 @@ export class CheWorkspace { /** * Validates machine token for the workspace - * + * * @param workspaceId workspace ID * @param token Che machine token */ - validateMachineToken(workspaceId: string, token: string): ng.IPromise { - const defer = this.$q.defer(); + validateMachineToken(workspaceId: string, token: string): ng.IPromise { + const defer = this.$q.defer(); - const promise: ng.IHttpPromise = this.$http.get(`/api/workspace/${workspaceId}`, { + const promise: ng.IHttpPromise = this.$http.get(`/api/workspace/${workspaceId}`, { headers: { 'Authorization': `Bearer ${token}` }