SQOOP-3309: Implement HiveServer2 client
[sqoop.git] / src / test / org / apache / sqoop / hive / minicluster / PasswordAuthenticationConfiguration.java
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
19 package org.apache.sqoop.hive.minicluster;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.hive.service.auth.HiveAuthFactory;
23 import org.apache.hive.service.auth.PasswdAuthenticationProvider;
24 import org.apache.sqoop.db.JdbcConnectionFactory;
25
26 import javax.security.sasl.AuthenticationException;
27 import java.security.PrivilegedAction;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION;
32 import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.HIVE_SERVER2_CUSTOM_AUTHENTICATION_CLASS;
33
34 public class PasswordAuthenticationConfiguration implements AuthenticationConfiguration {
35
36 private static String TEST_USERNAME;
37
38 private static String TEST_PASSWORD;
39
40 private static final class TestPasswordAuthenticationProvider implements PasswdAuthenticationProvider {
41
42 @Override
43 public void Authenticate(String user, String password) throws AuthenticationException {
44 if (!(TEST_USERNAME.equals(user) && TEST_PASSWORD.equals(password))) {
45 throw new AuthenticationException("Authentication failed!");
46 }
47 }
48 }
49
50 public PasswordAuthenticationConfiguration(String testUsername, String testPassword) {
51 TEST_USERNAME = testUsername;
52 TEST_PASSWORD = testPassword;
53 }
54
55 @Override
56 public Map<String, String> getAuthenticationConfig() {
57 Map<String, String> result = new HashMap<>();
58 result.put(HIVE_SERVER2_AUTHENTICATION.varname, HiveAuthFactory.AuthTypes.CUSTOM.getAuthName());
59 result.put(HIVE_SERVER2_CUSTOM_AUTHENTICATION_CLASS.varname, TestPasswordAuthenticationProvider.class.getName());
60
61 return result;
62 }
63
64 @Override
65 public String getUrlParams() {
66 return StringUtils.EMPTY;
67 }
68
69 @Override
70 public <T> T doAsAuthenticated(PrivilegedAction<T> action) {
71 return action.run();
72 }
73
74 @Override
75 public void init() {
76 //do nothing
77 }
78
79 @Override
80 public JdbcConnectionFactory decorateConnectionFactory(JdbcConnectionFactory connectionFactory) {
81 return connectionFactory;
82 }
83 }