Fix restarting a workspace from iframe in single-user mode (#15549)

* Fix restarting a workspace from iframe

Signed-off-by: Oleksii Kurinnyi <okurinny@redhat.com>

* Check keycloak presence before updating keycloak token

Signed-off-by: Oleksii Kurinnyi <okurinny@redhat.com>
7.20.x
Oleksii Kurinnyi 2019-12-21 12:02:10 +02:00 committed by GitHub
parent 688115587b
commit c6ca8b44f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View File

@ -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<void>();
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');
});
}
}
/**

View File

@ -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<any> {
const defer = this.$q.defer();
validateMachineToken(workspaceId: string, token: string): ng.IPromise<void> {
const defer = this.$q.defer<void>();
const promise: ng.IHttpPromise<any> = this.$http.get(`/api/workspace/${workspaceId}`, {
const promise: ng.IHttpPromise<che.IWorkspace> = this.$http.get(`/api/workspace/${workspaceId}`, {
headers: {
'Authorization': `Bearer ${token}`
}