HADOOP-13461. NPE in KeyProvider.rollNewVersion. Contributed by Colm O hEigeartaigh.

(cherry picked from commit e83be44af5)
This commit is contained in:
Xiao Chen 2016-08-10 16:26:16 -07:00
parent de6eafc696
commit 01fc975ed9
2 changed files with 31 additions and 1 deletions

View File

@ -557,6 +557,10 @@ public abstract class KeyProvider {
public KeyVersion rollNewVersion(String name) throws NoSuchAlgorithmException, public KeyVersion rollNewVersion(String name) throws NoSuchAlgorithmException,
IOException { IOException {
Metadata meta = getMetadata(name); Metadata meta = getMetadata(name);
if (meta == null) {
throw new IOException("Can't find Metadata for key " + name);
}
byte[] material = generateKey(meta.getBitLength(), meta.getCipher()); byte[] material = generateKey(meta.getBitLength(), meta.getCipher());
return rollNewVersion(name, material); return rollNewVersion(name, material);
} }

View File

@ -22,6 +22,7 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.ProviderUtils; import org.apache.hadoop.security.ProviderUtils;
import org.apache.hadoop.test.GenericTestUtils;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -38,6 +39,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
public class TestKeyProvider { public class TestKeyProvider {
@ -182,8 +184,11 @@ public class TestKeyProvider {
@Override @Override
public Metadata getMetadata(String name) throws IOException { public Metadata getMetadata(String name) throws IOException {
if (!"unknown".equals(name)) {
return new Metadata(CIPHER, 128, "description", null, new Date(), 0); return new Metadata(CIPHER, 128, "description", null, new Date(), 0);
} }
return null;
}
@Override @Override
public KeyVersion createKey(String name, byte[] material, public KeyVersion createKey(String name, byte[] material,
@ -236,6 +241,27 @@ public class TestKeyProvider {
Assert.assertNotNull(kp.material); Assert.assertNotNull(kp.material);
} }
@Test
public void testRolloverUnknownKey() throws Exception {
MyKeyProvider kp = new MyKeyProvider(new Configuration());
KeyProvider.Options options = new KeyProvider.Options(new Configuration());
options.setCipher(CIPHER);
options.setBitLength(128);
kp.createKey("hello", options);
Assert.assertEquals(128, kp.size);
Assert.assertEquals(CIPHER, kp.algorithm);
Assert.assertNotNull(kp.material);
kp = new MyKeyProvider(new Configuration());
try {
kp.rollNewVersion("unknown");
fail("should have thrown");
} catch (IOException e) {
String expectedError = "Can't find Metadata for key";
GenericTestUtils.assertExceptionContains(expectedError, e);
}
}
@Test @Test
public void testConfiguration() throws Exception { public void testConfiguration() throws Exception {
Configuration conf = new Configuration(false); Configuration conf = new Configuration(false);