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.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.LocalHBaseCluster;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.client.Connection;
33  import org.apache.hadoop.hbase.client.ConnectionFactory;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
36  import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
37  import org.apache.hadoop.hbase.ipc.AsyncRpcClient;
38  import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
39  import org.apache.hadoop.hbase.ipc.RpcClient;
40  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
41  import org.apache.hadoop.hbase.ipc.RpcClientImpl;
42  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
43  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos;
44  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.GetAuthenticationTokenRequest;
45  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIRequest;
46  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIResponse;
47  import org.apache.hadoop.hbase.security.AccessDeniedException;
48  import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
49  import org.apache.hadoop.hbase.testclassification.MediumTests;
50  import org.apache.hadoop.hdfs.DFSConfigKeys;
51  import org.apache.hadoop.http.HttpConfig;
52  import org.apache.hadoop.minikdc.MiniKdc;
53  import org.apache.hadoop.security.UserGroupInformation;
54  import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
55  import org.apache.hadoop.security.token.Token;
56  import org.apache.hadoop.security.token.TokenIdentifier;
57  import org.junit.AfterClass;
58  import org.junit.BeforeClass;
59  import org.junit.Test;
60  import org.junit.experimental.categories.Category;
61  
62  import com.google.protobuf.ServiceException;
63  
64  @Category(MediumTests.class)
65  public class TestGenerateDelegationToken {
66  
67    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
68  
69    private static LocalHBaseCluster CLUSTER;
70  
71    private static final File KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri()
72        .getPath());
73    private static MiniKdc KDC;
74  
75    private static String HOST = "localhost";
76  
77    private static String USERNAME;
78  
79    private static String PRINCIPAL;
80  
81    private static String HTTP_PRINCIPAL;
82  
83    private static void setHdfsSecuredConfiguration(Configuration conf) throws Exception {
84      // change XXX_USER_NAME_KEY to XXX_KERBEROS_PRINCIPAL_KEY after we drop support for hadoop-2.4.1
85      conf.set(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY, PRINCIPAL + "@" + KDC.getRealm());
86      conf.set(DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
87      conf.set(DFSConfigKeys.DFS_DATANODE_USER_NAME_KEY, PRINCIPAL + "@" + KDC.getRealm());
88      conf.set(DFSConfigKeys.DFS_DATANODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
89      conf.set(DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY, HTTP_PRINCIPAL + "@"
90          + KDC.getRealm());
91      conf.setBoolean(DFSConfigKeys.DFS_BLOCK_ACCESS_TOKEN_ENABLE_KEY, true);
92      conf.set("dfs.http.policy", HttpConfig.Policy.HTTPS_ONLY.name());
93      conf.set(DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY, "localhost:0");
94      conf.set(DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY, "localhost:0");
95  
96      File keystoresDir = new File(TEST_UTIL.getDataTestDir("keystore").toUri().getPath());
97      keystoresDir.mkdirs();
98      String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestGenerateDelegationToken.class);
99      KeyStoreTestUtil.setupSSLConfig(keystoresDir.getAbsolutePath(), sslConfDir, conf, false);
100 
101     conf.setBoolean("ignore.secure.ports.for.testing", true);
102   }
103 
104   @BeforeClass
105   public static void setUp() throws Exception {
106     Properties conf = MiniKdc.createConf();
107     conf.put(MiniKdc.DEBUG, true);
108     KDC = new MiniKdc(conf, new File(TEST_UTIL.getDataTestDir("kdc").toUri().getPath()));
109     KDC.start();
110     USERNAME = UserGroupInformation.getLoginUser().getShortUserName();
111     PRINCIPAL = USERNAME + "/" + HOST;
112     HTTP_PRINCIPAL = "HTTP/" + HOST;
113     KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, HTTP_PRINCIPAL);
114     TEST_UTIL.startMiniZKCluster();
115 
116     HBaseKerberosUtils.setKeytabFileForTesting(KEYTAB_FILE.getAbsolutePath());
117     HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm());
118     HBaseKerberosUtils.setSecuredConfiguration(TEST_UTIL.getConfiguration());
119     setHdfsSecuredConfiguration(TEST_UTIL.getConfiguration());
120     UserGroupInformation.setConfiguration(TEST_UTIL.getConfiguration());
121     TEST_UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
122       TokenProvider.class.getName());
123     TEST_UTIL.startMiniDFSCluster(1);
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 }