@Test(expected = InternalErrorException.class) public void testSecureCopyConfFileFailsCreateDirectory() throws CommandFailureException { when(Config.mockShellCommandFactory.executeRemoteCommand(anyString(), eq(Command.CREATE_DIR), anyString())).thenReturn(new RemoteCommandReturnInfo(1, "", "FAILED BACK UP")); final Jvm mockJvm = mock(Jvm.class); when(mockJvm.getHostName()).thenReturn("host"); when(Config.mockJvmPersistenceService.getJvm(any(Identifier.class))).thenReturn(mockJvm); when(Config.mockShellCommandFactory.executeRemoteCommand(eq("host"), eq(Command.SCP), anyString(), anyString())).thenReturn(mock(RemoteCommandReturnInfo.class)); final ControlJvmRequest mockControlJvmRequest = mock(ControlJvmRequest.class); when(mockControlJvmRequest.getControlOperation()).thenReturn(JvmControlOperation.SCP); jvmControlService.secureCopyFile(mockControlJvmRequest, "./source/path", "./dest/path", "user-id", true); }
@Override public CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath, final String destPath, String userId, boolean overwrite) throws CommandFailureException { final Identifier<Jvm> jvmId = secureCopyRequest.getJvmId(); final String event = secureCopyRequest.getControlOperation().name(); final Jvm jvm = jvmPersistenceService.getJvm(jvmId); final int beginIndex = destPath.lastIndexOf("/"); final String fileName = destPath.substring(beginIndex + 1, destPath.length()); final String name = jvm.getJvmName(); final String hostName = jvm.getHostName(); final String parentDir; if (destPath.startsWith("~")) { parentDir = destPath.substring(0, destPath.lastIndexOf("/")); } else { parentDir = Paths.get(destPath).getParent().toString().replaceAll("\\\\", "/"); } CommandOutput commandOutput = executeCreateDirectoryCommand(jvm, parentDir); if (commandOutput.getReturnCode().wasSuccessful()) { LOGGER.info("Successfully created parent dir {} on host {}", parentDir, hostName); } else { final String standardError = commandOutput.getStandardError().isEmpty() ? commandOutput.getStandardOutput() : commandOutput.getStandardError(); LOGGER.error("create command failed with error trying to create parent directory {} on {} :: ERROR: {}", parentDir, hostName, standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError.isEmpty() ? CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc() : standardError); } commandOutput = executeCheckFileExistsCommand(jvm, destPath); final boolean fileExists = commandOutput.getReturnCode().wasSuccessful(); if (fileExists && overwrite) { commandOutput = executeBackUpCommand(jvm, destPath); if (!commandOutput.getReturnCode().wasSuccessful()) { final String standardError = "Failed to back up the " + destPath + " for " + name + "."; LOGGER.error(standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError); } else { LOGGER.info("Successfully backed up " + destPath + " at " + hostName); } } else if (fileExists) { String message = MessageFormat.format("Skipping scp of file: {0} already exists and overwrite is set to false.", destPath); LOGGER.info(message); historyFacadeService.write(hostName, jvm.getGroups(), message, EventType.SYSTEM_INFO, userId); return new CommandOutput(new ExecReturnCode(0), message, ""); } if (!ApplicationProperties.get("remote.commands.user-scripts").endsWith(fileName)) { final String eventDescription = event + " sending file " + fileName + " to remote path " + destPath + " on host " + jvm.getHostName(); historyFacadeService.write(getServerName(jvm), new ArrayList<>(jvm.getGroups()), eventDescription, EventType.USER_ACTION_INFO, userId); } RemoteCommandReturnInfo remoteCommandReturnInfo = shellCommandFactory.executeRemoteCommand(jvm.getHostName(), Command.SCP, sourcePath, destPath); return new CommandOutput(new ExecReturnCode(remoteCommandReturnInfo.retCode), remoteCommandReturnInfo.standardOuput, remoteCommandReturnInfo.errorOupout); }
JvmControlServiceImpl implements JvmControlService { @Override public CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath, final String destPath, String userId, boolean overwrite) throws CommandFailureException { final Identifier<Jvm> jvmId = secureCopyRequest.getJvmId(); final String event = secureCopyRequest.getControlOperation().name(); final Jvm jvm = jvmPersistenceService.getJvm(jvmId); final int beginIndex = destPath.lastIndexOf("/"); final String fileName = destPath.substring(beginIndex + 1, destPath.length()); final String name = jvm.getJvmName(); final String hostName = jvm.getHostName(); final String parentDir; if (destPath.startsWith("~")) { parentDir = destPath.substring(0, destPath.lastIndexOf("/")); } else { parentDir = Paths.get(destPath).getParent().toString().replaceAll("\\\\", "/"); } CommandOutput commandOutput = executeCreateDirectoryCommand(jvm, parentDir); if (commandOutput.getReturnCode().wasSuccessful()) { LOGGER.info("Successfully created parent dir {} on host {}", parentDir, hostName); } else { final String standardError = commandOutput.getStandardError().isEmpty() ? commandOutput.getStandardOutput() : commandOutput.getStandardError(); LOGGER.error("create command failed with error trying to create parent directory {} on {} :: ERROR: {}", parentDir, hostName, standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError.isEmpty() ? CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc() : standardError); } commandOutput = executeCheckFileExistsCommand(jvm, destPath); final boolean fileExists = commandOutput.getReturnCode().wasSuccessful(); if (fileExists && overwrite) { commandOutput = executeBackUpCommand(jvm, destPath); if (!commandOutput.getReturnCode().wasSuccessful()) { final String standardError = "Failed to back up the " + destPath + " for " + name + "."; LOGGER.error(standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError); } else { LOGGER.info("Successfully backed up " + destPath + " at " + hostName); } } else if (fileExists) { String message = MessageFormat.format("Skipping scp of file: {0} already exists and overwrite is set to false.", destPath); LOGGER.info(message); historyFacadeService.write(hostName, jvm.getGroups(), message, EventType.SYSTEM_INFO, userId); return new CommandOutput(new ExecReturnCode(0), message, ""); } if (!ApplicationProperties.get("remote.commands.user-scripts").endsWith(fileName)) { final String eventDescription = event + " sending file " + fileName + " to remote path " + destPath + " on host " + jvm.getHostName(); historyFacadeService.write(getServerName(jvm), new ArrayList<>(jvm.getGroups()), eventDescription, EventType.USER_ACTION_INFO, userId); } RemoteCommandReturnInfo remoteCommandReturnInfo = shellCommandFactory.executeRemoteCommand(jvm.getHostName(), Command.SCP, sourcePath, destPath); return new CommandOutput(new ExecReturnCode(remoteCommandReturnInfo.retCode), remoteCommandReturnInfo.standardOuput, remoteCommandReturnInfo.errorOupout); } }
JvmControlServiceImpl implements JvmControlService { @Override public CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath, final String destPath, String userId, boolean overwrite) throws CommandFailureException { final Identifier<Jvm> jvmId = secureCopyRequest.getJvmId(); final String event = secureCopyRequest.getControlOperation().name(); final Jvm jvm = jvmPersistenceService.getJvm(jvmId); final int beginIndex = destPath.lastIndexOf("/"); final String fileName = destPath.substring(beginIndex + 1, destPath.length()); final String name = jvm.getJvmName(); final String hostName = jvm.getHostName(); final String parentDir; if (destPath.startsWith("~")) { parentDir = destPath.substring(0, destPath.lastIndexOf("/")); } else { parentDir = Paths.get(destPath).getParent().toString().replaceAll("\\\\", "/"); } CommandOutput commandOutput = executeCreateDirectoryCommand(jvm, parentDir); if (commandOutput.getReturnCode().wasSuccessful()) { LOGGER.info("Successfully created parent dir {} on host {}", parentDir, hostName); } else { final String standardError = commandOutput.getStandardError().isEmpty() ? commandOutput.getStandardOutput() : commandOutput.getStandardError(); LOGGER.error("create command failed with error trying to create parent directory {} on {} :: ERROR: {}", parentDir, hostName, standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError.isEmpty() ? CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc() : standardError); } commandOutput = executeCheckFileExistsCommand(jvm, destPath); final boolean fileExists = commandOutput.getReturnCode().wasSuccessful(); if (fileExists && overwrite) { commandOutput = executeBackUpCommand(jvm, destPath); if (!commandOutput.getReturnCode().wasSuccessful()) { final String standardError = "Failed to back up the " + destPath + " for " + name + "."; LOGGER.error(standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError); } else { LOGGER.info("Successfully backed up " + destPath + " at " + hostName); } } else if (fileExists) { String message = MessageFormat.format("Skipping scp of file: {0} already exists and overwrite is set to false.", destPath); LOGGER.info(message); historyFacadeService.write(hostName, jvm.getGroups(), message, EventType.SYSTEM_INFO, userId); return new CommandOutput(new ExecReturnCode(0), message, ""); } if (!ApplicationProperties.get("remote.commands.user-scripts").endsWith(fileName)) { final String eventDescription = event + " sending file " + fileName + " to remote path " + destPath + " on host " + jvm.getHostName(); historyFacadeService.write(getServerName(jvm), new ArrayList<>(jvm.getGroups()), eventDescription, EventType.USER_ACTION_INFO, userId); } RemoteCommandReturnInfo remoteCommandReturnInfo = shellCommandFactory.executeRemoteCommand(jvm.getHostName(), Command.SCP, sourcePath, destPath); return new CommandOutput(new ExecReturnCode(remoteCommandReturnInfo.retCode), remoteCommandReturnInfo.standardOuput, remoteCommandReturnInfo.errorOupout); } JvmControlServiceImpl(final JvmPersistenceService jvmPersistenceService,
                                 final JvmStateService jvmStateService,
                                 final HistoryFacadeService historyFacadeService); }
JvmControlServiceImpl implements JvmControlService { @Override public CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath, final String destPath, String userId, boolean overwrite) throws CommandFailureException { final Identifier<Jvm> jvmId = secureCopyRequest.getJvmId(); final String event = secureCopyRequest.getControlOperation().name(); final Jvm jvm = jvmPersistenceService.getJvm(jvmId); final int beginIndex = destPath.lastIndexOf("/"); final String fileName = destPath.substring(beginIndex + 1, destPath.length()); final String name = jvm.getJvmName(); final String hostName = jvm.getHostName(); final String parentDir; if (destPath.startsWith("~")) { parentDir = destPath.substring(0, destPath.lastIndexOf("/")); } else { parentDir = Paths.get(destPath).getParent().toString().replaceAll("\\\\", "/"); } CommandOutput commandOutput = executeCreateDirectoryCommand(jvm, parentDir); if (commandOutput.getReturnCode().wasSuccessful()) { LOGGER.info("Successfully created parent dir {} on host {}", parentDir, hostName); } else { final String standardError = commandOutput.getStandardError().isEmpty() ? commandOutput.getStandardOutput() : commandOutput.getStandardError(); LOGGER.error("create command failed with error trying to create parent directory {} on {} :: ERROR: {}", parentDir, hostName, standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError.isEmpty() ? CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc() : standardError); } commandOutput = executeCheckFileExistsCommand(jvm, destPath); final boolean fileExists = commandOutput.getReturnCode().wasSuccessful(); if (fileExists && overwrite) { commandOutput = executeBackUpCommand(jvm, destPath); if (!commandOutput.getReturnCode().wasSuccessful()) { final String standardError = "Failed to back up the " + destPath + " for " + name + "."; LOGGER.error(standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError); } else { LOGGER.info("Successfully backed up " + destPath + " at " + hostName); } } else if (fileExists) { String message = MessageFormat.format("Skipping scp of file: {0} already exists and overwrite is set to false.", destPath); LOGGER.info(message); historyFacadeService.write(hostName, jvm.getGroups(), message, EventType.SYSTEM_INFO, userId); return new CommandOutput(new ExecReturnCode(0), message, ""); } if (!ApplicationProperties.get("remote.commands.user-scripts").endsWith(fileName)) { final String eventDescription = event + " sending file " + fileName + " to remote path " + destPath + " on host " + jvm.getHostName(); historyFacadeService.write(getServerName(jvm), new ArrayList<>(jvm.getGroups()), eventDescription, EventType.USER_ACTION_INFO, userId); } RemoteCommandReturnInfo remoteCommandReturnInfo = shellCommandFactory.executeRemoteCommand(jvm.getHostName(), Command.SCP, sourcePath, destPath); return new CommandOutput(new ExecReturnCode(remoteCommandReturnInfo.retCode), remoteCommandReturnInfo.standardOuput, remoteCommandReturnInfo.errorOupout); } JvmControlServiceImpl(final JvmPersistenceService jvmPersistenceService,
                                 final JvmStateService jvmStateService,
                                 final HistoryFacadeService historyFacadeService); @Override CommandOutput controlJvm(final ControlJvmRequest controlJvmRequest, final User aUser); @Override CommandOutput controlJvmSynchronously(final ControlJvmRequest controlJvmRequest, final long timeout,
                                                 final User user); @Override CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath,
                                        final String destPath, String userId, boolean overwrite); @Override CommandOutput executeChangeFileModeCommand(final Jvm jvm, final String modifiedPermissions, final String targetAbsoluteDir, final String targetFile); @Override CommandOutput executeCreateDirectoryCommand(final Jvm jvm, final String dirAbsolutePath); @Override CommandOutput executeCheckFileExistsCommand(final Jvm jvm, final String filename); @Override CommandOutput executeBackUpCommand(final Jvm jvm, final String filename); void setJvmCommandFactory(JvmCommandFactory jvmCommandFactory); }
