/// <summary> /// Update Private Key selection /// </summary> private void UpdateIdentity() { View.PkCheckboxEnabled = View.SelectedProtocol == Protocol.SFTP; if (View.SelectedProtocol == Protocol.SFTP) { if (Utils.IsNotBlank(View.Hostname)) { OpenSshConfig.Host entry = OpenSshConfig.create().lookup(View.Hostname); if (null != entry.getIdentityFile()) { if (!View.PkCheckboxState) { // No previously manually selected key View.PkCheckboxState = true; View.PkLabel = LocalFactory.createLocal(entry.getIdentityFile().getAbsolutePath()).getAbbreviatedPath(); } } else { View.PkCheckboxState = false; View.PkLabel = Locale.localizedString("No private key selected"); } if (Utils.IsNotBlank(entry.getUser())) { View.Username = entry.getUser(); } } } else { View.PkCheckboxState = false; View.PkLabel = Locale.localizedString("No private key selected"); } }
protected override void configure(OpenSshConfig.Host hc, Session session) { if (!hc.isBatchMode()) { #warning need something to replace jgit gui infrastructure as gitsharp is library only throw new NotImplementedException("GUI Configuration is not available"); } }
public override void SetUp() { base.SetUp(); home = new FilePath(trash, "home"); FileUtils.Mkdir(home); configFile = new FilePath(new FilePath(home, ".ssh"), Constants.CONFIG); FileUtils.Mkdir(configFile.GetParentFile()); Runtime.SetProperty("user.name", "jex_junit"); osc = new OpenSshConfig(home, configFile); }
protected override void Configure(OpenSshConfig.Host hc, Session session) { var config = new Properties(); config["StrictHostKeyChecking"] = "no"; config["PreferredAuthentications"] = "publickey"; session.SetConfig(config); var jsch = this.GetJSch(hc, FS.DETECTED); jsch.AddIdentity("KeyPair", Encoding.UTF8.GetBytes(PrivateKey), Encoding.UTF8.GetBytes(PublicKey), null); }
public override void setUp() { base.setUp(); _home = new DirectoryInfo(Path.Combine(trash.ToString(), "home")); _configFile = new FileInfo(Path.Combine(_home.ToString(), ".ssh")); Directory.CreateDirectory(_configFile.ToString()); _configFile = new FileInfo(Path.Combine(_configFile.ToString(), "config")); // can't do //Environment.UserName = "******"; _osc = new OpenSshConfig(_home, _configFile); }
protected override NSch.JSch GetJSch(OpenSshConfig.Host hc, NGit.Util.FS fs) { var jsch = base.GetJSch(hc, fs); foreach (var certificate in GitCertificates.GetAllCertificates()) { jsch.AddIdentity(certificate.Path); } // Set the known hosts file. jsch.SetKnownHosts(GetKnownHostsFilename()); return jsch; }
public override void setUp() { base.setUp(); _home = new DirectoryInfo(Path.Combine(trash.FullName, "home")); _configFile = new FileInfo(Path.Combine(_home.FullName, ".ssh")); Directory.CreateDirectory(_configFile.FullName); _configFile = new FileInfo(Path.Combine(_configFile.FullName, "config")); // can't do //Environment.UserName = "******"; _osc = new OpenSshConfig(_home, _configFile); }
/// <summary> /// Update this host credentials from the OpenSSH configuration file in ~/.ssh/config /// </summary> private void ReadOpenSshConfiguration() { if (_host.getProtocol().equals(Protocol.SFTP)) { OpenSshConfig.Host entry = OpenSshConfig.create().lookup(_host.getHostname()); if (null != entry.getIdentityFile()) { _host.getCredentials().setIdentity( LocalFactory.createLocal(entry.getIdentityFile().getAbsolutePath())); } if (Utils.IsNotBlank(entry.getUser())) { _host.getCredentials().setUsername(entry.getUser()); } } else { _host.getCredentials().setIdentity(null); } }
/// <summary>Obtain the JSch used to create new sessions.</summary> /// <remarks>Obtain the JSch used to create new sessions.</remarks> /// <param name="hc">host configuration</param> /// <param name="fs"> /// the file system abstraction which will be necessary to /// perform certain file system operations. /// </param> /// <returns>the JSch instance to use.</returns> /// <exception cref="NSch.JSchException">the user configuration could not be created. /// </exception> protected internal virtual JSch GetJSch(OpenSshConfig.Host hc, FS fs) { if (defaultJSch == null) { defaultJSch = CreateDefaultJSch(fs); foreach (object name in defaultJSch.GetIdentityNames()) { byIdentityFile.Put((string)name, defaultJSch); } } FilePath identityFile = hc.GetIdentityFile(); if (identityFile == null) { return defaultJSch; } string identityKey = identityFile.GetAbsolutePath(); JSch jsch = byIdentityFile.Get(identityKey); if (jsch == null) { jsch = new JSch(); jsch.SetHostKeyRepository(defaultJSch.GetHostKeyRepository()); jsch.AddIdentity(identityKey); byIdentityFile.Put(identityKey, jsch); } return jsch; }
protected override void Configure(OpenSshConfig.Host hc, NSch.Session session) { }
protected Session createSession(OpenSshConfig.Host hc, string user, string host, int port) { return getJSch(hc).getSession(user, host, port); }
protected abstract void configure(OpenSshConfig.Host hc, Session session);
private OpenSshConfig getConfig() { if (config == null) config = OpenSshConfig.get(); return config; }
protected JSch getJSch(OpenSshConfig.Host hc) { JSch def = getDefaultJSch(); FileInfo identityFile = hc.getIdentityFile(); if (identityFile == null) return def; string identityKey = Path.GetFullPath(identityFile.ToString()); JSch jsch = byIdentityFile[identityKey]; if (jsch == null) { jsch = new JSch(); jsch.setHostKeyRepository(def.getHostKeyRepository()); jsch.addIdentity(identityKey); byIdentityFile.Add(identityKey, jsch); } return jsch; }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); int retries = 0; while (!session.IsConnected() && retries < 3) { try { retries++; session.Connect(tms); } catch (JSchException e) { session.Disconnect(); session = null; // if authentication failed maybe credentials changed at the // remote end therefore reset credentials and retry if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail" )) { credentialsProvider.Reset(uri); session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); } else { throw; } } } return new JschSession(session, uri); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(hc, user, host, port, fs); if (pass != null) { session.SetPassword(pass); } string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking(); if (strictHostKeyCheckingPolicy != null) { session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy); } string pauth = hc.GetPreferredAuthentications(); if (pauth != null) { session.SetConfig("PreferredAuthentications", pauth); } if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive ())) { session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider) ); } Configure(hc, session); if (!session.IsConnected()) { session.Connect(tms); } return new JschSession(session, uri); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }
/// <summary>Create a new remote session for the requested address.</summary> /// <remarks>Create a new remote session for the requested address.</remarks> /// <param name="hc">host configuration</param> /// <param name="user">login to authenticate as.</param> /// <param name="host">server name to connect to.</param> /// <param name="port">port number of the SSH daemon (typically 22).</param> /// <param name="fs"> /// the file system abstraction which will be necessary to /// perform certain file system operations. /// </param> /// <returns>new session instance, but otherwise unconfigured.</returns> /// <exception cref="NSch.JSchException">the session could not be created.</exception> protected internal virtual Session CreateSession(OpenSshConfig.Host hc, string user , string host, int port, FS fs) { return GetJSch(hc, fs).GetSession(user, host, port); }
/// <summary> /// Provide additional configuration for the session based on the host /// information. /// </summary> /// <remarks> /// Provide additional configuration for the session based on the host /// information. This method could be used to supply /// <see cref="NSch.UserInfo">NSch.UserInfo</see> /// . /// </remarks> /// <param name="hc">host configuration</param> /// <param name="session">session to configure</param> protected internal abstract void Configure(OpenSshConfig.Host hc, Session session );
protected override void configure(OpenSshConfig.Host hc, ISshSession session) { // No additional configuration required. }
/// <exception cref="NSch.JSchException"></exception> public override Session GetSession(string user, string pass, string host, int port , CredentialsProvider credentialsProvider, FS fs) { lock (this) { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(hc, user, host, port, fs); if (pass != null) { session.SetPassword(pass); } string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking(); if (strictHostKeyCheckingPolicy != null) { session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy); } string pauth = hc.GetPreferredAuthentications(); if (pauth != null) { session.SetConfig("PreferredAuthentications", pauth); } if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive ())) { session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider) ); } Configure(hc, session); return session; } }
protected internal override void Configure(OpenSshConfig.Host hc, Session session ) { }
/// <exception cref="NSch.JSchException"></exception> private Session CreateSession(CredentialsProvider credentialsProvider, FS fs, string user, string pass, string host, int port, OpenSshConfig.Host hc) { Session session = CreateSession(hc, user, host, port, fs); if (pass != null) { session.SetPassword(pass); } string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking(); if (strictHostKeyCheckingPolicy != null) { session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy); } string pauth = hc.GetPreferredAuthentications(); if (pauth != null) { session.SetConfig("PreferredAuthentications", pauth); } if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive ())) { session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider) ); } Configure(hc, session); return session; }