org.eclipse.jgit.api.TransportConfigCallback Java Examples

The following examples show how to use org.eclipse.jgit.api.TransportConfigCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void sshPropertiesWithinNestedRepo() throws Exception {
	TransportConfigCallback callback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(callback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);

	PropertiesBasedSshTransportConfigCallback configCallback = (PropertiesBasedSshTransportConfigCallback) callback;
	MultipleJGitEnvironmentProperties sshUriProperties = configCallback
			.getSshUriProperties();
	assertThat(configCallback.getSshUriProperties().getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_1);

	assertThat(sshUriProperties.getRepos().get("repo1")).isNotNull();
	assertThat(sshUriProperties.getRepos().get("repo1").getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_2);
}
 
Example #2
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void sshPropertiesWithinNestedRepo() throws Exception {
	TransportConfigCallback callback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(callback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);

	PropertiesBasedSshTransportConfigCallback configCallback = (PropertiesBasedSshTransportConfigCallback) callback;
	MultipleJGitEnvironmentProperties sshUriProperties = configCallback
			.getSshUriProperties();
	assertThat(configCallback.getSshUriProperties().getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_1);

	assertThat(sshUriProperties.getRepos().get("repo1")).isNotNull();
	assertThat(sshUriProperties.getRepos().get("repo1").getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_2);
}
 
Example #3
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@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, new JGitEnvironmentProperties());
	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);
}
 
Example #4
Source File: MultipleJGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@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();

	assertThat(mockCallback1).isEqualTo(repo1.getTransportConfigCallback());
	assertThat(mockCallback2).isEqualTo(repo2.getTransportConfigCallback());
}
 
Example #5
Source File: TransportConfigCallbackFactory.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
public TransportConfigCallback build(
		MultipleJGitEnvironmentProperties environmentProperties) {

	// customTransportConfigCallback has the highest priority. If someone put
	// a TransportConfigCallback bean in to the Spring context, we use it for
	// all repositories.
	if (customTransportConfigCallback != null) {
		return customTransportConfigCallback;
	}

	// If the currently configured repository is a Google Cloud Source repository
	// we use GoogleCloudSourceSupport.
	if (googleCloudSourceSupport != null) {
		final String uri = environmentProperties.getUri();
		if (googleCloudSourceSupport.canHandle(uri)) {
			return googleCloudSourceSupport.createTransportConfigCallback();
		}
	}

	// Otherwise - legacy behaviour - use SshTransportConfigCallback for all
	// repositories.
	return buildSshTransportConfigCallback(environmentProperties);
}
 
Example #6
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyBasedTransportCallbackIsConfigured() throws Exception {
	TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(transportConfigCallback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);
}
 
Example #7
Source File: TestSshGit.java    From Jpom with MIT License 5 votes vote down vote up
public static void main(String[] args) throws GitAPIException {
    Git.cloneRepository()
            .setURI("[email protected]:jiangzeyin/test.git")
            .setDirectory(new File("D:\\test\\gitssh"))
            .setTransportConfigCallback(new TransportConfigCallback() {
                @Override
                public void configure(Transport transport) {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
                        @Override
                        protected void configure(OpenSshConfig.Host hc, Session session) {
                            session.setConfig("StrictHostKeyChecking", "no");
                            session.setPassword("");
                        }

                        @Override
                        protected JSch createDefaultJSch(FS fs) throws JSchException {
                            JSch defaultJSch = super.createDefaultJSch(fs);

                            defaultJSch.addIdentity("C:\\Users\\Colorful\\.ssh\\id_rsa.pub");
                            return defaultJSch;
                        }

                    });
                }
            })
            .call();
}
 
Example #8
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyDoesNothingForSshGCSRepo() throws URISyntaxException {
	TransportConfigCallback callback = transportConfigCallbackWith(
			createAuthHeaders());
	TransportHttp transport = mockTransportHttp(SSH_GOOGLE_CLOUD_SOURCE_REPO);

	callback.configure(transport);

	verifyOnlyValidInteraction(transport);
}
 
Example #9
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyDoesNothingForHttpsOtherRepo() throws URISyntaxException {
	TransportConfigCallback callback = transportConfigCallbackWith(
			createAuthHeaders());
	TransportHttp transport = mockTransportHttp(HTTPS_OTHER_REPO);

	callback.configure(transport);

	verifyOnlyValidInteraction(transport);
}
 
Example #10
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyDoesNothingForNonHttpTransports() throws URISyntaxException {
	TransportConfigCallback callback = transportConfigCallbackWith(
			createAuthHeaders());
	Transport transport = mockSshTransport(SSH_GOOGLE_CLOUD_SOURCE_REPO);

	callback.configure(transport);

	verifyOnlyValidInteraction(transport);
}
 
Example #11
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyBasedTransportCallbackIsConfigured() throws Exception {
	TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(transportConfigCallback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);
}
 
