类org.eclipse.jgit.transport.SshSessionFactory源码实例Demo

下面列出了怎么用org.eclipse.jgit.transport.SshSessionFactory的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: studio   文件: GitRepositoryHelper.java
public SshSessionFactory getSshSessionFactory(String privateKey, final Path tempKey)  {
    try {
        Files.write(tempKey, privateKey.getBytes());
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host hc, Session session) {
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = new JSch();
                defaultJSch.addIdentity(tempKey.toAbsolutePath().toString());
                return defaultJSch;
            }
        };
        return sshSessionFactory;
    } catch (IOException e) {
        logger.error("Failed to create private key for SSH connection.", e);
    }
    return null;
}
 
源代码2 项目: studio   文件: GitContentRepository.java
private SshSessionFactory getSshSessionFactory(String privateKey, final Path tempKey)  {
    try {
        Files.write(tempKey, privateKey.getBytes());
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host hc, Session session) {
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = new JSch();
                defaultJSch.addIdentity(tempKey.toAbsolutePath().toString());
                return defaultJSch;
            }
        };
        return sshSessionFactory;
    } catch (IOException e) {
        logger.error("Failed to create private key for SSH connection.", e);
    }
    return null;
}
 
源代码3 项目: studio   文件: GitContentRepositoryHelper.java
private SshSessionFactory getSshSessionFactory(String remotePrivateKey, final Path tempKey) {
    try {

        Files.write(tempKey, remotePrivateKey.getBytes());
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host hc, Session session) {
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = super.createDefaultJSch(fs);
                defaultJSch.addIdentity(tempKey.toAbsolutePath().toString());
                return defaultJSch;
            }
        };
        return sshSessionFactory;
    } catch (IOException e) {
        logger.error("Failed to create private key for SSH connection.", e);
    }
    return null;
}
 
源代码4 项目: git-client-plugin   文件: JGitAPIImpl.java
JGitAPIImpl(File workspace, TaskListener listener, final PreemptiveAuthHttpClientConnectionFactory httpConnectionFactory) {
    /* If workspace is null, then default to current directory to match 
     * CliGitAPIImpl behavior */
    super(workspace == null ? new File(".") : workspace);
    this.listener = listener;

    // to avoid rogue plugins from clobbering what we use, always
    // make a point of overwriting it with ours.
    SshSessionFactory.setInstance(new TrileadSessionFactory());

    if (httpConnectionFactory != null) {
        httpConnectionFactory.setCredentialsProvider(asSmartCredentialsProvider());
        // allow override of HttpConnectionFactory to avoid JENKINS-37934
        HttpTransport.setConnectionFactory(httpConnectionFactory);
    }
}
 
源代码5 项目: netbeans   文件: GitRepository.java
private synchronized JGitRepository getRepository () {
    if (gitRepository == null) {
        gitRepository = new JGitRepository(repositoryLocation);
        SshSessionFactory.setInstance(JGitSshSessionFactory.getDefault());
    }
    return gitRepository;
}
 
源代码6 项目: incubator-gobblin   文件: GitMonitoringService.java
/**
 * Create an object to manage the git repository stored locally at repoDir with a repository URI of repoDir
 * @param repoUri URI of repository
 * @param repoDir Directory to hold the local copy of the repository
 * @param branchName Branch name
 * @param providerSessionFactoryEither Either {@link UsernamePasswordCredentialsProvider} or {@link SshSessionFactory}
 * @param shouldCheckpointHashes a boolean to determine whether to checkpoint commit hashes
 * @throws GitAPIException
 * @throws IOException
 */
GitRepository(String repoUri, String repoDir, String branchName, Either<CredentialsProvider, SshSessionFactory>
    providerSessionFactoryEither, boolean shouldCheckpointHashes) throws GitAPIException, IOException {
  this.repoUri = repoUri;
  this.repoDir = repoDir;
  this.branchName = branchName;
  this.providerSessionFactoryEither = providerSessionFactoryEither;
  this.shouldCheckpointHashes = shouldCheckpointHashes;

  initRepository();
}
 
源代码7 项目: incubator-gobblin   文件: GitMonitoringService.java
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);
  };
}
 
