Merge "Reset SSH connection and credentials on "Auth fail""
This commit is contained in:
commit
d497c2ac2d
|
@ -134,4 +134,13 @@ public boolean get(URIish uri, List<CredentialItem> items)
|
||||||
throws UnsupportedCredentialItem {
|
throws UnsupportedCredentialItem {
|
||||||
return get(uri, items.toArray(new CredentialItem[items.size()]));
|
return get(uri, items.toArray(new CredentialItem[items.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the credentials provider for the given URI
|
||||||
|
*
|
||||||
|
* @param uri
|
||||||
|
*/
|
||||||
|
public void reset(URIish uri) {
|
||||||
|
// default does nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,26 +106,29 @@ public synchronized RemoteSession getSession(URIish uri,
|
||||||
if (user == null)
|
if (user == null)
|
||||||
user = hc.getUser();
|
user = hc.getUser();
|
||||||
|
|
||||||
final Session session = createSession(hc, user, host, port, fs);
|
Session session = createSession(credentialsProvider, fs, user,
|
||||||
if (pass != null)
|
pass, host, port, hc);
|
||||||
session.setPassword(pass);
|
|
||||||
final String strictHostKeyCheckingPolicy = hc
|
|
||||||
.getStrictHostKeyChecking();
|
|
||||||
if (strictHostKeyCheckingPolicy != null)
|
|
||||||
session.setConfig("StrictHostKeyChecking",
|
|
||||||
strictHostKeyCheckingPolicy);
|
|
||||||
final 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())
|
int retries = 0;
|
||||||
session.connect(tms);
|
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.getCause() == null
|
||||||
|
&& e.getMessage().equals("Auth fail")) {
|
||||||
|
credentialsProvider.reset(uri);
|
||||||
|
session = createSession(credentialsProvider, fs, user,
|
||||||
|
pass, host, port, hc);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new JschSession(session, uri);
|
return new JschSession(session, uri);
|
||||||
|
|
||||||
|
@ -140,6 +143,29 @@ public synchronized RemoteSession getSession(URIish uri,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Session createSession(CredentialsProvider credentialsProvider,
|
||||||
|
FS fs, String user, final String pass, String host, int port,
|
||||||
|
final OpenSshConfig.Host hc) throws JSchException {
|
||||||
|
final Session session = createSession(hc, user, host, port, fs);
|
||||||
|
if (pass != null)
|
||||||
|
session.setPassword(pass);
|
||||||
|
final String strictHostKeyCheckingPolicy = hc
|
||||||
|
.getStrictHostKeyChecking();
|
||||||
|
if (strictHostKeyCheckingPolicy != null)
|
||||||
|
session.setConfig("StrictHostKeyChecking",
|
||||||
|
strictHostKeyCheckingPolicy);
|
||||||
|
final 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new remote session for the requested address.
|
* Create a new remote session for the requested address.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue