Repository: ambari Updated Branches: refs/heads/branch-2.5 c358ae0c2 -> 435006d4b
AMBARI-19132. Use "rm" to delete Ambari Server keytab files when disabling Kerberos (Attila Doroszlai via rlevas) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/435006d4 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/435006d4 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/435006d4 Branch: refs/heads/branch-2.5 Commit: 435006d4b763b626336a92e2dd43adc72c6a7c10 Parents: c358ae0 Author: Attila Doroszlai <[email protected]> Authored: Mon Dec 19 13:02:25 2016 -0500 Committer: Robert Levas <[email protected]> Committed: Mon Dec 19 13:02:29 2016 -0500 ---------------------------------------------------------------------- .../kerberos/DestroyPrincipalsServerAction.java | 11 +++++- .../ambari/server/utils/ShellCommandUtil.java | 41 +++++++++++++++++++- .../server/utils/TestShellCommandUtil.java | 35 +++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/435006d4/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java index c28b725..a25357c 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java @@ -25,10 +25,12 @@ import org.apache.ambari.server.audit.event.kerberos.DestroyPrincipalKerberosAud import org.apache.ambari.server.controller.KerberosHelper; import org.apache.ambari.server.orm.dao.KerberosPrincipalDAO; import org.apache.ambari.server.orm.entities.KerberosPrincipalEntity; +import org.apache.ambari.server.utils.ShellCommandUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -140,8 +142,13 @@ public class DestroyPrincipalsServerAction extends KerberosServerAction { if (hostName != null && hostName.equalsIgnoreCase(KerberosHelper.AMBARI_SERVER_HOST_NAME)) { String keytabFilePath = identityRecord.get(KerberosIdentityDataFileReader.KEYTAB_FILE_PATH); if (keytabFilePath != null) { - if (!new File(keytabFilePath).delete()) { - LOG.debug(String.format("Failed to remove ambari keytab for %s", evaluatedPrincipal)); + try { + ShellCommandUtil.Result result = ShellCommandUtil.delete(keytabFilePath, true, true); + if (!result.isSuccessful()) { + LOG.warn("Failed to remove ambari keytab for {} due to {}", evaluatedPrincipal, result.getStderr()); + } + } catch (IOException|InterruptedException e) { + LOG.warn("Failed to remove ambari keytab for " + evaluatedPrincipal, e); } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/435006d4/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java index 95a4b27..961fa22 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java @@ -28,6 +28,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -342,7 +343,7 @@ public class ShellCommandUtil { command.add(directoryPath); - return runCommand(command.toArray(new String[command.size()]), null, null, sudo); + return runCommand(command, null, null, sudo); } } @@ -377,7 +378,43 @@ public class ShellCommandUtil { command.add(srcFile); command.add(destFile); - return runCommand(command.toArray(new String[command.size()]), null, null, sudo); + return runCommand(command, null, null, sudo); + } + + /** + * Deletes the <code>file</code>. + * + * @param file the path to the file to be deleted + * @param force true to force copy even if the file exists + * @param sudo true to execute the command using sudo (ambari-sudo); otherwise false + * @return the shell command result + */ + public static Result delete(String file, boolean force, boolean sudo) throws IOException, InterruptedException { + List<String> command = new ArrayList<>(); + + if (WINDOWS) { + command.add("del"); + if (force) { + command.add("/f"); + } + } else { + command.add("/bin/rm"); + if (force) { + command.add("-f"); + } + } + + command.add(file); + + return runCommand(command, null, null, sudo); + } + + /** + * @see #runCommand(String[], Map, InteractiveHandler, boolean) + */ + public static Result runCommand(List<String> args, Map<String, String> vars, InteractiveHandler interactiveHandler, boolean sudo) + throws IOException, InterruptedException { + return runCommand(args.toArray(new String[args.size()]), vars, interactiveHandler, sudo); } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/435006d4/ambari-server/src/test/java/org/apache/ambari/server/utils/TestShellCommandUtil.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/utils/TestShellCommandUtil.java b/ambari-server/src/test/java/org/apache/ambari/server/utils/TestShellCommandUtil.java index 24af9bd..3dfc99a 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/utils/TestShellCommandUtil.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/utils/TestShellCommandUtil.java @@ -213,4 +213,39 @@ public class TestShellCommandUtil { Assert.assertTrue(destFile.length() > 0); Assert.assertEquals(destFile.length(), srcFile.length()); } + + @Test + public void deleteExistingFile() throws Exception { + File file = temp.newFile(); + + ShellCommandUtil.Result result = ShellCommandUtil.delete(file.getAbsolutePath(), false, false); + + Assert.assertTrue(result.getStderr(), result.isSuccessful()); + Assert.assertFalse(file.exists()); + } + + @Test + public void deleteNonexistentFile() throws Exception { + File file = temp.newFile(); + + if (file.delete()) { + ShellCommandUtil.Result result = ShellCommandUtil.delete(file.getAbsolutePath(), false, false); + + Assert.assertFalse(result.getStderr(), result.isSuccessful()); + Assert.assertFalse(file.exists()); + } + } + + @Test + public void forceDeleteNonexistentFile() throws Exception { + File file = temp.newFile(); + + if (file.delete()) { + ShellCommandUtil.Result result = ShellCommandUtil.delete(file.getAbsolutePath(), true, false); + + Assert.assertTrue(result.getStderr(), result.isSuccessful()); + Assert.assertFalse(file.exists()); + } + } + }
