kgyrtkirk commented on code in PR #17774:
URL: https://github.com/apache/druid/pull/17774#discussion_r2014345753


##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty

Review Comment:
   its odd to see that both the field and the corresponding method has 
`@JsonProperty`



##########
server/src/test/java/org/apache/druid/server/coordination/ServerManagerTest.java:
##########
@@ -123,103 +118,77 @@
 
 public class ServerManagerTest
 {
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
+  private static ImmutableSet<DataSegment> DATA_SEGMENTS = new 
ImmutableSet.Builder<DataSegment>()
+      .add(TestSegmentUtils.makeSegment("test", "1", 
Intervals.of("P1d/2011-04-01")))
+      .add(TestSegmentUtils.makeSegment("test", "1", 
Intervals.of("P1d/2011-04-02")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("P1d/2011-04-02")))
+      .add(TestSegmentUtils.makeSegment("test", "1", 
Intervals.of("P1d/2011-04-03")))
+      .add(TestSegmentUtils.makeSegment("test", "1", 
Intervals.of("P1d/2011-04-04")))
+      .add(TestSegmentUtils.makeSegment("test", "1", 
Intervals.of("P1d/2011-04-05")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("PT1h/2011-04-04T01")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("PT1h/2011-04-04T02")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("PT1h/2011-04-04T03")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("PT1h/2011-04-04T05")))
+      .add(TestSegmentUtils.makeSegment("test", "2", 
Intervals.of("PT1h/2011-04-04T06")))
+      .add(TestSegmentUtils.makeSegment("test2", "1", 
Intervals.of("P1d/2011-04-01")))
+      .add(TestSegmentUtils.makeSegment("test2", "1", 
Intervals.of("P1d/2011-04-02")))
+      .build();
+
+  @Bind
+  private QueryRunnerFactoryConglomerate conglomerate;
+  @Bind
+  private SegmentManager segmentManager;
+  @Bind
+  private AuthConfig authConfig;
+  @Bind
+  private ServerConfig serverConfig;
+  @Bind
+  private ServiceEmitter serviceEmitter;
+  @Bind
+  private QueryProcessingPool queryProcessingPool;
+  @Bind
+  private CachePopulator cachePopulator;
+  @Bind
+  @Smile
+  private ObjectMapper objectMapper;
+  @Bind
+  private Cache cache;
+  @Bind
+  private CacheConfig cacheConfig;
 
-  private ServerManager serverManager;
   private MyQueryRunnerFactory factory;
-  private CountDownLatch queryWaitLatch;
-  private CountDownLatch queryWaitYieldLatch;
-  private CountDownLatch queryNotifyLatch;
   private ExecutorService serverManagerExec;
-  private SegmentManager segmentManager;
 
-  @Rule
-  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+  @Inject
+  private ServerManager serverManager;
 
   @Before
   public void setUp()
   {
-    final SegmentLoaderConfig loaderConfig = new SegmentLoaderConfig()
-    {
-      @Override
-      public File getInfoDir()
-      {
-        return temporaryFolder.getRoot();
-      }
-
-      @Override
-      public List<StorageLocationConfig> getLocations()
-      {
-        return Collections.singletonList(
-            new StorageLocationConfig(temporaryFolder.getRoot(), null, null)
-        );
-      }
-    };
-
-    final List<StorageLocation> storageLocations = 
loaderConfig.toStorageLocations();
-    final SegmentLocalCacheManager localCacheManager = new 
SegmentLocalCacheManager(
-        storageLocations,
-        loaderConfig,
-        new LeastBytesUsedStorageLocationSelectorStrategy(storageLocations),
-        TestIndex.INDEX_IO,
-        TestHelper.makeJsonMapper()
-    )
-    {
-      @Override
-      public ReferenceCountingSegment getSegment(final DataSegment dataSegment)
-      {
-        if (dataSegment.isTombstone()) {
-          return ReferenceCountingSegment
-              
.wrapSegment(TombstoneSegmentizerFactory.segmentForTombstone(dataSegment), 
dataSegment.getShardSpec());
-        } else {
-          return ReferenceCountingSegment.wrapSegment(new 
TestSegmentUtils.SegmentForTesting(
-              dataSegment.getDataSource(),
-              (Interval) dataSegment.getLoadSpec().get("interval"),
-              MapUtils.getString(dataSegment.getLoadSpec(), "version")
-          ), dataSegment.getShardSpec());
-        }
-      }
-    };
+    serviceEmitter = new NoopServiceEmitter();
+    EmittingLogger.registerEmitter(new NoopServiceEmitter());
+    segmentManager = new SegmentManager(new 
TestSegmentCacheManager(DATA_SEGMENTS));
+    for (DataSegment segment : DATA_SEGMENTS) {
+      loadQueryable(segment.getDataSource(), segment.getVersion(), 
segment.getInterval());
+    }
 
-    segmentManager = new SegmentManager(localCacheManager);
+    factory = new MyQueryRunnerFactory(new CountDownLatch(1), new 
CountDownLatch(1), new CountDownLatch(1));
+    // Only SearchQuery is supported in this test.
+    conglomerate = 
DefaultQueryRunnerFactoryConglomerate.buildFromQueryRunnerFactories(ImmutableMap.of(
+        SearchQuery.class,
+        factory
+    ));
 
-    EmittingLogger.registerEmitter(new NoopServiceEmitter());
-    queryWaitLatch = new CountDownLatch(1);
-    queryWaitYieldLatch = new CountDownLatch(1);
-    queryNotifyLatch = new CountDownLatch(1);
-    factory = new MyQueryRunnerFactory(queryWaitLatch, queryWaitYieldLatch, 
queryNotifyLatch);
     serverManagerExec = Execs.multiThreaded(2, "ServerManagerTest-%d");
-    QueryRunnerFactoryConglomerate conglomerate = 
DefaultQueryRunnerFactoryConglomerate.buildFromQueryRunnerFactories(ImmutableMap
-        .<Class<? extends Query>, QueryRunnerFactory>builder()
-        .put(SearchQuery.class, factory)
-        .build());
-    serverManager = new ServerManager(
-        conglomerate,
-        new NoopServiceEmitter(),
-        new ForwardingQueryProcessingPool(serverManagerExec),
-        new ForegroundCachePopulator(new DefaultObjectMapper(), new 
CachePopulatorStats(), -1),
-        new DefaultObjectMapper(),
-        new LocalCacheProvider().get(),
-        new CacheConfig(),
-        segmentManager,
-        new ServerConfig()
-    );
-
-    loadQueryable("test", "1", Intervals.of("P1d/2011-04-01"));
-    loadQueryable("test", "1", Intervals.of("P1d/2011-04-02"));
-    loadQueryable("test", "2", Intervals.of("P1d/2011-04-02"));
-    loadQueryable("test", "1", Intervals.of("P1d/2011-04-03"));
-    loadQueryable("test", "1", Intervals.of("P1d/2011-04-04"));
-    loadQueryable("test", "1", Intervals.of("P1d/2011-04-05"));
-    loadQueryable("test", "2", Intervals.of("PT1h/2011-04-04T01"));
-    loadQueryable("test", "2", Intervals.of("PT1h/2011-04-04T02"));
-    loadQueryable("test", "2", Intervals.of("PT1h/2011-04-04T03"));
-    loadQueryable("test", "2", Intervals.of("PT1h/2011-04-04T05"));
-    loadQueryable("test", "2", Intervals.of("PT1h/2011-04-04T06"));
-    loadQueryable("test2", "1", Intervals.of("P1d/2011-04-01"));
-    loadQueryable("test2", "1", Intervals.of("P1d/2011-04-02"));
-    loadQueryable("testTombstone", "1", Intervals.of("P1d/2011-04-02"));
+    queryProcessingPool = new ForwardingQueryProcessingPool(serverManagerExec);
+    cachePopulator = new ForegroundCachePopulator(new DefaultObjectMapper(), 
new CachePopulatorStats(), -1);
+    objectMapper = new DefaultObjectMapper();
+    cache = new LocalCacheProvider().get();
+    cacheConfig = new CacheConfig();
+    serverConfig = new ServerConfig();
+    authConfig = new AuthConfig();
+
+    Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);

Review Comment:
   
   instead of `BoundFieldModule` - would it be possible to create the injector 
and populate the necessary fields from that?
   



##########
server/src/test/java/org/apache/druid/server/coordination/ServerManagerTest.java:
##########
@@ -464,7 +430,49 @@ public void testMultipleDrops() throws Exception
   }
 
   @Test
