/
TestHiveMetaStoreAuthorizer.java
333 lines (304 loc) · 12.5 KB
/
TestHiveMetaStoreAuthorizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* 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.hive.ql.security.authorization.plugin.metastore;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.common.FileUtils;
import org.apache.hadoop.hive.metastore.ColumnType;
import org.apache.hadoop.hive.metastore.*;
import org.apache.hadoop.hive.metastore.MetaStoreTestUtils;
import org.apache.hadoop.hive.metastore.api.*;
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
import org.apache.hadoop.hive.metastore.client.builder.*;
import org.apache.hadoop.hive.metastore.events.*;
import org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals;
/*
Test whether HiveAuthorizer for MetaStore operation is trigger and HiveMetaStoreAuthzInfo is created by HiveMetaStoreAuthorizer
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestHiveMetaStoreAuthorizer {
private static final String dbName = "test";
private static final String tblName = "tmptbl";
private static final String viewName = "tmpview";
private static final String roleName = "tmpRole";
private static final String catalogName = "testCatalog";
private static final String unAuthorizedUser = "bob";
private static final String authorizedUser = "sam";
private static final String superUser = "hive";
private static final String metaConfVal = "";
private static final String TEST_DATA_DIR = new File("file:///testdata").getPath();
private RawStore rawStore;
private Configuration conf;
private HiveMetaStore.HMSHandler hmsHandler;
@Before
public void setUp() throws Exception {
conf = MetastoreConf.newMetastoreConf();
MetastoreConf.setBoolVar(conf, ConfVars.HIVE_IN_TEST, true);
MetastoreConf.setBoolVar(conf, ConfVars.HIVE_TXN_STATS_ENABLED, true);
MetastoreConf.setBoolVar(conf, ConfVars.AGGREGATE_STATS_CACHE_ENABLED, false);
MetastoreConf.setVar(conf, ConfVars.PARTITION_NAME_WHITELIST_PATTERN, metaConfVal);
MetastoreConf.setLongVar(conf, ConfVars.THRIFT_CONNECTION_RETRIES, 3);
MetastoreConf.setBoolVar(conf, ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
MetastoreConf.setVar(conf, ConfVars.HIVE_AUTHORIZATION_MANAGER, DummyHiveAuthorizerFactory.class.getName());
MetastoreConf.setVar(conf, ConfVars.PRE_EVENT_LISTENERS, HiveMetaStoreAuthorizer.class.getName());
MetastoreConf.setVar(conf, ConfVars.HIVE_METASTORE_AUTHENTICATOR_MANAGER, HadoopDefaultMetastoreAuthenticator.class.getName() );
conf.set("hadoop.proxyuser.hive.groups", "*");
conf.set("hadoop.proxyuser.hive.hosts", "*");
conf.set("hadoop.proxyuser.hive.users", "*");
MetaStoreTestUtils.setConfForStandloneMode(conf);
hmsHandler = new HiveMetaStore.HMSHandler("test", conf, true);
rawStore = new ObjectStore();
rawStore.setConf(hmsHandler.getConf());
// Create the 'hive' catalog with new warehouse directory
HiveMetaStore.HMSHandler.createDefaultCatalog(rawStore, new Warehouse(conf));
try {
hmsHandler.drop_table(dbName, tblName, true);
hmsHandler.drop_database(dbName, true, false);
hmsHandler.drop_catalog(new DropCatalogRequest(catalogName));
FileUtils.deleteDirectory(new File(TEST_DATA_DIR));
} catch (Exception e) {
// NoSuchObjectException will be ignored if the step objects are not there
}
}
@Test
public void testA_CreateDatabase_unAuthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(unAuthorizedUser));
try {
Database db = new DatabaseBuilder()
.setName(dbName)
.build(conf);
hmsHandler.create_database(db);
} catch (Exception e) {
String err = e.getMessage();
String expected = "Operation type " + HiveOperationType.CREATEDATABASE+ " not allowed for user:" + unAuthorizedUser;
assertEquals(expected, err);
}
}
@Test
public void testB_CreateTable_unAuthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(unAuthorizedUser));
try {
Table table = new TableBuilder()
.setTableName(tblName)
.addCol("name", ColumnType.STRING_TYPE_NAME)
.setOwner(unAuthorizedUser)
.build(conf);
hmsHandler.create_table(table);
} catch (Exception e) {
String err = e.getMessage();
String expected = "Operation type " + HiveOperationType.CREATETABLE + " not allowed for user:" + unAuthorizedUser;
assertEquals(expected, err);
}
}
@Test
public void testC_CreateView_anyUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Table viewObj = new TableBuilder()
.setTableName(viewName)
.setType(TableType.VIRTUAL_VIEW.name())
.addCol("name", ColumnType.STRING_TYPE_NAME)
.setOwner(authorizedUser)
.build(conf);
hmsHandler.create_table(viewObj);
} catch (Exception e) {
String err = e.getMessage();
String expected = "Operation type CREATE_VIEW not allowed for user:" + authorizedUser;
assertEquals(expected, err);
}
}
@Test
public void testD_CreateView_SuperUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(superUser));
try {
Table viewObj = new TableBuilder()
.setTableName(viewName)
.setType(TableType.VIRTUAL_VIEW.name())
.addCol("name", ColumnType.STRING_TYPE_NAME)
.build(conf);
hmsHandler.create_table(viewObj);
} catch (Exception e) {
// no Exceptions for superuser as hive is allowed CREATE_VIEW operation
}
}
@Test
public void testE_CreateRole__anyUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Role role = new RoleBuilder()
.setRoleName(roleName)
.setOwnerName(authorizedUser)
.build();
hmsHandler.create_role(role);
} catch (Exception e) {
String err = e.getMessage();
String expected = "Operation type " + PreEventContext.PreEventType.AUTHORIZATION_API_CALL.name()+ " not allowed for user:" + authorizedUser;
assertEquals(expected, err);
}
}
@Test
public void testF_CreateCatalog_anyUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Catalog catalog = new CatalogBuilder()
.setName(catalogName)
.setLocation(TEST_DATA_DIR)
.build();
hmsHandler.create_catalog(new CreateCatalogRequest(catalog));
} catch (Exception e) {
String err = e.getMessage();
String expected = "Operation type " + PreEventContext.PreEventType.CREATE_CATALOG.name()+ " not allowed for user:" + authorizedUser;
assertEquals(expected,err);
}
}
@Test
public void testG_CreateCatalog_SuperUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(superUser));
try {
Catalog catalog = new CatalogBuilder()
.setName(catalogName)
.setLocation(TEST_DATA_DIR)
.build();
hmsHandler.create_catalog(new CreateCatalogRequest(catalog));
} catch (Exception e) {
// no Exceptions for superuser as hive is allowed CREATE CATALOG operation
}
}
@Test
public void testH_CreateDatabase_authorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Database db = new DatabaseBuilder()
.setName(dbName)
.build(conf);
hmsHandler.create_database(db);
} catch (Exception e) {
// No Exception for create database for authorized user
}
}
@Test
public void testI_CreateTable_authorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Table table = new TableBuilder()
.setTableName(tblName)
.addCol("name", ColumnType.STRING_TYPE_NAME)
.setOwner(authorizedUser)
.build(conf);
hmsHandler.create_table(table);
} catch (Exception e) {
// No Exception for create table for authorized user
}
}
@Test
public void testJ_AlterTable_AuthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
Table table = new TableBuilder()
.setTableName(tblName)
.addCol("name", ColumnType.STRING_TYPE_NAME)
.setOwner(authorizedUser)
.build(conf);
hmsHandler.create_table(table);
Table alteredTable = new TableBuilder()
.addCol("dep", ColumnType.STRING_TYPE_NAME)
.build(conf);
hmsHandler.alter_table("default",tblName,alteredTable);
} catch (Exception e) {
// No Exception for create table for authorized user
}
}
@Test
public void testK_DropTable_authorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
hmsHandler.drop_table(dbName,tblName,true);
} catch (Exception e) {
// No Exception for create table for authorized user
}
}
@Test
public void testL_DropDatabase_authorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
hmsHandler.drop_database(dbName,true,true);
} catch (Exception e) {
// No Exception for dropDatabase for authorized user
}
}
@Test
public void testM_DropCatalog_SuperUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(superUser));
try {
hmsHandler.drop_catalog(new DropCatalogRequest(catalogName));
} catch (Exception e) {
// no Exceptions for superuser as hive is allowed CREATE CATALOG operation
}
}
@Test
public void testNShowDatabaseAuthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
hmsHandler.get_all_databases();
} catch (Exception e) {
// no Exceptions for show database as authorized user.
}
}
@Test
public void testOShowDatabaseUnauthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(unAuthorizedUser));
try {
hmsHandler.get_all_databases();
} catch (Exception e) {
String err = e.getMessage();
if (StringUtils.isNotEmpty(err)) {
assert(true);
}
}
}
@Test
public void testPShowTablesAuthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(authorizedUser));
try {
hmsHandler.get_all_tables("default");
} catch (Exception e) {
// no Exceptions for show tables as authorized user.
}
}
@Test
public void testQShowTablesUnauthorizedUser() throws Exception {
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser(unAuthorizedUser));
try {
hmsHandler.get_all_tables("default");
} catch (Exception e) {
String err = e.getMessage();
if (StringUtils.isNotEmpty(err)) {
assert(true);
}
}
}
}