Search in sources :

Example 1 with HivePrincipal

use of io.trino.plugin.hive.metastore.HivePrincipal in project trino by trinodb.

the class FileHiveMetastore method listTablePrivileges.

@Override
public synchronized Set<HivePrivilegeInfo> listTablePrivileges(String databaseName, String tableName, Optional<String> tableOwner, Optional<HivePrincipal> principal) {
    Table table = getRequiredTable(databaseName, tableName);
    Path permissionsDirectory = getPermissionsDirectory(table);
    if (principal.isEmpty()) {
        Builder<HivePrivilegeInfo> privileges = ImmutableSet.<HivePrivilegeInfo>builder().addAll(readAllPermissions(permissionsDirectory));
        tableOwner.ifPresent(owner -> privileges.add(new HivePrivilegeInfo(OWNERSHIP, true, new HivePrincipal(USER, owner), new HivePrincipal(USER, owner))));
        return privileges.build();
    }
    ImmutableSet.Builder<HivePrivilegeInfo> result = ImmutableSet.builder();
    if (principal.get().getType() == USER && table.getOwner().orElseThrow().equals(principal.get().getName())) {
        result.add(new HivePrivilegeInfo(OWNERSHIP, true, principal.get(), principal.get()));
    }
    result.addAll(readPermissionsFile(getPermissionsPath(permissionsDirectory, principal.get())));
    return result.build();
}
Also used : Path(org.apache.hadoop.fs.Path) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal)

Example 2 with HivePrincipal

use of io.trino.plugin.hive.metastore.HivePrincipal in project trino by trinodb.

the class FileHiveMetastore method setTablePrivileges.

private synchronized void setTablePrivileges(HivePrincipal grantee, String databaseName, String tableName, Collection<HivePrivilegeInfo> privileges) {
    requireNonNull(grantee, "grantee is null");
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    requireNonNull(privileges, "privileges is null");
    try {
        Table table = getRequiredTable(databaseName, tableName);
        Path permissionsDirectory = getPermissionsDirectory(table);
        boolean created = metadataFileSystem.mkdirs(permissionsDirectory);
        if (!created && !metadataFileSystem.isDirectory(permissionsDirectory)) {
            throw new TrinoException(HIVE_METASTORE_ERROR, "Could not create permissions directory");
        }
        Path permissionFilePath = getPermissionsPath(permissionsDirectory, grantee);
        List<PermissionMetadata> permissions = privileges.stream().map(hivePrivilegeInfo -> new PermissionMetadata(hivePrivilegeInfo.getHivePrivilege(), hivePrivilegeInfo.isGrantOption(), grantee)).collect(toList());
        writeFile("permissions", permissionFilePath, permissionsCodec, permissions, true);
    } catch (IOException e) {
        throw new TrinoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ThriftMetastoreUtil.updateStatisticsParameters(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.updateStatisticsParameters) Arrays(java.util.Arrays) FileSystem(org.apache.hadoop.fs.FileSystem) USER(io.trino.spi.security.PrincipalType.USER) FileStatus(org.apache.hadoop.fs.FileStatus) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) DATABASE(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.DATABASE) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Column(io.trino.plugin.hive.metastore.Column) Map(java.util.Map) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) EnumSet(java.util.EnumSet) TABLE_COMMENT(io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT) AcidTransaction(io.trino.plugin.hive.acid.AcidTransaction) HdfsEnvironment(io.trino.plugin.hive.HdfsEnvironment) Table(io.trino.plugin.hive.metastore.Table) ConnectorIdentity(io.trino.spi.security.ConnectorIdentity) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) MANAGED_TABLE(org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE) SchemaTableName(io.trino.spi.connector.SchemaTableName) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) OWNERSHIP(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP) HdfsConfig(io.trino.plugin.hive.HdfsConfig) ByteStreams(com.google.common.io.ByteStreams) HdfsConfigurationInitializer(io.trino.plugin.hive.HdfsConfigurationInitializer) Partition(io.trino.plugin.hive.metastore.Partition) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) PartitionNotFoundException(io.trino.plugin.hive.PartitionNotFoundException) ColumnNotFoundException(io.trino.spi.connector.ColumnNotFoundException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HiveType(io.trino.plugin.hive.HiveType) ThriftMetastoreUtil(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) NodeVersion(io.trino.plugin.hive.NodeVersion) SchemaAlreadyExistsException(io.trino.plugin.hive.SchemaAlreadyExistsException) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) LinkedHashSet(java.util.LinkedHashSet) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) SPARK_TABLE_PROVIDER_KEY(io.trino.plugin.hive.util.HiveUtil.SPARK_TABLE_PROVIDER_KEY) IOException(java.io.IOException) HdfsConfiguration(io.trino.plugin.hive.HdfsConfiguration) RoleGrant(io.trino.spi.security.RoleGrant) File(java.io.File) TableType(org.apache.hadoop.hive.metastore.TableType) ArrayDeque(java.util.ArrayDeque) HivePrivilege(io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege) HivePartitionManager.extractPartitionValues(io.trino.plugin.hive.HivePartitionManager.extractPartitionValues) ThriftMetastoreUtil.getHiveBasicStatistics(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.getHiveBasicStatistics) MetastoreUtil.makePartitionName(io.trino.plugin.hive.metastore.MetastoreUtil.makePartitionName) HiveUtil.toPartitionValues(io.trino.plugin.hive.util.HiveUtil.toPartitionValues) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) Database(io.trino.plugin.hive.metastore.Database) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) NoHdfsAuthentication(io.trino.plugin.hive.authentication.NoHdfsAuthentication) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MATERIALIZED_VIEW(org.apache.hadoop.hive.metastore.TableType.MATERIALIZED_VIEW) Locale(java.util.Locale) ALREADY_EXISTS(io.trino.spi.StandardErrorCode.ALREADY_EXISTS) Path(org.apache.hadoop.fs.Path) HiveHdfsConfiguration(io.trino.plugin.hive.HiveHdfsConfiguration) Collectors.toSet(java.util.stream.Collectors.toSet) HiveBasicStatistics(io.trino.plugin.hive.HiveBasicStatistics) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TableAlreadyExistsException(io.trino.plugin.hive.TableAlreadyExistsException) TrinoException(io.trino.spi.TrinoException) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) Objects(java.util.Objects) HdfsContext(io.trino.plugin.hive.HdfsEnvironment.HdfsContext) List(java.util.List) PARTITION(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.PARTITION) Entry(java.util.Map.Entry) Optional(java.util.Optional) HivePrivilegeInfo(io.trino.plugin.hive.metastore.HivePrivilegeInfo) FileUtils.unescapePathName(org.apache.hadoop.hive.common.FileUtils.unescapePathName) JsonCodec(io.airlift.json.JsonCodec) VERSION_COMPATIBILITY_CONFIG(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VERSION_COMPATIBILITY_CONFIG) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) Type(io.trino.spi.type.Type) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) Builder(com.google.common.collect.ImmutableSet.Builder) HiveColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics) ImmutableList(com.google.common.collect.ImmutableList) HIVE_METASTORE_ERROR(io.trino.plugin.hive.HiveErrorCode.HIVE_METASTORE_ERROR) UNSAFE_ASSUME_COMPATIBILITY(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VersionCompatibility.UNSAFE_ASSUME_COMPATIBILITY) Objects.requireNonNull(java.util.Objects.requireNonNull) VIRTUAL_VIEW(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW) VersionCompatibility(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig.VersionCompatibility) OutputStream(java.io.OutputStream) DELTA_LAKE_PROVIDER(io.trino.plugin.hive.util.HiveUtil.DELTA_LAKE_PROVIDER) TupleDomain(io.trino.spi.predicate.TupleDomain) ROLE(io.trino.spi.security.PrincipalType.ROLE) Collectors.toList(java.util.stream.Collectors.toList) MetastoreUtil.verifyCanDropColumn(io.trino.plugin.hive.metastore.MetastoreUtil.verifyCanDropColumn) TABLE(io.trino.plugin.hive.metastore.file.FileHiveMetastore.SchemaType.TABLE) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PrincipalPrivileges(io.trino.plugin.hive.metastore.PrincipalPrivileges) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException)

Example 3 with HivePrincipal

use of io.trino.plugin.hive.metastore.HivePrincipal in project trino by trinodb.

the class FileHiveMetastore method grantRoles.

@Override
public synchronized void grantRoles(Set<String> roles, Set<HivePrincipal> grantees, boolean adminOption, HivePrincipal grantor) {
    Set<String> existingRoles = listRoles();
    Set<RoleGrant> existingGrants = listRoleGrantsSanitized();
    Set<RoleGrant> modifiedGrants = new HashSet<>(existingGrants);
    for (HivePrincipal grantee : grantees) {
        for (String role : roles) {
            checkArgument(existingRoles.contains(role), "Role does not exist: %s", role);
            if (grantee.getType() == ROLE) {
                checkArgument(existingRoles.contains(grantee.getName()), "Role does not exist: %s", grantee.getName());
            }
            RoleGrant grantWithAdminOption = new RoleGrant(grantee.toTrinoPrincipal(), role, true);
            RoleGrant grantWithoutAdminOption = new RoleGrant(grantee.toTrinoPrincipal(), role, false);
            if (adminOption) {
                modifiedGrants.remove(grantWithoutAdminOption);
                modifiedGrants.add(grantWithAdminOption);
            } else {
                modifiedGrants.remove(grantWithAdminOption);
                modifiedGrants.add(grantWithoutAdminOption);
            }
        }
    }
    modifiedGrants = removeDuplicatedEntries(modifiedGrants);
    if (!existingGrants.equals(modifiedGrants)) {
        writeRoleGrantsFile(modifiedGrants);
    }
}
Also used : RoleGrant(io.trino.spi.security.RoleGrant) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 4 with HivePrincipal