-  public void 
testGetQueryRunnerForIntervalsWhenTimelineIsMissingReturningNoopQueryRunner()
+  public void testReferenceCounting_restrictedSegment() throws Exception
+  {
+    factory = new MyQueryRunnerFactory(new CountDownLatch(1), new 
CountDownLatch(1), new CountDownLatch(2));
+    conglomerate = 
DefaultQueryRunnerFactoryConglomerate.buildFromQueryRunnerFactories(ImmutableMap.of(
+        SearchQuery.class,
+        factory
+    ));
+    serverManager = 
Guice.createInjector(BoundFieldModule.of(this)).getInstance(ServerManager.class);

Review Comment:
   some things of the `@Before` is being overriden and repeated here



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());
+  }
+
+  /**
+   * Returns true if the security level requires that, every table must have a 
policy during query execution stage,
+   * this means the table must have a non-empty value in the policy map.
+   */
+  public boolean policyMustBeCheckedAndExistOnAllTables()
+  {
+    return tablePolicySecurityLevel.securityLevel >= 2.0;
+  }
+
+  @Override
+  public String toString()
+  {
+    return "PolicyConfig{tablePolicySecurityLevel="
+           + tablePolicySecurityLevel
+           + ", allowedPolicies="
+           + allowedPolicies
+           + '}';
+  }
+
+  @Override
+  public boolean equals(Object o)
+  {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    PolicyConfig that = (PolicyConfig) o;
+    return Objects.equals(tablePolicySecurityLevel, 
that.tablePolicySecurityLevel) &&
+           Objects.equals(allowedPolicies, that.allowedPolicies);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return Objects.hash(tablePolicySecurityLevel, allowedPolicies);
+  }
+
+
+  /**
+   * Defines how strict we want to enforce the policy on tables during query 
execution process.
+   * <ol>
+   *   <li>{@code APPLY_WHEN_APPLICABLE}, the most basic level, restriction is 
applied whenever seen fit.
+   *   <li>{@code POLICY_CHECKED_ON_ALL_TABLES_POLICY_MUST_EXIST}, every table 
must have a policy when requests come from external users.
+   * </ol>
+   */
+  public enum TablePolicySecurityLevel
+  {
+    @JsonProperty("0") APPLY_WHEN_APPLICABLE(0f),
+    @JsonProperty("2.0f") POLICY_CHECKED_ON_ALL_TABLES_POLICY_MUST_EXIST(2.0f);

Review Comment:
   why is `securityLevel` a `float`?
   what does `2.0f` mean?



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());
+  }
+
+  /**
+   * Returns true if the security level requires that, every table must have a 
policy during query execution stage,
+   * this means the table must have a non-empty value in the policy map.
+   */
+  public boolean policyMustBeCheckedAndExistOnAllTables()
+  {
+    return tablePolicySecurityLevel.securityLevel >= 2.0;
+  }
+
+  @Override
+  public String toString()
+  {
+    return "PolicyConfig{tablePolicySecurityLevel="
+           + tablePolicySecurityLevel
+           + ", allowedPolicies="
+           + allowedPolicies
+           + '}';
+  }
+
+  @Override
+  public boolean equals(Object o)
+  {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    PolicyConfig that = (PolicyConfig) o;
+    return Objects.equals(tablePolicySecurityLevel, 
that.tablePolicySecurityLevel) &&
+           Objects.equals(allowedPolicies, that.allowedPolicies);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return Objects.hash(tablePolicySecurityLevel, allowedPolicies);
+  }
+
+
+  /**
+   * Defines how strict we want to enforce the policy on tables during query 
execution process.
+   * <ol>
+   *   <li>{@code APPLY_WHEN_APPLICABLE}, the most basic level, restriction is 
applied whenever seen fit.

Review Comment:
   I think this line should be an apidoc on the enum value



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());
+  }
+
+  /**
+   * Returns true if the security level requires that, every table must have a 
policy during query execution stage,
+   * this means the table must have a non-empty value in the policy map.
+   */
+  public boolean policyMustBeCheckedAndExistOnAllTables()

Review Comment:
   I was wondering about an alternate way of implementing this:
   * `PolicyConfig` is a dispatcher
   * it has a list of `PolicyValidator` -s ; maybe those classes are 
instantiated with the default constructor?
   * the `allowPolicy` and other methods are dispatched to the ones below
   * `PolicyConfig` could also implement the `PolicyValidatory` interface
   
   I was also wondering if the whole `PolicyConfig` is a too much - wouldn't it 
be enough to enable the users to just bind a `PolicyValidator`  they want to 
use; by specifying its classname somehow - and leave the dispatch or not / etc 
problems alone :)



##########
server/src/test/java/org/apache/druid/server/coordination/SegmentBootstrapperTest.java:
##########
@@ -37,6 +37,7 @@
 import org.apache.druid.segment.loading.StorageLocationConfig;
 import org.apache.druid.server.SegmentManager;
 import org.apache.druid.server.metrics.DataSourceTaskIdHolder;
+import org.apache.druid.test.utils.TestSegmentCacheManager;

Review Comment:
   nit: are there any relevant changes in this file beyond this import?
   do we need all those indentation changes?



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());

Review Comment:
   I was not expecting `SimpleName` trick here...
   it seems to me that this kinda sets it in stone that a `Policy` may not have 
any state
   
   can you please add a validation that when the `PolicyConfig` is created its 
