|
| 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 java.net.URL; |
| 25 | +import org.apache.commons.codec.binary.Base64; |
| 26 | +import org.apache.directory.server.annotations.CreateLdapServer; |
| 27 | +import org.apache.directory.server.annotations.CreateTransport; |
| 28 | +import org.apache.directory.server.core.annotations.ApplyLdifs; |
| 29 | +import org.apache.directory.server.core.annotations.ContextEntry; |
| 30 | +import org.apache.directory.server.core.annotations.CreateDS; |
| 31 | +import org.apache.directory.server.core.annotations.CreatePartition; |
| 32 | +import org.apache.directory.server.core.integ.CreateLdapServerRule; |
| 33 | +import org.apache.hadoop.conf.Configuration; |
| 34 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 35 | +import org.apache.hadoop.hbase.http.resource.JerseyResource; |
| 36 | +import org.apache.hadoop.hbase.testclassification.MiscTests; |
| 37 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 38 | +import org.junit.AfterClass; |
| 39 | +import org.junit.BeforeClass; |
| 40 | +import org.junit.ClassRule; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.experimental.categories.Category; |
| 43 | +import org.slf4j.Logger; |
| 44 | +import org.slf4j.LoggerFactory; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test class for LDAP authentication on the HttpServer. |
| 48 | + */ |
| 49 | +@Category({ MiscTests.class, SmallTests.class }) |
| 50 | +@CreateLdapServer( |
| 51 | + transports = { @CreateTransport(protocol = "LDAP", address = LdapConstants.LDAP_SERVER_ADDR), }) |
| 52 | +@CreateDS(allowAnonAccess = true, |
| 53 | + partitions = { @CreatePartition(name = "Test_Partition", suffix = LdapConstants.LDAP_BASE_DN, |
| 54 | + contextEntry = @ContextEntry(entryLdif = "dn: " + LdapConstants.LDAP_BASE_DN + " \n" |
| 55 | + + "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n")) }) |
| 56 | +@ApplyLdifs({ "dn: uid=bjones," + LdapConstants.LDAP_BASE_DN, "cn: Bob Jones", "sn: Jones", |
| 57 | + "objectClass: inetOrgPerson", "uid: bjones", "userPassword: p@ssw0rd" }) |
| 58 | +public class TestLdapHttpServer extends HttpServerFunctionalTest { |
| 59 | + |
| 60 | + @ClassRule |
| 61 | + public static final HBaseClassTestRule CLASS_RULE = |
| 62 | + HBaseClassTestRule.forClass(TestLdapHttpServer.class); |
| 63 | + @ClassRule |
| 64 | + public static CreateLdapServerRule serverRule = new CreateLdapServerRule(); |
| 65 | + |
| 66 | + private static final Logger LOG = LoggerFactory.getLogger(TestLdapHttpServer.class); |
| 67 | + |
| 68 | + private static HttpServer server; |
| 69 | + private static URL baseUrl; |
| 70 | + |
| 71 | + @BeforeClass |
| 72 | + public static void setupServer() throws Exception { |
| 73 | + Configuration conf = new Configuration(); |
| 74 | + buildLdapConfiguration(conf); |
| 75 | + server = createTestServer(conf); |
| 76 | + server.addUnprivilegedServlet("echo", "/echo", TestHttpServer.EchoServlet.class); |
| 77 | + server.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); |
| 78 | + server.start(); |
| 79 | + baseUrl = getServerURL(server); |
| 80 | + |
| 81 | + LOG.info("HTTP server started: " + baseUrl); |
| 82 | + } |
| 83 | + |
| 84 | + @AfterClass |
| 85 | + public static void stopServer() throws Exception { |
| 86 | + try { |
| 87 | + if (null != server) { |
| 88 | + server.stop(); |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + LOG.info("Failed to stop info server", e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static Configuration buildLdapConfiguration(Configuration conf) { |
| 96 | + |
| 97 | + conf.setInt(HttpServer.HTTP_MAX_THREADS, TestHttpServer.MAX_THREADS); |
| 98 | + |
| 99 | + // Enable LDAP (pre-req) |
| 100 | + conf.set(HttpServer.HTTP_UI_AUTHENTICATION, "ldap"); |
| 101 | + conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY, |
| 102 | + "org.apache.hadoop.hbase.http.lib.AuthenticationFilterInitializer"); |
| 103 | + conf.set("hadoop.http.authentication.type", "ldap"); |
| 104 | + conf.set("hadoop.http.authentication.ldap.providerurl", String.format("ldap://%s:%s", |
| 105 | + LdapConstants.LDAP_SERVER_ADDR, serverRule.getLdapServer().getPort())); |
| 106 | + conf.set("hadoop.http.authentication.ldap.enablestarttls", "false"); |
| 107 | + conf.set("hadoop.http.authentication.ldap.basedn", LdapConstants.LDAP_BASE_DN); |
| 108 | + return conf; |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testUnauthorizedClientsDisallowed() throws IOException { |
| 113 | + URL url = new URL(getServerURL(server), "/echo?a=b"); |
| 114 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 115 | + assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testAllowedClient() throws IOException { |
| 120 | + URL url = new URL(getServerURL(server), "/echo?a=b"); |
| 121 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 122 | + final Base64 base64 = new Base64(0); |
| 123 | + String userCredentials = "bjones:p@ssw0rd"; |
| 124 | + String basicAuth = "Basic " + base64.encodeToString(userCredentials.getBytes()); |
| 125 | + conn.setRequestProperty("Authorization", basicAuth); |
| 126 | + assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testWrongAuthClientsDisallowed() throws IOException { |
| 131 | + URL url = new URL(getServerURL(server), "/echo?a=b"); |
| 132 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 133 | + final Base64 base64 = new Base64(0); |
| 134 | + String userCredentials = "bjones:password"; |
| 135 | + String basicAuth = "Basic " + base64.encodeToString(userCredentials.getBytes()); |
| 136 | + conn.setRequestProperty("Authorization", basicAuth); |
| 137 | + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments