Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.pinot.spi.auth.TableRowColAccessResult;
import org.apache.pinot.spi.auth.TableRowColAccessResultImpl;
import org.apache.pinot.spi.auth.broker.RequesterIdentity;
import org.apache.pinot.spi.utils.builder.TableNameBuilder;


@InterfaceAudience.Public
Expand Down Expand Up @@ -114,6 +115,21 @@ default TableAuthorizationResult authorize(RequesterIdentity requesterIdentity,
}


/// Authorizes the physical OFFLINE tables read by the single-stage `LOOKUP` function.
default TableAuthorizationResult authorizeLookupTables(RequesterIdentity requesterIdentity,
Set<String> offlineTableNames) {
return authorize(requesterIdentity, offlineTableNames);
}

/// Returns whether a row-level filter applies to the physical OFFLINE target of an authorized `LOOKUP`.
default boolean hasLookupTableRowFilters(RequesterIdentity requesterIdentity, String offlineTableName) {
String rawTableName = TableNameBuilder.extractRawTableName(offlineTableName);
return getRowColFilters(requesterIdentity, offlineTableName).getRLSFilters()
.filter(filters -> !filters.isEmpty()).isPresent()
|| getRowColFilters(requesterIdentity, rawTableName).getRLSFilters()
.filter(filters -> !filters.isEmpty()).isPresent();
}

/// Verifies that the requester can delete rows from the table with a SQL `DELETE` statement. The broker calls it
/// once the requester passed, in this order: [#authorize(RequesterIdentity)], [#authorize(RequesterIdentity, Set)]
/// with the table as its only element (the WHERE clause of the statement reads it), the fine-grained
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
package org.apache.pinot.broker.broker;

import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.StringUtils;
import org.apache.pinot.broker.api.AccessControl;
import org.apache.pinot.common.auth.BasicAuthTokenUtils;
import org.apache.pinot.common.request.BrokerRequest;
Expand All @@ -41,6 +44,7 @@
import org.apache.pinot.spi.auth.TableRowColAccessResultImpl;
import org.apache.pinot.spi.auth.broker.RequesterIdentity;
import org.apache.pinot.spi.env.PinotConfiguration;
import org.apache.pinot.spi.utils.builder.TableNameBuilder;


