Repository: ambari
Updated Branches:
  refs/heads/branch-2.4 f343a4624 -> 6edba95a3


AMBARI-18755. Deployment failing at creating principal (aonishuk)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6edba95a
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6edba95a
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6edba95a

Branch: refs/heads/branch-2.4
Commit: 6edba95a319d96ceabe77ea67aa9d81fec35057d
Parents: f343a46
Author: Andrew Onishuk <aonis...@hortonworks.com>
Authored: Wed Nov 9 14:27:04 2016 +0200
Committer: Andrew Onishuk <aonis...@hortonworks.com>
Committed: Wed Nov 9 14:27:04 2016 +0200

----------------------------------------------------------------------
 .../server/configuration/Configuration.java     |  7 +++
 .../kerberos/MITKerberosOperationHandler.java   | 28 +++++++++-
 .../MITKerberosOperationHandlerTest.java        | 57 ++++++++++----------
 3 files changed, 62 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/6edba95a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
index fae9378..a91eada 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
@@ -418,6 +418,8 @@ public class Configuration {
   public static final String KERBEROS_KEYTAB_CACHE_DIR_DEFAULT = 
AmbariPath.getPath("/var/lib/ambari-server/data/cache");
   public static final String KERBEROS_CHECK_JAAS_CONFIGURATION_KEY = 
"kerberos.check.jaas.configuration";
   public static final String KERBEROS_CHECK_JAAS_CONFIGURATION_DEFAULT = 
"false";
+  public static final String KERBEROS_OPERATION_RETRIES_KEY = 
"kerberos.operation.retries";
+  public static final String KERBEROS_OPERATION_RETRIES_DEFAULT = "3";
 
   /**
    * Recovery related configuration
@@ -3322,4 +3324,9 @@ public class Configuration {
             String.valueOf(TASK_ID_LIST_LIMIT_DEFAULT)));
   }
 
+  public int getKerberosOperationRetries(){
+    return Integer.parseInt(properties.getProperty(
+        KERBEROS_OPERATION_RETRIES_KEY,
+        KERBEROS_OPERATION_RETRIES_DEFAULT));
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/6edba95a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java
index 7e915fc..a21d330 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandler.java
@@ -18,6 +18,8 @@
 
 package org.apache.ambari.server.serveraction.kerberos;
 
+import com.google.inject.Inject;
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.security.credential.PrincipalKeyCredential;
 import org.apache.ambari.server.utils.ShellCommandUtil;
 import org.apache.commons.lang.ArrayUtils;
@@ -44,6 +46,9 @@ import java.util.regex.Pattern;
  */
 public class MITKerberosOperationHandler extends KerberosOperationHandler {
 
+  @Inject
+  private Configuration configuration;
+
   /**
    * A regular expression pattern to use to parse the key number from the text 
captured from the
    * get_principal kadmin command
@@ -388,7 +393,7 @@ public class MITKerberosOperationHandler extends 
KerberosOperationHandler {
       throw new KerberosOperationException("Missing kadmin query");
     }
 
-    ShellCommandUtil.Result result;
+    ShellCommandUtil.Result result = null;
     PrincipalKeyCredential administratorCredential = 
getAdministratorCredential();
     String defaultRealm = getDefaultRealm();
 
@@ -451,7 +456,26 @@ public class MITKerberosOperationHandler extends 
KerberosOperationHandler {
       LOG.debug(String.format("Executing: %s", command));
     }
 
-    result = executeCommand(command.toArray(new String[command.size()]), null, 
interactiveHandler);
+    int retryCount = configuration.getKerberosOperationRetries();
+    int tries = 0;
+
+    while (tries <= retryCount) {
+      try {
+        result = executeCommand(command.toArray(new String[command.size()]), 
null, interactiveHandler);
+      } catch (KerberosOperationException exception) {
+        if (tries == retryCount) {
+          throw exception;
+        }
+      } finally {
+        if (result != null && result.isSuccessful()) {
+          break; // break on successful result
+        }
+        tries++;
+        String message = String.format("Retrying to execute 
kadmin:\n\tCommand: %s", command);
+        LOG.warn(message);
+      }
+    }
+
 
     if (!result.isSuccessful()) {
       String message = String.format("Failed to execute kadmin:\n\tCommand: 
%s\n\tExitCode: %s\n\tSTDOUT: %s\n\tSTDERR: %s",

http://git-wip-us.apache.org/repos/asf/ambari/blob/6edba95a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java
index 6fd30ee..4c40a5d 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java
@@ -32,6 +32,7 @@ import org.apache.ambari.server.utils.ShellCommandUtil;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.easymock.IAnswer;
+import org.easymock.IMockBuilder;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -122,10 +123,11 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosPrincipalDoesNotExistException.class)
   public void testSetPrincipalPasswordPrincipalDoesNotExist() throws Exception 
{
+
     MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
         .addMockedMethod(methodExecuteCommand)
         .createNiceMock();
-
+    injector.injectMembers(handler);
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
           @Override
@@ -187,9 +189,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosPrincipalAlreadyExistsException.class)
   public void testCreatePrincipalPrincipalAlreadyNotExists() throws Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -254,9 +254,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosAdminAuthenticationException.class)
   public void testTestAdministratorCredentialsIncorrectAdminPassword() throws 
Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -287,9 +285,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosAdminAuthenticationException.class)
   public void testTestAdministratorCredentialsIncorrectAdminPrincipal() throws 
Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -320,9 +316,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosRealmException.class)
   public void testTestAdministratorCredentialsInvalidRealm() throws Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -353,9 +347,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosRealmException.class)
   public void testTestAdministratorCredentialsInvalidRealm2() throws Exception 
{
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -386,9 +378,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosKDCConnectionException.class)
   public void testTestAdministratorCredentialsKDCConnectionException() throws 
Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -419,9 +409,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test(expected = KerberosKDCConnectionException.class)
   public void testTestAdministratorCredentialsKDCConnectionException2() throws 
Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -452,9 +440,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test
   public void testTestAdministratorCredentialsNotFound() throws Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -485,9 +471,7 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
 
   @Test
   public void testTestAdministratorCredentialsSuccess() throws Exception {
-    MITKerberosOperationHandler handler = 
createMockBuilder(MITKerberosOperationHandler.class)
-        .addMockedMethod(methodExecuteCommand)
-        .createNiceMock();
+    MITKerberosOperationHandler handler = createMock();
 
     expect(handler.executeCommand(anyObject(String[].class), 
anyObject(Map.class), 
anyObject(MITKerberosOperationHandler.InteractivePasswordHandler.class)))
         .andAnswer(new IAnswer<ShellCommandUtil.Result>() {
@@ -562,4 +546,21 @@ public class MITKerberosOperationHandlerTest extends 
KerberosOperationHandlerTes
     handler.testAdministratorCredentials();
     handler.close();
   }
+
+  private MITKerberosOperationHandler createMock(){
+    return createMock(false);
+  }
+
+  private MITKerberosOperationHandler createMock(boolean strict) {
+    IMockBuilder<MITKerberosOperationHandler> mockBuilder = 
createMockBuilder(MITKerberosOperationHandler.class)
+        .addMockedMethod(methodExecuteCommand);
+    MITKerberosOperationHandler result;
+    if(strict){
+      result = mockBuilder.createStrictMock();
+    } else {
+      result = mockBuilder.createNiceMock();
+    }
+    injector.injectMembers(result);
+    return result;
+  }
 }
\ No newline at end of file

Reply via email to