| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.ssh.jsch.config; |
| 20 | |
| 21 | import javax.inject.Named; |
| 22 | |
| 23 | import org.jclouds.Constants; |
| 24 | import org.jclouds.domain.Credentials; |
| 25 | import org.jclouds.http.handlers.BackoffLimitedRetryHandler; |
| 26 | import org.jclouds.net.IPSocket; |
| 27 | import org.jclouds.predicates.InetSocketAddressConnect; |
| 28 | import org.jclouds.predicates.SocketOpen; |
| 29 | import org.jclouds.ssh.ConfiguresSshClient; |
| 30 | import org.jclouds.ssh.SshClient; |
| 31 | import org.jclouds.ssh.jsch.JschSshClient; |
| 32 | import org.jclouds.util.CredentialUtils; |
| 33 | |
| 34 | import com.google.inject.AbstractModule; |
| 35 | import com.google.inject.Inject; |
| 36 | import com.google.inject.Injector; |
| 37 | import com.google.inject.Scopes; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @ConfiguresSshClient |
| 44 | public class JschSshClientModule extends AbstractModule { |
| 45 | |
| 46 | protected void configure() { |
| 47 | bind(SshClient.Factory.class).to(Factory.class).in(Scopes.SINGLETON); |
| 48 | bind(SocketOpen.class).to(InetSocketAddressConnect.class).in(Scopes.SINGLETON); |
| 49 | } |
| 50 | |
| 51 | private static class Factory implements SshClient.Factory { |
| 52 | @Named(Constants.PROPERTY_CONNECTION_TIMEOUT) |
| 53 | @Inject(optional = true) |
| 54 | int timeout = 60000; |
| 55 | |
| 56 | private final BackoffLimitedRetryHandler backoffLimitedRetryHandler; |
| 57 | private final Injector injector; |
| 58 | |
| 59 | @SuppressWarnings("unused") |
| 60 | @Inject |
| 61 | public Factory(BackoffLimitedRetryHandler backoffLimitedRetryHandler, Injector injector) { |
| 62 | this.backoffLimitedRetryHandler = backoffLimitedRetryHandler; |
| 63 | this.injector = injector; |
| 64 | } |
| 65 | |
| 66 | public SshClient create(IPSocket socket, String username, String password) { |
| 67 | SshClient client = new JschSshClient(backoffLimitedRetryHandler, socket, timeout, username, password, null); |
| 68 | injector.injectMembers(client);// add logger |
| 69 | return client; |
| 70 | } |
| 71 | |
| 72 | public SshClient create(IPSocket socket, String username, byte[] privateKey) { |
| 73 | SshClient client = new JschSshClient(backoffLimitedRetryHandler, socket, timeout, username, null, privateKey); |
| 74 | injector.injectMembers(client);// add logger |
| 75 | return client; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public SshClient create(IPSocket socket, Credentials credentials) { |
| 80 | return CredentialUtils.isPrivateKeyCredential(credentials) ? create(socket, credentials.identity, |
| 81 | credentials.credential.getBytes()) : create(socket, credentials.identity, credentials.credential); |
| 82 | } |
| 83 | } |
| 84 | } |