@Test public void establishKnownHostsConfigurationAddUnknownPolicy() throws IOException, JSchException, URISyntaxException { RemoteSecureCopyInputs remoteSecureCopyInputs = getRemoteSecureCopyInputs(); scpCopier = new SCPCopier(remoteSecureCopyInputs); when(knownHostsFileMock.getPath()).thenReturn(pathMock); when(knownHostsFileMock.getPolicy()).thenReturn(StringUtils.EMPTY_STRING); when(pathMock.isAbsolute()).thenReturn(false); exception.expect(RuntimeException.class); exception.expectMessage(UNKNOWN_KNOWN_HOSTS_FILE_POLICY); scpCopier.establishKnownHostsConfiguration(knownHostsFileMock, 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();  }