View Javadoc

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.security.token;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.Properties;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.LocalHBaseCluster;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.client.Connection;
34  import org.apache.hadoop.hbase.client.ConnectionFactory;
35  import org.apache.hadoop.hbase.client.Table;
36  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
37  import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
38  import org.apache.hadoop.hbase.ipc.AsyncRpcClient;
39  import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
40  import org.apache.hadoop.hbase.ipc.RpcClient;
41  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
42  import org.apache.hadoop.hbase.ipc.RpcClientImpl;
43  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
44  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos;
45  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.GetAuthenticationTokenRequest;
46  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIRequest;
47  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIResponse;
48  import org.apache.hadoop.hbase.security.AccessDeniedException;
49  import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
50  import org.apache.hadoop.hbase.testclassification.MediumTests;
51  import org.apache.hadoop.hbase.util.FSUtils;
52  import org.apache.hadoop.hdfs.DFSConfigKeys;
53  import org.apache.hadoop.http.HttpConfig;
54  import org.apache.hadoop.minikdc.MiniKdc;
55  import org.apache.hadoop.security.UserGroupInformation;
56  import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
57  import org.apache.hadoop.security.token.Token;
58  import org.apache.hadoop.security.token.TokenIdentifier;
59  import org.junit.AfterClass;
60  import org.junit.BeforeClass;
61  import org.junit.Test;
62  import org.junit.experimental.categories.Category;
63  
64  import com.google.protobuf.ServiceException;
65  
66  @Category(MediumTests.class)
67  public class TestGenerateDelegationToken {
68  
69    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70  
71    private static LocalHBaseCluster CLUSTER;
72  
73    private static final File KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri()
74        .getPath());
75    private static MiniKdc KDC;
76  
77    private static String HOST = "localhost";
78  
79    private static String USERNAME;
80  
81    private static String PRINCIPAL;
82  
83    private static String HTTP_PRINCIPAL;
84  
85    private static void setHdfsSecuredConfiguration(Configuration conf) throws Exception {
86      // change XXX_USER_NAME_KEY to XXX_KERBEROS_PRINCIPAL_KEY after we drop support for hadoop-2.4.1
87      conf.set(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY, PRINCIPAL + "@" + KDC.getRealm());
88      conf.set(DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
89      conf.set(DFSConfigKeys.DFS_DATANODE_USER_NAME_KEY, PRINCIPAL + "@" + KDC.getRealm());
90      conf.set(DFSConfigKeys.DFS_DATANODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
91      conf.set(DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY, HTTP_PRINCIPAL + "@"
92          + KDC.getRealm());
93      conf.setBoolean(DFSConfigKeys.DFS_BLOCK_ACCESS_TOKEN_ENABLE_KEY, true);
94      conf.set("dfs.http.policy", HttpConfig.Policy.HTTPS_ONLY.name());
95      conf.set(DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY, "localhost:0");
96      conf.set(DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY, "localhost:0");
97  
98      File keystoresDir = new File(TEST_UTIL.getDataTestDir("keystore").toUri().getPath());
99      keystoresDir.mkdirs();
100     String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestGenerateDelegationToken.class);
101     KeyStoreTestUtil.setupSSLConfig(keystoresDir.getAbsolutePath(), sslConfDir, conf, false);
102 
103     conf.setBoolean("ignore.secure.ports.for.testing", true);
104   }
105 
106   @BeforeClass
107   public static void setUp() throws Exception {
108     KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
109     USERNAME = UserGroupInformation.getLoginUser().getShortUserName();
110     PRINCIPAL = USERNAME + "/" + HOST;
111     HTTP_PRINCIPAL = "HTTP/" + HOST;
112     KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, HTTP_PRINCIPAL);
113     TEST_UTIL.startMiniZKCluster();
114 
115     HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm());
116     HBaseKerberosUtils.setSecuredConfiguration(TEST_UTIL.getConfiguration());
117     setHdfsSecuredConfiguration(TEST_UTIL.getConfiguration());
118     UserGroupInformation.setConfiguration(TEST_UTIL.getConfiguration());
119     TEST_UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
120       TokenProvider.class.getName());
121     TEST_UTIL.startMiniDFSCluster(1);
122     Path rootdir = TEST_UTIL.getDataTestDirOnTestFS("TestGenerateDelegationToken");
123     FSUtils.setRootDir(TEST_UTIL.getConfiguration(), rootdir);
124     CLUSTER = new LocalHBaseCluster(TEST_UTIL.getConfiguration(), 1);
125     CLUSTER.startup();
126   }
127 
128   @AfterClass
129   public static void tearDown() throws Exception {
130     if (CLUSTER != null) {
131       CLUSTER.shutdown();
132     }
133     CLUSTER.join();
134     if (KDC != null) {
135       KDC.stop();
136     }
137     TEST_UTIL.shutdownMiniCluster();
138   }
139 
140   private void testTokenAuth(Class<? extends RpcClient> rpcImplClass) throws IOException,
141       ServiceException {
142     TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
143       rpcImplClass.getName());
144     try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
145         Table table = conn.getTable(TableName.META_TABLE_NAME)) {
146       CoprocessorRpcChannel rpcChannel = table.coprocessorService(HConstants.EMPTY_START_ROW);
147       AuthenticationProtos.AuthenticationService.BlockingInterface service =
148           AuthenticationProtos.AuthenticationService.newBlockingStub(rpcChannel);
149       WhoAmIResponse response = service.whoAmI(null, WhoAmIRequest.getDefaultInstance());
150       assertEquals(USERNAME, response.getUsername());
151       assertEquals(AuthenticationMethod.TOKEN.name(), response.getAuthMethod());
152       try {
153         service.getAuthenticationToken(null, GetAuthenticationTokenRequest.getDefaultInstance());
154       } catch (ServiceException e) {
155         AccessDeniedException exc = (AccessDeniedException) ProtobufUtil.getRemoteException(e);
156         assertTrue(exc.getMessage().contains(
157           "Token generation only allowed for Kerberos authenticated clients"));
158       }
159     }
160   }
161 
162   @Test
163   public void test() throws Exception {
164     try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
165       Token<? extends TokenIdentifier> token = TokenUtil.obtainToken(conn);
166       UserGroupInformation.getCurrentUser().addToken(token);
167       testTokenAuth(RpcClientImpl.class);
168       testTokenAuth(AsyncRpcClient.class);
169     }
170 
171   }
172 }