瀏覽代碼

HADOOP-13353. LdapGroupsMapping getPassward shouldn't return null when IOException throws. Contributed by Zhaohao Liang and Wei-Chiu Chuang.

Wei-Chiu Chuang 8 年之前
父節點
當前提交
49ba09a922

+ 4 - 8
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/LdapGroupsMapping.java

@@ -616,19 +616,15 @@ public class LdapGroupsMapping
   }
   }
 
 
   String getPassword(Configuration conf, String alias, String defaultPass) {
   String getPassword(Configuration conf, String alias, String defaultPass) {
-    String password = null;
+    String password = defaultPass;
     try {
     try {
       char[] passchars = conf.getPassword(alias);
       char[] passchars = conf.getPassword(alias);
       if (passchars != null) {
       if (passchars != null) {
         password = new String(passchars);
         password = new String(passchars);
       }
       }
-      else {
-        password = defaultPass;
-      }
-    }
-    catch (IOException ioe) {
-      LOG.warn("Exception while trying to password for alias " + alias + ": "
-          + ioe.getMessage());
+    } catch (IOException ioe) {
+      LOG.warn("Exception while trying to get password for alias " + alias
+              + ": ", ioe);
     }
     }
     return password;
     return password;
   }
   }

+ 21 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestLdapGroupsMapping.java

@@ -57,6 +57,7 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import org.mockito.Mockito;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -364,4 +365,24 @@ public class TestLdapGroupsMapping extends TestLdapGroupsMappingBase {
     }
     }
   }
   }
 
 
+  /**
+   * Make sure that when
+   * {@link Configuration#getPassword(String)} throws an IOException,
+   * {@link LdapGroupsMapping#setConf(Configuration)} does not throw an NPE.
+   *
+   * @throws Exception
+   */
+  @Test(timeout = 10000)
+  public void testSetConf() throws Exception {
+    Configuration conf = new Configuration();
+    Configuration mockConf = Mockito.spy(conf);
+    when(mockConf.getPassword(anyString()))
+        .thenThrow(new IOException("injected IOException"));
+    // Set a dummy LDAP server URL.
+    mockConf.set(LdapGroupsMapping.LDAP_URL_KEY, "ldap://test");
+
+    LdapGroupsMapping groupsMapping = getGroupsMapping();
+    groupsMapping.setConf(mockConf);
+  }
+
 }
 }