@Override public synchronized void createTable(Table table) { SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName()); Table tableCopy = table.deepCopy(); if (tableCopy.getSd() == null) { tableCopy.setSd(new StorageDescriptor()); } else if (tableCopy.getSd().getLocation() != null) { File directory = new File(new Path(tableCopy.getSd().getLocation()).toUri()); checkArgument(directory.exists(), "Table directory does not exist"); checkArgument( isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory"); } if (relations.putIfAbsent(schemaTableName, tableCopy) != null) { throw new TableAlreadyExistsException(schemaTableName); } if (tableCopy.getTableType().equals(TableType.VIRTUAL_VIEW.name())) { views.put(schemaTableName, tableCopy); } PrincipalPrivilegeSet privileges = table.getPrivileges(); if (privileges != null) { for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getUserPrivileges().entrySet()) { String user = entry.getKey(); Set<HivePrivilegeInfo> userPrivileges = entry .getValue() .stream() .map(HivePrivilegeInfo::parsePrivilege) .flatMap(Collection::stream) .collect(toImmutableSet()); setTablePrivileges(user, USER, table.getDbName(), table.getTableName(), userPrivileges); } for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getRolePrivileges().entrySet()) { String role = entry.getKey(); Set<HivePrivilegeInfo> rolePrivileges = entry .getValue() .stream() .map(HivePrivilegeInfo::parsePrivilege) .flatMap(Collection::stream) .collect(toImmutableSet()); setTablePrivileges(role, ROLE, table.getDbName(), table.getTableName(), rolePrivileges); } } }
private Set<HivePrivilege> getPrivileges(String user, HiveObjectRef objectReference) { ImmutableSet.Builder<HivePrivilege> privileges = ImmutableSet.builder(); try (HiveMetastoreClient client = clientProvider.createMetastoreClient()) { PrincipalPrivilegeSet privilegeSet = client.getPrivilegeSet(objectReference, user, null); if (privilegeSet != null) { Map<String, List<PrivilegeGrantInfo>> userPrivileges = privilegeSet.getUserPrivileges(); if (userPrivileges != null) { privileges.addAll(toGrants(userPrivileges.get(user))); } for (List<PrivilegeGrantInfo> rolePrivileges : privilegeSet.getRolePrivileges().values()) { privileges.addAll(toGrants(rolePrivileges)); } // We do not add the group permissions as Hive does not seem to process these } } catch (TException e) { throw new PrestoException(HIVE_METASTORE_ERROR, e); } return privileges.build(); }
@Override public boolean hasPrivilegeWithGrantOptionOnTable( String user, String databaseName, String tableName, HivePrivilege hivePrivilege) { try (HiveMetastoreClient metastoreClient = clientProvider.createMetastoreClient()) { PrincipalPrivilegeSet principalPrivilegeSet = metastoreClient.getPrivilegeSet( new HiveObjectRef(HiveObjectType.TABLE, databaseName, tableName, null, null), user, null); for (PrivilegeGrantInfo privilegeGrantInfo : principalPrivilegeSet.getUserPrivileges().get(user)) { if (privilegeGrantInfo.getPrivilege().equalsIgnoreCase(hivePrivilege.name()) && privilegeGrantInfo.isGrantOption()) { return true; } } return false; } catch (TException e) { throw new PrestoException(HIVE_METASTORE_ERROR, e); } }