Search in sources :

Example 1 with SshPair

use of org.eclipse.che.api.ssh.shared.model.SshPair in project che by eclipse.

the class GitHubService method updateSSHKey.

@POST
@Path("ssh/generate")
public void updateSSHKey() throws ApiException {
    final String host = "github.com";
    SshPair sshPair = null;
    try {
        sshPair = sshServiceClient.getPair("vcs", host);
    } catch (NotFoundException ignored) {
    }
    if (sshPair != null) {
        if (sshPair.getPublicKey() == null) {
            sshServiceClient.removePair("vcs", host);
            sshPair = sshServiceClient.generatePair(newDto(GenerateSshPairRequest.class).withService("vcs").withName(host));
        }
    } else {
        sshPair = sshServiceClient.generatePair(newDto(GenerateSshPairRequest.class).withService("vcs").withName(host));
    }
    // update public key
    try {
        githubKeyUploader.uploadKey(sshPair.getPublicKey());
    } catch (IOException e) {
        LOG.error("Upload github ssh key fail", e);
        throw new GitException(e.getMessage(), e);
    }
}
Also used : SshPair(org.eclipse.che.api.ssh.shared.model.SshPair) GitException(org.eclipse.che.api.git.exception.GitException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with SshPair

use of org.eclipse.che.api.ssh.shared.model.SshPair in project che by eclipse.

the class SshKeyProviderImpl method getPrivateKey.

/**
     * Get private ssh key and upload public ssh key to repository hosting service.
     *
     * @param url
     *         url to the repository
     * @return private ssh key
     * @throws ServerException
     *         if an error occurs while generating or uploading keys
     */
@Override
public byte[] getPrivateKey(String url) throws ServerException {
    String host = UrlUtils.getHost(url);
    SshPair pair;
    try {
        pair = sshService.getPair("vcs", host);
    } catch (ServerException | NotFoundException e) {
        throw new ServerException(DtoFactory.newDto(ExtendedError.class).withMessage("Unable get private ssh key").withErrorCode(ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY));
    }
    // check keys existence
    String privateKey = pair.getPrivateKey();
    if (privateKey == null) {
        throw new ServerException(DtoFactory.newDto(ExtendedError.class).withMessage("Unable get private ssh key").withErrorCode(ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY));
    }
    final String publicKey = pair.getPublicKey();
    if (publicKey != null) {
        final Optional<SshKeyUploader> optionalKeyUploader = sshKeyUploaders.stream().filter(keyUploader -> keyUploader.match(url)).findFirst();
        if (optionalKeyUploader.isPresent()) {
            final SshKeyUploader uploader = optionalKeyUploader.get();
            try {
                uploader.uploadKey(publicKey);
            } catch (IOException e) {
                throw new ServerException(e.getMessage(), e);
            } catch (UnauthorizedException e) {
                // action might fail without uploaded public SSH key.
                LOG.warn(String.format("Unable upload public SSH key with %s", uploader.getClass().getSimpleName()), e);
            }
        } else {
            // action might fail without uploaded public SSH key.
            LOG.warn(String.format("Not found ssh key uploader for %s", host));
        }
    }
    return privateKey.getBytes();
}
Also used : Logger(org.slf4j.Logger) ErrorCodes(org.eclipse.che.api.core.ErrorCodes) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) UrlUtils(org.eclipse.che.plugin.ssh.key.utils.UrlUtils) SshPair(org.eclipse.che.api.ssh.shared.model.SshPair) ServerException(org.eclipse.che.api.core.ServerException) Optional(java.util.Optional) SshServiceClient(org.eclipse.che.plugin.ssh.key.SshServiceClient) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) DtoFactory(org.eclipse.che.dto.server.DtoFactory) SshPair(org.eclipse.che.api.ssh.shared.model.SshPair) ServerException(org.eclipse.che.api.core.ServerException) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 SshPair (org.eclipse.che.api.ssh.shared.model.SshPair)2 Inject (com.google.inject.Inject)1 Optional (java.util.Optional)1 Set (java.util.Set)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 ErrorCodes (org.eclipse.che.api.core.ErrorCodes)1 ServerException (org.eclipse.che.api.core.ServerException)1 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)1 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)1 GitException (org.eclipse.che.api.git.exception.GitException)1 DtoFactory (org.eclipse.che.dto.server.DtoFactory)1 SshServiceClient (org.eclipse.che.plugin.ssh.key.SshServiceClient)1 UrlUtils (org.eclipse.che.plugin.ssh.key.utils.UrlUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1