You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2016/12/20 14:35:11 UTC

[09/15] ambari git commit: AMBARI-19132. Use "rm" to delete Ambari Server keytab files when disabling Kerberos (Attila Doroszlai via rlevas)

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/f0544dfa
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f0544dfa
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f0544dfa

Branch: refs/heads/branch-dev-patch-upgrade
Commit: f0544dfa9a1bf462451dbbaf688ceb22e5ceb773
Parents: f9d37df
Author: Attila Doroszlai <ad...@hortonworks.com>
Authored: Mon Dec 19 12:40:53 2016 -0500
Committer: Robert Levas <rl...@hortonworks.com>
Committed: Mon Dec 19 12:40:53 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/f0544dfa/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 8197e76..3f631b4 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
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.serveraction.kerberos;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -30,6 +31,7 @@ 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;
 
@@ -141,8 +143,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/f0544dfa/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 57044d7..344c8a8 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
@@ -24,6 +24,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;
 
 import org.apache.commons.lang.StringUtils;
@@ -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/f0544dfa/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 797ceb2..1595a24 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
@@ -214,4 +214,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());
+    }
+  }
+
 }