Search in sources :

Example 1 with TransportConfigCallback

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!");
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 2 with TransportConfigCallback

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);
}
Also used : HashMap(java.util.HashMap) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PatternMatchingJGitEnvironmentRepository(org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.PatternMatchingJGitEnvironmentRepository) Test(org.junit.Test)

Example 3 with TransportConfigCallback

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());
}
Also used : TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) SshTransport(org.eclipse.jgit.transport.SshTransport) Test(org.testng.annotations.Test)

Example 4 with TransportConfigCallback

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);
}
Also used : TransportHttp(org.eclipse.jgit.transport.TransportHttp) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) Test(org.testng.annotations.Test)

Example 5 with TransportConfigCallback

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);
            }
        }
    };
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SshSessionFactory(org.eclipse.jgit.transport.SshSessionFactory) JSch(com.jcraft.jsch.JSch) FS(org.eclipse.jgit.util.FS) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) JschConfigSessionFactory(org.eclipse.jgit.transport.JschConfigSessionFactory) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) SshTransport(org.eclipse.jgit.transport.SshTransport)

Aggregations

TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)9 Test (org.junit.Test)4 JSch (com.jcraft.jsch.JSch)3 SshTransport (org.eclipse.jgit.transport.SshTransport)3 FS (org.eclipse.jgit.util.FS)3 Session (com.jcraft.jsch.Session)2 UserInfo (com.jcraft.jsch.UserInfo)2 File (java.io.File)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 JschConfigSessionFactory (org.eclipse.jgit.transport.JschConfigSessionFactory)2 SshSessionFactory (org.eclipse.jgit.transport.SshSessionFactory)2 Transport (org.eclipse.jgit.transport.Transport)2 SshUriProperties (org.springframework.cloud.config.server.ssh.SshUriProperties)2 Test (org.testng.annotations.Test)2 JSchException (com.jcraft.jsch.JSchException)1 KeyPair (com.jcraft.jsch.KeyPair)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 SSLSession (javax.net.ssl.SSLSession)1 FetchCommand (org.eclipse.jgit.api.FetchCommand)1