cecemei commented on code in PR #17564:
URL: https://github.com/apache/druid/pull/17564#discussion_r1891060395
##########
server/src/main/java/org/apache/druid/server/security/Access.java:
##########
@@ -21,52 +21,86 @@
import com.google.common.base.Strings;
import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.filter.DimFilter;
+
+import javax.annotation.Nullable;
+import java.util.Objects;
+import java.util.Optional;
public class Access
{
public static final String DEFAULT_ERROR_MESSAGE = "Unauthorized";
+ public static final String DEFAULT_AUTHORIZED_MESSAGE = "Authorized";
- public static final Access OK = new Access(true);
- public static final Access DENIED = new Access(false);
+ public static final Access OK = Access.allow();
+ public static final Access DENIED = Access.deny("");
private final boolean allowed;
private final String message;
+ // A row-level policy filter on top of table-level read access. It should be
empty if there are no policy restrictions
+ // or if access is requested for an action other than reading the table.
+ private final Optional<DimFilter> rowFilter;
+ /**
+ * @deprecated use {@link #allow()} or {@link #deny(String)} instead
+ */
+ @Deprecated
public Access(boolean allowed)
{
- this(allowed, "");
+ this(allowed, "", Optional.empty());
}
- public Access(boolean allowed, String message)
+ Access(boolean allowed, String message, Optional<DimFilter> rowFilter)
{
this.allowed = allowed;
this.message = message;
+ this.rowFilter = rowFilter;
+ }
+
+ public static Access allow()
+ {
+ return new Access(true, "", Optional.empty());
+ }
+
+ public static Access deny(@Nullable String message)
+ {
+ return new Access(false, Objects.isNull(message) ? "" : message,
Optional.empty());
Review Comment:
sg, updated this
##########
processing/src/main/java/org/apache/druid/query/RestrictedDataSource.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.query;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.query.filter.DimFilter;
+import org.apache.druid.query.filter.TrueDimFilter;
+import org.apache.druid.query.planning.DataSourceAnalysis;
+import org.apache.druid.segment.RestrictedSegment;
+import org.apache.druid.segment.SegmentReference;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
+
+/**
+ * Reperesents a TableDataSource with row-level restriction.
+ * <p>
+ * A RestrictedDataSource means the base TableDataSource has restriction
imposed. A table without any restriction should
+ * never be transformed to a RestrictedDataSource. Druid internal system and
admin users would have a null rowFilter,
+ * while external users would have a rowFilter based on the applied
restriction.
+ */
+public class RestrictedDataSource implements DataSource
+{
+ private final TableDataSource base;
+ @Nullable
+ private final DimFilter rowFilter;
+
+ @JsonProperty("base")
+ public TableDataSource getBase()
+ {
+ return base;
+ }
+
+ /**
+ * Returns true if the row-level filter imposes no restrictions.
+ */
+ public boolean allowAll()
+ {
+ return Objects.isNull(rowFilter) ||
rowFilter.equals(TrueDimFilter.instance());
+ }
+
+ @Nullable
+ @JsonProperty("filter")
+ public DimFilter getFilter()
+ {
+ return rowFilter;
+ }
+
+ RestrictedDataSource(TableDataSource base, @Nullable DimFilter rowFilter)
+ {
+ this.base = base;
+ this.rowFilter = rowFilter;
+ }
+
+ @JsonCreator
+ public static RestrictedDataSource create(
+ @JsonProperty("base") DataSource base,
+ @Nullable @JsonProperty("filter") DimFilter rowFilter
+ )
+ {
+ if (!(base instanceof TableDataSource)) {
+ throw new IAE("Expected a TableDataSource, got [%s]", base.getClass());
+ }
+ return new RestrictedDataSource((TableDataSource) base, rowFilter);
+ }
+
+ @Override
+ public Set<String> getTableNames()
+ {
+ return base.getTableNames();
+ }
+
+ @Override
+ public List<DataSource> getChildren()
+ {
+ return ImmutableList.of(base);
+ }
+
+ @Override
+ public DataSource withChildren(List<DataSource> children)
+ {
+ if (children.size() != 1) {
+ throw new IAE("Expected [1] child, got [%d]", children.size());
+ }
+
+ return create(children.get(0), rowFilter);
+ }
+
+ @Override
+ public boolean isCacheable(boolean isBroker)
+ {
+ return false;
+ }
+
+ @Override
+ public boolean isGlobal()
+ {
+ return base.isGlobal();
+ }
+
+ @Override
+ public boolean isConcrete()
+ {
+ return base.isConcrete();
+ }
+
+ @Override
+ public Function<SegmentReference, SegmentReference> createSegmentMapFunction(
+ Query query,
+ AtomicLong cpuTimeAccumulator
+ )
+ {
+ return JvmUtils.safeAccumulateThreadCpuTime(
+ cpuTimeAccumulator,
+ () -> base.createSegmentMapFunction(
+ query,
+ cpuTimeAccumulator
+ ).andThen((segment) -> (new RestrictedSegment(segment, rowFilter)))
+ );
+ }
+
+ @Override
+ public DataSource withUpdatedDataSource(DataSource newSource)
+ {
+ return create(newSource, rowFilter);
+ }
+
+ @Override
+ public DataSource mapWithRestriction(Map<String, Optional<DimFilter>>
rowFilters, boolean enableStrictPolicyCheck)
+ {
+ if (!rowFilters.containsKey(this.base.getName()) &&
enableStrictPolicyCheck) {
+ throw DruidException.defensive("Missing row filter for table [%s]",
this.base.getName());
+ }
+
+ Optional<DimFilter> newFilter =
rowFilters.getOrDefault(this.base.getName(), Optional.empty());
+ if (!newFilter.isPresent()) {
+ throw DruidException.defensive(
+ "No restriction found on table [%s], but had %s before.",
+ this.base.getName(),
+ this.rowFilter
+ );
+ }
+ if (newFilter.get().equals(TrueDimFilter.instance())) {
+ // The internal druid_system always has a TrueDimFilter, whic can be
applied in conjunction with an external user's filter.
+ return this;
+ } else if (newFilter.get().equals(rowFilter)) {
+ // This likely occurs when we perform an authentication check for the
same user more than once, which is not ideal.
+ return this;
+ } else {
+ throw new ISE("Incompatible restrictions on [%s]: %s and %s",
this.base.getName(), rowFilter, newFilter.get());
+ }
+ }
+
+ @Override
+ public String toString()
+ {
+ try {
+ return "RestrictedDataSource{" +
+ "base=" + base +
+ ", filter='" + rowFilter + '}';
+ }
+ catch (Exception e) {
Review Comment:
true, updated this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]