View Javadoc

1   /**
2    *    Copyright 2012 meltmedia
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *        http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package com.meltmedia.cadmium.core.git;
17  
18  import org.eclipse.jgit.errors.TransportException;
19  import org.eclipse.jgit.transport.CredentialsProvider;
20  import org.eclipse.jgit.transport.JschConfigSessionFactory;
21  import org.eclipse.jgit.transport.OpenSshConfig;
22  import org.eclipse.jgit.transport.OpenSshConfig.Host;
23  import org.eclipse.jgit.transport.RemoteSession;
24  import org.eclipse.jgit.transport.URIish;
25  import org.eclipse.jgit.util.FS;
26  
27  import com.jcraft.jsch.JSch;
28  import com.jcraft.jsch.Session;
29  import com.jcraft.jsch.UserInfo;
30  import com.meltmedia.cadmium.core.FileSystemManager;
31  
32  public class LocalConfigSessionFactory extends JschConfigSessionFactory {
33  	
34  	private String privateKeyFile;
35  	private String knownHostsFile;
36  	private String sshDir;
37  	private boolean noPrompt = false;
38  	
39  	public LocalConfigSessionFactory( String sshDir, boolean noPrompt ) {
40  		this.privateKeyFile = sshDir+"/meltmedia-gene-deploy";
41  		this.knownHostsFile = sshDir+"/known_hosts";
42  		this.sshDir = sshDir;
43  		this.noPrompt = noPrompt;
44  	}
45  	
46  	@Override
47  	protected void configure(Host host, Session session) {
48  		session.setUserInfo(new UserInfo() {
49  
50  			@Override
51  			public String getPassphrase() {
52  			  if(noPrompt) {
53  			    return "";
54  			  } else {
55  			    return new String(System.console().readPassword());
56  			  }
57  			}
58  
59  			@Override
60  			public String getPassword() {
61          if(noPrompt) {
62            return "";
63          } else {
64            return new String(System.console().readPassword());
65          }
66  			}
67  
68  			@Override
69  			public boolean promptPassphrase(String arg0) {
70  			  if(!noPrompt) {
71  	        System.err.print("Enter "+arg0+": ");
72  			  }
73  				return true;
74  			}
75  
76  			@Override
77  			public boolean promptPassword(String arg0) {
78          if(!noPrompt) {
79            System.err.print("Enter "+arg0+": ");
80          }
81  				return true;
82  			}
83  
84  			@Override
85  			public boolean promptYesNo(String arg0) {
86  				return false;
87  			}
88  
89  			@Override
90  			public void showMessage(String arg0) {
91  				System.err.println("Password or passphrase needed:");
92  			}
93  			
94  		});
95  	}
96  
97  	
98  	@Override
99  	public synchronized RemoteSession getSession(URIish arg0,
100 			CredentialsProvider arg1, FS arg2, int arg3)
101 			throws TransportException {
102 		return super.getSession(arg0, arg1, arg2, arg3);
103 	}
104 
105 	protected com.jcraft.jsch.JSch getJSch(OpenSshConfig.Host hc, FS fs)
106       throws com.jcraft.jsch.JSchException
107       {
108 		JSch jsch = super.getJSch(hc, fs);
109     JSch.setConfig("StrictHostKeyChecking", "no");
110 		  if(FileSystemManager.exists(privateKeyFile)) {
111 		    jsch.addIdentity(privateKeyFile);
112 		  } else if (FileSystemManager.exists(sshDir + "/id_rsa")) {
113 		    jsch.addIdentity(sshDir + "/id_rsa");
114 		  }
115 	    jsch.setKnownHosts(knownHostsFile);
116 		return jsch;
117       }
118 
119 }