use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gerrit by GerritCodeReview.
the class AbstractHttpPushTag method cloneProjectOverHttp.
@Before
public void cloneProjectOverHttp() throws Exception {
// clone with user to avoid inherited tag permissions of admin user
CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider(user.username(), user.httpPassword()));
testRepo = GitUtil.cloneProject(project, user.getHttpUrl(server) + "/" + project.get());
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project che by eclipse.
the class JGitConnection method executeRemoteCommand.
/**
* Execute remote jgit command.
*
* @param remoteUrl
* remote url
* @param command
* command to execute
* @return executed command
* @throws GitException
* @throws GitAPIException
* @throws UnauthorizedException
*/
@VisibleForTesting
Object executeRemoteCommand(String remoteUrl, TransportCommand command, @Nullable String username, @Nullable String password) throws GitException, GitAPIException, UnauthorizedException {
File keyDirectory = null;
UserCredential credentials = null;
try {
if (GitUrlUtils.isSSH(remoteUrl)) {
keyDirectory = Files.createTempDir();
final File sshKey = writePrivateKeyFile(remoteUrl, keyDirectory);
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
JSch jsch = super.getJSch(hc, fs);
jsch.removeAllIdentity();
jsch.addIdentity(sshKey.getAbsolutePath());
return jsch;
}
};
command.setTransportConfigCallback(transport -> {
if (transport instanceof SshTransport) {
((SshTransport) transport).setSshSessionFactory(sshSessionFactory);
}
});
} else {
if (remoteUrl != null && GIT_URL_WITH_CREDENTIALS_PATTERN.matcher(remoteUrl).matches()) {
username = remoteUrl.substring(remoteUrl.indexOf("://") + 3, remoteUrl.lastIndexOf(":"));
password = remoteUrl.substring(remoteUrl.lastIndexOf(":") + 1, remoteUrl.indexOf("@"));
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
} else {
if (username != null && password != null) {
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
} else {
credentials = credentialsLoader.getUserCredential(remoteUrl);
if (credentials != null) {
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(credentials.getUserName(), credentials.getPassword()));
}
}
}
}
ProxyAuthenticator.initAuthenticator(remoteUrl);
return command.call();
} catch (GitException | TransportException exception) {
if ("Unable get private ssh key".equals(exception.getMessage())) {
throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY);
} else if (exception.getMessage().contains(ERROR_AUTHENTICATION_REQUIRED)) {
final ProviderInfo info = credentialsLoader.getProviderInfo(remoteUrl);
if (info != null) {
throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION, ImmutableMap.of(PROVIDER_NAME, info.getProviderName(), AUTHENTICATE_URL, info.getAuthenticateUrl(), "authenticated", Boolean.toString(credentials != null)));
}
throw new UnauthorizedException(exception.getMessage(), ErrorCodes.UNAUTHORIZED_GIT_OPERATION);
} else {
throw exception;
}
} finally {
if (keyDirectory != null && keyDirectory.exists()) {
try {
FileUtils.delete(keyDirectory, FileUtils.RECURSIVE);
} catch (IOException exception) {
throw new GitException("Can't remove SSH key directory", exception);
}
}
ProxyAuthenticator.resetAuthenticator();
}
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project che by eclipse.
the class JGitConnectionTest method shouldExecuteRemoteCommandByHttpOrHttpsUrlWithCredentials.
@Test(dataProvider = "gitUrlsWithCredentials")
public void shouldExecuteRemoteCommandByHttpOrHttpsUrlWithCredentials(String url) throws Exception {
//given
ArgumentCaptor<UsernamePasswordCredentialsProvider> captor = ArgumentCaptor.forClass(UsernamePasswordCredentialsProvider.class);
Field usernameField = UsernamePasswordCredentialsProvider.class.getDeclaredField("username");
Field passwordField = UsernamePasswordCredentialsProvider.class.getDeclaredField("password");
usernameField.setAccessible(true);
passwordField.setAccessible(true);
//when
jGitConnection.executeRemoteCommand(url, transportCommand, null, null);
//then
verify(transportCommand).setCredentialsProvider(captor.capture());
UsernamePasswordCredentialsProvider credentialsProvider = captor.getValue();
String username = (String) usernameField.get(credentialsProvider);
char[] password = (char[]) passwordField.get(credentialsProvider);
assertEquals(username, "username");
assertEquals(String.valueOf(password), "password");
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gerrit by GerritCodeReview.
the class HttpPushForReviewIT method selectHttpUrl.
@Before
public void selectHttpUrl() throws Exception {
CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider(admin.username, admin.httpPassword));
selectProtocol(Protocol.HTTP);
}
use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project camel by apache.
the class GitProducer method doPush.
protected void doPush(Exchange exchange, String operation) throws Exception {
Iterable<PushResult> result = null;
try {
if (ObjectHelper.isEmpty(endpoint.getRemoteName())) {
throw new IllegalArgumentException("Remote name must be specified to execute " + operation);
}
if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
}
if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && ObjectHelper.isNotEmpty(endpoint.getPassword())) {
UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(endpoint.getUsername(), endpoint.getPassword());
result = git.push().setCredentialsProvider(credentials).setRemote(endpoint.getRemoteName()).call();
} else {
result = git.push().setRemote(endpoint.getRemoteName()).call();
}
} catch (Exception e) {
LOG.error("There was an error in Git " + operation + " operation");
throw e;
}
updateExchange(exchange, result);
}
Aggregations