源代码8 项目: incubator-gobblin   文件: GitMonitoringService.java
private SshSessionFactory getSshSessionFactory() {
  JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
    @Override
    protected void configure(OpenSshConfig.Host hc, Session session) {
      if (!GitMonitoringService.this.strictHostKeyCheckingEnabled) {
        session.setConfig("StrictHostKeyChecking", "no");
      }
    }

    @Override
    protected JSch createDefaultJSch(FS fs) throws JSchException {
      if (GitMonitoringService.this.isJschLoggerEnabled) {
        JSch.setLogger(new JschLogger());
      }
      JSch defaultJSch = super.createDefaultJSch(fs);
      defaultJSch.getIdentityRepository().removeAll();
      if (GitMonitoringService.this.privateKeyPath != null) {
        defaultJSch.addIdentity(GitMonitoringService.this.privateKeyPath, GitMonitoringService.this.passphrase);
      } else {
        defaultJSch.addIdentity("gaas-git", GitMonitoringService.this.privateKey, null,
            GitMonitoringService.this.passphrase.getBytes(Charset.forName("UTF-8")));
      }
      if (!Strings.isNullOrEmpty(GitMonitoringService.this.knownHosts)) {
        defaultJSch.setKnownHosts(new ByteArrayInputStream(GitMonitoringService.this.knownHosts.getBytes(Charset.forName("UTF-8"))));
      } else if (!Strings.isNullOrEmpty(GitMonitoringService.this.knownHostsFile)) {
        defaultJSch.setKnownHosts(GitMonitoringService.this.knownHostsFile);
      }
      return defaultJSch;
    }
  };
  return sessionFactory;
}
 
源代码9 项目: Flashtool   文件: DevicesGit.java
public static void gitSync() throws IOException, InvalidRemoteException, org.eclipse.jgit.api.errors.TransportException, GitAPIException {
	SshSessionFactory.setInstance(new JschConfigSessionFactory() {
		  public void configure(Host hc, Session session) {
		    session.setConfig("StrictHostKeyChecking", "no");
		  };
		}
	);
	if (openRepository()) {
		pullRepository();
	}
	else cloneRepository();
}
 
private void initialize() {
	if (!this.initialized) {
		SshSessionFactory.setInstance(new JschConfigSessionFactory() {
			@Override
			protected void configure(Host hc, Session session) {
				session.setConfig("StrictHostKeyChecking",
						isStrictHostKeyChecking() ? "yes" : "no");
			}
		});
		this.initialized = true;
	}
}
 
@Override
public void configure(Transport transport) {
	SshSessionFactory.setInstance(new JschConfigSessionFactory() {
		@Override
		protected void configure(OpenSshConfig.Host hc, Session session) {
			session.setConfig("StrictHostKeyChecking",
					FileBasedSshTransportConfigCallback.this.sshUriProperties
							.isStrictHostKeyChecking() ? "yes" : "no");
		}
	});
}
 
@Test
public void strictHostKeyCheckShouldCheck() throws Exception {
	String uri = "git+ssh://[email protected]/somegitrepo";
	SshSessionFactory.setInstance(null);
	this.jGitEnvironmentRepository.setUri(uri);
	this.jGitEnvironmentRepository.setBasedir(new File("./mybasedir"));
	assertThat(this.jGitEnvironmentRepository.isStrictHostKeyChecking())
			.isTrue();
	this.jGitEnvironmentRepository.setCloneOnStart(true);
	try {
		// this will throw but we don't care about connecting.
		this.jGitEnvironmentRepository.afterPropertiesSet();
	}
	catch (Exception e) {
		final OpenSshConfig.Host hc = OpenSshConfig.get(FS.detect())
				.lookup("github.com");
		JschConfigSessionFactory factory = (JschConfigSessionFactory) SshSessionFactory
				.getInstance();
		// There's no public method that can be used to inspect the ssh
		// configuration, so we'll reflect
		// the configure method to allow us to check that the config
		// property is set as expected.
		Method configure = factory.getClass().getDeclaredMethod("configure",
				OpenSshConfig.Host.class, Session.class);
		configure.setAccessible(true);
		Session session = mock(Session.class);
		ArgumentCaptor<String> keyCaptor = ArgumentCaptor
				.forClass(String.class);
		ArgumentCaptor<String> valueCaptor = ArgumentCaptor
				.forClass(String.class);
		configure.invoke(factory, hc, session);
		verify(session).setConfig(keyCaptor.capture(), valueCaptor.capture());
		configure.setAccessible(false);
		assertThat("yes".equals(valueCaptor.getValue())).isTrue();
	}
}
 
