@Test public void establishKnownHostsConfigurationAllow() throws IOException, JSchException { RemoteSecureCopyInputs remoteSecureCopyInputs = getRemoteSecureCopyInputs(); scpCopier = new SCPCopier(remoteSecureCopyInputs); scpCopier.establishKnownHostsConfiguration(new KnownHostsFile(KNOWN_HOSTS_PATH, KNOWN_HOSTS_POLICY_ALLOW), jSchMock, sessionMock); verify(sessionMock).setConfig(STRICT_HOST_KEY_CHECKING, NONSTRICT); }
protected void establishKnownHostsConfiguration(KnownHostsFile knownHostsFile, JSch jsch, Session session) throws JSchException, IOException { String policy = knownHostsFile.getPolicy(); Path knownHostsFilePath = knownHostsFile.getPath(); switch (policy.toLowerCase(Locale.ENGLISH)){ case KNOWN_HOSTS_ALLOW: session.setConfig("StrictHostKeyChecking", "no"); break; case KNOWN_HOSTS_STRICT: jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "yes"); break; case KNOWN_HOSTS_ADD: if (!knownHostsFilePath.isAbsolute()){ throw new RuntimeException ("The known_hosts file path should be absolute."); } if (!Files.exists(knownHostsFilePath)) { Files.createDirectories(knownHostsFilePath.getParent()); Files.createFile(knownHostsFilePath); } jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "no"); break; default: throw new RuntimeException("Unknown known_hosts file policy."); } }
SCPCopier { protected void establishKnownHostsConfiguration(KnownHostsFile knownHostsFile, JSch jsch, Session session) throws JSchException, IOException { String policy = knownHostsFile.getPolicy(); Path knownHostsFilePath = knownHostsFile.getPath(); switch (policy.toLowerCase(Locale.ENGLISH)){ case KNOWN_HOSTS_ALLOW: session.setConfig("StrictHostKeyChecking", "no"); break; case KNOWN_HOSTS_STRICT: jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "yes"); break; case KNOWN_HOSTS_ADD: if (!knownHostsFilePath.isAbsolute()){ throw new RuntimeException ("The known_hosts file path should be absolute."); } if (!Files.exists(knownHostsFilePath)) { Files.createDirectories(knownHostsFilePath.getParent()); Files.createFile(knownHostsFilePath); } jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "no"); break; default: throw new RuntimeException("Unknown known_hosts file policy."); } } }
SCPCopier { protected void establishKnownHostsConfiguration(KnownHostsFile knownHostsFile, JSch jsch, Session session) throws JSchException, IOException { String policy = knownHostsFile.getPolicy(); Path knownHostsFilePath = knownHostsFile.getPath(); switch (policy.toLowerCase(Locale.ENGLISH)){ case KNOWN_HOSTS_ALLOW: session.setConfig("StrictHostKeyChecking", "no"); break; case KNOWN_HOSTS_STRICT: jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "yes"); break; case KNOWN_HOSTS_ADD: if (!knownHostsFilePath.isAbsolute()){ throw new RuntimeException ("The known_hosts file path should be absolute."); } if (!Files.exists(knownHostsFilePath)) { Files.createDirectories(knownHostsFilePath.getParent()); Files.createFile(knownHostsFilePath); } jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "no"); break; default: throw new RuntimeException("Unknown known_hosts file policy."); } } SCPCopier(RemoteSecureCopyInputs remoteSecureCopyInputs); }
SCPCopier { protected void establishKnownHostsConfiguration(KnownHostsFile knownHostsFile, JSch jsch, Session session) throws JSchException, IOException { String policy = knownHostsFile.getPolicy(); Path knownHostsFilePath = knownHostsFile.getPath(); switch (policy.toLowerCase(Locale.ENGLISH)){ case KNOWN_HOSTS_ALLOW: session.setConfig("StrictHostKeyChecking", "no"); break; case KNOWN_HOSTS_STRICT: jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "yes"); break; case KNOWN_HOSTS_ADD: if (!knownHostsFilePath.isAbsolute()){ throw new RuntimeException ("The known_hosts file path should be absolute."); } if (!Files.exists(knownHostsFilePath)) { Files.createDirectories(knownHostsFilePath.getParent()); Files.createFile(knownHostsFilePath); } jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "no"); break; default: throw new RuntimeException("Unknown known_hosts file policy."); } } SCPCopier(RemoteSecureCopyInputs remoteSecureCopyInputs); boolean copyFromRemoteToRemote(); boolean copyFromLocalToRemote(); boolean copyFromRemoteToLocal(); }
SCPCopier { protected void establishKnownHostsConfiguration(KnownHostsFile knownHostsFile, JSch jsch, Session session) throws JSchException, IOException { String policy = knownHostsFile.getPolicy(); Path knownHostsFilePath = knownHostsFile.getPath(); switch (policy.toLowerCase(Locale.ENGLISH)){ case KNOWN_HOSTS_ALLOW: session.setConfig("StrictHostKeyChecking", "no"); break; case KNOWN_HOSTS_STRICT: jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "yes"); break; case KNOWN_HOSTS_ADD: if (!knownHostsFilePath.isAbsolute()){ throw new RuntimeException ("The known_hosts file path should be absolute."); } if (!Files.exists(knownHostsFilePath)) { Files.createDirectories(knownHostsFilePath.getParent()); Files.createFile(knownHostsFilePath); } jsch.setKnownHosts(knownHostsFilePath.toString()); session.setConfig("StrictHostKeyChecking", "no"); break; default: throw new RuntimeException("Unknown known_hosts file policy."); } } SCPCopier(RemoteSecureCopyInputs remoteSecureCopyInputs); boolean copyFromRemoteToRemote(); boolean copyFromLocalToRemote(); boolean copyFromRemoteToLocal();  }