1 /* 2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com> 3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others 4 * 5 * This program and the accompanying materials are made available under the 6 * terms of the Eclipse Distribution License v. 1.0 which is available at 7 * https://www.eclipse.org/org/documents/edl-v10.php. 8 * 9 * SPDX-License-Identifier: BSD-3-Clause 10 */ 11 12 package org.eclipse.jgit.transport; 13 14 import java.security.AccessController; 15 import java.security.PrivilegedAction; 16 import java.util.Iterator; 17 import java.util.ServiceLoader; 18 19 import org.eclipse.jgit.errors.TransportException; 20 import org.eclipse.jgit.lib.Constants; 21 import org.eclipse.jgit.util.FS; 22 import org.eclipse.jgit.util.SystemReader; 23 24 /** 25 * Creates and destroys SSH connections to a remote system. 26 * <p> 27 * Different implementations of the session factory may be used to control 28 * communicating with the end-user as well as reading their personal SSH 29 * configuration settings, such as known hosts and private keys. 30 * <p> 31 * A {@link org.eclipse.jgit.transport.RemoteSession} must be returned to the 32 * factory that created it. Callers are encouraged to retain the 33 * SshSessionFactory for the duration of the period they are using the Session. 34 */ 35 public abstract class SshSessionFactory { 36 private static SshSessionFactory INSTANCE = loadSshSessionFactory(); 37 38 private static SshSessionFactory loadSshSessionFactory() { 39 ServiceLoader<SshSessionFactory> loader = ServiceLoader.load(SshSessionFactory.class); 40 Iterator<SshSessionFactory> iter = loader.iterator(); 41 if(iter.hasNext()) { 42 return iter.next(); 43 } 44 return null; 45 } 46 /** 47 * Get the currently configured JVM-wide factory. 48 * <p> 49 * A factory is always available. By default the factory will read from the 50 * user's <code>$HOME/.ssh</code> and assume OpenSSH compatibility. 51 * 52 * @return factory the current factory for this JVM. 53 */ 54 public static SshSessionFactory getInstance() { 55 return INSTANCE; 56 } 57 58 /** 59 * Change the JVM-wide factory to a different implementation. 60 * 61 * @param newFactory 62 * factory for future sessions to be created through. If null the 63 * default factory will be restored.s 64 */ 65 public static void setInstance(SshSessionFactory newFactory) { 66 if (newFactory != null) { 67 INSTANCE = newFactory; 68 } else { 69 INSTANCE = loadSshSessionFactory(); 70 } 71 } 72 73 /** 74 * Retrieves the local user name as defined by the system property 75 * "user.name". 76 * 77 * @return the user name 78 * @since 5.2 79 */ 80 public static String getLocalUserName() { 81 return AccessController 82 .doPrivileged((PrivilegedAction<String>) () -> SystemReader 83 .getInstance().getProperty(Constants.OS_USER_NAME_KEY)); 84 } 85 86 /** 87 * Open (or reuse) a session to a host. 88 * <p> 89 * A reasonable UserInfo that can interact with the end-user (if necessary) 90 * is installed on the returned session by this method. 91 * <p> 92 * The caller must connect the session by invoking <code>connect()</code> if 93 * it has not already been connected. 94 * 95 * @param uri 96 * URI information about the remote host 97 * @param credentialsProvider 98 * provider to support authentication, may be null. 99 * @param fs 100 * the file system abstraction which will be necessary to perform 101 * certain file system operations. 102 * @param tms 103 * Timeout value, in milliseconds. 104 * @return a session that can contact the remote host. 105 * @throws org.eclipse.jgit.errors.TransportException 106 * the session could not be created. 107 */ 108 public abstract RemoteSession getSession(URIish uri, 109 CredentialsProvider credentialsProvider, FS fs, int tms) 110 throws TransportException; 111 112 /** 113 * Close (or recycle) a session to a host. 114 * 115 * @param session 116 * a session previously obtained from this factory's 117 * {@link #getSession(URIish, CredentialsProvider, FS, int)} 118 * method. 119 */ 120 public void releaseSession(RemoteSession session) { 121 session.disconnect(); 122 } 123 }