Statistics
| Branch: | Revision:

root / extensions / ssh / jsch / src / main / java / org / jclouds / ssh / jsch / config / JschSshClientModule.java @ 35e7942d

History | View | Annotate | Download (2.9 kB)

1
/**
2
 *
3
 * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
4
 *
5
 * ====================================================================
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 * ====================================================================
18
 */
19
package org.jclouds.ssh.jsch.config;
20

    
21
import java.io.ByteArrayOutputStream;
22
import java.net.InetSocketAddress;
23
import java.util.Map;
24

    
25
import javax.inject.Named;
26

    
27
import org.jclouds.Constants;
28
import org.jclouds.ssh.ConfiguresSshClient;
29
import org.jclouds.ssh.SshClient;
30
import org.jclouds.ssh.jsch.JschSshClient;
31

    
32
import com.google.common.base.Throwables;
33
import com.google.common.collect.ImmutableMap;
34
import com.google.inject.AbstractModule;
35
import com.google.inject.Inject;
36
import com.google.inject.Scopes;
37
import com.jcraft.jsch.JSch;
38
import com.jcraft.jsch.JSchException;
39
import com.jcraft.jsch.KeyPair;
40

    
41
/**
42
 * 
43
 * @author Adrian Cole
44
 */
45
@ConfiguresSshClient
46
public class JschSshClientModule extends AbstractModule {
47

    
48
   protected void configure() {
49
      bind(SshClient.Factory.class).to(Factory.class).in(Scopes.SINGLETON);
50
   }
51

    
52
   private static class Factory implements SshClient.Factory {
53
      @Named(Constants.PROPERTY_CONNECTION_TIMEOUT)
54
      @Inject(optional=true)
55
      int timeout = 60000;
56
      
57
      public SshClient create(InetSocketAddress socket, String username, String password) {
58
         return new JschSshClient(socket, timeout, username, password);
59
      }
60

    
61
      public SshClient create(InetSocketAddress socket, String username, byte[] privateKey) {
62
         return new JschSshClient(socket, timeout, username, privateKey);
63
      }
64

    
65
      @Override
66
      public Map<String, String> generateRSAKeyPair(String comment, String passphrase) {
67
         KeyPair pair = null;
68
         try {
69
            pair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA);
70
         } catch (JSchException e) {
71
            Throwables.propagate(e);
72
         }
73
         if (passphrase != null)
74
            pair.setPassphrase(passphrase);
75
         ByteArrayOutputStream privateKey = new ByteArrayOutputStream();
76
         pair.writePrivateKey(privateKey);
77
         ByteArrayOutputStream publicKey = new ByteArrayOutputStream();
78
         pair.writePublicKey(publicKey, comment);
79
         return ImmutableMap.of("comment", comment, "passphrase", passphrase, "private",
80
                  new String(privateKey.toByteArray()), "public", new String(publicKey
81
                           .toByteArray()));
82
      }
83
   }
84
}