Skip to content

Commit

Permalink
Introduce ENABLED_ROLES view
Browse files Browse the repository at this point in the history
Extracted-From: prestodb/presto#10904
  • Loading branch information
Andrii Rosa authored and sopel39 committed Jan 29, 2019
1 parent 8234650 commit 671d653
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class InformationSchemaMetadata
public static final SchemaTableName TABLE_TABLE_PRIVILEGES = new SchemaTableName(INFORMATION_SCHEMA, "table_privileges");
public static final SchemaTableName TABLE_ROLES = new SchemaTableName(INFORMATION_SCHEMA, "roles");
public static final SchemaTableName TABLE_APPLICABLE_ROLES = new SchemaTableName(INFORMATION_SCHEMA, "applicable_roles");
public static final SchemaTableName TABLE_ENABLED_ROLES = new SchemaTableName(INFORMATION_SCHEMA, "enabled_roles");

public static final Map<SchemaTableName, ConnectorTableMetadata> TABLES = schemaMetadataBuilder()
.table(tableMetadataBuilder(TABLE_COLUMNS)
Expand Down Expand Up @@ -125,6 +126,9 @@ public class InformationSchemaMetadata
.column("role_name", createUnboundedVarcharType())
.column("is_grantable", createUnboundedVarcharType())
.build())
.table(tableMetadataBuilder(TABLE_ENABLED_ROLES)
.column("role_name", createUnboundedVarcharType())
.build())
.build();

private static final InformationSchemaColumnHandle CATALOG_COLUMN_HANDLE = new InformationSchemaColumnHandle("table_catalog");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static com.google.common.collect.Sets.union;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_APPLICABLE_ROLES;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_COLUMNS;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_ENABLED_ROLES;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_ROLES;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_SCHEMATA;
import static io.prestosql.connector.informationSchema.InformationSchemaMetadata.TABLE_TABLES;
Expand Down Expand Up @@ -132,6 +133,9 @@ public InternalTable getInformationSchemaTable(Session session, String catalog,
if (table.equals(TABLE_APPLICABLE_ROLES)) {
return buildApplicableRoles(session, catalog);
}
if (table.equals(TABLE_ENABLED_ROLES)) {
return buildEnabledRoles(session, catalog);
}

throw new IllegalArgumentException(format("table does not exist: %s", table));
}
Expand Down Expand Up @@ -253,4 +257,13 @@ private InternalTable buildApplicableRoles(Session session, String catalog)
}
return table.build();
}

private InternalTable buildEnabledRoles(Session session, String catalog)
{
InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_ENABLED_ROLES));
for (String role : metadata.listEnabledRoles(session, catalog)) {
table.add(role);
}
return table.build();
}
}
5 changes: 5 additions & 0 deletions presto-main/src/main/java/io/prestosql/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public interface Metadata
*/
Set<RoleGrant> listApplicableRoles(Session session, PrestoPrincipal principal, String catalog);

/**
* List applicable roles, including the transitive grants, in given session
*/
Set<String> listEnabledRoles(Session session, String catalog);

/**
* Grants the specified privilege to the specified user on the specified table
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ public Set<RoleGrant> listApplicableRoles(Session session, PrestoPrincipal princ
return ImmutableSet.copyOf(metadata.listApplicableRoles(connectorSession, principal));
}

@Override
public Set<String> listEnabledRoles(Session session, String catalog)
{
Optional<CatalogMetadata> catalogMetadata = getOptionalCatalogMetadata(session, catalog);
if (!catalogMetadata.isPresent()) {
return ImmutableSet.of();
}
ConnectorId connectorId = catalogMetadata.get().getConnectorId();
ConnectorSession connectorSession = session.toConnectorSession(connectorId);
ConnectorMetadata metadata = catalogMetadata.get().getMetadataFor(connectorId);
return ImmutableSet.copyOf(metadata.listEnabledRoles(connectorSession));
}

@Override
public void grantTablePrivileges(Session session, QualifiedObjectName tableName, Set<Privilege> privileges, String grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ public Set<RoleGrant> listApplicableRoles(Session session, PrestoPrincipal princ
throw new UnsupportedOperationException();
}

@Override
public Set<String> listEnabledRoles(Session session, String catalog)
{
throw new UnsupportedOperationException();
}

@Override
public void grantTablePrivileges(Session session, QualifiedObjectName tableName, Set<Privilege> privileges, String grantee, boolean grantOption)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/
package io.prestosql.tests.hive;

import com.facebook.presto.tests.utils.JdbcDriverUtils;
import com.google.common.collect.ImmutableMap;
import io.prestodb.tempto.ProductTest;
import io.prestodb.tempto.assertions.QueryAssert.Row;
import io.prestodb.tempto.query.QueryResult;
import io.prestosql.tests.utils.JdbcDriverUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ system| information_schema| columns| is_nullable| varchar| YES| null| null|
system| information_schema| columns| data_type| varchar| YES| null| null|
system| information_schema| columns| comment| varchar| YES| null| null|
system| information_schema| columns| extra_info| varchar| YES| null| null|
system| information_schema| enabled_roles| role_name| varchar| YES| null| null|
system| information_schema| roles| role_name| varchar| YES| null| null|
system| information_schema| schemata| catalog_name| varchar| YES| null| null|
system| information_schema| schemata| schema_name| varchar| YES| null| null|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ default Set<RoleGrant> listApplicableRoles(ConnectorSession session, PrestoPrinc
throw new PrestoException(NOT_SUPPORTED, "This connector does not support roles");
}

/**
* List applicable roles, including the transitive grants, in given session
*/
default Set<String> listEnabledRoles(ConnectorSession session)
{
throw new PrestoException(NOT_SUPPORTED, "This connector does not support roles");
}

/**
* Grants the specified privilege to the specified user on the specified table
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ public Set<RoleGrant> listApplicableRoles(ConnectorSession session, PrestoPrinci
}
}

@Override
public Set<String> listEnabledRoles(ConnectorSession session)
{
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(classLoader)) {
return delegate.listEnabledRoles(session);
}
}

@Override
public void grantTablePrivileges(ConnectorSession session, SchemaTableName tableName, Set<Privilege> privileges, String grantee, boolean grantOption)
{
Expand Down

0 comments on commit 671d653

Please sign in to comment.