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

Generate a new SSH key if no keys are registered while ssh mount #14767

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package org.eclipse.che.workspace.infrastructure.kubernetes.provision;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
Expand All @@ -25,11 +27,13 @@
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.ssh.server.SshManager;
Expand Down Expand Up @@ -95,25 +99,33 @@ public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
throws InfrastructureException {
TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);

List<SshPairImpl> sshPairs = emptyList();
try {
List<SshPairImpl> sshPairs = sshManager.getPairs(identity.getOwnerId(), "vcs");
if (sshPairs.isEmpty()) {
sshPairs = sshManager.getPairs(identity.getOwnerId(), "vcs");
} catch (ServerException e) {
LOG.warn("Unable to get SSH Keys. Cause: {}", e.getMessage());
return;
}
if (sshPairs.isEmpty()) {
try {
sshPairs =
singletonList(
sshManager.generatePair(
identity.getOwnerId(), "vcs", "default-" + new Date().getTime()));
} catch (ServerException | ConflictException e) {
LOG.warn("Unable to generate the initial SSH key. Cause: {}", e.getMessage());
return;
}
}

StringBuilder sshConfigData = new StringBuilder();

for (SshPair sshPair : sshPairs) {
doProvisionSshKey(sshPair, k8sEnv, identity.getWorkspaceId());

sshConfigData.append(buildConfig(sshPair.getName()));
}

String sshConfigMapName = identity.getWorkspaceId() + SSH_CONFIG_MAP_NAME_SUFFIX;
doProvisionSshConfig(sshConfigMapName, sshConfigData.toString(), k8sEnv);
} catch (ServerException e) {
LOG.warn("Unable get SSH Keys. Cause: %s", e.getMessage(), e);
StringBuilder sshConfigData = new StringBuilder();
for (SshPair sshPair : sshPairs) {
doProvisionSshKey(sshPair, k8sEnv, identity.getWorkspaceId());
sshConfigData.append(buildConfig(sshPair.getName()));
}

String sshConfigMapName = identity.getWorkspaceId() + SSH_CONFIG_MAP_NAME_SUFFIX;
doProvisionSshConfig(sshConfigMapName, sshConfigData.toString(), k8sEnv);
}

private void doProvisionSshKey(SshPair sshPair, KubernetesEnvironment k8sEnv, String wsId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.provision;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
Expand Down Expand Up @@ -73,13 +74,16 @@ public void setup() {
}

@Test
public void doNotDoAnythingIfNoSshKeys() throws Exception {
public void generateSshKeyIfNoSshKeys() throws Exception {
when(sshManager.getPairs(someUser, "vcs")).thenReturn(Collections.emptyList());
when(sshManager.generatePair(eq(someUser), eq("vcs"), anyString()))
.thenReturn(
new SshPairImpl(
someUser, "vcs", "default-" + UUID.randomUUID().toString(), "public", "private"));

vcsSshKeysProvisioner.provision(k8sEnv, runtimeIdentity);

assertTrue(k8sEnv.getSecrets().isEmpty());
verifyZeroInteractions(podSpec);
assertEquals(k8sEnv.getSecrets().size(), 1);
}

@Test
Expand Down