|
@@ -18,16 +18,22 @@
|
|
|
package org.apache.hadoop.security.authorize;
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.fail;
|
|
|
+
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.UnknownHostException;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
|
|
import org.apache.hadoop.ipc.TestRPC.TestProtocol;
|
|
|
+import org.apache.hadoop.security.UserGroupInformation;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
public class TestServiceAuthorization {
|
|
|
|
|
|
private static final String ACL_CONFIG = "test.protocol.acl";
|
|
|
private static final String ACL_CONFIG1 = "test.protocol1.acl";
|
|
|
+ private static final String ADDRESS = "0.0.0.0";
|
|
|
|
|
|
public interface TestProtocol1 extends TestProtocol {};
|
|
|
|
|
@@ -64,4 +70,115 @@ public class TestServiceAuthorization {
|
|
|
acl = serviceAuthorizationManager.getProtocolsAcls(TestProtocol1.class);
|
|
|
assertEquals("user2 group2", acl.getAclString());
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testBlockedAcl() throws UnknownHostException {
|
|
|
+ UserGroupInformation drwho =
|
|
|
+ UserGroupInformation.createUserForTesting("drwho@EXAMPLE.COM",
|
|
|
+ new String[] { "group1", "group2" });
|
|
|
+
|
|
|
+ ServiceAuthorizationManager serviceAuthorizationManager =
|
|
|
+ new ServiceAuthorizationManager();
|
|
|
+ Configuration conf = new Configuration ();
|
|
|
+
|
|
|
+ //test without setting a blocked acl
|
|
|
+ conf.set(ACL_CONFIG, "user1 group1");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ //now set a blocked acl with another user and another group
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "drwho2 group3");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ //now set a blocked acl with the user and another group
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "drwho group3");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ fail();
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ //now set a blocked acl with another user and another group
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "drwho2 group3");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ //now set a blocked acl with another user and group that the user belongs to
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "drwho2 group2");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ fail();
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ //expects Exception
|
|
|
+ }
|
|
|
+ //reset blocked acl so that there is no blocked ACL
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDefaultBlockedAcl() throws UnknownHostException {
|
|
|
+ UserGroupInformation drwho =
|
|
|
+ UserGroupInformation.createUserForTesting("drwho@EXAMPLE.COM",
|
|
|
+ new String[] { "group1", "group2" });
|
|
|
+
|
|
|
+ ServiceAuthorizationManager serviceAuthorizationManager =
|
|
|
+ new ServiceAuthorizationManager();
|
|
|
+ Configuration conf = new Configuration ();
|
|
|
+
|
|
|
+ //test without setting a default blocked acl
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol1.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+
|
|
|
+ //set a restrictive default blocked acl and an non-restricting blocked acl for TestProtocol
|
|
|
+ conf.set(
|
|
|
+ CommonConfigurationKeys.HADOOP_SECURITY_SERVICE_AUTHORIZATION_DEFAULT_BLOCKED_ACL,
|
|
|
+ "user2 group2");
|
|
|
+ conf.set(ACL_CONFIG + ServiceAuthorizationManager.BLOCKED, "user2");
|
|
|
+ serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
|
|
|
+ //drwho is authorized to access TestProtocol
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ //drwho is not authorized to access TestProtocol1 because it uses the default blocked acl.
|
|
|
+ try {
|
|
|
+ serviceAuthorizationManager.authorize(drwho, TestProtocol1.class, conf,
|
|
|
+ InetAddress.getByName(ADDRESS));
|
|
|
+ fail();
|
|
|
+ } catch (AuthorizationException e) {
|
|
|
+ //expects Exception
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|