Jackie-Jiang commented on code in PR #14844:
URL: https://github.com/apache/pinot/pull/14844#discussion_r1983743775


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/function/GroovyStaticAnalyzerConfig.java:
##########
@@ -0,0 +1,153 @@
+/**
+ * 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.pinot.segment.local.function;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.apache.pinot.spi.utils.JsonUtils;
+
+
+public class GroovyStaticAnalyzerConfig {
+  private final List<String> _allowedReceivers;
+  private final List<String> _allowedImports;
+  private final List<String> _allowedStaticImports;
+  private final List<String> _disallowedMethodNames;
+  private final boolean _methodDefinitionAllowed;
+
+  public GroovyStaticAnalyzerConfig(
+      @JsonProperty("allowedReceivers")
+      List<String> allowedReceivers,
+      @JsonProperty("allowedImports")
+      List<String> allowedImports,
+      @JsonProperty("allowedStaticImports")
+      List<String> allowedStaticImports,
+      @JsonProperty("disallowedMethodNames")
+      List<String> disallowedMethodNames,
+      @JsonProperty("methodDefinitionAllowed")
+      boolean methodDefinitionAllowed) {
+    _allowedImports = allowedImports;
+    _allowedReceivers = allowedReceivers;
+    _allowedStaticImports = allowedStaticImports;
+    _disallowedMethodNames = disallowedMethodNames;
+    _methodDefinitionAllowed = methodDefinitionAllowed;
+  }
+
+  @JsonProperty("allowedReceivers")
+  public List<String> getAllowedReceivers() {
+    return _allowedReceivers;
+  }
+
+  @JsonProperty("allowedImports")
+  public List<String> getAllowedImports() {
+    return _allowedImports;
+  }
+
+  @JsonProperty("allowedStaticImports")
+  public List<String> getAllowedStaticImports() {
+    return _allowedStaticImports;
+  }
+
+  @JsonProperty("disallowedMethodNames")
+  public List<String> getDisallowedMethodNames() {
+    return _disallowedMethodNames;
+  }
+
+  @JsonProperty("methodDefinitionAllowed")
+  public boolean isMethodDefinitionAllowed() {
+    return _methodDefinitionAllowed;
+  }
+
+  public ZNRecord toZNRecord() throws JsonProcessingException {
+    ZNRecord record = new ZNRecord("groovySecurityConfiguration");
+    record.setSimpleField("staticAnalyzerConfig", toJson());
+    return record;
+  }
+
+  public static GroovyStaticAnalyzerConfig fromZNRecord(ZNRecord zr) throws 
JsonProcessingException {
+    
Preconditions.checkArgument(zr.getId().equals("groovySecurityConfiguration"),
+        "Expected ZNRecord with ID \"groovySecurityConfiguration\" but got 
{}", zr.getId());
+
+    final String configJson = zr.getSimpleField("staticAnalyzerConfig");
+    return fromJson(configJson);
+  }

Review Comment:
   Should we remove these 2 methods?



##########
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotClusterConfigs.java:
##########
@@ -168,4 +177,88 @@ public SuccessResponse deleteClusterConfig(
       throw new ControllerApplicationException(LOGGER, errStr, 
Response.Status.INTERNAL_SERVER_ERROR, e);
     }
   }
+
+  @GET
+  @Path("/cluster/configs/groovy/staticAnalyzerConfig")
+  @Authorize(targetType = TargetType.CLUSTER, action = 
Actions.Cluster.GET_GROOVY_STATIC_ANALYZER_CONFIG)
+  @Produces(MediaType.APPLICATION_JSON)
+  @ApiOperation(value = "Get the configuration for Groovy Static analysis",
+      notes = "Get the configuration for Groovy static analysis")
+  @ApiResponses(value = {
+      @ApiResponse(code = 200, message = "Success"),
+      @ApiResponse(code = 500, message = "Internal server error")
+  })
+  public String getGroovyStaticAnalysisConfig()
+      throws Exception {
+    HelixAdmin helixAdmin = _pinotHelixResourceManager.getHelixAdmin();
+    HelixConfigScope configScope = new 
HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER)
+        .forCluster(_pinotHelixResourceManager.getHelixClusterName()).build();
+    Map<String, String> configs = helixAdmin.getConfig(configScope, 
GROOVY_STATIC_ANALYZER_CONFIG_LIST);
+    if (configs == null) {
+      return null;
+    }
+
+    Map<String, GroovyStaticAnalyzerConfig> groovyStaticAnalyzerConfigMap = 
new HashMap<>();
+    for (Map.Entry<String, String> entry : configs.entrySet()) {
+      groovyStaticAnalyzerConfigMap.put(entry.getKey(), 
GroovyStaticAnalyzerConfig.fromJson(entry.getValue()));
+    }
+    return JsonUtils.objectToString(groovyStaticAnalyzerConfigMap);
+  }
+
+  @POST
+  @Path("/cluster/configs/groovy/staticAnalyzerConfig")
+  @Authorize(targetType = TargetType.CLUSTER, action = 
Actions.Cluster.UPDATE_GROOVY_STATIC_ANALYZER_CONFIG)
+  @Authenticate(AccessType.UPDATE)
+  @ApiOperation(value = "Update Groovy static analysis configuration")
+  @Produces(MediaType.APPLICATION_JSON)
+  @ApiResponses(value = {
+      @ApiResponse(code = 200, message = "Success"),
+      @ApiResponse(code = 500, message = "Server error updating configuration")
+  })
+  public SuccessResponse setGroovyStaticAnalysisConfig(String body) {
+    try {
+      JsonNode jsonNode = JsonUtils.stringToJsonNode(body);
+      Iterator<String> fieldNamesIterator = jsonNode.fieldNames();
+      Map<String, String> properties = new TreeMap<>();
+      while (fieldNamesIterator.hasNext()) {
+        String key = fieldNamesIterator.next();
+        if (!GROOVY_STATIC_ANALYZER_CONFIG_LIST.contains(key)) {
+          throw new IOException(String.format("Invalid groovy static analysis 
config: %s. Valid configs are: %s",
+              key, GROOVY_STATIC_ANALYZER_CONFIG_LIST));
+        }
+        JsonNode valueNode = jsonNode.get(key);

Review Comment:
   We want to validate if the value node is a valid 
`GroovyStaticAnalyzerConfig`. Does it work if we make this method directly 
taking a `Map<String, GroovyStaticAnalyzerConfig>`?



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