Search in sources :

Example 1 with SshPairImpl

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

the class SshDaoTest method shouldGetSshPairByNameOwnerAndService.

@Test
public void shouldGetSshPairByNameOwnerAndService() throws Exception {
    SshPairImpl sshPair = pairs[0];
    sshDao.get(sshPair.getOwner(), sshPair.getService(), sshPair.getName());
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) Test(org.testng.annotations.Test)

Example 2 with SshPairImpl

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

the class SshDaoTest method shouldGetSshPairListByNameAndService.

@Test
public void shouldGetSshPairListByNameAndService() throws Exception {
    SshPairImpl sshPair1 = pairs[0];
    SshPairImpl sshPair2 = pairs[1];
    assertEquals(sshPair1.getOwner(), sshPair2.getOwner(), "Owner must be the same");
    assertEquals(sshPair1.getService(), sshPair2.getService(), "Service must be the same");
    final List<SshPairImpl> found = sshDao.get(sshPair1.getOwner(), sshPair1.getService());
    assertEquals(new HashSet<>(found), new HashSet<>(asList(sshPair1, sshPair2)));
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) Test(org.testng.annotations.Test)

Example 3 with SshPairImpl

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

the class SshDaoTest method shouldRemoveSshKeyPair.

@Test(expectedExceptions = NotFoundException.class, dependsOnMethods = "shouldThrowNotFoundExceptionIfPairWithSuchNameOwnerAndServiceDoesNotExist")
public void shouldRemoveSshKeyPair() throws Exception {
    final SshPairImpl pair = pairs[4];
    try {
        sshDao.remove(pair.getOwner(), pair.getService(), pair.getName());
    } catch (NotFoundException x) {
        fail("SshKeyPair should be removed");
    }
    sshDao.get(pair.getOwner(), pair.getService(), pair.getName());
}
Also used : SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) Test(org.testng.annotations.Test)

Example 4 with SshPairImpl

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

the class SshManager method generatePair.

/**
     * Generates and stores ssh pair for specified user.
     *
     * @param owner
     *         the id of the user who will be the owner of the ssh pair
     * @param service
     *         service name pf ssh pair
     * @param name
     *         name of pair
     * @return instance of generated ssh pair
     * @throws ConflictException
     *         when given ssh pair cannot be generated or created
     * @throws ServerException
     *         when any other error occurs during ssh pair generating or creating
     */
public SshPairImpl generatePair(String owner, String service, String name) throws ServerException, ConflictException {
    KeyPair keyPair;
    try {
        keyPair = KeyPair.genKeyPair(genJSch, 2, 2048);
    } catch (JSchException e) {
        throw new ServerException("Failed to generate ssh pair.", e);
    }
    ByteArrayOutputStream privateBuff = new ByteArrayOutputStream();
    keyPair.writePrivateKey(privateBuff);
    ByteArrayOutputStream publicBuff = new ByteArrayOutputStream();
    keyPair.writePublicKey(publicBuff, null);
    final SshPairImpl generatedSshPair = new SshPairImpl(owner, service, name, publicBuff.toString(), privateBuff.toString());
    sshDao.create(generatedSshPair);
    return generatedSshPair;
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) ServerException(org.eclipse.che.api.core.ServerException) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 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(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
@GenerateLink(rel = Constants.LINK_REL_CREATE_PAIR)
public Response createPair(Iterator<FileItem> formData) throws BadRequestException, ServerException, ConflictException {
    String service = null;
    String name = null;
    String privateKey = null;
    String publicKey = null;
    while (formData.hasNext()) {
        FileItem item = formData.next();
        String fieldName = item.getFieldName();
        switch(fieldName) {
            case "service":
                service = item.getString();
                break;
            case "name":
                name = item.getString();
                break;
            case "privateKey":
                privateKey = item.getString();
                break;
            case "publicKey":
                publicKey = item.getString();
                break;
            default:
        }
    }
    requiredNotNull(service, "Service name required");
    requiredNotNull(name, "Name required");
    if (privateKey == null && publicKey == null) {
        throw new BadRequestException("Key content was not provided.");
    }
    sshManager.createPair(new SshPairImpl(getCurrentUserId(), service, name, publicKey, privateKey));
    // through specific of html form that doesn't invoke complete submit handler
    return Response.ok("", MediaType.TEXT_HTML).build();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) 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) Produces(javax.ws.rs.Produces) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink)

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