use of org.eclipse.jgit.api.TransportConfigCallback in project opsgenie-configuration-backup by opsgenie.
the class BaseBackup method cloneGit.
void cloneGit(final BackupProperties properties) throws GitAPIException {
properties.setPath(properties.getPath() + "/OpsGenieBackupGitRepository");
String rootPath = properties.getPath();
File backupGitDirectory = new File(rootPath);
if (backupGitDirectory.exists() && (backupGitDirectory.list() != null) && (backupGitDirectory.list().length > 0)) {
logger.warn("Destination path " + rootPath + " already exists and is not an empty directory");
logger.warn("Destination path " + rootPath + " will be deleted inorder to clone remote git repository");
BackupUtils.deleteDirectory(backupGitDirectory);
backupGitDirectory.mkdirs();
} else {
backupGitDirectory.mkdirs();
}
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
CredentialsProvider provider = new CredentialsProvider() {
@Override
public boolean isInteractive() {
return false;
}
@Override
public boolean supports(CredentialItem... items) {
return true;
}
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
for (CredentialItem item : items) {
((CredentialItem.StringType) item).setValue(backupProperties.getPassphrase());
}
return true;
}
};
UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
session.setUserInfo(userInfo);
}
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.removeAllIdentity();
defaultJSch.addIdentity(properties.getSshKeyPath());
return defaultJSch;
}
};
callBack = new TransportConfigCallback() {
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
};
logger.info("Cloning remote git operation started!");
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI(properties.getGitSshUri());
cloneCommand.setTransportConfigCallback(callBack);
StatusLogger.getLogger().setLevel(Level.OFF);
cloneCommand.setDirectory(backupGitDirectory);
git = cloneCommand.call();
logger.info("Cloning remote git operation finished!");
}
use of org.eclipse.jgit.api.TransportConfigCallback in project spring-cloud-config by spring-cloud.
the class MultipleJGitEnvironmentRepositoryTests method shouldSetTransportConfigCallback.
@Test
public void shouldSetTransportConfigCallback() throws Exception {
TransportConfigCallback mockCallback1 = mock(TransportConfigCallback.class);
TransportConfigCallback mockCallback2 = mock(TransportConfigCallback.class);
PatternMatchingJGitEnvironmentRepository repo1 = createRepository("test1", "*test1*", "test1Uri");
PatternMatchingJGitEnvironmentRepository repo2 = createRepository("test2", "*test2*", "test2Uri");
repo2.setTransportConfigCallback(mockCallback2);
Map<String, PatternMatchingJGitEnvironmentRepository> repos = new HashMap<>();
repos.put("test1", repo1);
repos.put("test2", repo2);
this.repository.setRepos(repos);
this.repository.setTransportConfigCallback(mockCallback1);
this.repository.afterPropertiesSet();
assertEquals(repo1.getTransportConfigCallback(), mockCallback1);
assertEquals(repo2.getTransportConfigCallback(), mockCallback2);
}
use of org.eclipse.jgit.api.TransportConfigCallback in project che by eclipse.
the class JGitConnectionTest method shouldSetSshSessionFactoryWhenSshTransportReceived.
@Test
public void shouldSetSshSessionFactoryWhenSshTransportReceived() throws Exception {
//given
SshTransport sshTransport = mock(SshTransport.class);
when(sshKeyProvider.getPrivateKey(anyString())).thenReturn(new byte[0]);
doAnswer(invocation -> {
TransportConfigCallback callback = (TransportConfigCallback) invocation.getArguments()[0];
callback.configure(sshTransport);
return null;
}).when(transportCommand).setTransportConfigCallback(any());
//when
jGitConnection.executeRemoteCommand("ssh://host.xz/repo.git", transportCommand, null, null);
//then
verify(sshTransport).setSshSessionFactory(any());
}
use of org.eclipse.jgit.api.TransportConfigCallback in project che by eclipse.
the class JGitConnectionTest method shouldDoNothingWhenTransportHttpReceived.
@Test
public void shouldDoNothingWhenTransportHttpReceived() throws Exception {
//given
/*
* We need create {@link TransportHttp} mock, but this class has parent
* abstract class {@link Transport}. Class Transport uses fields of children
* classes for static initialization collection {@link Transport#protocols}.
* When we create mock for {@link TransportHttp} - Mockito mocks fields and
* they return null value. For full mock creation TransportHttp Mockito
* launches static block in the parent class {@link Transport}, but static
* block initializes collection with help mocked children fields which
* return null values, so Transport class loses real field value in the
* collection. It creates troubles in other tests when we use real object
* of TransportHttp(collection 'protocols' contains not all values).
* To realize right initialization {@link Transport#protocols} we create
* mock of {@link Transport} and this class initializes collection "protocols"
* with help real children {@link TransportHttp}, which returns real not null
* value. And then we can create mock {@link TransportHttp}.
*/
mock(Transport.class);
TransportHttp transportHttp = mock(TransportHttp.class);
when(sshKeyProvider.getPrivateKey(anyString())).thenReturn(new byte[0]);
doAnswer(invocation -> {
TransportConfigCallback callback = (TransportConfigCallback) invocation.getArguments()[0];
callback.configure(transportHttp);
return null;
}).when(transportCommand).setTransportConfigCallback(any());
//when
jGitConnection.executeRemoteCommand("ssh://host.xz/repo.git", transportCommand, null, null);
//then
verifyZeroInteractions(transportHttp);
}
use of org.eclipse.jgit.api.TransportConfigCallback in project blueocean-plugin by jenkinsci.
the class GitUtils method getSSHKeyTransport.
private static TransportConfigCallback getSSHKeyTransport(final BasicSSHUserPrivateKey privateKey) {
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, com.jcraft.jsch.Session session) {
// jenkins user doesn't likely have the host key
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch getJSch(OpenSshConfig.Host hc, FS fs) throws JSchException {
JSch jsch = new JSch();
configureJSch(jsch);
// TODO: might need this: jsch.setHostKeyRepository(new KnownHosts(this));
try {
KeyPair pair = KeyPair.load(jsch, privateKey.getPrivateKey().getBytes("utf-8"), null);
byte[] passphrase = new byte[0];
jsch.addIdentity(privateKey.getUsername(), pair.forSSHAgent(), null, passphrase);
return jsch;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
};
return new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
if (transport instanceof SshTransport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}
};
}
Aggregations