|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.http; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.HttpURLConnection; |
| 24 | +import org.apache.directory.server.annotations.CreateLdapServer; |
| 25 | +import org.apache.directory.server.annotations.CreateTransport; |
| 26 | +import org.apache.directory.server.core.annotations.ApplyLdifs; |
| 27 | +import org.apache.directory.server.core.annotations.ContextEntry; |
| 28 | +import org.apache.directory.server.core.annotations.CreateDS; |
| 29 | +import org.apache.directory.server.core.annotations.CreatePartition; |
| 30 | +import org.apache.hadoop.conf.Configuration; |
| 31 | +import org.apache.hadoop.fs.CommonConfigurationKeys; |
| 32 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 33 | +import org.apache.hadoop.hbase.http.resource.JerseyResource; |
| 34 | +import org.apache.hadoop.hbase.testclassification.MiscTests; |
| 35 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 36 | +import org.junit.BeforeClass; |
| 37 | +import org.junit.ClassRule; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.experimental.categories.Category; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +/** |
| 44 | + * Test class for admin ACLs with LDAP authentication on the HttpServer. |
| 45 | + */ |
| 46 | +@Category({ MiscTests.class, SmallTests.class }) |
| 47 | +@CreateLdapServer( |
| 48 | + transports = { @CreateTransport(protocol = "LDAP", address = LdapConstants.LDAP_SERVER_ADDR), }) |
| 49 | +@CreateDS(name = "TestLdapAdminACL", allowAnonAccess = true, |
| 50 | + partitions = { @CreatePartition(name = "Test_Partition", suffix = LdapConstants.LDAP_BASE_DN, |
| 51 | + contextEntry = @ContextEntry(entryLdif = "dn: " + LdapConstants.LDAP_BASE_DN + " \n" |
| 52 | + + "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n")) }) |
| 53 | +@ApplyLdifs({ "dn: uid=bjones," + LdapConstants.LDAP_BASE_DN, "cn: Bob Jones", "sn: Jones", |
| 54 | + "objectClass: inetOrgPerson", "uid: bjones", "userPassword: p@ssw0rd", |
| 55 | + |
| 56 | + "dn: uid=jdoe," + LdapConstants.LDAP_BASE_DN, "cn: John Doe", "sn: Doe", |
| 57 | + "objectClass: inetOrgPerson", "uid: jdoe", "userPassword: secure123" }) |
| 58 | +public class TestLdapAdminACL extends LdapServerTestBase { |
| 59 | + |
| 60 | + @ClassRule |
| 61 | + public static final HBaseClassTestRule CLASS_RULE = |
| 62 | + HBaseClassTestRule.forClass(TestLdapAdminACL.class); |
| 63 | + private static final Logger LOG = LoggerFactory.getLogger(TestLdapAdminACL.class); |
| 64 | + |
| 65 | + private static final String ADMIN_CREDENTIALS = "bjones:p@ssw0rd"; |
| 66 | + private static final String NON_ADMIN_CREDENTIALS = "jdoe:secure123"; |
| 67 | + private static final String WRONG_CREDENTIALS = "bjones:password"; |
| 68 | + |
| 69 | + @BeforeClass |
| 70 | + public static void setupServer() throws Exception { |
| 71 | + Configuration conf = new Configuration(); |
| 72 | + setLdapConfigurationWithACLs(conf); |
| 73 | + |
| 74 | + server = createTestServer(conf, InfoServer.buildAdminAcl(conf)); |
| 75 | + server.addUnprivilegedServlet("echo", "/echo", TestHttpServer.EchoServlet.class); |
| 76 | + // we will reuse /jmx which is a privileged servlet |
| 77 | + server.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); |
| 78 | + server.start(); |
| 79 | + |
| 80 | + baseUrl = getServerURL(server); |
| 81 | + LOG.info("HTTP server started: " + baseUrl); |
| 82 | + } |
| 83 | + |
| 84 | + private static void setLdapConfigurationWithACLs(Configuration conf) { |
| 85 | + setLdapConfigurations(conf); |
| 86 | + |
| 87 | + // Enable LDAP admin ACL |
| 88 | + conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, true); |
| 89 | + conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_INSTRUMENTATION_REQUIRES_ADMIN, true); |
| 90 | + conf.set(HttpServer.HTTP_LDAP_AUTHENTICATION_ADMIN_USERS_KEY, "bjones"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testAdminAllowedUnprivilegedServletAccess() throws IOException { |
| 95 | + HttpURLConnection conn = openConnection("/echo?a=b", ADMIN_CREDENTIALS); |
| 96 | + assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testAdminAllowedPrivilegedServletAccess() throws IOException { |
| 101 | + HttpURLConnection conn = openConnection("/jmx", ADMIN_CREDENTIALS); |
| 102 | + assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testNonAdminAllowedUnprivilegedServletAccess() throws IOException { |
| 107 | + HttpURLConnection conn = openConnection("/echo?a=b", NON_ADMIN_CREDENTIALS); |
| 108 | + assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testNonAdminDisallowedPrivilegedServletAccess() throws IOException { |
| 113 | + HttpURLConnection conn = openConnection("/jmx", NON_ADMIN_CREDENTIALS); |
| 114 | + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testWrongAuthDisallowedUnprivilegedServletAccess() throws IOException { |
| 119 | + HttpURLConnection conn = openConnection("/echo?a=b", WRONG_CREDENTIALS); |
| 120 | + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testWrongAuthDisallowedPrivilegedServletAccess() throws IOException { |
| 125 | + HttpURLConnection conn = openConnection("/jmx", WRONG_CREDENTIALS); |
| 126 | + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); |
| 127 | + } |
| 128 | +} |
0 commit comments