001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.net.HttpURLConnection;
024import org.apache.directory.server.annotations.CreateLdapServer;
025import org.apache.directory.server.annotations.CreateTransport;
026import org.apache.directory.server.core.annotations.ApplyLdifs;
027import org.apache.directory.server.core.annotations.ContextEntry;
028import org.apache.directory.server.core.annotations.CreateDS;
029import org.apache.directory.server.core.annotations.CreatePartition;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037/**
038 * Test class for LDAP authentication on the HttpServer.
039 */
040@Category({ MiscTests.class, SmallTests.class })
041@CreateLdapServer(
042    transports = { @CreateTransport(protocol = "LDAP", address = LdapConstants.LDAP_SERVER_ADDR), })
043@CreateDS(name = "TestLdapHttpServer", allowAnonAccess = true,
044    partitions = { @CreatePartition(name = "Test_Partition", suffix = LdapConstants.LDAP_BASE_DN,
045        contextEntry = @ContextEntry(entryLdif = "dn: " + LdapConstants.LDAP_BASE_DN + " \n"
046          + "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n")) })
047@ApplyLdifs({ "dn: uid=bjones," + LdapConstants.LDAP_BASE_DN, "cn: Bob Jones", "sn: Jones",
048  "objectClass: inetOrgPerson", "uid: bjones", "userPassword: p@ssw0rd" })
049public class TestLdapHttpServer extends LdapServerTestBase {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestLdapHttpServer.class);
054
055  private static final String BJONES_CREDENTIALS = "bjones:p@ssw0rd";
056  private static final String WRONG_CREDENTIALS = "bjones:password";
057
058  @Test
059  public void testUnauthorizedClientsDisallowed() throws IOException {
060    HttpURLConnection conn = openConnection("/echo?a=b", null);
061    assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
062  }
063
064  @Test
065  public void testAllowedClient() throws IOException {
066    HttpURLConnection conn = openConnection("/echo?a=b", BJONES_CREDENTIALS);
067    assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
068  }
069
070  @Test
071  public void testWrongAuthClientsDisallowed() throws IOException {
072    HttpURLConnection conn = openConnection("/echo?a=b", WRONG_CREDENTIALS);
073    assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode());
074  }
075}