Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-2059 : Each time a workspace is created, register/associate ssh key #2949

Merged
merged 8 commits into from
Nov 4, 2016

Conversation

benoitf
Copy link
Contributor

@benoitf benoitf commented Oct 31, 2016

What does this PR do?

Adds ssh key for creator of workspace. This ssh key can be used to connect on machines

What issues does this PR fix or reference?

#2059

Previous behavior

no ssh key by default

New behavior

Create a workspace
SSH key is associated
You can use then this key to connect to the workspace machines.

Change-Id: Ifddfe5398cffd1af31aa93beb5d0674b29270f4f
Signed-off-by: Florent BENOIT fbenoit@codenvy.com

@benoitf benoitf added status/code-review This issue has a pull request posted for it and is awaiting code review completion by the community. kind/enhancement A feature request - must adhere to the feature request template. sprint/current labels Oct 31, 2016
@benoitf benoitf self-assigned this Oct 31, 2016
@benoitf
Copy link
Contributor Author

benoitf commented Oct 31, 2016

@sleshchenko @garagatyi if you could review
issue link is #2059

@codenvy-ci
Copy link

Build # 864 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/864/ to view the results.

@codenvy-ci
Copy link

Build # 868 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/868/ to view the results.

// build list of all pairs.
final List<String> publicKeys;
if (sshWorkspacePair != null && sshWorkspacePair.getPublicKey() != null) {
publicKeys = new ArrayList<>(publicMachineKeys.size() + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you create list for publicMachineKeys.size() + 1 but put there only one element publicKeys.add(sshWorkspacePair.getPublicKey());? Am I correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct
I need to fix my PR it is missing the other keys

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also tests are not on my PR I think I failed to merge changes to master

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like list creation and addition of publicMachineKeys should be out of if clause. Then size + 1 is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


// build list of all pairs.
final List<String> publicKeys;
if (sshWorkspacePair != null && sshWorkspacePair.getPublicKey() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

.filter(sshPair -> sshPair.getPublicKey() != null)
.map(SshPairImpl::getPublicKey)
.collect(Collectors.toList());

// get workspace keypair
SshPairImpl sshWorkspacePair = sshManager.getPair(machine.getOwner(), "workspace", event.getWorkspaceId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should catch NotFoundException here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to catch yes, else it won't work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@benoitf benoitf changed the title CHE-2059 : Each time a workspace is created, register/associate ssh key [WiP] CHE-2059 : Each time a workspace is created, register/associate ssh key Nov 1, 2016
// build list of all pairs.
final List<String> publicKeys;
if (sshWorkspacePair != null && sshWorkspacePair.getPublicKey() != null) {
publicKeys = new ArrayList<>(publicMachineKeys.size() + 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like list creation and addition of publicMachineKeys should be out of if clause. Then size + 1 is fine.


@Inject
public WorkspaceManager(WorkspaceDao workspaceDao,
WorkspaceRuntimes workspaceRegistry,
EventService eventService,
AccountManager accountManager,
@Named("che.workspace.auto_snapshot") boolean defaultAutoSnapshot,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that correct names were changed, please check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes fixed

@@ -877,6 +882,10 @@ private WorkspaceImpl doCreateWorkspace(WorkspaceConfig config,
.setAttributes(attributes)
.setTemporary(isTemporary)
.build();

// Register default SSH keypair for this workspace
SshPairImpl sshPair = this.sshManager.generatePair(EnvironmentContext.getCurrent().getSubject().getUserId(), "workspace", workspace.getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate what this operation does, because it is not obvious why result of this operation is never used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to capture the result (forgot to cleanup)

@@ -117,6 +118,8 @@
private AccountManager accountManager;
@Mock
private SnapshotDao snapshotDao;
@Mock
private SshManager sshManager;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please align variables, because author kept them aligned

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -784,7 +789,8 @@ public void shouldStartWorkspaceFromSnapshotUsingDefaultValueForAutoRestore() th
accountManager,
false,
true,
snapshotDao));
snapshotDao,
sshManager));
final WorkspaceImpl workspace = workspaceManager.createWorkspace(createConfig(), NAMESPACE);
when(workspaceDao.get(workspace.getId())).thenReturn(workspace);
when(runtimes.get(any())).thenThrow(new NotFoundException(""));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests to changed code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Change-Id: Ifddfe5398cffd1af31aa93beb5d0674b29270f4f
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
…e ssh key

Change-Id: I92b699f2d0744b7789d6b3d4a6cd68fc23ca8777
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@benoitf benoitf changed the title [WiP] CHE-2059 : Each time a workspace is created, register/associate ssh key CHE-2059 : Each time a workspace is created, register/associate ssh key Nov 2, 2016
Copy link

@garagatyi garagatyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codenvy-ci
Copy link

// build list of all pairs.
final List<String> publicKeys;
if (sshWorkspacePair != null && sshWorkspacePair.getPublicKey() != null) {
publicKeys = new ArrayList<>(publicMachineKeys.size() + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we create new list? Why we can't just add sshWorkspacePair to publicMachineKeys list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existing list may be immutable no ?


// Register default SSH keypair for this workspace.
try {
this.sshManager.generatePair(EnvironmentContext.getCurrent().getSubject().getUserId(), "workspace", workspace.getId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would better to rewrite this logic by events in ...api-ssh... or che-plugin-machine-ext-server module? Because then ssh module will contain logic related to ssh and workspace module won't contain logic related to ssh :)
@garagatyi WDYT?

Also should we remove default ssh key when workspace is removed?

Copy link

@garagatyi garagatyi Nov 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea.

Also should we remove default ssh key when workspace is removed?

I think so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • about events yes why not but then in api-ssh machine-ext-server is not at the right "level" as it needs to be at workspace level
    it would mean that api-ssh depends on api-workspace (in my PR it's the opposite)
  • on removal of ssh key yes make sense

Copy link
Member

@sleshchenko sleshchenko Nov 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api-ssh machine-ext-server is not at the right "level" as it needs to be at workspace level

The main problem is that machine-ext-server module has wrong name :)
We can see here only KeysInjector and Module with binding for it and for RecipeScriptDownloadService. So RecipeScriptDownloadService was moved to api-workspace but binding wasn't. So we should move binding to module in api-workspace or to OnpremisesIdeApiModule
Then machine-ext-server will contains only classes related to ssh keys and you can rename it as you wish, I think :)
WDYT?

…e ssh key

Change-Id: I65d149e973f6ebd77ac3fc4c5539373c0b3d672e
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@benoitf
Copy link
Contributor Author

benoitf commented Nov 2, 2016

@sleshchenko updated with events

…e ssh key

Change-Id: If2f6a5827ed162cd2f5a2d661fed77242ecf6c67
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
…e ssh key

Change-Id: I1e3d39ea38feb286f904161f46d77b1b53eb4315
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@codenvy-ci
Copy link

Build # 903 - FAILED

Please check console output at https://ci.codenvycorp.com/job/che-pullrequests-build/903/ to view the results.

…e ssh key

Change-Id: Icbd591ddc13c7e9ee4740dee41e674cc7c5cee45
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@codenvy-ci
Copy link

sshManager.removePair(EnvironmentContext.getCurrent().getSubject().getUserId(), "workspace",
workspaceRemovedEvent.getWorkspace().getId());
} catch (NotFoundException e) {
LOG.error(String.format("Do not remove default keypair from workspace %s as it is not existing (workspace ID %s)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not log this case as error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes copy/paste error fixed

@sleshchenko
Copy link
Member

Small question: when user 1 create workspace A and share it for user2. When user2 starts this workspace, he is not able to see this default ssh key. So he is not able to connect to it by ssh.
@benoitf It is OK?

…e ssh key

Change-Id: I96c1470f8ca973f50b6d12218cd6d419127228c1
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@benoitf
Copy link
Contributor Author

benoitf commented Nov 3, 2016

@sleshchenko I discussed that a couple of weeks ago with Tyler. It was OK that only owner of workspace has the key. Not all people to which we shared the workspace.

try {
final Instance machine = environmentEngine.getMachine(event.getWorkspaceId(),
event.getMachineId());
machine = environmentEngine.getMachine(event.getWorkspaceId(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we plan to inject keys into each machine or dev-machines only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each machine

@codenvy-ci
Copy link

workspaceRemovedEvent.getWorkspace().getId()));
} catch (ServerException e) {
LOG.error(String.format(
"Error when trying to remove default ssh pair for the workspace %s as it is not existing (workspace ID %s)",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why message says that "it is not existing" on server exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

workspaceRemovedEvent.getWorkspace().getConfig().getName(),
workspaceRemovedEvent.getWorkspace().getId()));
} catch (ServerException e) {
LOG.error(String.format(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use logger format with curly brackets instead of String.format()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

* @author Florent Benoit
*/
@Singleton // must be eager
public class WorkspaceSshKeys {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purpose of this class is not clear from its name, can you rename it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me the name is clear.
Also there is javadoc explaining the purpose so it's subjective

verifyZeroInteractions(docker, environmentEngine, sshManager);
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove not needed empty lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

EnvironmentContext.setCurrent(new EnvironmentContext() {
@Override
public Subject getSubject() {
return new SubjectImpl(NAMESPACE, OWNER_ID, "token", false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to revert changes in EnvironmentContext to not affect other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -1051,6 +1051,8 @@ public void shouldBeAbleToGetMachineInstanceIfWorkspaceIsStarting() throws Excep
verify(runtimes).getMachine(workspace.getId(), machine.getId());
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these extra lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

…e ssh key

Change-Id: Ia3acda4e8cdf3edbcdb6c17536a37b7310736f0f
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
@benoitf
Copy link
Contributor Author

benoitf commented Nov 3, 2016

@garagatyi fixed

@codenvy-ci
Copy link

@benoitf benoitf merged commit 4a86b5e into master Nov 4, 2016
@benoitf benoitf deleted the CHE-2059-2 branch November 4, 2016 09:31
@benoitf benoitf added this to the 5.0.0-M7 milestone Nov 4, 2016
@benoitf benoitf removed status/code-review This issue has a pull request posted for it and is awaiting code review completion by the community. sprint/current labels Nov 4, 2016
@bmicklea bmicklea mentioned this pull request Nov 16, 2016
66 tasks
JPinkney pushed a commit to JPinkney/che that referenced this pull request Aug 17, 2017
…ey (eclipse-che#2949)

* CHE-2059 : Each time a workspace is created, register/associate ssh key

Change-Id: Ifddfe5398cffd1af31aa93beb5d0674b29270f4f
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement A feature request - must adhere to the feature request template.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants