-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-2059 : Each time a workspace is created, register/associate ssh k…
…ey (#2949) * CHE-2059 : Each time a workspace is created, register/associate ssh key Change-Id: Ifddfe5398cffd1af31aa93beb5d0674b29270f4f Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
- Loading branch information
Showing
5 changed files
with
409 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...ext-server/src/main/java/org/eclipse/che/ide/ext/machine/server/ssh/WorkspaceSshKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.ext.machine.server.ssh; | ||
|
||
import org.eclipse.che.api.core.ConflictException; | ||
import org.eclipse.che.api.core.NotFoundException; | ||
import org.eclipse.che.api.core.ServerException; | ||
import org.eclipse.che.api.core.notification.EventService; | ||
import org.eclipse.che.api.core.notification.EventSubscriber; | ||
import org.eclipse.che.api.ssh.server.SshManager; | ||
import org.eclipse.che.api.workspace.server.event.WorkspaceCreatedEvent; | ||
import org.eclipse.che.api.workspace.server.event.WorkspaceRemovedEvent; | ||
import org.eclipse.che.commons.env.EnvironmentContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* Creates SSH keyPair each time a workspace is created (and delete it when workspace is removed) | ||
* | ||
* @author Florent Benoit | ||
*/ | ||
@Singleton // must be eager | ||
public class WorkspaceSshKeys { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static final Logger LOG = LoggerFactory.getLogger(WorkspaceSshKeys.class); | ||
|
||
/** | ||
* The event service used to subscribe on create and delete events on any workspaces. | ||
*/ | ||
private final EventService eventService; | ||
|
||
/** | ||
* SSH manager handling ssh keys. Used to generate ssh keypair or remove the default keypair when workspace is removed. | ||
*/ | ||
private final SshManager sshManager; | ||
|
||
|
||
/** | ||
* Default injection by using event service and ssh manager. | ||
* | ||
* @param eventService | ||
* used to get CREATE/DELETE events for workspace | ||
* @param sshManager | ||
* used to generate/remove default ssh keys | ||
*/ | ||
@Inject | ||
public WorkspaceSshKeys(final EventService eventService, final SshManager sshManager) { | ||
this.eventService = eventService; | ||
this.sshManager = sshManager; | ||
} | ||
|
||
/** | ||
* When component is initialized, subscribe to workspace events in order to generate/delete ssh keys. | ||
*/ | ||
@PostConstruct | ||
public void start() { | ||
eventService.subscribe(new EventSubscriber<WorkspaceCreatedEvent>() { | ||
@Override | ||
public void onEvent(WorkspaceCreatedEvent workspaceCreatedEvent) { | ||
// Register default SSH keypair for this workspace. | ||
try { | ||
sshManager.generatePair(EnvironmentContext.getCurrent().getSubject().getUserId(), "workspace", | ||
workspaceCreatedEvent.getWorkspace().getId()); | ||
} catch (ServerException | ConflictException e) { | ||
// Conflict shouldn't happen as workspace id is new each time. | ||
LOG.error("Unable to generate a default ssh pair for the workspace with ID {}", | ||
workspaceCreatedEvent.getWorkspace().getId(), e); | ||
} | ||
} | ||
}); | ||
|
||
eventService.subscribe(new EventSubscriber<WorkspaceRemovedEvent>() { | ||
@Override | ||
public void onEvent(WorkspaceRemovedEvent workspaceRemovedEvent) { | ||
// Unregister default SSH keypair for this workspace (if any) | ||
try { | ||
sshManager.removePair(EnvironmentContext.getCurrent().getSubject().getUserId(), "workspace", | ||
workspaceRemovedEvent.getWorkspace().getId()); | ||
} catch (NotFoundException e) { | ||
LOG.debug("Do not remove default keypair from workspace {} as it is not existing (workspace ID {})", | ||
workspaceRemovedEvent.getWorkspace().getConfig().getName(), | ||
workspaceRemovedEvent.getWorkspace().getId()); | ||
} catch (ServerException e) { | ||
LOG.error("Error when trying to remove default ssh pair for the workspace {} (workspace ID {})", | ||
workspaceRemovedEvent.getWorkspace().getConfig().getName(), workspaceRemovedEvent.getWorkspace().getId()); | ||
} | ||
} | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.