Reidddddd commented on a change in pull request #884: HBASE-23347 Allowable custom authentication methods for RPCs URL: https://github.com/apache/hbase/pull/884#discussion_r358576260
########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/security/provider/TestCustomSaslAuthenticationProvider.java ########## @@ -0,0 +1,539 @@ +/* + * 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.security.provider; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutput; +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; +import java.security.PrivilegedExceptionAction; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.sasl.AuthorizeCallback; +import javax.security.sasl.RealmCallback; +import javax.security.sasl.RealmChoiceCallback; +import javax.security.sasl.Sasl; +import javax.security.sasl.SaslClient; +import javax.security.sasl.SaslServer; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.LocalHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.client.RetriesExhaustedException; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; +import org.apache.hadoop.hbase.ipc.BlockingRpcClient; +import org.apache.hadoop.hbase.ipc.RpcClientFactory; +import org.apache.hadoop.hbase.ipc.RpcServerFactory; +import org.apache.hadoop.hbase.ipc.SimpleRpcServer; +import org.apache.hadoop.hbase.security.AccessDeniedException; +import org.apache.hadoop.hbase.security.HBaseKerberosUtils; +import org.apache.hadoop.hbase.security.SaslUtil; +import org.apache.hadoop.hbase.security.SecurityInfo; +import org.apache.hadoop.hbase.security.token.SecureTestCluster; +import org.apache.hadoop.hbase.security.token.TokenProvider; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.SecurityTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.FSUtils; +import org.apache.hadoop.hbase.util.Pair; +import org.apache.hadoop.io.Text; +import org.apache.hadoop.io.WritableUtils; +import org.apache.hadoop.minikdc.MiniKdc; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; +import org.apache.hadoop.security.token.SecretManager; +import org.apache.hadoop.security.token.SecretManager.InvalidToken; +import org.apache.hadoop.security.token.Token; +import org.apache.hadoop.security.token.TokenIdentifier; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.UserInformation; + +/** + * Tests the pluggable authentication framework with SASL using a contrived authentication system. + * + * This tests holds a "user database" in memory as a hashmap. Clients provide their password + * in the client Hadoop configuration. The servers validate this password via the "user database". + */ +@Category({MediumTests.class, SecurityTests.class}) +public class TestCustomSaslAuthenticationProvider { + private static final Logger LOG = LoggerFactory.getLogger( + TestCustomSaslAuthenticationProvider.class); + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestCustomSaslAuthenticationProvider.class); + + private static final Map<String,String> USER_DATABASE = createUserDatabase(); + + private static final String USER1_PASSWORD = "foobarbaz"; + private static final String USER2_PASSWORD = "bazbarfoo"; + + private static Map<String,String> createUserDatabase() { + Map<String,String> db = new ConcurrentHashMap<>(); + db.put("user1", USER1_PASSWORD); + db.put("user2", USER2_PASSWORD); + return db; + } + + public static String getPassword(String user) { + String password = USER_DATABASE.get(user); + if (password == null) { + throw new IllegalStateException("Cannot request password for a user that doesn't exist"); + } + return password; + } + + /** + * A custom tokenidentifier for our custom auth'n method. Unique from the TokenIdentifier + * used for delegation tokens. + */ + public static class PasswordAuthTokenIdentifier extends TokenIdentifier { + public static final Text PASSWORD_AUTH_TOKEN = new Text("HBASE_PASSWORD_TEST_TOKEN"); + private String username; + + public PasswordAuthTokenIdentifier() {} + + public PasswordAuthTokenIdentifier(String username) { + this.username = username; + } + + @Override + public void readFields(DataInput in) throws IOException { + this.username = WritableUtils.readString(in); + } + + @Override + public void write(DataOutput out) throws IOException { + WritableUtils.writeString(out, username); + } + + @Override + public Text getKind() { + return PASSWORD_AUTH_TOKEN; + } + + @Override + public UserGroupInformation getUser() { + if (username == null || "".equals(username)) { + return null; + } + return UserGroupInformation.createRemoteUser(username); + } + } + + public static Token<? extends TokenIdentifier> createPasswordToken( + String username, String password, String clusterId) { + PasswordAuthTokenIdentifier id = new PasswordAuthTokenIdentifier(username); + Token<? extends TokenIdentifier> token = new Token<>(id.getBytes(), Bytes.toBytes(password), + id.getKind(), new Text(clusterId)); + return token; + } + + /** + * Client provider that finds custom Token in the user's UGI and authenticates with the server + * via DIGEST-MD5 using that password. + */ + public static class InMemoryClientProvider extends AbstractSaslClientAuthenticationProvider { + public static final String MECHANISM = "DIGEST-MD5"; + public static final SaslAuthMethod SASL_AUTH_METHOD = new SaslAuthMethod( + "IN_MEMORY", (byte)42, MECHANISM, AuthenticationMethod.TOKEN); + + @Override + public SaslClient createClient(Configuration conf, InetAddress serverAddr, + SecurityInfo securityInfo, Token<? extends TokenIdentifier> token, boolean fallbackAllowed, + Map<String, String> saslProps) throws IOException { + return Sasl.createSaslClient(new String[] { MECHANISM }, null, null, + SaslUtil.SASL_DEFAULT_REALM, saslProps, new InMemoryClientProviderCallbackHandler(token)); Review comment: Possibly, the failure mentioned in comment (https://github.com/apache/hbase/pull/884#discussion_r358563326) is because of this SASL_DEFAULT_REALM? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