Example #12
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySetsAuthHeadersForHttpGCSRepo() throws URISyntaxException {
	Map<String, String> authHeaders = createAuthHeaders();
	TransportConfigCallback callback = transportConfigCallbackWith(authHeaders);

	TransportHttp transport = mockTransportHttp(HTTP_GOOGLE_CLOUD_SOURCE_REPO);
	Map<String, String> actualHeaders = recordSetHeaders(transport);

	callback.configure(transport);

	assertThat(actualHeaders).containsAllEntriesOf(authHeaders);
}
 
Example #13
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void privateKeyPropertyWithLineBreaks() throws Exception {
	TransportConfigCallback callback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(callback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);

	PropertiesBasedSshTransportConfigCallback configCallback = (PropertiesBasedSshTransportConfigCallback) callback;
	assertThat(configCallback.getSshUriProperties().getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_1);
}
 
Example #14
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void privateKeyPropertyWithLineBreaks() throws Exception {
	TransportConfigCallback callback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(callback)
			.isInstanceOf(PropertiesBasedSshTransportConfigCallback.class);

	PropertiesBasedSshTransportConfigCallback configCallback = (PropertiesBasedSshTransportConfigCallback) callback;
	assertThat(configCallback.getSshUriProperties().getPrivateKey())
			.isEqualTo(TestProperties.TEST_PRIVATE_KEY_1);
}
 
Example #15
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void fileBasedTransportCallbackIsConfigured() throws Exception {
	TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(transportConfigCallback)
			.isInstanceOf(FileBasedSshTransportConfigCallback.class);
}
 
Example #16
Source File: TransportConfigurationIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void fileBasedTransportCallbackIsConfigured() throws Exception {
	TransportConfigCallback transportConfigCallback = this.jGitEnvironmentRepository
			.getTransportConfigCallback();
	assertThat(transportConfigCallback)
			.isInstanceOf(FileBasedSshTransportConfigCallback.class);
}
 
Example #17
Source File: GitContentRepository.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
private boolean isRemoteValid(Git git, String remote, String authenticationType,
                              String remoteUsername, String remotePassword, String remoteToken,
                              String remotePrivateKey)
        throws CryptoException, IOException, ServiceLayerException, GitAPIException {
    LsRemoteCommand lsRemoteCommand = git.lsRemote();
    lsRemoteCommand.setRemote(remote);
    switch (authenticationType) {
        case NONE:
            logger.debug("No authentication");
            break;
        case BASIC:
            logger.debug("Basic authentication");
            lsRemoteCommand.setCredentialsProvider(
                    new UsernamePasswordCredentialsProvider(remoteUsername, remotePassword));
            break;
        case TOKEN:
            logger.debug("Token based authentication");
            lsRemoteCommand.setCredentialsProvider(
                    new UsernamePasswordCredentialsProvider(remoteToken, EMPTY));
            break;
        case PRIVATE_KEY:
            logger.debug("Private key authentication");
            final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
            tempKey.toFile().deleteOnExit();
            lsRemoteCommand.setTransportConfigCallback(
                    new TransportConfigCallback() {
                        @Override
                        public void configure(Transport transport) {
                            SshTransport sshTransport = (SshTransport) transport;
                            sshTransport.setSshSessionFactory(getSshSessionFactory(remotePrivateKey, tempKey));
                        }
                    });
            Files.delete(tempKey);
            break;
        default:
            throw new ServiceLayerException("Unsupported authentication type " + authenticationType);
    }
    lsRemoteCommand.call();
    return true;
}
 
Example #18
Source File: EnvironmentRepositoryConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
public MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory(
		ConfigurableEnvironment environment, ConfigServerProperties server,
		Optional<ConfigurableHttpConnectionFactory> jgitHttpConnectionFactory,
		Optional<TransportConfigCallback> customTransportConfigCallback,
		Optional<GoogleCloudSourceSupport> googleCloudSourceSupport) {
	final TransportConfigCallbackFactory transportConfigCallbackFactory = new TransportConfigCallbackFactory(
			customTransportConfigCallback.orElse(null),
			googleCloudSourceSupport.orElse(null));
	return new MultipleJGitEnvironmentRepositoryFactory(environment, server,
			jgitHttpConnectionFactory, transportConfigCallbackFactory);
}
 
Example #19
Source File: GitMonitoringService.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private TransportConfigCallback buildTransportConfigCallback() {
  if (this.providerSessionFactoryEither instanceof Either.Left) return null;

  SshSessionFactory sshSessionFactory = ((Either.Right<CredentialsProvider, SshSessionFactory>) this.providerSessionFactoryEither).getRight();
  return transport -> {
    SshTransport sshTransport = (SshTransport) transport;
    sshTransport.setSshSessionFactory(sshSessionFactory);
  };
}
 