use of io.trino.plugin.hive.metastore.HivePrincipal in project trino by trinodb.

the class FileHiveMetastore method createTable.

@Override
public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) {
    verifyTableNotExists(table.getDatabaseName(), table.getTableName());
    Path tableMetadataDirectory = getTableMetadataDirectory(table);
    // validate table location
    if (table.getTableType().equals(VIRTUAL_VIEW.name())) {
        checkArgument(table.getStorage().getLocation().isEmpty(), "Storage location for view must be empty");
    } else if (table.getTableType().equals(MANAGED_TABLE.name())) {
        if (!tableMetadataDirectory.equals(new Path(table.getStorage().getLocation()))) {
            throw new TrinoException(HIVE_METASTORE_ERROR, "Table directory must be " + tableMetadataDirectory);
        }
    } else if (table.getTableType().equals(EXTERNAL_TABLE.name())) {
        try {
            Path externalLocation = new Path(table.getStorage().getLocation());
            FileSystem externalFileSystem = hdfsEnvironment.getFileSystem(hdfsContext, externalLocation);
            if (!externalFileSystem.isDirectory(externalLocation)) {
                throw new TrinoException(HIVE_METASTORE_ERROR, "External table location does not exist");
            }
        } catch (IOException e) {
            throw new TrinoException(HIVE_METASTORE_ERROR, "Could not validate external location", e);
        }
    } else if (!table.getTableType().equals(MATERIALIZED_VIEW.name())) {
        throw new TrinoException(NOT_SUPPORTED, "Table type not supported: " + table.getTableType());
    }
    writeSchemaFile(TABLE, tableMetadataDirectory, tableCodec, new TableMetadata(currentVersion, table), false);
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
        setTablePrivileges(new HivePrincipal(USER, entry.getKey()), table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
        setTablePrivileges(new HivePrincipal(ROLE, entry.getKey()), table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) FileSystem(org.apache.hadoop.fs.FileSystem) TrinoException(io.trino.spi.TrinoException) Collection(java.util.Collection) IOException(java.io.IOException)

Example 5 with HivePrincipal

use of io.trino.plugin.hive.metastore.HivePrincipal in project trino by trinodb.

the class FileHiveMetastore method revokeRoles.

@Override
public synchronized void revokeRoles(Set<String> roles, Set<HivePrincipal> grantees, boolean adminOption, HivePrincipal grantor) {
    Set<RoleGrant> existingGrants = listRoleGrantsSanitized();
    Set<RoleGrant> modifiedGrants = new HashSet<>(existingGrants);
    for (HivePrincipal grantee : grantees) {
        for (String role : roles) {
            RoleGrant grantWithAdminOption = new RoleGrant(grantee.toTrinoPrincipal(), role, true);
            RoleGrant grantWithoutAdminOption = new RoleGrant(grantee.toTrinoPrincipal(), role, false);
            if (modifiedGrants.contains(grantWithAdminOption) || modifiedGrants.contains(grantWithoutAdminOption)) {
                if (adminOption) {
                    modifiedGrants.remove(grantWithAdminOption);
                    modifiedGrants.add(grantWithoutAdminOption);
                } else {
                    modifiedGrants.remove(grantWithAdminOption);
                    modifiedGrants.remove(grantWithoutAdminOption);
                }
            }
        }
    }
    modifiedGrants = removeDuplicatedEntries(modifiedGrants);
    if (!existingGrants.equals(modifiedGrants)) {
        writeRoleGrantsFile(modifiedGrants);
    }
}
Also used : RoleGrant(io.trino.spi.security.RoleGrant) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

HivePrincipal (io.trino.plugin.hive.metastore.HivePrincipal)18 RoleGrant (io.trino.spi.security.RoleGrant)9 ImmutableSet (com.google.common.collect.ImmutableSet)7 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)7 HivePrivilegeInfo (io.trino.plugin.hive.metastore.HivePrivilegeInfo)7 TrinoException (io.trino.spi.TrinoException)6 Database (io.trino.plugin.hive.metastore.Database)4 OWNERSHIP (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP)4 Table (io.trino.plugin.hive.metastore.Table)4 SchemaTableName (io.trino.spi.connector.SchemaTableName)4 HashSet (java.util.HashSet)4 HivePrivilege (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege)3 DELETE (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.DELETE)3 INSERT (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.INSERT)3 SELECT (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.SELECT)3 UPDATE (io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.UPDATE)3 HiveUtil.isIcebergTable (io.trino.plugin.hive.util.HiveUtil.isIcebergTable)3 NOT_SUPPORTED (io.trino.spi.StandardErrorCode.NOT_SUPPORTED)3 ConnectorIdentity (io.trino.spi.security.ConnectorIdentity)3 ROLE (io.trino.spi.security.PrincipalType.ROLE)3