You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "abhishekrb19 (via GitHub)" <gi...@apache.org> on 2023/03/29 20:01:52 UTC

[GitHub] [druid] abhishekrb19 commented on a diff in pull request #13989: Allow for Input source security in SQL layer

abhishekrb19 commented on code in PR #13989:
URL: https://github.com/apache/druid/pull/13989#discussion_r1152418051


##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteInsertDmlTest.java:
##########
@@ -307,6 +308,39 @@ public void testInsertFromExternal()
         .verify();
   }
 
+  @Test
+  public void testInsertFromExternalWithInputSourceSecurityEnabled()
+  {
+    testIngestionQuery()
+        .sql("INSERT INTO dst SELECT * FROM %s PARTITIONED BY ALL TIME", externSql(externalDataSource))
+        .authentication(CalciteTests.SUPER_USER_AUTH_RESULT)
+        .authConfig(AuthConfig.newBuilder().setEnableInputSourceSecurity(true).build())
+        .expectTarget("dst", externalDataSource.getSignature())
+        .expectResources(dataSourceWrite("dst"), externalRead("inline"))
+        .expectQuery(
+            newScanQueryBuilder()
+                .dataSource(externalDataSource)
+                .intervals(querySegmentSpec(Filtration.eternity()))
+                .columns("x", "y", "z")
+                .context(PARTITIONED_BY_ALL_TIME_QUERY_CONTEXT)
+                .build()
+        )
+        .expectLogicalPlanFrom("insertFromExternal")
+        .verify();
+  }
+
+  @Test
+  public void testUnauthorizedInsertFromExternalWithInputSourceSecurityEnabled()
+  {
+    testIngestionQuery()
+        .sql("INSERT INTO dst SELECT * FROM %s PARTITIONED BY ALL TIME", externSql(externalDataSource))
+        .authentication(CalciteTests.REGULAR_USER_AUTH_RESULT)
+        .authConfig(AuthConfig.newBuilder().setEnableInputSourceSecurity(true).build())

Review Comment:
   All the tests either implicitly disable the input source security (via a default) or explicitly enable it. It'll be nice also to have a test where we explicitly disable it.
   
   Perhaps a unit test similar to this, where we `setEnableInputSourceSecurity` set to `false`, and we shouldn't get a `ForbiddenException`. 



##########
sql/src/main/java/org/apache/druid/sql/calcite/external/DruidExternTableMacro.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.sql.calcite.external;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.util.NlsString;
+import org.apache.druid.server.security.Action;
+import org.apache.druid.server.security.Resource;
+import org.apache.druid.server.security.ResourceAction;
+import org.apache.druid.server.security.ResourceType;
+import org.apache.druid.sql.calcite.table.DruidTable;
+
+import javax.validation.constraints.NotNull;
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Used by {@link ExternalOperatorConversion} to generate a {@link DruidTable}
+ * that references an {@link ExternalDataSource}.
+ */
+public class DruidExternTableMacro extends DruidUserDefinedTableMacro
+{
+  public DruidExternTableMacro(DruidTableMacro macro)
+  {
+    super(macro);
+  }
+
+  @Override
+  public Set<ResourceAction> computeResources(final SqlCall call, boolean inputSourceTypeSecurityEnabled)
+  {
+    if (!inputSourceTypeSecurityEnabled) {
+      return Collections.singleton(Externals.EXTERNAL_RESOURCE_ACTION);
+    }
+    String inputSourceStr = getInputSourceArgument(call);
+
+    try {
+      JsonNode jsonNode = ((DruidTableMacro) macro).getJsonMapper().readTree(inputSourceStr);
+      return Collections.singleton(new ResourceAction(new Resource(
+          ResourceType.EXTERNAL,
+          jsonNode.get("type").asText()
+      ), Action.READ));
+    }
+    catch (JsonProcessingException e) {
+      // this shouldn't happen, the input source paraemeter should have been validated before this
+      throw new RuntimeException(e);
+    }
+  }
+
+  @NotNull
+  private String getInputSourceArgument(final SqlCall call)
+  {
+    // this covers case where parameters are used positionally
+    if (call.getOperandList().size() > 0) {
+      if (call.getOperandList().get(0) instanceof SqlCharStringLiteral) {
+        return ((SqlCharStringLiteral) call.getOperandList().get(0)).toValue();
+      }
+    }
+
+    // this covers case where named parameters are used.
+    for (SqlNode sqlNode : call.getOperandList()) {
+      if (sqlNode instanceof SqlCall) {
+        String argumentName = ((SqlCall) sqlNode).getOperandList().size() > 1 ?
+                             ((SqlCall) sqlNode).getOperandList().get(1).toString()
+                             : null;
+        if (ExternalOperatorConversion.INPUT_SOURCE_PARAM.equals(argumentName)) {
+          return ((NlsString) ((SqlCharStringLiteral) ((SqlCall) call.getOperandList().get(0))
+              .getOperandList()
+              .get(0))
+              .getValue())
+              .getValue();
+        }
+      }
+    }
+    // this shouldn't happen, as the sqlCall should have been validated by this point,

Review Comment:
   nit: `sqlCall` -> `call` or if you're referring to the type `SqlCall`



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/SqlResourceCollectorShuttle.java:
##########
@@ -51,19 +51,24 @@ public class SqlResourceCollectorShuttle extends SqlShuttle
   private final Set<ResourceAction> resourceActions;
   private final PlannerContext plannerContext;
   private final SqlValidator validator;
+  private final boolean inputSourceTypeSecurityEnabled;
 
   public SqlResourceCollectorShuttle(SqlValidator validator, PlannerContext plannerContext)
   {
     this.validator = validator;
     this.resourceActions = new HashSet<>();
     this.plannerContext = plannerContext;
+    inputSourceTypeSecurityEnabled = plannerContext.getPlannerToolbox().getAuthConfig().isEnableInputSourceSecurity();

Review Comment:
   This class already holds `plannerContext` state, so we could just reference this line inline directly in the `visit()` call?



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org