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);
}
};
}
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);
}
}
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);
}
}
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;
}
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;
}
}
Aggregations