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.jupiter.api.Assertions.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.testclassification.MiscTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034 035/** 036 * Test class for LDAP authentication on the HttpServer. 037 */ 038@Tag(MiscTests.TAG) 039@Tag(SmallTests.TAG) 040@CreateLdapServer( 041 transports = { @CreateTransport(protocol = "LDAP", address = LdapConstants.LDAP_SERVER_ADDR), }) 042@CreateDS(name = "TestLdapHttpServer", allowAnonAccess = true, 043 partitions = { @CreatePartition(name = "Test_Partition", suffix = LdapConstants.LDAP_BASE_DN, 044 contextEntry = @ContextEntry(entryLdif = "dn: " + LdapConstants.LDAP_BASE_DN + " \n" 045 + "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n")) }) 046@ApplyLdifs({ "dn: uid=bjones," + LdapConstants.LDAP_BASE_DN, "cn: Bob Jones", "sn: Jones", 047 "objectClass: inetOrgPerson", "uid: bjones", "userPassword: p@ssw0rd" }) 048public class TestLdapHttpServer extends LdapServerTestBase { 049 050 private static final String BJONES_CREDENTIALS = "bjones:p@ssw0rd"; 051 private static final String WRONG_CREDENTIALS = "bjones:password"; 052 053 @Test 054 public void testUnauthorizedClientsDisallowed() throws IOException { 055 HttpURLConnection conn = openConnection("/echo?a=b", null); 056 assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode()); 057 } 058 059 @Test 060 public void testAllowedClient() throws IOException { 061 HttpURLConnection conn = openConnection("/echo?a=b", BJONES_CREDENTIALS); 062 assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); 063 } 064 065 @Test 066 public void testWrongAuthClientsDisallowed() throws IOException { 067 HttpURLConnection conn = openConnection("/echo?a=b", WRONG_CREDENTIALS); 068 assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); 069 } 070}