Search in sources :

Example 1 with MDBPrivilege

use of org.apache.hadoop.hive.metastore.model.MDBPrivilege in project hive by apache.

the class ObjectStore method listDatabaseGrants.

@SuppressWarnings("unchecked")
private List<MDBPrivilege> listDatabaseGrants(String dbName, QueryWrapper queryWrapper) {
    dbName = normalizeIdentifier(dbName);
    boolean success = false;
    try {
        LOG.debug("Executing listDatabaseGrants");
        openTransaction();
        Query query = queryWrapper.query = pm.newQuery(MDBPrivilege.class, "database.name == t1");
        query.declareParameters("java.lang.String t1");
        List<MDBPrivilege> mSecurityDBList = (List<MDBPrivilege>) query.executeWithArray(dbName);
        pm.retrieveAll(mSecurityDBList);
        success = commitTransaction();
        LOG.debug("Done retrieving all objects for listDatabaseGrants");
        return mSecurityDBList;
    } finally {
        if (!success) {
            rollbackTransaction();
        }
    }
}
Also used : Query(javax.jdo.Query) LinkedList(java.util.LinkedList) MStringList(org.apache.hadoop.hive.metastore.model.MStringList) ArrayList(java.util.ArrayList) List(java.util.List) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege)

Example 2 with MDBPrivilege

use of org.apache.hadoop.hive.metastore.model.MDBPrivilege in project hive by apache.

the class ObjectStore method listPrincipalAllDBGrant.

@SuppressWarnings("unchecked")
private List<MDBPrivilege> listPrincipalAllDBGrant(String principalName, PrincipalType principalType, QueryWrapper queryWrapper) {
    boolean success = false;
    Query query = null;
    List<MDBPrivilege> mSecurityDBList = null;
    try {
        LOG.debug("Executing listPrincipalAllDBGrant");
        openTransaction();
        if (principalName != null && principalType != null) {
            query = queryWrapper.query = pm.newQuery(MDBPrivilege.class, "principalName == t1 && principalType == t2");
            query.declareParameters("java.lang.String t1, java.lang.String t2");
            mSecurityDBList = (List<MDBPrivilege>) query.execute(principalName, principalType.toString());
        } else {
            query = queryWrapper.query = pm.newQuery(MDBPrivilege.class);
            mSecurityDBList = (List<MDBPrivilege>) query.execute();
        }
        pm.retrieveAll(mSecurityDBList);
        success = commitTransaction();
        LOG.debug("Done retrieving all objects for listPrincipalAllDBGrant");
    } finally {
        if (!success) {
            rollbackTransaction();
        }
    }
    return mSecurityDBList;
}
Also used : Query(javax.jdo.Query) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege)

Example 3 with MDBPrivilege

use of org.apache.hadoop.hive.metastore.model.MDBPrivilege in project hive by apache.

the class ObjectStore method listDatabaseGrants.

private List<MDBPrivilege> listDatabaseGrants(String catName, String dbName, String authorizer) throws Exception {
    LOG.debug("Executing listDatabaseGrants");
    Preconditions.checkState(currentTransaction.isActive());
    dbName = normalizeIdentifier(dbName);
    catName = normalizeIdentifier(catName);
    final Query query;
    final String[] args;
    if (authorizer != null) {
        query = pm.newQuery(MDBPrivilege.class, "database.name == t1 && database.catalogName == t2 && authorizer == t3");
        query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
        args = new String[] { dbName, catName, authorizer };
    } else {
        query = pm.newQuery(MDBPrivilege.class, "database.name == t1 && database.catalogName == t2");
        query.declareParameters("java.lang.String t1, java.lang.String t2");
        args = new String[] { dbName, catName };
    }
    try (QueryWrapper q = new QueryWrapper(query)) {
        final List<MDBPrivilege> mSecurityDBList = (List<MDBPrivilege>) q.executeWithArray(args);
        pm.retrieveAll(mSecurityDBList);
        LOG.debug("Done retrieving all objects for listDatabaseGrants: {}", mSecurityDBList);
        return Collections.unmodifiableList(new ArrayList<>(mSecurityDBList));
    }
}
Also used : ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery) Query(javax.jdo.Query) MScheduledQuery(org.apache.hadoop.hive.metastore.model.MScheduledQuery) ValidWriteIdList(org.apache.hadoop.hive.common.ValidWriteIdList) ReplicationMetricList(org.apache.hadoop.hive.metastore.api.ReplicationMetricList) LinkedList(java.util.LinkedList) MStringList(org.apache.hadoop.hive.metastore.model.MStringList) ArrayList(java.util.ArrayList) ValidReaderWriteIdList(org.apache.hadoop.hive.common.ValidReaderWriteIdList) List(java.util.List) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege)

