|
@@ -17,6 +17,9 @@
|
|
|
*/
|
|
|
package org.apache.hadoop.crypto.key.kms.server;
|
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
@@ -37,8 +40,7 @@ import org.apache.hadoop.crypto.key.UserProvider;
|
|
|
import org.apache.hadoop.crypto.key.kms.server.KeyAuthorizationKeyProvider.KeyACLs;
|
|
|
import org.apache.hadoop.crypto.key.kms.server.KeyAuthorizationKeyProvider.KeyOpType;
|
|
|
import org.apache.hadoop.security.UserGroupInformation;
|
|
|
-import org.junit.Assert;
|
|
|
-import org.junit.Test;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
|
public class TestKeyAuthorizationKeyProvider {
|
|
|
|
|
@@ -66,14 +68,14 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
kpExt.createKey("foo", SecureRandom.getSeed(16),
|
|
|
newOptions(conf));
|
|
|
} catch (IOException ioe) {
|
|
|
- Assert.fail("User should be Authorized !!");
|
|
|
+ fail("User should be Authorized !!");
|
|
|
}
|
|
|
|
|
|
// "bar" key not configured
|
|
|
try {
|
|
|
kpExt.createKey("bar", SecureRandom.getSeed(16),
|
|
|
newOptions(conf));
|
|
|
- Assert.fail("User should NOT be Authorized !!");
|
|
|
+ fail("User should NOT be Authorized !!");
|
|
|
} catch (IOException ioe) {
|
|
|
// Ignore
|
|
|
}
|
|
@@ -90,7 +92,7 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
try {
|
|
|
kpExt.createKey("foo", SecureRandom.getSeed(16),
|
|
|
newOptions(conf));
|
|
|
- Assert.fail("User should NOT be Authorized !!");
|
|
|
+ fail("User should NOT be Authorized !!");
|
|
|
} catch (IOException ioe) {
|
|
|
// Ignore
|
|
|
}
|
|
@@ -138,17 +140,17 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
kpExt.rollNewVersion(kv.getName(), SecureRandom.getSeed(16));
|
|
|
kpExt.deleteKey(kv.getName());
|
|
|
} catch (IOException ioe) {
|
|
|
- Assert.fail("User should be Authorized !!");
|
|
|
+ fail("User should be Authorized !!");
|
|
|
}
|
|
|
|
|
|
KeyVersion retkv = null;
|
|
|
try {
|
|
|
retkv = kpExt.createKey("bar", SecureRandom.getSeed(16), opt);
|
|
|
kpExt.generateEncryptedKey(retkv.getName());
|
|
|
- Assert.fail("User should NOT be Authorized to generate EEK !!");
|
|
|
+ fail("User should NOT be Authorized to generate EEK !!");
|
|
|
} catch (IOException ioe) {
|
|
|
}
|
|
|
- Assert.assertNotNull(retkv);
|
|
|
+ assertNotNull(retkv);
|
|
|
return retkv;
|
|
|
}
|
|
|
}
|
|
@@ -161,7 +163,7 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
public EncryptedKeyVersion run() throws Exception {
|
|
|
try {
|
|
|
kpExt.deleteKey(barKv.getName());
|
|
|
- Assert.fail("User should NOT be Authorized to "
|
|
|
+ fail("User should NOT be Authorized to "
|
|
|
+ "perform any other operation !!");
|
|
|
} catch (IOException ioe) {
|
|
|
}
|
|
@@ -175,7 +177,7 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
public KeyVersion run() throws Exception {
|
|
|
try {
|
|
|
kpExt.deleteKey(barKv.getName());
|
|
|
- Assert.fail("User should NOT be Authorized to "
|
|
|
+ fail("User should NOT be Authorized to "
|
|
|
+ "perform any other operation !!");
|
|
|
} catch (IOException ioe) {
|
|
|
}
|
|
@@ -200,7 +202,7 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
kpExt.decryptEncryptedKey(ekv);
|
|
|
kpExt.deleteKey(kv.getName());
|
|
|
} catch (IOException ioe) {
|
|
|
- Assert.fail("User should be Allowed to do everything !!");
|
|
|
+ fail("User should be Allowed to do everything !!");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -216,56 +218,55 @@ public class TestKeyAuthorizationKeyProvider {
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Test(expected = IllegalArgumentException.class)
|
|
|
+ @Test
|
|
|
public void testDecryptWithKeyVersionNameKeyMismatch() throws Exception {
|
|
|
- final Configuration conf = new Configuration();
|
|
|
- KeyProvider kp =
|
|
|
- new UserProvider.Factory().createProvider(new URI("user:///"), conf);
|
|
|
- KeyACLs mock = mock(KeyACLs.class);
|
|
|
- when(mock.isACLPresent("testKey", KeyOpType.MANAGEMENT)).thenReturn(true);
|
|
|
- when(mock.isACLPresent("testKey", KeyOpType.GENERATE_EEK)).thenReturn(true);
|
|
|
- when(mock.isACLPresent("testKey", KeyOpType.DECRYPT_EEK)).thenReturn(true);
|
|
|
- when(mock.isACLPresent("testKey", KeyOpType.ALL)).thenReturn(true);
|
|
|
- UserGroupInformation u1 = UserGroupInformation.createRemoteUser("u1");
|
|
|
- UserGroupInformation u2 = UserGroupInformation.createRemoteUser("u2");
|
|
|
- UserGroupInformation u3 = UserGroupInformation.createRemoteUser("u3");
|
|
|
- UserGroupInformation sudo = UserGroupInformation.createRemoteUser("sudo");
|
|
|
- when(mock.hasAccessToKey("testKey", u1,
|
|
|
+ assertThrows(IllegalArgumentException.class, () -> {
|
|
|
+ final Configuration conf = new Configuration();
|
|
|
+ KeyProvider kp = new UserProvider.Factory().createProvider(new URI("user:///"), conf);
|
|
|
+ KeyACLs mock = mock(KeyACLs.class);
|
|
|
+ when(mock.isACLPresent("testKey", KeyOpType.MANAGEMENT)).thenReturn(true);
|
|
|
+ when(mock.isACLPresent("testKey", KeyOpType.GENERATE_EEK)).thenReturn(true);
|
|
|
+ when(mock.isACLPresent("testKey", KeyOpType.DECRYPT_EEK)).thenReturn(true);
|
|
|
+ when(mock.isACLPresent("testKey", KeyOpType.ALL)).thenReturn(true);
|
|
|
+ UserGroupInformation u1 = UserGroupInformation.createRemoteUser("u1");
|
|
|
+ UserGroupInformation u2 = UserGroupInformation.createRemoteUser("u2");
|
|
|
+ UserGroupInformation u3 = UserGroupInformation.createRemoteUser("u3");
|
|
|
+ UserGroupInformation sudo = UserGroupInformation.createRemoteUser("sudo");
|
|
|
+ when(mock.hasAccessToKey("testKey", u1,
|
|
|
KeyOpType.MANAGEMENT)).thenReturn(true);
|
|
|
- when(mock.hasAccessToKey("testKey", u2,
|
|
|
+ when(mock.hasAccessToKey("testKey", u2,
|
|
|
KeyOpType.GENERATE_EEK)).thenReturn(true);
|
|
|
- when(mock.hasAccessToKey("testKey", u3,
|
|
|
+ when(mock.hasAccessToKey("testKey", u3,
|
|
|
KeyOpType.DECRYPT_EEK)).thenReturn(true);
|
|
|
- when(mock.hasAccessToKey("testKey", sudo,
|
|
|
+ when(mock.hasAccessToKey("testKey", sudo,
|
|
|
KeyOpType.ALL)).thenReturn(true);
|
|
|
- final KeyProviderCryptoExtension kpExt =
|
|
|
- new KeyAuthorizationKeyProvider(
|
|
|
+ final KeyProviderCryptoExtension kpExt =
|
|
|
+ new KeyAuthorizationKeyProvider(
|
|
|
KeyProviderCryptoExtension.createKeyProviderCryptoExtension(kp),
|
|
|
mock);
|
|
|
-
|
|
|
- sudo.doAs(
|
|
|
- new PrivilegedExceptionAction<Void>() {
|
|
|
- @Override
|
|
|
- public Void run() throws Exception {
|
|
|
- Options opt = newOptions(conf);
|
|
|
- Map<String, String> m = new HashMap<String, String>();
|
|
|
- m.put("key.acl.name", "testKey");
|
|
|
- opt.setAttributes(m);
|
|
|
- KeyVersion kv =
|
|
|
- kpExt.createKey("foo", SecureRandom.getSeed(16), opt);
|
|
|
- kpExt.rollNewVersion(kv.getName());
|
|
|
- kpExt.rollNewVersion(kv.getName(), SecureRandom.getSeed(16));
|
|
|
- EncryptedKeyVersion ekv = kpExt.generateEncryptedKey(kv.getName());
|
|
|
- ekv = EncryptedKeyVersion.createForDecryption(
|
|
|
- ekv.getEncryptionKeyName() + "x",
|
|
|
- ekv.getEncryptionKeyVersionName(),
|
|
|
- ekv.getEncryptedKeyIv(),
|
|
|
- ekv.getEncryptedKeyVersion().getMaterial());
|
|
|
- kpExt.decryptEncryptedKey(ekv);
|
|
|
- return null;
|
|
|
+ sudo.doAs(
|
|
|
+ new PrivilegedExceptionAction<Void>() {
|
|
|
+ @Override
|
|
|
+ public Void run() throws Exception {
|
|
|
+ Options opt = newOptions(conf);
|
|
|
+ Map<String, String> m = new HashMap<String, String>();
|
|
|
+ m.put("key.acl.name", "testKey");
|
|
|
+ opt.setAttributes(m);
|
|
|
+ KeyVersion kv =
|
|
|
+ kpExt.createKey("foo", SecureRandom.getSeed(16), opt);
|
|
|
+ kpExt.rollNewVersion(kv.getName());
|
|
|
+ kpExt.rollNewVersion(kv.getName(), SecureRandom.getSeed(16));
|
|
|
+ EncryptedKeyVersion ekv = kpExt.generateEncryptedKey(kv.getName());
|
|
|
+ ekv = EncryptedKeyVersion.createForDecryption(
|
|
|
+ ekv.getEncryptionKeyName() + "x",
|
|
|
+ ekv.getEncryptionKeyVersionName(),
|
|
|
+ ekv.getEncryptedKeyIv(),
|
|
|
+ ekv.getEncryptedKeyVersion().getMaterial());
|
|
|
+ kpExt.decryptEncryptedKey(ekv);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- );
|
|
|
+ );
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
}
|