-
Notifications
You must be signed in to change notification settings - Fork 3.3k
HBASE-29244 Support admin users acl setting with LDAP (Web UI only) #6923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
10da6eb
HBASE-29244 Support admin users acl setting with LDAP (Web UI only)
NihalJain ef0d4d4
HBASE-29244 Support admin users acl setting with LDAP (Web UI only)
NihalJain 7dc8e62
Fix tests, cleanup code
NihalJain 3e2891b
* Address review comments
NihalJain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
hbase-http/src/test/java/org/apache/hadoop/hbase/http/LdapServerTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.http; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.directory.server.core.integ.CreateLdapServerRule; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.http.resource.JerseyResource; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Base class for setting up and testing an HTTP server with LDAP authentication. | ||
*/ | ||
public class LdapServerTestBase extends HttpServerFunctionalTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(LdapServerTestBase.class); | ||
|
||
@ClassRule | ||
public static CreateLdapServerRule ldapRule = new CreateLdapServerRule(); | ||
|
||
protected static HttpServer server; | ||
protected static URL baseUrl; | ||
|
||
private static final String AUTH_TYPE = "Basic "; | ||
|
||
/** | ||
* Sets up the HTTP server with LDAP authentication before any tests are run. | ||
* @throws Exception if an error occurs during server setup | ||
*/ | ||
@BeforeClass | ||
public static void setupServer() throws Exception { | ||
Configuration conf = new Configuration(); | ||
setLdapConfigurations(conf); | ||
|
||
server = createTestServer(conf); | ||
server.addUnprivilegedServlet("echo", "/echo", TestHttpServer.EchoServlet.class); | ||
server.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); | ||
server.start(); | ||
|
||
baseUrl = getServerURL(server); | ||
LOG.info("HTTP server started: " + baseUrl); | ||
} | ||
|
||
/** | ||
* Stops the HTTP server after all tests are completed. | ||
* @throws Exception if an error occurs during server shutdown | ||
*/ | ||
@AfterClass | ||
public static void stopServer() throws Exception { | ||
try { | ||
if (null != server) { | ||
server.stop(); | ||
} | ||
} catch (Exception e) { | ||
LOG.info("Failed to stop info server", e); | ||
} | ||
} | ||
|
||
/** | ||
* Configures the provided Configuration object for LDAP authentication. | ||
* @param conf the Configuration object to set LDAP properties on | ||
* @return the configured Configuration object | ||
*/ | ||
protected static void setLdapConfigurations(Configuration conf) { | ||
conf.setInt(HttpServer.HTTP_MAX_THREADS, TestHttpServer.MAX_THREADS); | ||
|
||
// Enable LDAP (pre-req) | ||
conf.set(HttpServer.HTTP_UI_AUTHENTICATION, "ldap"); | ||
conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY, | ||
"org.apache.hadoop.hbase.http.lib.AuthenticationFilterInitializer"); | ||
conf.set("hadoop.http.authentication.type", "ldap"); | ||
conf.set("hadoop.http.authentication.ldap.providerurl", String.format("ldap://%s:%s", | ||
LdapConstants.LDAP_SERVER_ADDR, ldapRule.getLdapServer().getPort())); | ||
conf.set("hadoop.http.authentication.ldap.enablestarttls", "false"); | ||
conf.set("hadoop.http.authentication.ldap.basedn", LdapConstants.LDAP_BASE_DN); | ||
} | ||
|
||
/** | ||
* Generates a Basic Authentication header from the provided credentials. | ||
* @param credentials the credentials to encode | ||
* @return the Basic Authentication header | ||
*/ | ||
private String getBasicAuthHeader(String credentials) { | ||
return AUTH_TYPE + new Base64(0).encodeToString(credentials.getBytes()); | ||
} | ||
|
||
/** | ||
* Opens an HTTP connection to the specified endpoint with optional Basic Authentication. | ||
* @param endpoint the endpoint to connect to | ||
* @param credentials the credentials for Basic Authentication (optional) | ||
* @return the opened HttpURLConnection | ||
* @throws IOException if an error occurs while opening the connection | ||
*/ | ||
protected HttpURLConnection openConnection(String endpoint, String credentials) | ||
throws IOException { | ||
URL url = new URL(getServerURL(server) + endpoint); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
if (credentials != null) { | ||
conn.setRequestProperty("Authorization", getBasicAuthHeader(credentials)); | ||
} | ||
return conn; | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestLdapAdminACL.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.http; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import org.apache.directory.server.annotations.CreateLdapServer; | ||
import org.apache.directory.server.annotations.CreateTransport; | ||
import org.apache.directory.server.core.annotations.ApplyLdifs; | ||
import org.apache.directory.server.core.annotations.ContextEntry; | ||
import org.apache.directory.server.core.annotations.CreateDS; | ||
import org.apache.directory.server.core.annotations.CreatePartition; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.CommonConfigurationKeys; | ||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.http.resource.JerseyResource; | ||
import org.apache.hadoop.hbase.testclassification.MiscTests; | ||
import org.apache.hadoop.hbase.testclassification.SmallTests; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Test class for admin ACLs with LDAP authentication on the HttpServer. | ||
*/ | ||
@Category({ MiscTests.class, SmallTests.class }) | ||
@CreateLdapServer( | ||
transports = { @CreateTransport(protocol = "LDAP", address = LdapConstants.LDAP_SERVER_ADDR), }) | ||
@CreateDS(name = "TestLdapAdminACL", allowAnonAccess = true, | ||
partitions = { @CreatePartition(name = "Test_Partition", suffix = LdapConstants.LDAP_BASE_DN, | ||
contextEntry = @ContextEntry(entryLdif = "dn: " + LdapConstants.LDAP_BASE_DN + " \n" | ||
+ "dc: example\n" + "objectClass: top\n" + "objectClass: domain\n\n")) }) | ||
@ApplyLdifs({ "dn: uid=bjones," + LdapConstants.LDAP_BASE_DN, "cn: Bob Jones", "sn: Jones", | ||
"objectClass: inetOrgPerson", "uid: bjones", "userPassword: p@ssw0rd", | ||
|
||
"dn: uid=jdoe," + LdapConstants.LDAP_BASE_DN, "cn: John Doe", "sn: Doe", | ||
"objectClass: inetOrgPerson", "uid: jdoe", "userPassword: secure123" }) | ||
public class TestLdapAdminACL extends LdapServerTestBase { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestLdapAdminACL.class); | ||
private static final Logger LOG = LoggerFactory.getLogger(TestLdapAdminACL.class); | ||
|
||
private static final String ADMIN_CREDENTIALS = "bjones:p@ssw0rd"; | ||
private static final String NON_ADMIN_CREDENTIALS = "jdoe:secure123"; | ||
private static final String WRONG_CREDENTIALS = "bjones:password"; | ||
|
||
@BeforeClass | ||
public static void setupServer() throws Exception { | ||
Configuration conf = new Configuration(); | ||
setLdapConfigurationWithACLs(conf); | ||
|
||
server = createTestServer(conf, InfoServer.buildAdminAcl(conf)); | ||
server.addUnprivilegedServlet("echo", "/echo", TestHttpServer.EchoServlet.class); | ||
// we will reuse /jmx which is a privileged servlet | ||
server.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); | ||
server.start(); | ||
|
||
baseUrl = getServerURL(server); | ||
LOG.info("HTTP server started: " + baseUrl); | ||
} | ||
|
||
private static void setLdapConfigurationWithACLs(Configuration conf) { | ||
setLdapConfigurations(conf); | ||
|
||
// Enable LDAP admin ACL | ||
conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, true); | ||
conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_INSTRUMENTATION_REQUIRES_ADMIN, true); | ||
conf.set(HttpServer.HTTP_LDAP_AUTHENTICATION_ADMIN_USERS_KEY, "bjones"); | ||
} | ||
|
||
@Test | ||
public void testAdminAllowedUnprivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/echo?a=b", ADMIN_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); | ||
} | ||
|
||
@Test | ||
public void testAdminAllowedPrivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/jmx", ADMIN_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); | ||
} | ||
|
||
@Test | ||
public void testNonAdminAllowedUnprivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/echo?a=b", NON_ADMIN_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); | ||
} | ||
|
||
@Test | ||
public void testNonAdminDisallowedPrivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/jmx", NON_ADMIN_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); | ||
} | ||
|
||
@Test | ||
public void testWrongAuthDisallowedUnprivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/echo?a=b", WRONG_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); | ||
} | ||
|
||
@Test | ||
public void testWrongAuthDisallowedPrivilegedServletAccess() throws IOException { | ||
HttpURLConnection conn = openConnection("/jmx", WRONG_CREDENTIALS); | ||
assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.