Example 4 with MDBPrivilege

use of org.apache.hadoop.hive.metastore.model.MDBPrivilege in project hive by apache.

the class ObjectStore method listPrincipalMDBGrants.

private List<MDBPrivilege> listPrincipalMDBGrants(String principalName, PrincipalType principalType, String catName, String dbName, String authorizer) {
    boolean success = false;
    Query query = null;
    List<MDBPrivilege> mSecurityDBList = new ArrayList<>();
    dbName = normalizeIdentifier(dbName);
    try {
        LOG.debug("Executing listPrincipalDBGrants");
        openTransaction();
        List<MDBPrivilege> mPrivs;
        if (authorizer != null) {
            query = pm.newQuery(MDBPrivilege.class, "principalName == t1 && principalType == t2 && database.name == t3 && " + "database.catalogName == t4 && authorizer == t5");
            query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3, java.lang.String t4, " + "java.lang.String t5");
            mPrivs = (List<MDBPrivilege>) query.executeWithArray(principalName, principalType.toString(), dbName, catName, authorizer);
        } else {
            query = pm.newQuery(MDBPrivilege.class, "principalName == t1 && principalType == t2 && database.name == t3 && database.catalogName == t4");
            query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3, java.lang.String t4");
            mPrivs = (List<MDBPrivilege>) query.executeWithArray(principalName, principalType.toString(), dbName, catName);
        }
        pm.retrieveAll(mPrivs);
        success = commitTransaction();
        mSecurityDBList.addAll(mPrivs);
        LOG.debug("Done retrieving all objects for listPrincipalDBGrants");
    } finally {
        rollbackAndCleanup(success, query);
    }
    return mSecurityDBList;
}
Also used : ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery) Query(javax.jdo.Query) MScheduledQuery(org.apache.hadoop.hive.metastore.model.MScheduledQuery) ArrayList(java.util.ArrayList) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege)

Example 5 with MDBPrivilege

use of org.apache.hadoop.hive.metastore.model.MDBPrivilege in project hive by apache.

the class ObjectStore method revokePrivileges.

