You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by "pan3793 (via GitHub)" <gi...@apache.org> on 2023/04/10 05:19:35 UTC

[GitHub] [kyuubi] pan3793 commented on a diff in pull request #4677: [AUTHZ] Check generated policy file in test suite

pan3793 commented on code in PR #4677:
URL: https://github.com/apache/kyuubi/pull/4677#discussion_r1161440901


##########
extensions/spark/kyuubi-spark-authz/src/test/gen/scala/org/apache/kyuubi/plugin/spark/authz/gen/PolicyJsonFileGenerator.scala:
##########
@@ -17,49 +17,59 @@
 
 package org.apache.kyuubi.plugin.spark.authz.gen
 
-import java.nio.file.Paths
+import java.nio.charset.StandardCharsets
+import java.nio.file.{Files, Paths, StandardOpenOption}
 import java.util.UUID
 import java.util.concurrent.atomic.AtomicLong
 
-import scala.language.implicitConversions
+import scala.collection.JavaConverters._
 
 import com.fasterxml.jackson.annotation.JsonInclude.Include
 import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
 import com.fasterxml.jackson.databind.json.JsonMapper
 import com.fasterxml.jackson.databind.node.ObjectNode
 import com.fasterxml.jackson.module.scala.DefaultScalaModule
 import org.apache.ranger.plugin.model.RangerPolicy
+// scalastyle:off
+import org.scalatest.funsuite.AnyFunSuite
 
 import org.apache.kyuubi.plugin.spark.authz.gen.KRangerPolicyItemAccess.allowTypes
-import org.apache.kyuubi.plugin.spark.authz.gen.PolicyJsonFileGenerator.RangerAccessType.{all, alter, create, drop, index, lock, read, select, update, use, write, RangerAccessType}
+import org.apache.kyuubi.plugin.spark.authz.gen.RangerAccessType._
 import org.apache.kyuubi.plugin.spark.authz.gen.RangerClassConversions.getRangerObject
-
-/**
- * Generates the policy file to test/main/resources dir.
- *
- * Usage:
- * build/mvn scala:run -pl :kyuubi-spark-authz_2.12
- * -DmainClass=org.apache.kyuubi.plugin.spark.authz.gen.PolicyJsonFileGenerator
- */
-private object PolicyJsonFileGenerator {
-  def main(args: Array[String]): Unit = {
-    writeRangerServicePolicesJson()
-  }
-
+class PolicyJsonFileGenerator extends AnyFunSuite {
+  // scalastyle:on
   final private val mapper: ObjectMapper = JsonMapper.builder()
     .addModule(DefaultScalaModule)
     .serializationInclusion(Include.NON_NULL)
     .build()
 
-  def writeRangerServicePolicesJson(): Unit = {
+  test("Check ranger policy file") {
     val pluginHome = getClass.getProtectionDomain.getCodeSource.getLocation.getPath
       .split("target").head
     val policyFileName = "sparkSql_hive_jenkins.json"
-    val policyFile = Paths.get(pluginHome, "src", "test", "resources", policyFileName).toFile
-    // scalastyle:off println
-    println(s"Writing ranger policies to $policyFileName.")
-    // scalastyle:on println
-    mapper.writerWithDefaultPrettyPrinter().writeValue(policyFile, servicePolicies)
+    val policyFilePath =
+      Paths.get(pluginHome, "src", "test", "resources", policyFileName)
+    val generatedStr = mapper.writerWithDefaultPrettyPrinter()
+      .writeValueAsString(servicePolicies)
+
+    if (sys.env.get("KYUUBI_UPDATE").contains("1")) {
+      // scalastyle:off println
+      println(s"Writing ranger policies to $policyFileName.")
+      // scalastyle:on println
+      Files.write(
+        policyFilePath,
+        generatedStr.getBytes(StandardCharsets.UTF_8),
+        StandardOpenOption.CREATE,
+        StandardOpenOption.TRUNCATE_EXISTING)
+    } else {
+      val existedFileContent = Files.readAllLines(policyFilePath).asScala.mkString("\n")
+      withClue("Please regenerate the ranger policy file by running"
+        + "`KYUUBI_UPDATE=1 build/mvn clean test -Pgenpolicy"

Review Comment:
   maybe `gen-policy` is better, align the style w/ others



-- 
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: notifications-unsubscribe@kyuubi.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org