Search in sources :

Example 6 with SshPairImpl

use of org.eclipse.che.api.ssh.server.model.impl.SshPairImpl in project che by eclipse.

the class SshTckModule method configure.

@Override
protected void configure() {
    H2DBTestServer server = H2DBTestServer.startDefault();
    install(new PersistTestModuleBuilder().setDriver(Driver.class).runningOn(server).addEntityClasses(SshPairImpl.class, UserImpl.class, AccountImpl.class).setExceptionHandler(H2ExceptionHandler.class).build());
    bind(DBInitializer.class).asEagerSingleton();
    bind(SchemaInitializer.class).toInstance(new FlywaySchemaInitializer(server.getDataSource(), "che-schema"));
    bind(TckResourcesCleaner.class).toInstance(new H2JpaCleaner(server));
    bind(SshDao.class).to(JpaSshDao.class);
    bind(new TypeLiteral<TckRepository<SshPairImpl>>() {
    }).toInstance(new JpaTckRepository<>(SshPairImpl.class));
    bind(new TypeLiteral<TckRepository<UserImpl>>() {
    }).toInstance(new JpaTckRepository<>(UserImpl.class));
}
Also used : TckResourcesCleaner(org.eclipse.che.commons.test.tck.TckResourcesCleaner) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) H2DBTestServer(org.eclipse.che.commons.test.db.H2DBTestServer) AccountImpl(org.eclipse.che.account.spi.AccountImpl) Driver(org.h2.Driver) H2JpaCleaner(org.eclipse.che.commons.test.db.H2JpaCleaner) SshDao(org.eclipse.che.api.ssh.server.spi.SshDao) PersistTestModuleBuilder(org.eclipse.che.commons.test.db.PersistTestModuleBuilder) SchemaInitializer(org.eclipse.che.core.db.schema.SchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) TypeLiteral(com.google.inject.TypeLiteral) DBInitializer(org.eclipse.che.core.db.DBInitializer) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl)

Example 7 with SshPairImpl

use of org.eclipse.che.api.ssh.server.model.impl.SshPairImpl in project che by eclipse.

the class KeysInjectorTest method shouldInjectSshKeysWhenThereIsNoPublicWorkspaceKeyButMachineKeys.

/**
     * Validate the usecase: There is a workspace sshkeypair (without public key) but there is a machine keypair
     * Expect that only the machine keypair is injected (as workspace keypair has no public key).
     */
@Test
public void shouldInjectSshKeysWhenThereIsNoPublicWorkspaceKeyButMachineKeys() throws Exception {
    // no machine key pairs
    when(sshManager.getPairs(anyString(), eq("machine"))).thenReturn(Arrays.asList(new SshPairImpl(OWNER, "machine", "myPair", "publicKey1", null)));
    // workspace keypair without public key
    when(sshManager.getPair(anyString(), eq("workspace"), anyString())).thenReturn(new SshPairImpl(OWNER, "workspace", WORKSPACE_ID, null, null));
    subscriber.onEvent(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withMachineId(MACHINE_ID).withWorkspaceId(WORKSPACE_ID));
    verify(environmentEngine).getMachine(eq(WORKSPACE_ID), eq(MACHINE_ID));
    // check calls for machine and workspace ssh pairs
    verify(sshManager).getPairs(eq(OWNER), eq("machine"));
    verify(sshManager).getPair(eq(OWNER), eq("workspace"), eq(WORKSPACE_ID));
    ArgumentCaptor<CreateExecParams> argumentCaptor = ArgumentCaptor.forClass(CreateExecParams.class);
    verify(docker).createExec(argumentCaptor.capture());
    assertEquals(argumentCaptor.getValue().getCmd(), new String[] { "/bin/bash", "-c", "mkdir ~/.ssh/ -p" + "&& echo 'publicKey1' >> ~/.ssh/authorized_keys" });
    verify(docker).startExec(eq(StartExecParams.create(EXEC_ID)), anyObject());
    verifyZeroInteractions(docker, environmentEngine, sshManager);
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) CreateExecParams(org.eclipse.che.plugin.docker.client.params.CreateExecParams) Test(org.testng.annotations.Test)

Example 8 with SshPairImpl

use of org.eclipse.che.api.ssh.server.model.impl.SshPairImpl in project che by eclipse.

the class KeysInjectorTest method shouldInjectSshKeysWhenThereIsOnlyWorkspaceKey.

/**
     * Validate the usecase: There is a workspace sshkeypair but no machine keypair (empty list)
     * Expect that the workspace public key is injected.
     */