JvmControlServiceImpl implements JvmControlService { @Override public CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath, final String destPath, String userId, boolean overwrite) throws CommandFailureException { final Identifier<Jvm> jvmId = secureCopyRequest.getJvmId(); final String event = secureCopyRequest.getControlOperation().name(); final Jvm jvm = jvmPersistenceService.getJvm(jvmId); final int beginIndex = destPath.lastIndexOf("/"); final String fileName = destPath.substring(beginIndex + 1, destPath.length()); final String name = jvm.getJvmName(); final String hostName = jvm.getHostName(); final String parentDir; if (destPath.startsWith("~")) { parentDir = destPath.substring(0, destPath.lastIndexOf("/")); } else { parentDir = Paths.get(destPath).getParent().toString().replaceAll("\\\\", "/"); } CommandOutput commandOutput = executeCreateDirectoryCommand(jvm, parentDir); if (commandOutput.getReturnCode().wasSuccessful()) { LOGGER.info("Successfully created parent dir {} on host {}", parentDir, hostName); } else { final String standardError = commandOutput.getStandardError().isEmpty() ? commandOutput.getStandardOutput() : commandOutput.getStandardError(); LOGGER.error("create command failed with error trying to create parent directory {} on {} :: ERROR: {}", parentDir, hostName, standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError.isEmpty() ? CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc() : standardError); } commandOutput = executeCheckFileExistsCommand(jvm, destPath); final boolean fileExists = commandOutput.getReturnCode().wasSuccessful(); if (fileExists && overwrite) { commandOutput = executeBackUpCommand(jvm, destPath); if (!commandOutput.getReturnCode().wasSuccessful()) { final String standardError = "Failed to back up the " + destPath + " for " + name + "."; LOGGER.error(standardError); throw new InternalErrorException(FaultType.REMOTE_COMMAND_FAILURE, standardError); } else { LOGGER.info("Successfully backed up " + destPath + " at " + hostName); } } else if (fileExists) { String message = MessageFormat.format("Skipping scp of file: {0} already exists and overwrite is set to false.", destPath); LOGGER.info(message); historyFacadeService.write(hostName, jvm.getGroups(), message, EventType.SYSTEM_INFO, userId); return new CommandOutput(new ExecReturnCode(0), message, ""); } if (!ApplicationProperties.get("remote.commands.user-scripts").endsWith(fileName)) { final String eventDescription = event + " sending file " + fileName + " to remote path " + destPath + " on host " + jvm.getHostName(); historyFacadeService.write(getServerName(jvm), new ArrayList<>(jvm.getGroups()), eventDescription, EventType.USER_ACTION_INFO, userId); } RemoteCommandReturnInfo remoteCommandReturnInfo = shellCommandFactory.executeRemoteCommand(jvm.getHostName(), Command.SCP, sourcePath, destPath); return new CommandOutput(new ExecReturnCode(remoteCommandReturnInfo.retCode), remoteCommandReturnInfo.standardOuput, remoteCommandReturnInfo.errorOupout); } JvmControlServiceImpl(final JvmPersistenceService jvmPersistenceService,
                                 final JvmStateService jvmStateService,
                                 final HistoryFacadeService historyFacadeService); @Override CommandOutput controlJvm(final ControlJvmRequest controlJvmRequest, final User aUser); @Override CommandOutput controlJvmSynchronously(final ControlJvmRequest controlJvmRequest, final long timeout,
                                                 final User user); @Override CommandOutput secureCopyFile(final ControlJvmRequest secureCopyRequest, final String sourcePath,
                                        final String destPath, String userId, boolean overwrite); @Override CommandOutput executeChangeFileModeCommand(final Jvm jvm, final String modifiedPermissions, final String targetAbsoluteDir, final String targetFile); @Override CommandOutput executeCreateDirectoryCommand(final Jvm jvm, final String dirAbsolutePath); @Override CommandOutput executeCheckFileExistsCommand(final Jvm jvm, final String filename); @Override CommandOutput executeBackUpCommand(final Jvm jvm, final String filename); void setJvmCommandFactory(JvmCommandFactory jvmCommandFactory);  }