Add support for authorizing query context params (#12396)
authorJihoon Son <jihoonson@apache.org>
Thu, 21 Apr 2022 08:51:16 +0000 (01:51 -0700)
committerGitHub <noreply@github.com>
Thu, 21 Apr 2022 08:51:16 +0000 (14:21 +0530)
commit73ce5df22dc64a675be2ec5e6b86b1b6ad211808
tree9c8b253c3081446e430bb560567d5cdb72af6d72
parent4c6ba73823bc0561dad1ea9c0088238922438f23
Add support for authorizing query context params (#12396)

The query context is a way that the user gives a hint to the Druid query engine, so that they enforce a certain behavior or at least let the query engine prefer a certain plan during query planning. Today, there are 3 types of query context params as below.

Default context params. They are set via druid.query.default.context in runtime properties. Any user context params can be default params.
User context params. They are set in the user query request. See https://druid.apache.org/docs/latest/querying/query-context.html for parameters.
System context params. They are set by the Druid query engine during query processing. These params override other context params.
Today, any context params are allowed to users. This can cause
1) a bad UX if the context param is not matured yet or
2) even query failure or system fault in the worst case if a sensitive param is abused, ex) maxSubqueryRows.

This PR adds an ability to limit context params per user role. That means, a query will fail if you have a context param set in the query that is not allowed to you. To do that, this PR adds a new built-in resource type, QUERY_CONTEXT. The resource to authorize has a name of the context param (such as maxSubqueryRows) and the type of QUERY_CONTEXT. To allow a certain context param for a user, the user should be granted WRITE permission on the context param resource. Here is an example of the permission.

{
  "resourceAction" : {
    "resource" : {
      "name" : "maxSubqueryRows",
      "type" : "QUERY_CONTEXT"
    },
    "action" : "WRITE"
  },
  "resourceNamePattern" : "maxSubqueryRows"
}
Each role can have multiple permissions for context params. Each permission should be set for different context params.

When a query is issued with a query context X, the query will fail if the user who issued the query does not have WRITE permission on the query context X. In this case,

HTTP endpoints will return 403 response code.
JDBC will throw ForbiddenException.
Note: there is a context param called brokerService that is used only by the router. This param is used to pin your query to run it in a specific broker. Because the authorization is done not in the router, but in the broker, if you have brokerService set in your query without a proper permission, your query will fail in the broker after routing is done. Technically, this is not right because the authorization is checked after the context param takes effect. However, this should not cause any user-facing issue and thus should be OK. The query will still fail if the user doesn’t have permission for brokerService.

The context param authorization can be enabled using druid.auth.authorizeQueryContextParams. This is disabled by default to avoid any hassle when someone upgrades his cluster blindly without reading release notes.
48 files changed:
extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewQuery.java
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/sql/DoublesSketchApproxQuantileSqlAggregator.java
integration-tests/docker/environment-configs/common
integration-tests/docker/environment-configs/common-ldap
integration-tests/docker/ldap-configs/bootstrap.ldif
integration-tests/src/main/java/org/apache/druid/testing/utils/HttpUtil.java
integration-tests/src/test/java/org/apache/druid/tests/security/AbstractAuthConfigurationTest.java
integration-tests/src/test/java/org/apache/druid/tests/security/ITBasicAuthConfigurationTest.java
integration-tests/src/test/java/org/apache/druid/tests/security/ITBasicAuthLdapConfigurationTest.java
processing/src/main/java/org/apache/druid/query/BaseQuery.java
processing/src/main/java/org/apache/druid/query/Query.java
processing/src/main/java/org/apache/druid/query/QueryContext.java [new file with mode: 0644]
processing/src/main/java/org/apache/druid/query/select/SelectQuery.java
processing/src/test/java/org/apache/druid/query/QueryContextTest.java [new file with mode: 0644]
processing/src/test/java/org/apache/druid/query/scan/ScanQuerySpecTest.java
server/src/main/java/org/apache/druid/server/QueryLifecycle.java
server/src/main/java/org/apache/druid/server/QueryLifecycleFactory.java
server/src/main/java/org/apache/druid/server/QueryResource.java
server/src/main/java/org/apache/druid/server/security/AuthConfig.java
server/src/main/java/org/apache/druid/server/security/ResourceType.java
server/src/test/java/org/apache/druid/guice/security/DruidAuthModuleTest.java [new file with mode: 0644]
server/src/test/java/org/apache/druid/server/QueryLifecycleTest.java
server/src/test/java/org/apache/druid/server/security/AuthConfigTest.java [new file with mode: 0644]
sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
sql/src/main/java/org/apache/druid/sql/SqlLifecycleFactory.java
sql/src/main/java/org/apache/druid/sql/avatica/DruidConnection.java
sql/src/main/java/org/apache/druid/sql/avatica/DruidMeta.java
sql/src/main/java/org/apache/druid/sql/avatica/DruidStatement.java
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java
sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerConfig.java
sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerContext.java
sql/src/main/java/org/apache/druid/sql/calcite/planner/PlannerFactory.java
sql/src/main/java/org/apache/druid/sql/calcite/rel/DruidQuery.java
sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidJoinRule.java
sql/src/main/java/org/apache/druid/sql/calcite/view/DruidViewMacro.java
sql/src/main/java/org/apache/druid/sql/http/SqlResource.java
sql/src/test/java/org/apache/druid/sql/SqlLifecycleTest.java
sql/src/test/java/org/apache/druid/sql/avatica/DruidStatementTest.java
sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java
sql/src/test/java/org/apache/druid/sql/calcite/CalciteInsertDmlTest.java
sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java
sql/src/test/java/org/apache/druid/sql/calcite/DruidPlannerResourceAnalyzeTest.java
sql/src/test/java/org/apache/druid/sql/calcite/expression/ExpressionTestHelper.java
sql/src/test/java/org/apache/druid/sql/calcite/external/ExternalTableScanRuleTest.java
sql/src/test/java/org/apache/druid/sql/calcite/planner/DruidRexExecutorTest.java
sql/src/test/java/org/apache/druid/sql/calcite/rule/DruidJoinRuleTest.java
sql/src/test/java/org/apache/druid/sql/calcite/util/CalciteTests.java
sql/src/test/java/org/apache/druid/sql/http/SqlResourceTest.java