|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.connector.system; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import com.google.inject.Inject; |
| 18 | +import io.trino.FullConnectorSession; |
| 19 | +import io.trino.Session; |
| 20 | +import io.trino.metadata.Metadata; |
| 21 | +import io.trino.metadata.QualifiedTablePrefix; |
| 22 | +import io.trino.security.AccessControl; |
| 23 | +import io.trino.spi.connector.CatalogSchemaName; |
| 24 | +import io.trino.spi.connector.ConnectorSession; |
| 25 | +import io.trino.spi.connector.ConnectorTableMetadata; |
| 26 | +import io.trino.spi.connector.ConnectorTransactionHandle; |
| 27 | +import io.trino.spi.connector.InMemoryRecordSet; |
| 28 | +import io.trino.spi.connector.RecordCursor; |
| 29 | +import io.trino.spi.connector.SchemaTableName; |
| 30 | +import io.trino.spi.connector.SystemTable; |
| 31 | +import io.trino.spi.function.SchemaFunctionName; |
| 32 | +import io.trino.spi.predicate.TupleDomain; |
| 33 | +import io.trino.spi.security.FunctionAuthorization; |
| 34 | +import io.trino.spi.security.TrinoPrincipal; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 40 | +import static io.trino.metadata.MetadataListing.handleListingException; |
| 41 | +import static io.trino.metadata.MetadataListing.listAllAvailableSchemas; |
| 42 | +import static io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder; |
| 43 | +import static io.trino.spi.connector.SystemTable.Distribution.SINGLE_COORDINATOR; |
| 44 | +import static io.trino.spi.type.VarcharType.VARCHAR; |
| 45 | +import static io.trino.spi.type.VarcharType.createUnboundedVarcharType; |
| 46 | +import static java.util.Objects.requireNonNull; |
| 47 | + |
| 48 | +public class FunctionsAuthorization |
| 49 | + implements SystemTable |
| 50 | +{ |
| 51 | + public static final SchemaTableName FUNCTIONS_AUTHORIZATION_NAME = new SchemaTableName("metadata", "functions_authorization"); |
| 52 | + |
| 53 | + public static final ConnectorTableMetadata FUNCTIONS_AUTHORIZATION = tableMetadataBuilder(FUNCTIONS_AUTHORIZATION_NAME) |
| 54 | + .column("catalog", createUnboundedVarcharType()) |
| 55 | + .column("schema", createUnboundedVarcharType()) |
| 56 | + .column("name", createUnboundedVarcharType()) |
| 57 | + .column("authorization_type", createUnboundedVarcharType()) |
| 58 | + .column("authorization", createUnboundedVarcharType()) |
| 59 | + .build(); |
| 60 | + |
| 61 | + private final Metadata metadata; |
| 62 | + private final AccessControl accessControl; |
| 63 | + |
| 64 | + @Inject |
| 65 | + public FunctionsAuthorization(Metadata metadata, AccessControl accessControl) |
| 66 | + { |
| 67 | + this.metadata = requireNonNull(metadata, "metadata is null"); |
| 68 | + this.accessControl = requireNonNull(accessControl, "accessControl is null"); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Distribution getDistribution() |
| 73 | + { |
| 74 | + return SINGLE_COORDINATOR; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ConnectorTableMetadata getTableMetadata() |
| 79 | + { |
| 80 | + return FUNCTIONS_AUTHORIZATION; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint) |
| 85 | + { |
| 86 | + Session session = ((FullConnectorSession) connectorSession).getSession(); |
| 87 | + InMemoryRecordSet.Builder table = InMemoryRecordSet.builder(FUNCTIONS_AUTHORIZATION); |
| 88 | + for (CatalogFunctionAuthorization functionAuthorization : getFunctionsAuthorization(session, constraint)) { |
| 89 | + SchemaFunctionName schemaFunctionName = functionAuthorization.functionAuthorization().schemaFunctionName(); |
| 90 | + TrinoPrincipal trinoPrincipal = functionAuthorization.functionAuthorization().trinoPrincipal(); |
| 91 | + table.addRow( |
| 92 | + functionAuthorization.catalog(), |
| 93 | + schemaFunctionName.getSchemaName(), |
| 94 | + schemaFunctionName.getFunctionName(), |
| 95 | + trinoPrincipal.getType().toString(), |
| 96 | + trinoPrincipal.getName()); |
| 97 | + } |
| 98 | + return table.build().cursor(); |
| 99 | + } |
| 100 | + |
| 101 | + private List<CatalogFunctionAuthorization> getFunctionsAuthorization(Session session, TupleDomain<Integer> constraint) |
| 102 | + { |
| 103 | + try { |
| 104 | + return doGetFunctionsAuthorization(session, constraint); |
| 105 | + } |
| 106 | + catch (RuntimeException exception) { |
| 107 | + throw handleListingException(exception, "functions_authorization", "system"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private List<CatalogFunctionAuthorization> doGetFunctionsAuthorization(Session session, TupleDomain<Integer> constraint) |
| 112 | + { |
| 113 | + Set<CatalogSchemaName> availableSchemas = listAllAvailableSchemas(session, metadata, accessControl, constraint.getDomain(0, VARCHAR)); |
| 114 | + |
| 115 | + ImmutableList.Builder<CatalogFunctionAuthorization> result = ImmutableList.builder(); |
| 116 | + availableSchemas.forEach(catalogSchemaName -> { |
| 117 | + Set<FunctionAuthorization> allFunctionsAuthorization = metadata.getAllFunctionsAuthorizationInfo(session, new QualifiedTablePrefix(catalogSchemaName.getCatalogName())); |
| 118 | + Set<String> accessibleFunctions = metadata.listFunctions(session, new CatalogSchemaName(catalogSchemaName.getCatalogName(), catalogSchemaName.getSchemaName())).stream() |
| 119 | + .flatMap(functionMetadata -> functionMetadata.getNames().stream()) |
| 120 | + .collect(toImmutableSet()); |
| 121 | + |
| 122 | + allFunctionsAuthorization.stream() |
| 123 | + .filter(functionAuthorization -> accessibleFunctions.contains(functionAuthorization.schemaFunctionName().getFunctionName())) |
| 124 | + .map(functionAuthorization -> new CatalogFunctionAuthorization(catalogSchemaName.getCatalogName(), functionAuthorization)) |
| 125 | + .forEach(result::add); |
| 126 | + }); |
| 127 | + return result.build(); |
| 128 | + } |
| 129 | + |
| 130 | + private record CatalogFunctionAuthorization(String catalog, FunctionAuthorization functionAuthorization) |
| 131 | + { |
| 132 | + public CatalogFunctionAuthorization |
| 133 | + { |
| 134 | + requireNonNull(catalog, "catalog is null"); |
| 135 | + requireNonNull(functionAuthorization, "functionAuthorization is null"); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments