Package org.apache.airavata.credential.store.credential.impl.ssh

Source Code of org.apache.airavata.credential.store.credential.impl.ssh.SSHCredentialGenerator

/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package org.apache.airavata.credential.store.credential.impl.ssh;

import java.io.File;
import java.io.IOException;

import org.apache.airavata.credential.store.store.CredentialStoreException;
import org.apache.airavata.credential.store.store.impl.SSHCredentialWriter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.KeyPair;

/**
* A class which generates an SSH credential
*/
public class SSHCredentialGenerator {
 
  private static Logger logger = LoggerFactory.getLogger(SSHCredentialWriter.class);
 
  /**
   *
   * @return a SSH Credential generated and encrypted using a randomly generated password
   * @throws CredentialStoreException
   */
  public SSHCredential generateCredential(String tokenId) throws CredentialStoreException {
        JSch jsch=new JSch();
        try {
            KeyPair kpair=KeyPair.genKeyPair(jsch, KeyPair.RSA);
            File file;
     
        file = File.createTempFile("id_rsa", "");
     
            String fileName = file.getAbsolutePath();

            String password = generateRandomString();
            // We are encrypting the private key with the hash of (tokenId+password).
            // Any client which wants to use this private key will also generate a hash and then use it to decrypt the key. 
            kpair.writePrivateKey(fileName,generateHash(tokenId,password).getBytes());
            kpair.writePublicKey(fileName + ".pub"  , "");
            kpair.dispose();
            byte[] priKey = FileUtils.readFileToByteArray(new File(fileName));
            byte[] pubKey = FileUtils.readFileToByteArray(new File(fileName + ".pub"));
            SSHCredential sshCredential = new SSHCredential();
            sshCredential.setPrivateKey(priKey);
            sshCredential.setPublicKey(pubKey);
            sshCredential.setPassphrase(password);
            return sshCredential;
    } catch (IOException e) {
      logger.error("IO Exception when creating SSH credential ",e);
      throw new CredentialStoreException("Unable to generate SSH Credential", e);
    } catch (JSchException e) {
      logger.error("JSch SSH credential creation exception ",e);
      throw new CredentialStoreException("Unable to generate SSH Credential. JSch exception ", e);
    }
  }
 
  private String generateHash(String tokenId, String password) {
    // TODO: Hash the password generated and return it
    return null;
  }

  // Generate a random alphanumberic string of 16 characters length
  private String generateRandomString() {
    return RandomStringUtils.randomAlphanumeric(16);
  }
}
TOP

Related Classes of org.apache.airavata.credential.store.credential.impl.ssh.SSHCredentialGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.