Skip to content

Commit 7dec301

Browse files
author
Ramesh Mani
committed
RANGER-3474:RangerHivePlugin enhancement to handle new Hive commands
1 parent 871b0dc commit 7dec301

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

hive-agent/src/main/java/org/apache/ranger/authorization/hive/authorizer/RangerHiveAuthorizer.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,14 @@ public void checkPrivileges(HiveOperationType hiveOpType,
905905
//
906906
RangerHiveAccessRequest request = new RangerHiveAccessRequest(resource, user, groups, roles, hiveOpType.name(), HiveAccessType.REPLADMIN, context, sessionContext);
907907
requests.add(request);
908+
} else if (hiveOpType.equals(HiveOperationType.ALTERTABLE_OWNER)) {
909+
RangerHiveAccessRequest request = buildRequestForAlterTableSetOwnerFromCommandString(user, groups, roles, hiveOpType.name(), context, sessionContext);
910+
if (request != null) {
911+
requests.add(request);
912+
} else {
913+
throw new HiveAccessControlException(String.format("Permission denied: user [%s] does not have privilege for [%s] command",
914+
user, hiveOpType.name()));
915+
}
908916
} else {
909917
if (LOG.isDebugEnabled()) {
910918
LOG.debug("RangerHiveAuthorizer.checkPrivileges: Unexpected operation type[" + hiveOpType + "] received with empty input objects list!");
@@ -3079,6 +3087,28 @@ private RangerRole getRangerRoleForRoleName(String roleName) {
30793087
}
30803088
return ret;
30813089
}
3090+
3091+
private RangerHiveAccessRequest buildRequestForAlterTableSetOwnerFromCommandString(String user,
3092+
Set<String> userGroups,
3093+
Set<String> userRoles,
3094+
String hiveOpTypeName,
3095+
HiveAuthzContext context,
3096+
HiveAuthzSessionContext sessionContext) {
3097+
RangerHiveResource resource = null;
3098+
RangerHiveAccessRequest request = null;
3099+
HiveObj hiveObj = new HiveObj();
3100+
hiveObj.fetchHiveObjForAlterTable(context);
3101+
String dbName = hiveObj.getDatabaseName();
3102+
String tableName = hiveObj.getTableName();
3103+
if (LOG.isDebugEnabled()) {
3104+
LOG.debug("Database: " + dbName + " Table: " + tableName);
3105+
}
3106+
if (dbName != null && tableName != null) {
3107+
resource = new RangerHiveResource(HiveObjectType.TABLE, dbName, tableName);
3108+
request = new RangerHiveAccessRequest(resource, user, userGroups, userRoles, hiveOpTypeName, HiveAccessType.ALTER, context, sessionContext);
3109+
}
3110+
return request;
3111+
}
30823112
}
30833113

30843114
enum HiveObjectType { NONE, DATABASE, TABLE, VIEW, PARTITION, INDEX, COLUMN, FUNCTION, URI, SERVICE_NAME, GLOBAL };
@@ -3088,6 +3118,8 @@ class HiveObj {
30883118
String databaseName;
30893119
String tableName;
30903120

3121+
HiveObj() {}
3122+
30913123
HiveObj(HiveAuthzContext context) {
30923124
fetchHiveObj(context);
30933125
}
@@ -3120,6 +3152,29 @@ private void fetchHiveObj(HiveAuthzContext context) {
31203152
}
31213153
}
31223154

3155+
public void fetchHiveObjForAlterTable(HiveAuthzContext context) {
3156+
// cmd passed: Alter Table <database.tableName or tableName> set owner user|role <user_or_role>
3157+
if (context != null) {
3158+
String cmdString = context.getCommandString();
3159+
if (cmdString != null) {
3160+
String[] cmd = cmdString.trim().split("\\s+");
3161+
if (!ArrayUtils.isEmpty(cmd) && cmd.length > 2) {
3162+
tableName = cmd[2];
3163+
if (tableName.contains(".")) {
3164+
String[] result = splitDBName(tableName);
3165+
databaseName = result[0];
3166+
tableName = result[1];
3167+
} else {
3168+
SessionState sessionState = SessionState.get();
3169+
if (sessionState != null) {
3170+
databaseName = sessionState.getCurrentDatabase();
3171+
}
3172+
}
3173+
}
3174+
}
3175+
}
3176+
}
3177+
31233178
private String[] splitDBName(String dbName) {
31243179
String[] ret = null;
31253180
ret = dbName.split("\\.");

0 commit comments

Comments
 (0)