@Test
public void strictHostKeyCheckShouldCheck() throws Exception {
	String uri = "git+ssh://[email protected]/somegitrepo";
	SshSessionFactory.setInstance(null);
	this.jGitEnvironmentRepository.setUri(uri);
	this.jGitEnvironmentRepository.setBasedir(new File("./mybasedir"));
	assertThat(this.jGitEnvironmentRepository.isStrictHostKeyChecking())
			.isTrue();
	this.jGitEnvironmentRepository.setCloneOnStart(true);
	try {
		// this will throw but we don't care about connecting.
		this.jGitEnvironmentRepository.afterPropertiesSet();
	}
	catch (Exception e) {
		final OpenSshConfig.Host hc = OpenSshConfig.get(FS.detect())
				.lookup("github.com");
		JschConfigSessionFactory factory = (JschConfigSessionFactory) SshSessionFactory
				.getInstance();
		// There's no public method that can be used to inspect the ssh
		// configuration, so we'll reflect
		// the configure method to allow us to check that the config
		// property is set as expected.
		Method configure = factory.getClass().getDeclaredMethod("configure",
				OpenSshConfig.Host.class, Session.class);
		configure.setAccessible(true);
		Session session = mock(Session.class);
		ArgumentCaptor<String> keyCaptor = ArgumentCaptor
				.forClass(String.class);
		ArgumentCaptor<String> valueCaptor = ArgumentCaptor
				.forClass(String.class);
		configure.invoke(factory, hc, session);
		verify(session).setConfig(keyCaptor.capture(), valueCaptor.capture());
		configure.setAccessible(false);
		assertThat("yes".equals(valueCaptor.getValue())).isTrue();
	}
}
 
源代码14 项目: mOrgAnd   文件: JGitWrapper.java
private void setupJGitAuthentication(SharedPreferences preferences) {
    String username = preferences.getString("git_username", "");
    String password = preferences.getString("git_password", "");
    String keyLocation = preferences.getString("git_key_path", "");

    JGitConfigSessionFactory session = new JGitConfigSessionFactory(username, password, keyLocation);
    SshSessionFactory.setInstance(session);

    credentialsProvider = new JGitCredentialsProvider(username, password);
}
 
源代码15 项目: netbeans   文件: JGitSshSessionFactory.java
public static synchronized SshSessionFactory getDefault () {
    if (INSTANCE == null) {
        INSTANCE = new JGitSshSessionFactory();
    }
    return INSTANCE;
}
 
源代码16 项目: orion.server   文件: GitActivator.java
@Override
public void start(BundleContext context) throws Exception {
	context.registerService(IWebResourceDecorator.class, new GitFileDecorator(), null);
	SshSessionFactory.setInstance(new GitSshSessionFactory());
}
 
@AfterEach
void resetSshSessionFactory() throws Exception {
    SshSessionFactory.setInstance(null);    // force reload of known_host etc. (see also org.eclipse.jgit.transport.ssh.SshTestHarness)
}
 
源代码18 项目: incubator-gobblin   文件: GitMonitoringService.java
private CredentialsProvider getCredentialsProvider() {
  return (this.providerSessionFactoryEither instanceof Either.Right)? null :
      ((Either.Left<CredentialsProvider, SshSessionFactory>) this.providerSessionFactoryEither).getLeft();
}
 
@After
public void cleanup() {
	SshSessionFactory.setInstance(null);
}
 
源代码20 项目: attic-stratos   文件: GitBasedArtifactRepository.java
/**
 * Initializes SSH authentication
 */
private void initSSHAuthentication() {

    SshSessionFactory.setInstance(new CustomJschConfigSessionFactory());
}