Search in sources :

Example 16 with KeyPair

use of com.jcraft.jsch.KeyPair in project blueocean-plugin by jenkinsci.

the class GitUtils method getSSHKeyTransport.

private static TransportConfigCallback getSSHKeyTransport(final BasicSSHUserPrivateKey privateKey) {
    final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {

        @Override
        protected void configure(OpenSshConfig.Host hc, com.jcraft.jsch.Session session) {
            // jenkins user doesn't likely have the host key
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch getJSch(OpenSshConfig.Host hc, FS fs) throws JSchException {
            JSch jsch = new JSch();
            configureJSch(jsch);
            // TODO: might need this: jsch.setHostKeyRepository(new KnownHosts(this));
            KeyPair pair = KeyPair.load(jsch, privateKey.getPrivateKey().getBytes(StandardCharsets.UTF_8), null);
            byte[] passphrase = new byte[0];
            jsch.addIdentity(privateKey.getUsername(), pair.forSSHAgent(), null, passphrase);
            return jsch;
        }
    };
    return transport -> {
        if (transport instanceof SshTransport) {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    };
}
Also used : BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) StringUtils(org.apache.commons.lang.StringUtils) KeyPair(com.jcraft.jsch.KeyPair) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) Date(java.util.Date) JGitText(org.eclipse.jgit.internal.JGitText) LoggerFactory(org.slf4j.LoggerFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) CredentialsMatchers(com.cloudbees.plugins.credentials.CredentialsMatchers) ByteArrayInputStream(java.io.ByteArrayInputStream) GitClient(org.jenkinsci.plugins.gitclient.GitClient) CredentialsUtils(io.jenkins.blueocean.credential.CredentialsUtils) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileMode(org.eclipse.jgit.lib.FileMode) RefSpec(org.eclipse.jgit.transport.RefSpec) TimeZone(java.util.TimeZone) RefUpdate(org.eclipse.jgit.lib.RefUpdate) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) Constants(org.eclipse.jgit.lib.Constants) TransportCommand(org.eclipse.jgit.api.TransportCommand) ItemGroup(hudson.model.ItemGroup) RevTree(org.eclipse.jgit.revwalk.RevTree) StandardCharsets(java.nio.charset.StandardCharsets) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GitException(hudson.plugins.git.GitException) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) PushResult(org.eclipse.jgit.transport.PushResult) DirCache(org.eclipse.jgit.dircache.DirCache) FS(org.eclipse.jgit.util.FS) JSchException(com.jcraft.jsch.JSchException) Pattern(java.util.regex.Pattern) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) RevCommit(org.eclipse.jgit.revwalk.RevCommit) JSch(com.jcraft.jsch.JSch) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) Git(org.jenkinsci.plugins.gitclient.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MergeCommand(org.eclipse.jgit.api.MergeCommand) ACL(hudson.security.ACL) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) EnvVars(hudson.EnvVars) Nonnull(javax.annotation.Nonnull) PushCommand(org.eclipse.jgit.api.PushCommand) Nullable(javax.annotation.Nullable) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) TaskListener(hudson.model.TaskListener) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) Logger(org.slf4j.Logger) SmartCredentialsProvider(org.jenkinsci.plugins.gitclient.trilead.SmartCredentialsProvider) IOException(java.io.IOException) URIRequirementBuilder(com.cloudbees.plugins.credentials.domains.URIRequirementBuilder) ServiceException(io.jenkins.blueocean.commons.ServiceException) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) ObjectId(org.eclipse.jgit.lib.ObjectId) TransportException(org.eclipse.jgit.api.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) MergeResult(org.eclipse.jgit.api.MergeResult) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) InputStream(java.io.InputStream) KeyPair(com.jcraft.jsch.KeyPair) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 17 with KeyPair

use of com.jcraft.jsch.KeyPair in project blueocean-plugin by jenkinsci.

the class SSHKeyUtils method getPublicKey.

/**
 * Gets the public key, with a comment for the given private key
 * @param privateKey SSH private key to use
 * @param comment comment with the key
 * @return SSH public key
 */
