public static TransportConfigCallback getTransportConfigCallback() { final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { //session.setPassword(password); } }; return new TransportConfigCallback() { public void configure(Transport transport) { if (transport instanceof TransportHttp) return; SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sshSessionFactory); } }; }
@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()); }
@Test public void shouldSetTransportConfigCallbackOnCloneAndFetch() throws Exception { Git mockGit = mock(Git.class); FetchCommand fetchCommand = mock(FetchCommand.class); when(mockGit.fetch()).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(mock(FetchResult.class)); CloneCommand mockCloneCommand = mock(CloneCommand.class); when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand); when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand); TransportConfigCallback configCallback = mock(TransportConfigCallback.class); JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment); envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand)); envRepository.setUri("http://somegitserver/somegitrepo"); envRepository.setTransportConfigCallback(configCallback); envRepository.setCloneOnStart(true); envRepository.afterPropertiesSet(); verify(mockCloneCommand, times(1)).setTransportConfigCallback(configCallback); envRepository.fetch(mockGit, "master"); verify(fetchCommand, times(1)).setTransportConfigCallback(configCallback); }
@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); }
@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); }
/** * Configures the transport of the command to deal with things like SSH */ public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) { if (sshPrivateKey != null) { final CredentialsProvider provider = credentialsProvider; command.setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { if (transport instanceof SshTransport) { SshTransport sshTransport = (SshTransport) transport; SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { session.setConfig("StrictHostKeyChecking", "no"); UserInfo userInfo = new CredentialsProviderUserInfo(session, provider); session.setUserInfo(userInfo); } @Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jsch = super.createDefaultJSch(fs); jsch.removeAllIdentity(); String absolutePath = sshPrivateKey.getAbsolutePath(); if (LOG.isDebugEnabled()) { LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: " + sshPublicKey); } if (sshPublicKey != null) { jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null); } else { jsch.addIdentity(absolutePath); } return jsch; } }; sshTransport.setSshSessionFactory(sshSessionFactory); } } }); } }
@Override protected void configure() { install(RepositoryScope.module()); install(OperationScope.module()); bind(UserInfo.class).to(GUIUserInfo.class); bind(ImageSession.class).toProvider(ImageSessionProvider.class).in(ContextSingleton.class); bind(Repository.class).toProvider(RepositoryProvider.class); bind(Ref.class).annotatedWith(named("branch")).toProvider(BranchRefProvider.class); bind(AndroidAuthAgent.class).toProvider(AndroidAuthAgentProvider.class); bind(GitAsyncTaskFactory.class).toProvider(newFactory(GitAsyncTaskFactory.class, GitAsyncTask.class)); bind(ContextScopedViewInflatorFactory.class).toProvider(newFactory(ContextScopedViewInflatorFactory.class, ContextScopedViewInflator.class)); bind(SyncCampaignFactory.class).toProvider(newFactory(SyncCampaignFactory.class, SyncCampaign.class)); bind(TransportConfigCallback.class).to(AgitTransportConfig.class); bind(CredentialsProvider.class).to(GUICredentialsProvider.class); bind(SshSessionFactory.class).to(AndroidSshSessionFactory.class); bind(PromptUIRegistry.class); bind(HostKeyRepository.class).to(CuriousHostKeyRepository.class); bind(PromptUI.class).annotatedWith(named("status-bar")).to(StatusBarPromptUI.class); bind(RepoDomainType.class).annotatedWith(named("branch")).to(RDTBranch.class); bind(RepoDomainType.class).annotatedWith(named("remote")).to(RDTRemote.class); bind(RepoDomainType.class).annotatedWith(named("tag")).to(RDTTag.class); bind(CommitViewHolderFactory.class).toProvider(newFactory(CommitViewHolderFactory.class, CommitViewHolder.class)); bind(BranchViewHolderFactory.class).toProvider(newFactory(BranchViewHolderFactory.class, BranchViewHolder.class)); }
@ConditionalOnMissingBean(TransportConfigCallback.class) @Bean public TransportConfigCallback propertiesBasedSshTransportCallback(final SshUriProperties sshUriProperties) { if(sshUriProperties.isIgnoreLocalSshSettings()) { return new PropertiesBasedSshTransportConfigCallback(sshUriProperties); } else return new FileBasedSshTransportConfigCallback(sshUriProperties); }
@Test public void privateKeyPropertyWithLineBreaks() throws Exception { TransportConfigCallback transportConfigCallback = jGitEnvironmentRepository.getTransportConfigCallback(); assertThat(transportConfigCallback, is(instanceOf(TransportConfiguration.PropertiesBasedSshTransportConfigCallback.class))); TransportConfiguration.PropertiesBasedSshTransportConfigCallback configCallback = (TransportConfiguration.PropertiesBasedSshTransportConfigCallback) transportConfigCallback; assertThat(configCallback.getSshUriProperties().getPrivateKey(), is(equalTo(TestProperties.TEST_PRIVATE_KEY_1))); }
@Test public void sshPropertiesWithinNestedRepo() throws Exception { TransportConfigCallback transportConfigCallback = jGitEnvironmentRepository.getTransportConfigCallback(); assertThat(transportConfigCallback, is(instanceOf(TransportConfiguration.PropertiesBasedSshTransportConfigCallback.class))); TransportConfiguration.PropertiesBasedSshTransportConfigCallback configCallback = (TransportConfiguration.PropertiesBasedSshTransportConfigCallback) transportConfigCallback; SshUriProperties sshUriProperties = configCallback.getSshUriProperties(); assertThat(sshUriProperties.getPrivateKey(), is(equalTo(TestProperties.TEST_PRIVATE_KEY_1))); assertThat(sshUriProperties.getRepos().get("repo1"), is(notNullValue())); assertThat(sshUriProperties.getRepos().get("repo1").getPrivateKey(), is(equalTo(TestProperties.TEST_PRIVATE_KEY_2))); }
public static File cloneRepository(String cloneUrl, String repoPw) throws GitAPIException, JSONException, IOException { config = ConfigParser.getConfig(); File tmpDir = new File("temp_repo"); String key = null; String keyPassPhrase = null; if (config.has("privateKey")) { key = config.getString("privateKey"); keyPassPhrase = config.getString("privateKeyPassPhrase"); } // git clone will fail if the directory already exists, even if empty if (tmpDir.exists()) { FileUtils.deleteDirectory(tmpDir); } String pw = null; if (repoPw != null) { pw = repoPw; } else if (config.has("gitClonePassword")) { pw = config.getString("gitClonePassword"); } final String finalKeyPassPhrase = keyPassPhrase; final String finalKey = key; SshSessionFactory sessionFactory = new CustomJschConfigSessionFactory(); // use a private key if provided if (finalKey != null) { SshSessionFactory.setInstance(sessionFactory); } // use a password if provided if (pw != null) { final String finalPw = pw; SshSessionFactory.setInstance(new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { session.setPassword(finalPw); } }); } SshSessionFactory.setInstance(sessionFactory); Git.cloneRepository() .setURI(cloneUrl) .setDirectory(tmpDir) .setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sessionFactory); } }) .call(); return tmpDir; }
public TransportConfigCallback getTransportConfigCallback() { return transportConfigCallback; }
public void setTransportConfigCallback( TransportConfigCallback transportConfigCallback) { this.transportConfigCallback = transportConfigCallback; }
@Bean public TransportConfigCallback transportConfigCallback() { return Mockito.mock(TransportConfigCallback.class); }
@Test public void propertyBasedTransportCallbackIsConfigured() throws Exception { TransportConfigCallback transportConfigCallback = jGitEnvironmentRepository.getTransportConfigCallback(); assertThat(transportConfigCallback, is(instanceOf(TransportConfiguration.PropertiesBasedSshTransportConfigCallback.class))); }
@Test public void fileBasedTransportCallbackIsConfigured() throws Exception { TransportConfigCallback transportConfigCallback = jGitEnvironmentRepository.getTransportConfigCallback(); assertThat(transportConfigCallback, is(instanceOf(TransportConfiguration.FileBasedSshTransportConfigCallback.class))); }