Example #20
Source File: TransportConfigCallbackFactory.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
public TransportConfigCallbackFactory(
		TransportConfigCallback customTransportConfigCallback,
		GoogleCloudSourceSupport googleCloudSourceSupport) {

	this.customTransportConfigCallback = customTransportConfigCallback;
	this.googleCloudSourceSupport = googleCloudSourceSupport;
}
 
Example #21
Source File: TransportConfigCallbackFactory.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private TransportConfigCallback buildSshTransportConfigCallback(
		MultipleJGitEnvironmentProperties gitEnvironmentProperties) {

	if (gitEnvironmentProperties.isIgnoreLocalSshSettings()) {
		return new PropertiesBasedSshTransportConfigCallback(
				gitEnvironmentProperties);
	}
	return new FileBasedSshTransportConfigCallback(gitEnvironmentProperties);
}
 
Example #22
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySetsAuthHeadersForHttpsGCSRepo() throws URISyntaxException {
	Map<String, String> authHeaders = createAuthHeaders();
	TransportConfigCallback callback = transportConfigCallbackWith(authHeaders);

	TransportHttp transport = mockTransportHttp(HTTPS_GOOGLE_CLOUD_SOURCE_REPO);
	Map<String, String> actualHeaders = recordSetHeaders(transport);

	callback.configure(transport);

	assertThat(actualHeaders).containsAllEntriesOf(authHeaders);
}
 
Example #23
Source File: GoogleCloudSourceSupport.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
TransportConfigCallback createTransportConfigCallback(
		CredentialsProvider credentialsProvider) {
	return new GCSTransportConfigCallback(credentialsProvider);
}
 
Example #24
Source File: GoogleCloudSourceSupportTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private TransportConfigCallback transportConfigCallbackWith(
		Map<String, String> authHeaders) {
	CredentialsProvider credentialsProvider = () -> authHeaders;
	return new GoogleCloudSourceSupport()
			.createTransportConfigCallback(credentialsProvider);
}
 
Example #25
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Bean
public TransportConfigCallback transportConfigCallback() {
	return Mockito.mock(TransportConfigCallback.class);
}
 
Example #26
Source File: GoogleCloudSourceSupport.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
TransportConfigCallback createTransportConfigCallback() {
	return new GCSTransportConfigCallback(
			new ApplicationDefaultCredentialsProvider());
}
 
Example #27
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public TransportConfigCallback getTransportConfigCallback() {
	return this.transportConfigCallback;
}
 
Example #28
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public void setTransportConfigCallback(
		TransportConfigCallback transportConfigCallback) {
	this.transportConfigCallback = transportConfigCallback;
}
 
Example #29
Source File: GitConfigStore.java    From vertx-config with Apache License 2.0 4 votes vote down vote up
public GitConfigStore(Vertx vertx, JsonObject configuration) {
  this.vertx = vertx;

  String path = Objects.requireNonNull(configuration.getString("path"),
    "The `path` configuration is required.");
  this.path = new File(path);
  if (this.path.isFile()) {
    throw new IllegalArgumentException("The `path` must not be a file");
  }

  JsonArray filesets = Objects.requireNonNull(configuration
      .getJsonArray("filesets"),
    "The `filesets` element is required.");

  for (Object o : filesets) {
    JsonObject json = (JsonObject) o;
    FileSet set = new FileSet(vertx, this.path, json);
    this.filesets.add(set);
  }

  // Git repository
  url = Objects.requireNonNull(configuration.getString("url"),
    "The `url` configuration (Git repository location) is required.");
  branch = configuration.getString("branch", "master");
  remote = configuration.getString("remote", "origin");

  if (Objects.nonNull(configuration.getString("user")) &&
      Objects.nonNull(configuration.getString("password"))) {
    credentialProvider = new UsernamePasswordCredentialsProvider(
      configuration.getString("user"), configuration.getString("password"));
  } else {
    credentialProvider = null;
  }
  if(Objects.nonNull(configuration.getString("idRsaKeyPath"))){
    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
      @Override
      protected void configure(OpenSshConfig.Host host, Session session ) {
      }
      @Override
      protected JSch createDefaultJSch(FS fs ) throws JSchException {
        JSch defaultJSch = super.createDefaultJSch( fs );
        defaultJSch.setConfig("StrictHostKeyChecking", "no");
        defaultJSch.addIdentity(configuration.getString("idRsaKeyPath"));
        return defaultJSch;
      }
    };
    transportConfigCallback = new TransportConfigCallback() {
      @Override
      public void configure( Transport transport ) {
        SshTransport sshTransport = ( SshTransport )transport;
        sshTransport.setSshSessionFactory( sshSessionFactory );
      }
    };
  }else {
    transportConfigCallback = null;
  }

  try {
    git = initializeGit();
  } catch (Exception e) {
    throw new VertxException("Unable to initialize the Git repository", e);
  }
}
 
Example #30
Source File: BaseBackup.java    From opsgenie-configuration-backup with Apache License 2.0 4 votes vote down vote up
TransportConfigCallback getCallBack() {
    return callBack;
}