/// Basic Authentication based on http headers. Configured via the "pinot.broker.access.control" family of properties.
Expand All @@ -60,6 +64,7 @@
/// of its principals before.
public class BasicAuthAccessControlFactory extends AccessControlFactory {
private static final String PREFIX = "principals";
private static final String RLS_SUFFIX = ".rls";

private AccessControl _accessControl;

Expand All @@ -69,8 +74,17 @@ public BasicAuthAccessControlFactory() {

@Override
public void init(PinotConfiguration configuration) {
_accessControl = new BasicAuthAccessControl(
BasicAuthPrincipalUtils.extractBasicAuthPrincipals(configuration, PREFIX));
// LOOKUP may match a raw or physical grant. Keep the filter keys for both names even if only one name was
// listed in the principal's table grants. Ordinary queries continue using the principal's parsed filters.
Map<String, Boolean> lookupRowFilterKeys = configuration.getKeys().stream()
.filter(key -> key.startsWith(PREFIX + ".") && key.endsWith(RLS_SUFFIX))
.filter(key -> {
String filters = configuration.getProperty(key);
return filters != null && Arrays.stream(filters.split(",")).anyMatch(StringUtils::isNotBlank);
})
.collect(Collectors.toMap(key -> key, key -> Boolean.TRUE));
_accessControl = new BasicAuthAccessControl(BasicAuthPrincipalUtils.extractBasicAuthPrincipals(configuration,
PREFIX), new PinotConfiguration(lookupRowFilterKeys, Map.of()));
}

@Override
Expand All @@ -81,9 +95,11 @@ public AccessControl create() {
/// Access Control using header-based basic http authentication
private static class BasicAuthAccessControl implements AccessControl {
private final Map<String, BasicAuthPrincipal> _token2principal;
private final PinotConfiguration _lookupRowFilterKeys;

public BasicAuthAccessControl(Collection<BasicAuthPrincipal> principals) {
public BasicAuthAccessControl(Collection<BasicAuthPrincipal> principals, PinotConfiguration lookupRowFilterKeys) {
_token2principal = principals.stream().collect(Collectors.toMap(BasicAuthPrincipal::getToken, p -> p));
_lookupRowFilterKeys = lookupRowFilterKeys;
}

@Override
Expand Down Expand Up @@ -119,6 +135,17 @@ public AuthorizationResult authorize(RequesterIdentity requesterIdentity, Broker

@Override
public TableAuthorizationResult authorize(RequesterIdentity requesterIdentity, Set<String> tables) {
return authorizeTables(requesterIdentity, tables, BasicAuthPrincipal::hasTable);
}

@Override
public TableAuthorizationResult authorizeLookupTables(RequesterIdentity requesterIdentity,
Set<String> offlineTableNames) {
return authorizeTables(requesterIdentity, offlineTableNames, BasicAuthPrincipal::hasLookupTable);
}

private TableAuthorizationResult authorizeTables(RequesterIdentity requesterIdentity, Set<String> tables,
BiPredicate<BasicAuthPrincipal, String> hasTableAccess) {
Optional<BasicAuthPrincipal> principalOpt = getPrincipalOpt(requesterIdentity);

if (!principalOpt.isPresent()) {
Expand All @@ -131,7 +158,7 @@ public TableAuthorizationResult authorize(RequesterIdentity requesterIdentity, S
BasicAuthPrincipal principal = principalOpt.get();
Set<String> failedTables = new HashSet<>();
for (String table : tables) {
if (!principal.hasTable(table)) {
if (!hasTableAccess.test(principal, table)) {
failedTables.add(table);
}
}
Expand All @@ -152,6 +179,16 @@ public AuthorizationResult authorizeDeleteRows(RequesterIdentity requesterIdenti
principal != null && principal.hasTable(tableName));
}

@Override
public boolean hasLookupTableRowFilters(RequesterIdentity requesterIdentity, String offlineTableName) {
BasicAuthPrincipal principal = getPrincipalOpt(requesterIdentity)
.orElseThrow(() -> new IllegalStateException("Principal is not authorized"));
String principalPrefix = PREFIX + "." + principal.getName() + ".";
String rawTableName = TableNameBuilder.extractRawTableName(offlineTableName);
return _lookupRowFilterKeys.containsKey(principalPrefix + offlineTableName + RLS_SUFFIX)
|| _lookupRowFilterKeys.containsKey(principalPrefix + rawTableName + RLS_SUFFIX);
}

@Override
public TableRowColAccessResult getRowColFilters(RequesterIdentity requesterIdentity, String table) {
Optional<BasicAuthPrincipal> principalOpt = getPrincipalOpt(requesterIdentity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,17 @@ public BrokerResponse handleRequest(JsonNode request, @Nullable SqlNodeAndOption
String query = sql.textValue();
requestContext.setQuery(query);

// Parse the query if needed
if (sqlNodeAndOptions == null) {
try {
try {
if (sqlNodeAndOptions == null) {
sqlNodeAndOptions = RequestUtils.parseQuery(query, request);
} catch (Exception e) {
// Do not log or emit metric here because it is pure user error
QueryErrorCode errorCode = QueryErrorCode.fromThrowable(e, QueryErrorCode.SQL_PARSING);
requestContext.setErrorCode(errorCode);
return new BrokerResponseNative(errorCode, e.getMessage());
} else {
RequestUtils.validateClientQueryOptions(sqlNodeAndOptions.getOptions());
}
} catch (Exception e) {
// Do not log or emit metric here because it is pure user error
QueryErrorCode errorCode = QueryErrorCode.fromThrowable(e, QueryErrorCode.SQL_PARSING);
requestContext.setErrorCode(errorCode);
return new BrokerResponseNative(errorCode, e.getMessage());
}

// check app qps before doing anything
Expand Down Expand Up @@ -299,10 +300,17 @@ protected abstract BrokerResponse handleRequest(long requestId, String query, Sq
/// Validates whether the requester has access to all the tables.
protected TableAuthorizationResult hasTableAccess(RequesterIdentity requesterIdentity, Set<String> tableNames,
RequestContext requestContext, HttpHeaders httpHeaders) {
return hasTableAccess(requesterIdentity, tableNames, requestContext, httpHeaders, false);
}

protected TableAuthorizationResult hasTableAccess(RequesterIdentity requesterIdentity, Set<String> tableNames,
RequestContext requestContext, HttpHeaders httpHeaders, boolean lookupTables) {
final long startTimeNs = System.nanoTime();
AccessControl accessControl = _accessControlFactory.create();

TableAuthorizationResult tableAuthorizationResult = accessControl.authorize(requesterIdentity, tableNames);
TableAuthorizationResult tableAuthorizationResult = lookupTables
? accessControl.authorizeLookupTables(requesterIdentity, tableNames)
: accessControl.authorize(requesterIdentity, tableNames);

Set<String> failedTables = tableNames.stream()
.filter(table -> !accessControl.hasAccess(httpHeaders, TargetType.TABLE, table, Actions.Table.QUERY))
Expand Down
Loading