Skip to content

Commit 3598bd1

Browse files
committed
Classify HiveAccessControlException as ForbiddenException
1 parent f4c9685 commit 3598bd1

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveClientPool.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.hadoop.hive.metastore.api.MetaException;
2929
import org.apache.iceberg.ClientPoolImpl;
3030
import org.apache.iceberg.common.DynMethods;
31+
import org.apache.iceberg.exceptions.ForbiddenException;
3132
import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
3233
import org.apache.thrift.TException;
3334
import org.apache.thrift.transport.TTransportException;
@@ -75,6 +76,31 @@ protected IMetaStoreClient newClient() {
7576
}
7677
}
7778

79+
@Override
80+
public <R> R run(Action<R, IMetaStoreClient, TException> action) throws TException, InterruptedException {
81+
try {
82+
return super.run(action);
83+
} catch (MetaException e) {
84+
if (isAccessControlException(e)) {
85+
throw new ForbiddenException(e, "Access denied: %s", e.getMessage());
86+
}
87+
throw e;
88+
}
89+
}
90+
91+
@Override
92+
public <R> R run(Action<R, IMetaStoreClient, TException> action, boolean retry)
93+
throws TException, InterruptedException {
94+
try {
95+
return super.run(action, retry);
96+
} catch (MetaException e) {
97+
if (isAccessControlException(e)) {
98+
throw new ForbiddenException(e, "Access denied: %s", e.getMessage());
99+
}
100+
throw e;
101+
}
102+
}
103+
78104
@Override
79105
protected IMetaStoreClient reconnect(IMetaStoreClient client) {
80106
try {
@@ -92,6 +118,11 @@ protected boolean isConnectionException(Exception e) {
92118
e.getMessage().contains("Got exception: org.apache.thrift.transport.TTransportException");
93119
}
94120

121+
private boolean isAccessControlException(MetaException exception) {
122+
return exception.getMessage() != null && exception.getMessage().startsWith(
123+
"Got exception: org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException");
124+
}
125+
95126
@Override
96127
protected void close(IMetaStoreClient client) {
97128
client.close();

ql/src/java/org/apache/hadoop/hive/ql/security/authorization/plugin/metastore/HiveMetaStoreAuthorizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public final void onEvent(PreEventContext preEventContext)
136136
}
137137
} catch (Exception e) {
138138
LOG.error("HiveMetaStoreAuthorizer.onEvent(): failed", e);
139-
throw MetaStoreUtils.newMetaException(e);
139+
MetaStoreUtils.throwMetaException(e);
140140
}
141141

142142
LOG.debug("<== HiveMetaStoreAuthorizer.onEvent(): EventType=" + preEventContext.getEventType());

ql/src/test/org/apache/hadoop/hive/ql/security/authorization/plugin/metastore/TestHiveMetaStoreAuthorizer.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ public void testA_CreateDatabase_unAuthorizedUser() throws Exception {
204204
hmsHandler.create_database(db);
205205
} catch (Exception e) {
206206
String err = e.getMessage();
207-
String expected = "Operation type " + HiveOperationType.CREATEDATABASE + " not allowed for user:" + unAuthorizedUser;
207+
String expected = "Got exception: org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzPluginException Operation type "
208+
+ HiveOperationType.CREATEDATABASE + " not allowed for user:" + unAuthorizedUser;
208209
assertEquals(expected, err);
209210
}
210211
}
@@ -221,7 +222,8 @@ public void testB_CreateTable_unAuthorizedUser() throws Exception {
221222
hmsHandler.create_table(table);
222223
} catch (Exception e) {
223224
String err = e.getMessage();
224-
String expected = "Operation type " + HiveOperationType.CREATETABLE + " not allowed for user:" + unAuthorizedUser;
225+
String expected = "Got exception: org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzPluginException Operation type "
226+
+ HiveOperationType.CREATETABLE + " not allowed for user:" + unAuthorizedUser;
225227
assertEquals(expected, err);
226228
}
227229
}
@@ -297,7 +299,8 @@ public void testE_CreateRole__anyUser() throws Exception {
297299
hmsHandler.create_role(role);
298300
} catch (Exception e) {
299301
String err = e.getMessage();
300-
String expected = "Operation type " + PreEventContext.PreEventType.AUTHORIZATION_API_CALL.name() + " not allowed for user:" + authorizedUser;
302+
String expected = "Got exception: org.apache.hadoop.hive.metastore.api.MetaException Operation type "
303+
+ PreEventContext.PreEventType.AUTHORIZATION_API_CALL.name() + " not allowed for user:" + authorizedUser;
301304
assertEquals(expected, err);
302305
}
303306
}
@@ -313,7 +316,8 @@ public void testF_CreateCatalog_anyUser() throws Exception {
313316
hmsHandler.create_catalog(new CreateCatalogRequest(catalog));
314317
} catch (Exception e) {
315318
String err = e.getMessage();
316-
String expected = "Operation type " + PreEventContext.PreEventType.CREATE_CATALOG.name() + " not allowed for user:" + authorizedUser;
319+
String expected = "Got exception: org.apache.hadoop.hive.metastore.api.MetaException Operation type "
320+
+ PreEventContext.PreEventType.CREATE_CATALOG.name() + " not allowed for user:" + authorizedUser;
317321
assertEquals(expected, err);
318322
}
319323
}
@@ -658,7 +662,8 @@ public void testR_CreateDataConnector_unAuthorizedUser() {
658662
hmsHandler.create_dataconnector_req(connectorReq);
659663
} catch (Exception e) {
660664
String err = e.getMessage();
661-
String expected = "Operation type " + HiveOperationType.CREATEDATACONNECTOR + " not allowed for user:" + unAuthorizedUser;
665+
String expected = "Got exception: org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzPluginException Operation type "
666+
+ HiveOperationType.CREATEDATACONNECTOR + " not allowed for user:" + unAuthorizedUser;
662667
assertEquals(expected, err);
663668
}
664669
}

0 commit comments

Comments
 (0)