/** * @param knownHostFile * @return * @throws JSchException */ private static HostKeyRepository getHostKeyRepository( String knownHostFile ) throws JSchException { JSch jsch = new JSch(); jsch.setKnownHosts( knownHostFile ); return jsch.getHostKeyRepository(); }
@Test public void testKnownHostsFile() throws Exception { final JschBuilder builder = new JschBuilder(); builder.setKnownHostsFileName(knownHostsFile); builder.setPrivateKeyFileName(privateKeyFile); final JSch jsch = builder.build(); final HostKeyRepository knownHosts = jsch.getHostKeyRepository(); assertEquals(knownHostsFile, knownHosts.getKnownHostsRepositoryID()); }
/** * Enforce server reconnection (closes the current connection if it is still alive) */ public synchronized void connect() throws SFTPConnectionException { //checkLocked(); try { // Try to disconnect this.disconnect(); // Open new connection Logger.defaultLogger().info("Trying to connect to server : " + this.remoteServer + " ..."); debug("connect : connect", remoteServer); JSch jsch = new JSch(); if (checkHostKey) { HostKeyRepository hkr = jsch.getHostKeyRepository(); byte[] keybytes = this.getHostKeyAsByteArray(); if (keybytes == null) { throw new SFTPConnectionException("Incorrect hostkey : " + this.getHostKeyAsString()); } HostKey key = new HostKey(remoteServer, keybytes); hkr.add(key, null); jsch.setHostKeyRepository(hkr); } if (useCertificateAuth) { if (certificateFileName == null || certificateFileName.trim().length() == 0 || ! FileSystemManager.exists(new File(certificateFileName))) { throw new SFTPConnectionException("Certificate file not set or not found : " + certificateFileName); } else { Logger.defaultLogger().info("Using private key file : " + certificateFileName); if (certificateFileName.toLowerCase().endsWith(".ppk")) { Logger.defaultLogger().warn("Your private key file seems to be in PuTTY's \"ppk\" file format. Please convert it to the standard OpenSSH format (this can be done by using the \"puttygen.exe\" utility - see \"Convertions\" menu.)"); } jsch.addIdentity(certificateFileName); } } session = jsch.getSession(login, remoteServer, remotePort); UserInfo ui = new DefaultUserInfo(this.password, certPassPhrase, certificateFileName); session.setUserInfo(ui); session.setDaemonThread(true); session.setConfig("StrictHostKeyChecking", checkHostKey ? "yes":"no"); String preferredAuth; String configuredPAuth = FrameworkConfiguration.getInstance().getSftpPreferredAuthOverride(); if (configuredPAuth != null && configuredPAuth.trim().length() != 0) { preferredAuth = configuredPAuth; } else { preferredAuth = useCertificateAuth ? "publickey,password" : "password,publickey"; } Logger.defaultLogger().fine("Authentication methods: " + preferredAuth); session.setConfig("PreferredAuthentications", preferredAuth); session.setTimeout(FrameworkConfiguration.getInstance().getSFTPTimeout()); Logger.defaultLogger().info("Trying to log in with user : " + this.login + " (" + (useCertificateAuth ? "certificate":"password") + ") ..."); debug("connect : login", login); session.connect(); client = (ChannelSftp)session.openChannel("sftp"); client.connect(); this.connectionId = Util.getRndLong(); this.updateOpTime(); Logger.defaultLogger().info("Connected to server : " + this.remoteServer); } catch (JSchException e) { resetClient(e); throw new SFTPConnectionException("Unable to connect to server : " + this.remoteServer + " (" + e.getMessage() + ")"); } finally { clearCache(); } }
@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)); }
@Inject public AndroidSshSessionFactory(Provider<AndroidAuthAgent> androidAuthAgentProvider, UserInfo userInfo, HostKeyRepository hostKeyRepository) { this.androidAuthAgentProvider = androidAuthAgentProvider; this.userInfo = userInfo; this.hostKeyRepository = hostKeyRepository; }
/** * {@inheritDoc} */ @Override public int check(String host, byte[] key) { return HostKeyRepository.OK; }
@Override public int check(final String host, final byte[] bkey) { return HostKeyRepository.OK; }
@Override public int check(String host, byte[] key) { return HostKeyRepository.OK; }