Skip to content

Commit a91d308

Browse files
authored
HBASE-28368 Backport "HBASE-27693 Support for Hadoop's LDAP Authentication mechanism (Web UI only)" to branch-2 (#5144) (#5680)
* Fix failure in pre-commit checks for Yetus JDK8 Hadoop2 due to missing class SchemaLdifExtractor in ApacheDS 2.0.0.AM26. The issue arises from an overridden ApacheDS version incompatible with Hadoop 2. To resolve, skip LDAP tests for Hadoop 2 and apply dependency changes only for Hadoop 3. Co-authored-by: Yash Dodeja <[email protected]> Signed-off-by: Nick Dimiduk <[email protected]>
1 parent fb40fdf commit a91d308

File tree

6 files changed

+415
-0
lines changed

6 files changed

+415
-0
lines changed

hbase-http/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,19 @@
398398
</execution>
399399
</executions>
400400
</plugin>
401+
<plugin>
402+
<groupId>org.apache.maven.plugins</groupId>
403+
<artifactId>maven-compiler-plugin</artifactId>
404+
<configuration>
405+
<!--
406+
Below tests fail with hadoop 2 due to test dependency versioning issues. Hence, skip LDAP tests for Hadoop 2,
407+
see HBASE-28368 for more details!
408+
-->
409+
<testExcludes>
410+
<testExclude>**/org/apache/hadoop/hbase/http/TestLdapHttpServer**</testExclude>
411+
</testExcludes>
412+
</configuration>
413+
</plugin>
401414
</plugins>
402415
</build>
403416
</profile>
@@ -446,6 +459,60 @@
446459
<artifactId>hadoop-minikdc</artifactId>
447460
<scope>test</scope>
448461
</dependency>
462+
<dependency>
463+
<groupId>org.apache.directory.server</groupId>
464+
<artifactId>apacheds-core</artifactId>
465+
<version>${apacheds.version}</version>
466+
<scope>test</scope>
467+
<exclusions>
468+
<exclusion>
469+
<groupId>org.bouncycastle</groupId>
470+
<artifactId>bcprov-jdk15on</artifactId>
471+
</exclusion>
472+
</exclusions>
473+
</dependency>
474+
<dependency>
475+
<groupId>org.apache.directory.server</groupId>
476+
<artifactId>apacheds-protocol-ldap</artifactId>
477+
<version>${apacheds.version}</version>
478+
<scope>test</scope>
479+
<exclusions>
480+
<exclusion>
481+
<groupId>org.bouncycastle</groupId>
482+
<artifactId>bcprov-jdk15on</artifactId>
483+
</exclusion>
484+
</exclusions>
485+
</dependency>
486+
<dependency>
487+
<groupId>org.apache.directory.server</groupId>
488+
<artifactId>apacheds-ldif-partition</artifactId>
489+
<version>${apacheds.version}</version>
490+
<scope>test</scope>
491+
</dependency>
492+
<dependency>
493+
<groupId>org.apache.directory.api</groupId>
494+
<artifactId>api-ldap-codec-core</artifactId>
495+
<version>${ldap-api.version}</version>
496+
<scope>test</scope>
497+
</dependency>
498+
<dependency>
499+
<groupId>org.apache.directory.api</groupId>
500+
<artifactId>api-ldap-model</artifactId>
501+
<version>${ldap-api.version}</version>
502+
<scope>test</scope>
503+
</dependency>
504+
<dependency>
505+
<groupId>org.apache.directory.server</groupId>
506+
<artifactId>apacheds-server-integ</artifactId>
507+
<version>${apacheds.version}</version>
508+
<scope>test</scope>
509+
<exclusions>
510+
<exclusion>
511+
<groupId>log4j</groupId>
512+
<artifactId>log4j</artifactId>
513+
</exclusion>
514+
</exclusions>
515+
</dependency>
449516
</dependencies>
450517
<build>
451518
<plugins>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.lib;
19+
20+
import java.io.IOException;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.hadoop.hbase.HBaseInterfaceAudience;
25+
import org.apache.hadoop.hbase.http.FilterContainer;
26+
import org.apache.hadoop.hbase.http.FilterInitializer;
27+
import org.apache.hadoop.hbase.http.HttpServer;
28+
import org.apache.hadoop.security.SecurityUtil;
29+
import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
30+
import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler;
31+
import org.apache.yetus.audience.InterfaceAudience;
32+
33+
/**
34+
* This class is copied from Hadoop. Initializes hadoop-auth AuthenticationFilter which provides
35+
* support for Kerberos HTTP SPNEGO authentication.
36+
* <p>
37+
* It enables anonymous access, simple/pseudo and Kerberos HTTP SPNEGO authentication for HBase web
38+
* UI endpoints.
39+
* <p>
40+
* Refer to the <code>core-default.xml</code> file, after the comment 'HTTP Authentication' for
41+
* details on the configuration options. All related configuration properties have
42+
* 'hadoop.http.authentication.' as prefix.
43+
*/
44+
@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
45+
public class AuthenticationFilterInitializer extends FilterInitializer {
46+
47+
static final String PREFIX = "hadoop.http.authentication.";
48+
49+
/**
50+
* Initializes hadoop-auth AuthenticationFilter.
51+
* <p>
52+
* Propagates to hadoop-auth AuthenticationFilter configuration all Hadoop configuration
53+
* properties prefixed with "hadoop.http.authentication."
54+
* @param container The filter container
55+
* @param conf Configuration for run-time parameters
56+
*/
57+
@Override
58+
public void initFilter(FilterContainer container, Configuration conf) {
59+
Map<String, String> filterConfig = getFilterConfigMap(conf, PREFIX);
60+
61+
container.addFilter("authentication", AuthenticationFilter.class.getName(), filterConfig);
62+
}
63+
64+
public static Map<String, String> getFilterConfigMap(Configuration conf, String prefix) {
65+
Map<String, String> filterConfig = new HashMap<String, String>();
66+
67+
// setting the cookie path to root '/' so it is used for all resources.
68+
filterConfig.put(AuthenticationFilter.COOKIE_PATH, "/");
69+
Map<String, String> propsWithPrefix = conf.getPropsWithPrefix(prefix);
70+
71+
for (Map.Entry<String, String> entry : propsWithPrefix.entrySet()) {
72+
filterConfig.put(entry.getKey(), entry.getValue());
73+
}
74+
75+
// Resolve _HOST into bind address
76+
String bindAddress = conf.get(HttpServer.BIND_ADDRESS);
77+
String principal = filterConfig.get(KerberosAuthenticationHandler.PRINCIPAL);
78+
if (principal != null) {
79+
try {
80+
principal = SecurityUtil.getServerPrincipal(principal, bindAddress);
81+
} catch (IOException ex) {
82+
throw new RuntimeException("Could not resolve Kerberos principal name: " + ex.toString(),
83+
ex);
84+
}
85+
filterConfig.put(KerberosAuthenticationHandler.PRINCIPAL, principal);
86+
}
87+
return filterConfig;
88+
}
89+
90+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
21+
* This class defines the constants used by the LDAP integration tests.
22+
*/
23+
public final class LdapConstants {
24+
25+
/**
26+
* This class defines constants to be used for LDAP integration testing. Hence this class is not
27+
* expected to be instantiated.
28+
*/
29+
private LdapConstants() {
30+
}
31+
32+
public static final String LDAP_BASE_DN = "dc=example,dc=com";
33+
public static final String LDAP_SERVER_ADDR = "localhost";
34+
35+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)