@Override
public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) throws InvalidObjectException, MetaException, NoSuchObjectException {
    boolean committed = false;
    try {
        openTransaction();
        List<Object> persistentObjs = new ArrayList<>();
        List<HiveObjectPrivilege> privilegeList = privileges.getPrivileges();
        if (CollectionUtils.isNotEmpty(privilegeList)) {
            Iterator<HiveObjectPrivilege> privIter = privilegeList.iterator();
            while (privIter.hasNext()) {
                HiveObjectPrivilege privDef = privIter.next();
                HiveObjectRef hiveObject = privDef.getHiveObject();
                String privilegeStr = privDef.getGrantInfo().getPrivilege();
                if (privilegeStr == null || privilegeStr.trim().equals("")) {
                    continue;
                }
                String[] privs = privilegeStr.split(",");
                String userName = privDef.getPrincipalName();
                PrincipalType principalType = privDef.getPrincipalType();
                String catName = hiveObject.isSetCatName() ? hiveObject.getCatName() : getDefaultCatalog(conf);
                if (hiveObject.getObjectType() == HiveObjectType.GLOBAL) {
                    List<MGlobalPrivilege> mSecUser = this.listPrincipalMGlobalGrants(userName, principalType);
                    boolean found = false;
                    for (String privilege : privs) {
                        for (MGlobalPrivilege userGrant : mSecUser) {
                            String userGrantPrivs = userGrant.getPrivilege();
                            if (privilege.equals(userGrantPrivs)) {
                                found = true;
                                if (grantOption) {
                                    if (userGrant.getGrantOption()) {
                                        userGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(userGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No user grant found for privileges " + privilege);
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.DATABASE) {
                    MDatabase dbObj = getMDatabase(catName, hiveObject.getDbName());
                    String db = hiveObject.getDbName();
                    boolean found = false;
                    List<MDBPrivilege> dbGrants = this.listPrincipalMDBGrants(userName, principalType, catName, db);
                    for (String privilege : privs) {
                        for (MDBPrivilege dbGrant : dbGrants) {
                            String dbGrantPriv = dbGrant.getPrivilege();
                            if (privilege.equals(dbGrantPriv)) {
                                found = true;
                                if (grantOption) {
                                    if (dbGrant.getGrantOption()) {
                                        dbGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(dbGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No database grant found for privileges " + privilege + " on database " + db);
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.DATACONNECTOR) {
                    MDataConnector dCObj = getMDataConnector(hiveObject.getObjectName());
                    String dc = hiveObject.getObjectName();
                    boolean found = false;
                    List<MDCPrivilege> dcGrants = this.listPrincipalMDCGrants(userName, principalType, catName, dc);
                    for (String privilege : privs) {
                        for (MDCPrivilege dcGrant : dcGrants) {
                            String dcGrantPriv = dcGrant.getPrivilege();
                            if (privilege.equals(dcGrantPriv)) {
                                found = true;
                                if (grantOption) {
                                    if (dcGrant.getGrantOption()) {
                                        dcGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(dcGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No dataconnector grant found for privileges " + privilege + " on data connector " + dc);
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.TABLE) {
                    boolean found = false;
                    List<MTablePrivilege> tableGrants = this.listAllMTableGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName());
                    for (String privilege : privs) {
                        for (MTablePrivilege tabGrant : tableGrants) {
                            String tableGrantPriv = tabGrant.getPrivilege();
                            if (privilege.equalsIgnoreCase(tableGrantPriv)) {
                                found = true;
                                if (grantOption) {
                                    if (tabGrant.getGrantOption()) {
                                        tabGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(tabGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + hiveObject.getObjectName() + ", database is " + hiveObject.getDbName());
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.PARTITION) {
                    boolean found = false;
                    Table tabObj = this.getTable(catName, hiveObject.getDbName(), hiveObject.getObjectName(), null);
                    String partName = null;
                    if (hiveObject.getPartValues() != null) {
                        partName = Warehouse.makePartName(tabObj.getPartitionKeys(), hiveObject.getPartValues());
                    }
                    List<MPartitionPrivilege> partitionGrants = this.listPrincipalMPartitionGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), partName);
                    for (String privilege : privs) {
                        for (MPartitionPrivilege partGrant : partitionGrants) {
                            String partPriv = partGrant.getPrivilege();
                            if (partPriv.equalsIgnoreCase(privilege)) {
                                found = true;
                                if (grantOption) {
                                    if (partGrant.getGrantOption()) {
                                        partGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(partGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", partition is " + partName + ", database is " + tabObj.getDbName());
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.COLUMN) {
                    Table tabObj = this.getTable(catName, hiveObject.getDbName(), hiveObject.getObjectName(), null);
                    String partName = null;
                    if (hiveObject.getPartValues() != null) {
                        partName = Warehouse.makePartName(tabObj.getPartitionKeys(), hiveObject.getPartValues());
                    }
                    if (partName != null) {
                        List<MPartitionColumnPrivilege> mSecCol = listPrincipalMPartitionColumnGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), partName, hiveObject.getColumnName());
                        boolean found = false;
                        for (String privilege : privs) {
                            for (MPartitionColumnPrivilege col : mSecCol) {
                                String colPriv = col.getPrivilege();
                                if (colPriv.equalsIgnoreCase(privilege)) {
                                    found = true;
                                    if (grantOption) {
                                        if (col.getGrantOption()) {
                                            col.setGrantOption(false);
                                        } else {
                                            throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                        }
                                    }
                                    persistentObjs.add(col);
                                    break;
                                }
                            }
                            if (!found) {
                                throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", partition is " + partName + ", column name = " + hiveObject.getColumnName() + ", database is " + tabObj.getDbName());
                            }
                        }
                    } else {
                        List<MTableColumnPrivilege> mSecCol = listPrincipalMTableColumnGrants(userName, principalType, catName, hiveObject.getDbName(), hiveObject.getObjectName(), hiveObject.getColumnName());
                        boolean found = false;
                        for (String privilege : privs) {
                            for (MTableColumnPrivilege col : mSecCol) {
                                String colPriv = col.getPrivilege();
                                if (colPriv.equalsIgnoreCase(privilege)) {
                                    found = true;
                                    if (grantOption) {
                                        if (col.getGrantOption()) {
                                            col.setGrantOption(false);
                                        } else {
                                            throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                        }
                                    }
                                    persistentObjs.add(col);
                                    break;
                                }
                            }
                            if (!found) {
                                throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", column name = " + hiveObject.getColumnName() + ", database is " + tabObj.getDbName());
                            }
                        }
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(persistentObjs)) {
            if (grantOption) {
            // If grant option specified, only update the privilege, don't remove it.
            // Grant option has already been removed from the privileges in the section above
            } else {
                pm.deletePersistentAll(persistentObjs);
            }
        }
        committed = commitTransaction();
    } finally {
        if (!committed) {
            rollbackTransaction();
        }
    }
    return committed;
}
Also used : MDataConnector(org.apache.hadoop.hive.metastore.model.MDataConnector) ArrayList(java.util.ArrayList) MPartitionColumnPrivilege(org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege) MDCPrivilege(org.apache.hadoop.hive.metastore.model.MDCPrivilege) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) ValidWriteIdList(org.apache.hadoop.hive.common.ValidWriteIdList) ReplicationMetricList(org.apache.hadoop.hive.metastore.api.ReplicationMetricList) LinkedList(java.util.LinkedList) MStringList(org.apache.hadoop.hive.metastore.model.MStringList) ArrayList(java.util.ArrayList) ValidReaderWriteIdList(org.apache.hadoop.hive.common.ValidReaderWriteIdList) List(java.util.List) MTableColumnPrivilege(org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) MVersionTable(org.apache.hadoop.hive.metastore.model.MVersionTable) SourceTable(org.apache.hadoop.hive.metastore.api.SourceTable) Table(org.apache.hadoop.hive.metastore.api.Table) MTable(org.apache.hadoop.hive.metastore.model.MTable) HiveObjectRef(org.apache.hadoop.hive.metastore.api.HiveObjectRef) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege) MGlobalPrivilege(org.apache.hadoop.hive.metastore.model.MGlobalPrivilege) MDatabase(org.apache.hadoop.hive.metastore.model.MDatabase) HiveObjectPrivilege(org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege) MPartitionPrivilege(org.apache.hadoop.hive.metastore.model.MPartitionPrivilege) PrincipalType(org.apache.hadoop.hive.metastore.api.PrincipalType) MTablePrivilege(org.apache.hadoop.hive.metastore.model.MTablePrivilege)

Aggregations

MDBPrivilege (org.apache.hadoop.hive.metastore.model.MDBPrivilege)12 ArrayList (java.util.ArrayList)8 Query (javax.jdo.Query)5 LinkedList (java.util.LinkedList)4 List (java.util.List)4 HiveObjectPrivilege (org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege)4 HiveObjectRef (org.apache.hadoop.hive.metastore.api.HiveObjectRef)4 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)4 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)3 PrincipalType (org.apache.hadoop.hive.metastore.api.PrincipalType)3 PrivilegeGrantInfo (org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo)3 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)3 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)3 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)3 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)3 MConstraint (org.apache.hadoop.hive.metastore.model.MConstraint)3 MDCPrivilege (org.apache.hadoop.hive.metastore.model.MDCPrivilege)3 MDatabase (org.apache.hadoop.hive.metastore.model.MDatabase)3 MGlobalPrivilege (org.apache.hadoop.hive.metastore.model.MGlobalPrivilege)3 MPartitionColumnPrivilege (org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege)3