Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* Returns a {@code TableName} based on currently running test method name.
*/
public class TableNameTestExtension implements BeforeEachCallback {

private TableName tableName;

/**
* Helper to handle parameterized method names. Unlike regular test methods, parameterized method
* names look like 'foo[x]'. This is problematic for tests that use this name for HBase tables.
* This helper strips out the parameter suffixes.
* @return current test method name without parameterized suffixes.
*/
public static String cleanUpTestName(String methodName) {
int index = methodName.indexOf('[');
if (index == -1) {
return methodName;
}
return methodName.substring(0, index);
}

public TableName getTableName() {
return tableName;
}

@Override
public void beforeEach(ExtensionContext context) {
tableName = TableName.valueOf(cleanUpTestName(context.getRequiredTestMethod().getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@
*/
package org.apache.hadoop.hbase.security;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import org.apache.commons.lang3.SystemUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.testclassification.SecurityTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;

@Category({ SecurityTests.class, SmallTests.class })
@Tag(SecurityTests.TAG)
@Tag(SmallTests.TAG)
public class TestUser {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestUser.class);

private static final Logger LOG = LoggerFactory.getLogger(TestUser.class);

@Test
Expand Down Expand Up @@ -113,8 +113,8 @@ public void testCacheGetGroupsRoot() throws Exception {
public void testBasicAttributes() throws Exception {
Configuration conf = HBaseConfiguration.create();
User user = User.createUserForTesting(conf, "simple", new String[] { "foo" });
assertEquals("Username should match", "simple", user.getName());
assertEquals("Short username should match", "simple", user.getShortName());
assertEquals("simple", user.getName(), "Username should match");
assertEquals("simple", user.getShortName(), "Short username should match");
// don't test shortening of kerberos names because regular Hadoop doesn't support them
}

Expand All @@ -131,12 +131,12 @@ public String run() throws IOException {
};

String username = user.runAs(action);
assertEquals("Current user within runAs() should match", "testuser", username);
assertEquals("testuser", username, "Current user within runAs() should match");

// ensure the next run is correctly set
User user2 = User.createUserForTesting(conf, "testuser2", new String[] { "foo" });
String username2 = user2.runAs(action);
assertEquals("Second username should match second user", "testuser2", username2);
assertEquals("testuser2", username2, "Second username should match second user");

// check the exception version
username = user.runAs(new PrivilegedExceptionAction<String>() {
Expand All @@ -145,16 +145,16 @@ public String run() throws Exception {
return User.getCurrent().getName();
}
});
assertEquals("User name in runAs() should match", "testuser", username);
assertEquals("testuser", username, "User name in runAs() should match");

// verify that nested contexts work
user2.runAs(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws IOException, InterruptedException {
String nestedName = user.runAs(action);
assertEquals("Nest name should match nested user", "testuser", nestedName);
assertEquals("Current name should match current user", "testuser2",
User.getCurrent().getName());
assertEquals("testuser", nestedName, "Nest name should match nested user");
assertEquals("testuser2", User.getCurrent().getName(),
"Current name should match current user");
return null;
}
});
Expand All @@ -173,7 +173,7 @@ public String run() {
}
});

assertEquals("Current user within runAs() should match", "testuser", username);
assertEquals("testuser", username, "Current user within runAs() should match");
}

/**
Expand Down Expand Up @@ -211,32 +211,32 @@ public void testUserGroupNames() throws Exception {
}

private void assertUserGroup(User user, ImmutableSet<String> groups) {
assertNotNull("GroupNames should be not null", user.getGroupNames());
assertTrue("UserGroupNames length should be == " + groups.size(),
user.getGroupNames().length == groups.size());
assertNotNull(user.getGroupNames(), "GroupNames should be not null");
assertTrue(user.getGroupNames().length == groups.size(),
"UserGroupNames length should be == " + groups.size());

for (String group : user.getGroupNames()) {
assertTrue("groupName should be in set ", groups.contains(group));
assertTrue(groups.contains(group), "groupName should be in set ");
}
}

@Test
public void testSecurityForNonSecureHadoop() {
assertFalse("Security should be disable in non-secure Hadoop", User.isSecurityEnabled());
assertFalse(User.isSecurityEnabled(), "Security should be disable in non-secure Hadoop");

Configuration conf = HBaseConfiguration.create();
conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
conf.set(User.HBASE_SECURITY_CONF_KEY, "kerberos");
assertTrue("Security should be enabled", User.isHBaseSecurityEnabled(conf));
assertTrue(User.isHBaseSecurityEnabled(conf), "Security should be enabled");

conf = HBaseConfiguration.create();
conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
assertFalse("HBase security should not be enabled if " + User.HBASE_SECURITY_CONF_KEY
+ " is not set accordingly", User.isHBaseSecurityEnabled(conf));
assertFalse(User.isHBaseSecurityEnabled(conf), "HBase security should not be enabled if "
+ User.HBASE_SECURITY_CONF_KEY + " is not set accordingly");

conf = HBaseConfiguration.create();
conf.set(User.HBASE_SECURITY_CONF_KEY, "kerberos");
assertTrue("HBase security should be enabled regardless of underlying " + "HDFS settings",
User.isHBaseSecurityEnabled(conf));
assertTrue(User.isHBaseSecurityEnabled(conf),
"HBase security should be enabled regardless of underlying " + "HDFS settings");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,29 @@
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getKeytabFileForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getPrincipalForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getSecuredConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.AuthUtil;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.testclassification.SecurityTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Category({ SecurityTests.class, SmallTests.class })
@Tag(SecurityTests.TAG)
@Tag(SmallTests.TAG)
public class TestUsersOperationsWithSecureHadoop {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestUsersOperationsWithSecureHadoop.class);

private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
private static final File KEYTAB_FILE =
new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());
Expand All @@ -64,7 +59,7 @@ public class TestUsersOperationsWithSecureHadoop {

private static String OTHER_CLIENT_NAME;

@BeforeClass
@BeforeAll
public static void setUp() throws Exception {
KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
PRINCIPAL = "hbase/" + HOST;
Expand All @@ -77,7 +72,7 @@ public static void setUp() throws Exception {
HBaseKerberosUtils.setClientKeytabForTesting(KEYTAB_FILE.getAbsolutePath());
}

@AfterClass
@AfterAll
public static void tearDown() throws IOException {
if (KDC != null) {
KDC.stop();
Expand Down Expand Up @@ -110,8 +105,8 @@ public void testUserLoginInSecureHadoop() throws Exception {
User.login(conf, HBaseKerberosUtils.KRB_KEYTAB_FILE, HBaseKerberosUtils.KRB_PRINCIPAL,
"localhost");
UserGroupInformation successLogin = UserGroupInformation.getLoginUser();
assertFalse("ugi should be different in in case success login",
defaultLogin.equals(successLogin));
assertFalse(defaultLogin.equals(successLogin),
"ugi should be different in in case success login");
}

@Test
Expand All @@ -127,7 +122,7 @@ public void testLoginWithUserKeytabAndPrincipal() throws Exception {
UserGroupInformation.setConfiguration(conf);

UserProvider provider = UserProvider.instantiate(conf);
assertTrue("Client principal or keytab is empty", provider.shouldLoginFromKeytab());
assertTrue(provider.shouldLoginFromKeytab(), "Client principal or keytab is empty");

provider.login(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL);
User loginUser = provider.getCurrent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package org.apache.hadoop.hbase.security.access;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import com.google.protobuf.ServiceException;
import java.io.IOException;
Expand Down
Loading