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); }
/// <summary>Obtain the user's configuration data.</summary> /// <remarks> /// Obtain the user's configuration data. /// <p> /// The configuration file is always returned to the caller, even if no file /// exists in the user's home directory at the time the call was made. Lookup /// requests are cached and are automatically updated if the user modifies /// the configuration file since the last time it was cached. /// </remarks> /// <param name="fs"> /// the file system abstraction which will be necessary to /// perform certain file system operations. /// </param> /// <returns>a caching reader of the user's configuration file.</returns> public static NGit.Transport.OpenSshConfig Get(FS fs) { FilePath home = fs.UserHome(); if (home == null) { home = new FilePath(".").GetAbsoluteFile(); } FilePath config = new FilePath(new FilePath(home, ".ssh"), "config"); NGit.Transport.OpenSshConfig osc = new NGit.Transport.OpenSshConfig(home, config); osc.Refresh(); return(osc); }
/// <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); } }
/// <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" ) && retries < 3) { 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); } } }