@Test
public void shouldInjectSshKeysWhenThereIsOnlyWorkspaceKey() throws Exception {
    // no machine key pairs
    when(sshManager.getPairs(anyString(), eq("machine"))).thenReturn(Collections.emptyList());
    // workspace keypair
    when(sshManager.getPair(anyString(), eq("workspace"), anyString())).thenReturn(new SshPairImpl(OWNER, "workspace", WORKSPACE_ID, "publicKeyWorkspace", null));
    subscriber.onEvent(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withMachineId(MACHINE_ID).withWorkspaceId(WORKSPACE_ID));
    verify(environmentEngine).getMachine(eq(WORKSPACE_ID), eq(MACHINE_ID));
    // check calls for machine and workspace ssh pairs
    verify(sshManager).getPairs(eq(OWNER), eq("machine"));
    verify(sshManager).getPair(eq(OWNER), eq("workspace"), eq(WORKSPACE_ID));
    ArgumentCaptor<CreateExecParams> argumentCaptor = ArgumentCaptor.forClass(CreateExecParams.class);
    verify(docker).createExec(argumentCaptor.capture());
    assertEquals(argumentCaptor.getValue().getCmd(), new String[] { "/bin/bash", "-c", "mkdir ~/.ssh/ -p" + "&& echo 'publicKeyWorkspace' >> ~/.ssh/authorized_keys" });
    verify(docker).startExec(eq(StartExecParams.create(EXEC_ID)), anyObject());
    verifyZeroInteractions(docker, environmentEngine, sshManager);
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) CreateExecParams(org.eclipse.che.plugin.docker.client.params.CreateExecParams) Test(org.testng.annotations.Test)

Example 9 with SshPairImpl

use of org.eclipse.che.api.ssh.server.model.impl.SshPairImpl in project che by eclipse.

the class SshService method generatePair.

@POST
@Path("generate")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@GenerateLink(rel = Constants.LINK_REL_GENERATE_PAIR)
@ApiOperation(value = "Generate and stores ssh pair based on the request", notes = "This operation can be performed only by authorized user," + "this user will be the owner of the created ssh pair", response = SshPairDto.class)
@ApiResponses({ @ApiResponse(code = 201, message = "The ssh pair successfully generated"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 409, message = "Conflict error occurred during the ssh pair generation" + "(e.g. The Ssh pair with such name and service already exists)"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public Response generatePair(@ApiParam(value = "The configuration to generate the new ssh pair", required = true) GenerateSshPairRequest request) throws BadRequestException, ServerException, ConflictException {
    requiredNotNull(request, "Generate ssh pair request required");
    requiredNotNull(request.getService(), "Service name required");
    requiredNotNull(request.getName(), "Name required");
    final SshPairImpl generatedPair = sshManager.generatePair(getCurrentUserId(), request.getService(), request.getName());
    return Response.status(Response.Status.CREATED).entity(asDto(injectLinks(asDto(generatedPair)))).build();
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with SshPairImpl

use of org.eclipse.che.api.ssh.server.model.impl.SshPairImpl in project che by eclipse.

the class SshService method createPair.

@POST
@Consumes(APPLICATION_JSON)
@GenerateLink(rel = Constants.LINK_REL_CREATE_PAIR)
@ApiOperation(value = "Create a new ssh pair", notes = "This operation can be performed only by authorized user," + "this user will be the owner of the created ssh pair")
@ApiResponses({ @ApiResponse(code = 204, message = "The ssh pair successfully created"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 409, message = "Conflict error occurred during the ssh pair creation" + "(e.g. The Ssh pair with such name and service already exists)"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public void createPair(@ApiParam(value = "The ssh pair to create", required = true) SshPairDto sshPair) throws BadRequestException, ServerException, ConflictException {
    requiredNotNull(sshPair, "Ssh pair required");
    requiredNotNull(sshPair.getService(), "Service name required");
    requiredNotNull(sshPair.getName(), "Name required");
    if (sshPair.getPublicKey() == null && sshPair.getPrivateKey() == null) {
        throw new BadRequestException("Key content was not provided.");
    }
    sshManager.createPair(new SshPairImpl(getCurrentUserId(), sshPair));
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) BadRequestException(org.eclipse.che.api.core.BadRequestException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

SshPairImpl (org.eclipse.che.api.ssh.server.model.impl.SshPairImpl)15 Test (org.testng.annotations.Test)7 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 GenerateLink (org.eclipse.che.api.core.rest.annotations.GenerateLink)3 TypeLiteral (com.google.inject.TypeLiteral)2 Transactional (com.google.inject.persist.Transactional)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Produces (javax.ws.rs.Produces)2 AccountImpl (org.eclipse.che.account.spi.AccountImpl)2 BadRequestException (org.eclipse.che.api.core.BadRequestException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 ServerException (org.eclipse.che.api.core.ServerException)2 SshDao (org.eclipse.che.api.ssh.server.spi.SshDao)2 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)2 CreateExecParams (org.eclipse.che.plugin.docker.client.params.CreateExecParams)2 JSchException (com.jcraft.jsch.JSchException)1 KeyPair (com.jcraft.jsch.KeyPair)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1