protected Exception doInBackground(String... strings) {
      int length = Integer.parseInt(strings[0]);
      String passphrase = strings[1];
      String comment = strings[2];

      JSch jsch = new JSch();
      try {
        KeyPair kp = KeyPair.genKeyPair(jsch, KeyPair.RSA, length);

        File file = new File(getFilesDir() + "/.ssh_key");
        FileOutputStream out = new FileOutputStream(file, false);
        if (passphrase.length() > 0) {
          kp.writePrivateKey(out, passphrase.getBytes());
        } else {
          kp.writePrivateKey(out);
        }

        file = new File(getFilesDir() + "/.ssh_key.pub");
        out = new FileOutputStream(file, false);
        kp.writePublicKey(out, comment);
        return null;
      } catch (Exception e) {
        System.out.println("Exception caught :(");
        e.printStackTrace();
        return e;
      }
    }
 /**
  * Creates a dsa key pair at the given paths for the public and private key. Uses external means
  * (jsch KeyPair) and no internal functionality to create the keys (so that tests can limit
  * theirselves to test single functionality bits).
  *
  * @param publicKeyPath
  * @param privateKeyPath
  * @throws IOException
  * @throws JSchException
  */
 public static KeyPair createDsaKeyPair(String publicKeyPath, String privateKeyPath)
     throws IOException, JSchException {
   KeyPair keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA, 1024);
   keyPair.setPassphrase(DEFAULT_PASSPHRASE);
   keyPair.writePublicKey(publicKeyPath, "created by " + IOpenShiftConnection.DEFAULT_CLIENT_ID);
   keyPair.writePrivateKey(privateKeyPath);
   return keyPair;
 }
示例#3
0
  protected CreateKeyPairResponseType newKey(String ownerID, String keyName)
      throws KeyGenException, IOException, DisabledException {

    if (this.pubkeyOnly) {
      throw new DisabledException("SSH key generation is disabled");
    }

    final JSch jsch = new JSch();
    final KeyPair kpair;
    try {
      kpair = KeyPair.genKeyPair(jsch, this.keyType, this.keySize);
    } catch (JSchException e) {
      throw new KeyGenException(e.getMessage(), e);
    }

    final String generatedFingerprint = kpair.getFingerPrint();
    if (generatedFingerprint == null) {
      throw new KeyGenException("fingerprint is missing");
    }
    final String[] parts = generatedFingerprint.split(" ");
    if (parts.length != 2) {
      throw new KeyGenException(
          "fingerprint not in expected " + "format: '" + generatedFingerprint + "'");
    }

    final String fingerprint = parts[1];
    if (fingerprint == null || fingerprint.trim().length() == 0) {
      throw new KeyGenException(
          "fingerprint not in expected " + "format: '" + generatedFingerprint + "'");
    }

    final StringOutputStream pubsos = new StringOutputStream();
    final StringOutputStream privsos = new StringOutputStream();

    kpair.writePublicKey(pubsos, "clouduser-" + ownerID);
    kpair.writePrivateKey(privsos);

    final String pubKeyString = pubsos.toString();
    if (pubKeyString == null || pubKeyString.trim().length() == 0) {
      throw new KeyGenException("generated pubkey is missing");
    }

    final String privKeyString = privsos.toString();
    if (privKeyString == null || privKeyString.trim().length() == 0) {
      throw new KeyGenException("generated privkey is missing");
    }

    // register it before returning
    this.sshKeys.newKey(ownerID, keyName, pubKeyString, fingerprint);

    logger.info("New SSH key created, name='" + keyName + "', owner ID='" + ownerID + "'");

    final CreateKeyPairResponseType ckprt = new CreateKeyPairResponseType();
    ckprt.setKeyFingerprint(fingerprint);
    ckprt.setKeyMaterial(privKeyString);
    ckprt.setKeyName(keyName);
    return ckprt;
  }
示例#4
0
 public static File getPublicKeyEnsure(File privateKey) {
   File publicKey = getPublicKey(privateKey);
   if (!publicKey.exists()) {
     try {
       JSch jsch = new JSch();
       KeyPair kpair = KeyPair.load(jsch, privateKey.getAbsolutePath());
       kpair.writePublicKey(new FileOutputStream(publicKey), "jigit");
       kpair.dispose();
     } catch (Exception e) {
       // TODO
       e.printStackTrace();
     }
   }
   return publicKey;
 }
示例#5
0
 private void createKey(ClusterConfiguration clusterConfig, User user)
     throws JSchException, FileNotFoundException, IOException {
   String dir = "sshKey";
   Utils.checkDirAvailable(dir);
   JSch jsch = new JSch();
   String name = "cloudgene_" + System.currentTimeMillis() + "";
   KeyPair key;
   key = KeyPair.genKeyPair(jsch, com.jcraft.jsch.KeyPair.RSA);
   String publicKeyLoc = dir + File.separator + name + ".pub";
   String privateKeyLoc = dir + File.separatorChar + name + ".key";
   key.writePrivateKey(privateKeyLoc);
   key.writePublicKey(publicKeyLoc, "");
   // set for launch cluster
   clusterConfig.setSshPublic(publicKeyLoc);
   clusterConfig.setSshPrivate(privateKeyLoc);
   user.setSshKey(privateKeyLoc);
   user.setSshPub(publicKeyLoc);
   createZip(publicKeyLoc, privateKeyLoc);
   key.dispose();
 }
  /**
   * Generates a rsa key-pair in /tmp/jenkins-testkey for use with authenticating the trigger
   * against the mock server.
   *
   * @return the path to the private key file
   * @throws IOException if so.
   * @throws InterruptedException if interrupted while waiting for ssh-keygen to finish.
   * @throws JSchException if creation of the keys goes wrong.
   */
  public static KeyPairFiles generateKeyPair()
      throws IOException, InterruptedException, JSchException {
    File tmp = new File(System.getProperty("java.io.tmpdir"));
    File priv = new File(tmp, "jenkins-testkey");
    File pub = new File(tmp, "jenkins-testkey.pub");
    if (!(priv.exists() && pub.exists())) {
      if (priv.exists()) {
        if (!priv.delete()) {
          throw new IOException("Could not delete temp private key");
        }
      }
      if (pub.exists()) {
        if (!pub.delete()) {
          throw new IOException("Could not delete temp public key");
        }
      }
      System.out.println("Generating test key-pair.");
      JSch jsch = new JSch();
      KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);

      kpair.writePrivateKey(new FileOutputStream(priv));
      kpair.writePublicKey(new FileOutputStream(pub), "Test");
      System.out.println("Finger print: " + kpair.getFingerPrint());
      kpair.dispose();
      return new KeyPairFiles(priv, pub);
    } else {
      System.out.println("Test key-pair seems to already exist.");
      return new KeyPairFiles(priv, pub);
    }
  }
示例#7
0
  public static void createGridsshkey(char[] password, String id) throws Exception {

    if (gridsshkeyExists()) {
      throw new Exception("Key and/or Cert file(s) already exist.");
    }

    JSch jsch = new JSch();
    KeyPair kpair = KeyPair.genKeyPair(jsch, KEY_TYPE);
    kpair.setPassphrase(new String(password));
    kpair.writePrivateKey(CommonGridProperties.getDefault().getGridSSHKey());
    kpair.writePublicKey(CommonGridProperties.getDefault().getGridSSHCert(), id);
    kpair.dispose();

    Util.setFilePermissions(CommonGridProperties.getDefault().getGridSSHKey(), 600);
    Util.setFilePermissions(CommonGridProperties.getDefault().getGridSSHCert(), 600);
  }