use of org.apache.hadoop.hive.metastore.api.PrincipalType in project hive by apache.
the class HiveAuthorizationTaskFactoryImpl method createShowRoleGrantTask.
@Override
public Task<?> createShowRoleGrantTask(ASTNode ast, Path resultFile, Set<ReadEntity> inputs, Set<WriteEntity> outputs) {
ASTNode child = (ASTNode) ast.getChild(0);
PrincipalType principalType = PrincipalType.USER;
switch(child.getType()) {
case HiveParser.TOK_USER:
principalType = PrincipalType.USER;
break;
case HiveParser.TOK_GROUP:
principalType = PrincipalType.GROUP;
break;
case HiveParser.TOK_ROLE:
principalType = PrincipalType.ROLE;
break;
}
String principalName = BaseSemanticAnalyzer.unescapeIdentifier(child.getChild(0).getText());
ShowRoleGrantDesc showRoleGrantDesc = new ShowRoleGrantDesc(principalName, principalType, resultFile.toString());
return TaskFactory.get(new DDLWork(inputs, outputs, showRoleGrantDesc));
}
use of org.apache.hadoop.hive.metastore.api.PrincipalType in project hive by apache.
the class MetaStoreDirectSql method getTableAllColumnGrants.
public List<HiveObjectPrivilege> getTableAllColumnGrants(String catName, String dbName, String tableName, String authorizer) throws MetaException {
// These constants should match the SELECT clause of the query.
final int authorizerIndex = 0;
final int columnNameIndex = 1;
final int createTimeIndex = 2;
final int grantOptionIndex = 3;
final int grantorIndex = 4;
final int grantorTypeIndex = 5;
final int principalNameIndex = 6;
final int principalTypeIndex = 7;
final int privilegeIndex = 8;
// Retrieve the privileges from the object store. Just grab only the required fields.
String queryText = "select " + TBL_COL_PRIVS + ".\"AUTHORIZER\", " + TBL_COL_PRIVS + ".\"COLUMN_NAME\", " + TBL_COL_PRIVS + ".\"CREATE_TIME\", " + TBL_COL_PRIVS + ".\"GRANT_OPTION\", " + TBL_COL_PRIVS + ".\"GRANTOR\", " + TBL_COL_PRIVS + ".\"GRANTOR_TYPE\", " + TBL_COL_PRIVS + ".\"PRINCIPAL_NAME\", " + TBL_COL_PRIVS + ".\"PRINCIPAL_TYPE\", " + TBL_COL_PRIVS + ".\"TBL_COL_PRIV\" " + "FROM " + TBL_COL_PRIVS + " JOIN " + TBLS + " ON " + TBL_COL_PRIVS + ".\"TBL_ID\" = " + TBLS + ".\"TBL_ID\"" + " JOIN " + DBS + " ON " + TBLS + ".\"DB_ID\" = " + DBS + ".\"DB_ID\" " + " WHERE " + TBLS + ".\"TBL_NAME\" = ?" + " AND " + DBS + ".\"NAME\" = ?" + " AND " + DBS + ".\"CTLG_NAME\" = ?";
// Build the parameters, they should match the WHERE clause of the query.
int numParams = authorizer != null ? 4 : 3;
Object[] params = new Object[numParams];
params[0] = tableName;
params[1] = dbName;
params[2] = catName;
if (authorizer != null) {
queryText = queryText + " AND " + TBL_COL_PRIVS + ".\"AUTHORIZER\" = ?";
params[3] = authorizer;
}
// Collect the results into a list that the caller can consume.
List<HiveObjectPrivilege> result = new ArrayList<>();
final boolean doTrace = LOG.isDebugEnabled();
long start = doTrace ? System.nanoTime() : 0;
try (QueryWrapper query = new QueryWrapper(pm.newQuery("javax.jdo.query.SQL", queryText))) {
List<Object[]> queryResult = MetastoreDirectSqlUtils.ensureList(executeWithArray(query, params, queryText));
long end = doTrace ? System.nanoTime() : 0;
MetastoreDirectSqlUtils.timingTrace(doTrace, queryText, start, end);
// If there is some result convert it into HivePrivilege bag and return.
for (Object[] privLine : queryResult) {
String privAuthorizer = MetastoreDirectSqlUtils.extractSqlString(privLine[authorizerIndex]);
String principalName = MetastoreDirectSqlUtils.extractSqlString(privLine[principalNameIndex]);
PrincipalType ptype = PrincipalType.valueOf(MetastoreDirectSqlUtils.extractSqlString(privLine[principalTypeIndex]));
String columnName = MetastoreDirectSqlUtils.extractSqlString(privLine[columnNameIndex]);
String privilege = MetastoreDirectSqlUtils.extractSqlString(privLine[privilegeIndex]);
int createTime = MetastoreDirectSqlUtils.extractSqlInt(privLine[createTimeIndex]);
String grantor = MetastoreDirectSqlUtils.extractSqlString(privLine[grantorIndex]);
PrincipalType grantorType = PrincipalType.valueOf(MetastoreDirectSqlUtils.extractSqlString(privLine[grantorTypeIndex]));
boolean grantOption = MetastoreDirectSqlUtils.extractSqlBoolean(privLine[grantOptionIndex]);
HiveObjectRef objectRef = new HiveObjectRef(HiveObjectType.COLUMN, dbName, tableName, null, columnName);
objectRef.setCatName(catName);
PrivilegeGrantInfo grantInfo = new PrivilegeGrantInfo(privilege, createTime, grantor, grantorType, grantOption);
result.add(new HiveObjectPrivilege(objectRef, principalName, ptype, grantInfo, privAuthorizer));
}
}
return result;
}
use of org.apache.hadoop.hive.metastore.api.PrincipalType in project hive by apache.
the class ObjectStore method convertTableCols.
private List<HiveObjectPrivilege> convertTableCols(List<MTableColumnPrivilege> privs) {
List<HiveObjectPrivilege> result = new ArrayList<>();
for (MTableColumnPrivilege priv : privs) {
String pname = priv.getPrincipalName();
String authorizer = priv.getAuthorizer();
PrincipalType ptype = PrincipalType.valueOf(priv.getPrincipalType());
MTable mtable = priv.getTable();
MDatabase mdatabase = mtable.getDatabase();
HiveObjectRef objectRef = new HiveObjectRef(HiveObjectType.COLUMN, mdatabase.getName(), mtable.getTableName(), null, priv.getColumnName());
objectRef.setCatName(mdatabase.getCatalogName());
PrivilegeGrantInfo grantor = new PrivilegeGrantInfo(priv.getPrivilege(), priv.getCreateTime(), priv.getGrantor(), PrincipalType.valueOf(priv.getGrantorType()), priv.getGrantOption());
result.add(new HiveObjectPrivilege(objectRef, pname, ptype, grantor, authorizer));
}
return result;
}
use of org.apache.hadoop.hive.metastore.api.PrincipalType in project hive by apache.
the class TestHiveMetaStore method testSimpleFunction.
@Test
public void testSimpleFunction() throws Exception {
String dbName = "test_db";
String funcName = "test_func";
String className = "org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper";
String owner = "test_owner";
final int N_FUNCTIONS = 5;
PrincipalType ownerType = PrincipalType.USER;
int createTime = (int) (System.currentTimeMillis() / 1000);
FunctionType funcType = FunctionType.JAVA;
try {
cleanUp(dbName, null, null);
for (Function f : client.getAllFunctions().getFunctions()) {
client.dropFunction(f.getDbName(), f.getFunctionName());
}
createDb(dbName);
for (int i = 0; i < N_FUNCTIONS; i++) {
createFunction(dbName, funcName + "_" + i, className, owner, ownerType, createTime, funcType, null);
}
// Try the different getters
// getFunction()
Function func = client.getFunction(dbName, funcName + "_0");
assertEquals("function db name", dbName, func.getDbName());
assertEquals("function name", funcName + "_0", func.getFunctionName());
assertEquals("function class name", className, func.getClassName());
assertEquals("function owner name", owner, func.getOwnerName());
assertEquals("function owner type", PrincipalType.USER, func.getOwnerType());
assertEquals("function type", funcType, func.getFunctionType());
List<ResourceUri> resources = func.getResourceUris();
assertTrue("function resources", resources == null || resources.size() == 0);
boolean gotException = false;
try {
func = client.getFunction(dbName, "nonexistent_func");
} catch (NoSuchObjectException e) {
// expected failure
gotException = true;
}
assertEquals(true, gotException);
// getAllFunctions()
GetAllFunctionsResponse response = client.getAllFunctions();
List<Function> allFunctions = response.getFunctions();
assertEquals(N_FUNCTIONS, allFunctions.size());
assertEquals(funcName + "_3", allFunctions.get(3).getFunctionName());
// getFunctions()
List<String> funcs = client.getFunctions(dbName, "*_func_*");
assertEquals(N_FUNCTIONS, funcs.size());
assertEquals(funcName + "_0", funcs.get(0));
funcs = client.getFunctions(dbName, "nonexistent_func");
assertEquals(0, funcs.size());
// dropFunction()
for (int i = 0; i < N_FUNCTIONS; i++) {
client.dropFunction(dbName, funcName + "_" + i);
}
// Confirm that the function is now gone
funcs = client.getFunctions(dbName, funcName);
assertEquals(0, funcs.size());
response = client.getAllFunctions();
allFunctions = response.getFunctions();
assertEquals(0, allFunctions.size());
} catch (Exception e) {
System.err.println(StringUtils.stringifyException(e));
System.err.println("testConcurrentMetastores() failed.");
throw e;
} finally {
silentDropDatabase(dbName);
}
}
use of org.apache.hadoop.hive.metastore.api.PrincipalType in project hive by apache.
the class ObjectStore method grantPrivileges.
@Override
public boolean grantPrivileges(PrivilegeBag privileges) throws InvalidObjectException, MetaException, NoSuchObjectException {
boolean committed = false;
int now = (int) (System.currentTimeMillis() / 1000);
try {
openTransaction();
List<Object> persistentObjs = new ArrayList<>();
List<HiveObjectPrivilege> privilegeList = privileges.getPrivileges();
if (CollectionUtils.isNotEmpty(privilegeList)) {
Iterator<HiveObjectPrivilege> privIter = privilegeList.iterator();
Set<String> privSet = new HashSet<>();
while (privIter.hasNext()) {
HiveObjectPrivilege privDef = privIter.next();
HiveObjectRef hiveObject = privDef.getHiveObject();
String privilegeStr = privDef.getGrantInfo().getPrivilege();
String[] privs = privilegeStr.split(",");
String userName = privDef.getPrincipalName();
String authorizer = privDef.getAuthorizer();
PrincipalType principalType = privDef.getPrincipalType();
String grantor = privDef.getGrantInfo().getGrantor();
String grantorType = privDef.getGrantInfo().getGrantorType().toString();
boolean grantOption = privDef.getGrantInfo().isGrantOption();
privSet.clear();
if (principalType == PrincipalType.ROLE) {
validateRole(userName);
}
String catName = hiveObject.isSetCatName() ? hiveObject.getCatName() : getDefaultCatalog(conf);
if (hiveObject.getObjectType() == HiveObjectType.GLOBAL) {
List<MGlobalPrivilege> globalPrivs = this.listPrincipalMGlobalGrants(userName, principalType, authorizer);
for (MGlobalPrivilege priv : globalPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted by " + grantor);
}
MGlobalPrivilege mGlobalPrivs = new MGlobalPrivilege(userName, principalType.toString(), privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mGlobalPrivs);
}
} else if (hiveObject.getObjectType() == HiveObjectType.DATABASE) {
MDatabase dbObj = getMDatabase(catName, hiveObject.getDbName());
List<MDBPrivilege> dbPrivs = this.listPrincipalMDBGrants(userName, principalType, catName, hiveObject.getDbName(), authorizer);
for (MDBPrivilege priv : dbPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on database " + hiveObject.getDbName() + " by " + grantor);
}
MDBPrivilege mDb = new MDBPrivilege(userName, principalType.toString(), dbObj, privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mDb);
}
} else if (hiveObject.getObjectType() == HiveObjectType.DATACONNECTOR) {
MDataConnector dcObj = getMDataConnector(hiveObject.getObjectName());
List<MDCPrivilege> dcPrivs = this.listPrincipalMDCGrants(userName, principalType, hiveObject.getObjectName(), authorizer);
for (MDCPrivilege priv : dcPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on data connector " + hiveObject.getDbName() + " by " + grantor);
}
MDCPrivilege mDc = new MDCPrivilege(userName, principalType.toString(), dcObj, privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mDc);
}
} else if (hiveObject.getObjectType() == HiveObjectType.TABLE) {
MTable tblObj = getMTable(catName, hiveObject.getDbName(), hiveObject.getObjectName());
if (tblObj != null) {
List<MTablePrivilege> tablePrivs = this.listAllMTableGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), authorizer);
for (MTablePrivilege priv : tablePrivs) {
if (priv.getGrantor() != null && priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on table [" + hiveObject.getDbName() + "," + hiveObject.getObjectName() + "] by " + grantor);
}
MTablePrivilege mTab = new MTablePrivilege(userName, principalType.toString(), tblObj, privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mTab);
}
}
} else if (hiveObject.getObjectType() == HiveObjectType.PARTITION) {
MPartition partObj = this.getMPartition(catName, hiveObject.getDbName(), hiveObject.getObjectName(), hiveObject.getPartValues(), null);
String partName = null;
if (partObj != null) {
partName = partObj.getPartitionName();
List<MPartitionPrivilege> partPrivs = this.listPrincipalMPartitionGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), partObj.getPartitionName(), authorizer);
for (MPartitionPrivilege priv : partPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on partition [" + hiveObject.getDbName() + "," + hiveObject.getObjectName() + "," + partName + "] by " + grantor);
}
MPartitionPrivilege mTab = new MPartitionPrivilege(userName, principalType.toString(), partObj, privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mTab);
}
}
} else if (hiveObject.getObjectType() == HiveObjectType.COLUMN) {
MTable tblObj = getMTable(catName, hiveObject.getDbName(), hiveObject.getObjectName());
if (tblObj != null) {
if (hiveObject.getPartValues() != null) {
MPartition partObj = null;
List<MPartitionColumnPrivilege> colPrivs = null;
partObj = this.getMPartition(catName, hiveObject.getDbName(), hiveObject.getObjectName(), hiveObject.getPartValues(), tblObj);
if (partObj == null) {
continue;
}
colPrivs = this.listPrincipalMPartitionColumnGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), partObj.getPartitionName(), hiveObject.getColumnName(), authorizer);
for (MPartitionColumnPrivilege priv : colPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on column " + hiveObject.getColumnName() + " [" + hiveObject.getDbName() + "," + hiveObject.getObjectName() + "," + partObj.getPartitionName() + "] by " + grantor);
}
MPartitionColumnPrivilege mCol = new MPartitionColumnPrivilege(userName, principalType.toString(), partObj, hiveObject.getColumnName(), privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mCol);
}
} else {
List<MTableColumnPrivilege> colPrivs = null;
colPrivs = this.listPrincipalMTableColumnGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), hiveObject.getColumnName(), authorizer);
for (MTableColumnPrivilege priv : colPrivs) {
if (priv.getGrantor().equalsIgnoreCase(grantor)) {
privSet.add(priv.getPrivilege());
}
}
for (String privilege : privs) {
if (privSet.contains(privilege)) {
throw new InvalidObjectException(privilege + " is already granted on column " + hiveObject.getColumnName() + " [" + hiveObject.getDbName() + "," + hiveObject.getObjectName() + "] by " + grantor);
}
MTableColumnPrivilege mCol = new MTableColumnPrivilege(userName, principalType.toString(), tblObj, hiveObject.getColumnName(), privilege, now, grantor, grantorType, grantOption, authorizer);
persistentObjs.add(mCol);
}
}
}
}
}
}
if (CollectionUtils.isNotEmpty(persistentObjs)) {
pm.makePersistentAll(persistentObjs);
}
committed = commitTransaction();
} finally {
if (!committed) {
rollbackTransaction();
}
}
return committed;
}
Aggregations