Parcourir la source

HDFS-13485. DataNode WebHDFS endpoint throws NPE. Contributed by Siyao Meng.

Wei-Chiu Chuang il y a 6 ans
Parent
commit
d215357718

+ 5 - 0
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java

@@ -23,6 +23,7 @@ import com.google.protobuf.ByteString;
 import com.google.common.primitives.Bytes;
 import com.google.common.primitives.Bytes;
 
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.HadoopIllegalArgumentException;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.conf.Configuration;
@@ -358,6 +359,10 @@ public class Token<T extends TokenIdentifier> implements Writable {
    */
    */
   private static void decodeWritable(Writable obj,
   private static void decodeWritable(Writable obj,
                                      String newValue) throws IOException {
                                      String newValue) throws IOException {
+    if (newValue == null) {
+      throw new HadoopIllegalArgumentException(
+              "Invalid argument, newValue is null");
+    }
     Base64 decoder = new Base64(0, null, true);
     Base64 decoder = new Base64(0, null, true);
     DataInputBuffer buf = new DataInputBuffer();
     DataInputBuffer buf = new DataInputBuffer();
     byte[] decoded = decoder.decode(newValue);
     byte[] decoded = decoder.decode(newValue);

+ 18 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java

@@ -21,6 +21,7 @@ package org.apache.hadoop.security.token;
 import java.io.*;
 import java.io.*;
 import java.util.Arrays;
 import java.util.Arrays;
 
 
+import org.apache.hadoop.HadoopIllegalArgumentException;
 import org.apache.hadoop.io.*;
 import org.apache.hadoop.io.*;
 import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
 import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
 import org.apache.hadoop.security.token.delegation.TestDelegationToken.TestDelegationTokenIdentifier;
 import org.apache.hadoop.security.token.delegation.TestDelegationToken.TestDelegationTokenIdentifier;
@@ -100,6 +101,23 @@ public class TestToken {
     }
     }
   }
   }
 
 
+  /*
+   * Test decodeWritable() with null newValue string argument,
+   * should throw HadoopIllegalArgumentException.
+   */
+  @Test
+  public void testDecodeWritableArgSanityCheck() throws Exception {
+    Token<AbstractDelegationTokenIdentifier> token =
+            new Token<AbstractDelegationTokenIdentifier>();
+    try {
+      token.decodeFromUrlString(null);
+      fail("Should have thrown HadoopIllegalArgumentException");
+    }
+    catch (HadoopIllegalArgumentException e) {
+      Token.LOG.info("Test decodeWritable() sanity check success.");
+    }
+  }
+
   @Test
   @Test
   public void testDecodeIdentifier() throws IOException {
   public void testDecodeIdentifier() throws IOException {
     TestDelegationTokenSecretManager secretManager =
     TestDelegationTokenSecretManager secretManager =