ensured that `allowedPolicies` are possibly exisitng `Policy`-s



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());
+  }
+
+  /**
+   * Returns true if the security level requires that, every table must have a 
policy during query execution stage,
+   * this means the table must have a non-empty value in the policy map.
+   */
+  public boolean policyMustBeCheckedAndExistOnAllTables()
+  {
+    return tablePolicySecurityLevel.securityLevel >= 2.0;
+  }
+
+  @Override
+  public String toString()
+  {
+    return "PolicyConfig{tablePolicySecurityLevel="
+           + tablePolicySecurityLevel
+           + ", allowedPolicies="
+           + allowedPolicies
+           + '}';
+  }
+
+  @Override
+  public boolean equals(Object o)
+  {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    PolicyConfig that = (PolicyConfig) o;
+    return Objects.equals(tablePolicySecurityLevel, 
that.tablePolicySecurityLevel) &&
+           Objects.equals(allowedPolicies, that.allowedPolicies);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return Objects.hash(tablePolicySecurityLevel, allowedPolicies);
+  }
+
+
+  /**
+   * Defines how strict we want to enforce the policy on tables during query 
execution process.
+   * <ol>
+   *   <li>{@code APPLY_WHEN_APPLICABLE}, the most basic level, restriction is 
applied whenever seen fit.
+   *   <li>{@code POLICY_CHECKED_ON_ALL_TABLES_POLICY_MUST_EXIST}, every table 
must have a policy when requests come from external users.

Review Comment:
   I think a shorter one would be: `MANDATORY` ; or `ENFORCED` 



##########
server/src/main/java/org/apache/druid/server/coordination/ServerManager.java:
##########
@@ -190,6 +195,11 @@ public <T> QueryRunner<T> 
getQueryRunnerForSegments(Query<T> theQuery, Iterable<
       throw new ISE("Cannot handle subquery: %s", dataSourceFromQuery);
     }
 
+    if (!(theQuery instanceof SegmentMetadataQuery)
+        && 
!dataSourceFromQuery.validate(authConfig.getTableSecurityPolicyConfig())) {
+      throw new ISE("Failed security validation with dataSource [%s]", 
dataSourceFromQuery);
+    }

Review Comment:
   I think instead of an `insteanceof` there could be a `Query#validate` which 
would dispatch it correctly - but for `SegmentMetadataQuery` that's overriden.
   
   



##########
processing/src/main/java/org/apache/druid/query/policy/PolicyConfig.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.policy;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+
+import javax.annotation.Nonnull;
+import java.util.Objects;
+
+/**
+ * Defines how strict we want to enforce the policy on tables during query 
execution process.
+ */
+public class PolicyConfig
+{
+  @JsonProperty
+  private final TablePolicySecurityLevel tablePolicySecurityLevel;
+
+  @JsonProperty
+  private final ImmutableList<String> allowedPolicies;
+
+  @JsonCreator
+  public PolicyConfig(
+      @JsonProperty("tablePolicySecurityLevel") TablePolicySecurityLevel 
tablePolicySecurityLevel,
+      @JsonProperty("allowedPolicies") ImmutableList<String> allowedPolicies
+  )
+  {
+    this.tablePolicySecurityLevel = tablePolicySecurityLevel;
+    this.allowedPolicies = allowedPolicies;
+  }
+
+  /**
+   * Returns a {@link PolicyConfig} instance that applies restrictions 
whenever seen fit, and allows all policy types.
+   */
+  public static PolicyConfig defaultInstance()
+  {
+    return new PolicyConfig(TablePolicySecurityLevel.APPLY_WHEN_APPLICABLE, 
ImmutableList.of());
+  }
+
+  /**
+   * Returns the table policy security level, higher value means stricter 
policy.
+   *
+   * @see TablePolicySecurityLevel
+   */
+  @JsonProperty
+  public TablePolicySecurityLevel getTablePolicySecurityLevel()
+  {
+    return tablePolicySecurityLevel;
+  }
+
+  /**
+   * Returns a list of allowed policy class.
+   * <p>
+   * An empty list means all policy classes are allowed.
+   */
+  @JsonProperty
+  public ImmutableList<String> getAllowedPolicies()
+  {
+    return allowedPolicies;
+  }
+
+  public boolean allowPolicy(@Nonnull Policy policy)
+  {
+    return allowedPolicies.isEmpty() || 
allowedPolicies.contains(policy.getClass().getSimpleName());
+  }
+
+  /**
+   * Returns true if the security level requires that, every table must have a 
policy during query execution stage,
+   * this means the table must have a non-empty value in the policy map.
+   */
+  public boolean policyMustBeCheckedAndExistOnAllTables()
+  {
+    return tablePolicySecurityLevel.securityLevel >= 2.0;
+  }
+
+  @Override
+  public String toString()
+  {
+    return "PolicyConfig{tablePolicySecurityLevel="
+           + tablePolicySecurityLevel
+           + ", allowedPolicies="
+           + allowedPolicies
+           + '}';
+  }
+
+  @Override
+  public boolean equals(Object o)
+  {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    PolicyConfig that = (PolicyConfig) o;
+    return Objects.equals(tablePolicySecurityLevel, 
that.tablePolicySecurityLevel) &&
+           Objects.equals(allowedPolicies, that.allowedPolicies);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return Objects.hash(tablePolicySecurityLevel, allowedPolicies);
+  }
+
+
+  /**
+   * Defines how strict we want to enforce the policy on tables during query 
execution process.
+   * <ol>
+   *   <li>{@code APPLY_WHEN_APPLICABLE}, the most basic level, restriction is 
applied whenever seen fit.
+   *   <li>{@code POLICY_CHECKED_ON_ALL_TABLES_POLICY_MUST_EXIST}, every table 
must have a policy when requests come from external users.
+   * </ol>
+   */
+  public enum TablePolicySecurityLevel
+  {
+    @JsonProperty("0") APPLY_WHEN_APPLICABLE(0f),

Review Comment:
   I believe this name is a bit unfortunate; I feel like this wants to be 
something like "allow all when not blocked"
   if yes then it could be: `ALLOW_MISSING_POLICY`
   



-- 
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]


Reply via email to