public static String getPublicKey(String privateKey, String comment) {
    try {
        JSch jsch = new JSch();
        KeyPair pair = KeyPair.load(jsch, privateKey.getBytes(StandardCharsets.UTF_8), null);
        ByteArrayOutputStream keyOut = new ByteArrayOutputStream();
        pair.writePublicKey(keyOut, comment);
        return new String(keyOut.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception ex) {
        throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) IOException(java.io.IOException)

Example 18 with KeyPair

use of com.jcraft.jsch.KeyPair in project blueocean-plugin by jenkinsci.

the class SSHKeyUtils method generateKey.

/**
 * Generates a new SSH private key with specified keySize
 * @param keySize size to use for the key
 * @return a SSH private key
 */
public static String generateKey(int keySize) {
    try {
        JSch jsch = new JSch();
        KeyPair pair = KeyPair.genKeyPair(jsch, KeyPair.RSA, keySize);
        ByteArrayOutputStream keyOut = new ByteArrayOutputStream();
        pair.writePrivateKey(keyOut);
        return new String(keyOut.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception ex) {
        throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) IOException(java.io.IOException)

Example 19 with KeyPair

use of com.jcraft.jsch.KeyPair in project KeyBox by skavanagh.

the class SSHUtil method keyGen.

/**
 * generates system's public/private key par and returns passphrase
 *
 * @return passphrase for system generated key
 */
public static String keyGen(String passphrase) throws IOException, JSchException {
    FileUtils.forceMkdir(new File(KEY_PATH));
    deleteGenSSHKeys();
    if (StringUtils.isEmpty(AppConfig.getProperty(PRIVATE_KEY)) || StringUtils.isEmpty(AppConfig.getProperty(PUBLIC_KEY))) {
        // set key type
        int type = KeyPair.RSA;
        if ("dsa".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.DSA;
        } else if ("ecdsa".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ECDSA;
        } else if ("ed448".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ED448;
        } else if ("ed25519".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ED25519;
        }
        String comment = "bastillion@global_key";
        JSch jsch = new JSch();
        KeyPair keyPair = KeyPair.genKeyPair(jsch, type, KEY_LENGTH);
        keyPair.writePrivateKey(PVT_KEY, passphrase.getBytes());
        keyPair.writePublicKey(PUB_KEY, comment);
        System.out.println("Finger print: " + keyPair.getFingerPrint());
        keyPair.dispose();
    }
    return passphrase;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 20 with KeyPair

use of com.jcraft.jsch.KeyPair in project gerrit by GerritCodeReview.

the class AccountCreator method create.

public synchronized TestAccount create(@Nullable String username, @Nullable String email, @Nullable String fullName, String... groups) throws Exception {
    TestAccount account = accounts.get(username);
    if (account != null) {
        return account;
    }
    try (ReviewDb db = reviewDbProvider.open()) {
        Account.Id id = new Account.Id(db.nextAccountId());
        List<ExternalId> extIds = new ArrayList<>(2);
        String httpPass = null;
        if (username != null) {
            httpPass = "http-pass";
            extIds.add(ExternalId.createUsername(username, id, httpPass));
        }
        if (email != null) {
            extIds.add(ExternalId.createEmail(id, email));
        }
        externalIdsUpdate.create().insert(extIds);
        Account a = new Account(id, TimeUtil.nowTs());
        a.setFullName(fullName);
        a.setPreferredEmail(email);
        accountsUpdate.create().insert(db, a);
        if (groups != null) {
            for (String n : groups) {
                AccountGroup.NameKey k = new AccountGroup.NameKey(n);
                AccountGroup g = groupCache.get(k);
                checkArgument(g != null, "group not found: %s", n);
                AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(id, g.getId()));
                db.accountGroupMembers().insert(Collections.singleton(m));
            }
        }
        KeyPair sshKey = null;
        if (SshMode.useSsh() && username != null) {
            sshKey = genSshKey();
            authorizedKeys.addKey(id, publicKey(sshKey, email));
            sshKeyCache.evict(username);
        }
        if (username != null) {
            accountCache.evictByUsername(username);
        }
        byEmailCache.evict(email);
        indexer.index(id);
        account = new TestAccount(id, username, email, fullName, sshKey, httpPass);
        if (username != null) {
            accounts.put(username, account);
        }
        return account;
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) KeyPair(com.jcraft.jsch.KeyPair) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)30 JSch (com.jcraft.jsch.JSch)27 JSchException (com.jcraft.jsch.JSchException)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)3 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 SSHKeyPair (io.cdap.cdap.runtime.spi.ssh.SSHKeyPair)2 SSHPublicKey (io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)2 FileOutputStream (java.io.FileOutputStream)2 KeyException (java.security.KeyException)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 AlertDialog (android.support.v7.app.AlertDialog)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1