You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by ga...@apache.org on 2016/11/24 07:16:38 UTC

incubator-ranger git commit: RANGER-1225 : Unit tests for Ranger Hive Policies

Repository: incubator-ranger
Updated Branches:
  refs/heads/master 62160bb81 -> 35ff7ee79


RANGER-1225 : Unit tests for Ranger Hive Policies

Signed-off-by: Gautam Borad <ga...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/35ff7ee7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/35ff7ee7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/35ff7ee7

Branch: refs/heads/master
Commit: 35ff7ee79a5c18f50e62d4f78ff7a5a03598fe05
Parents: 62160bb
Author: Mehul Parikh <me...@freestoneinfotech.com>
Authored: Mon Nov 21 18:06:36 2016 +0530
Committer: Gautam Borad <ga...@apache.org>
Committed: Thu Nov 24 12:46:09 2016 +0530

----------------------------------------------------------------------
 .../services/hive/HIVERangerAuthorizerTest.java | 127 ++++++++++++++++++-
 .../src/test/resources/hive-policies.json       |  82 ++++++++++++
 2 files changed, 208 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/35ff7ee7/hive-agent/src/test/java/org/apache/ranger/services/hive/HIVERangerAuthorizerTest.java
----------------------------------------------------------------------
diff --git a/hive-agent/src/test/java/org/apache/ranger/services/hive/HIVERangerAuthorizerTest.java b/hive-agent/src/test/java/org/apache/ranger/services/hive/HIVERangerAuthorizerTest.java
index 3faa097..9b22794 100644
--- a/hive-agent/src/test/java/org/apache/ranger/services/hive/HIVERangerAuthorizerTest.java
+++ b/hive-agent/src/test/java/org/apache/ranger/services/hive/HIVERangerAuthorizerTest.java
@@ -48,7 +48,7 @@ import org.junit.Test;
  * c) "bob" can create any database
  * d) "dave" can do a select on the table "words" but only if the "count" column is >= 80
  * e) "jane" can do a select on the table "words", but only get a "hash" of the word, and not the word itself.
- *
+ * f) "da_test_user" is delegate admin for rangerauthz database.
  */
 public class HIVERangerAuthorizerTest {
 
@@ -580,4 +580,129 @@ public class HIVERangerAuthorizerTest {
         statement.close();
         connection.close();
     }
+
+        @Test
+        public void testCreateDropMacro() throws Exception {
+                String initialUrl = "jdbc:hive2://localhost:" + port;
+                Connection connection = DriverManager.getConnection(initialUrl, "admin", "admin");
+                Statement statement = connection.createStatement();
+                statement.execute("CREATE DATABASE IF NOT EXISTS rangerauthz2");
+
+                statement.close();
+                connection.close();
+
+                // Load data into HIVE
+                String url = "jdbc:hive2://localhost:" + port + "/rangerauthz2";
+                connection = DriverManager.getConnection(url, "admin", "admin");
+                statement = connection.createStatement();
+
+                statement.execute("create table if not exists rangerauthz2.macro_testing (a INT, b INT)");
+                statement.execute("insert into rangerauthz2.macro_testing (a, b) values (4, 5)");
+                statement.execute("insert into rangerauthz2.macro_testing (a, b) values (3, 5)");
+
+                ResultSet resultSet = statement.executeQuery("SELECT * FROM rangerauthz2.macro_testing where b == '5'");
+                //Verify Table Created And Contains Data
+
+                if (resultSet.next()) {
+                        Assert.assertEquals(5, resultSet.getInt(2));
+                } else {
+                        Assert.fail("No Resultset Found");
+                }
+
+                statement.execute("create temporary macro math_cube(x int) x*x*x");
+                ResultSet resultSet2 = statement.executeQuery("select math_cube(b) from rangerauthz2.macro_testing");
+
+                if (resultSet2.next()) {
+                        Assert.assertEquals(125, resultSet2.getInt(1));
+                } else {
+                        Assert.fail("Macro Not Created Properly");
+                }
+
+                statement.execute("drop temporary macro math_cube");
+
+                try{
+                        statement.executeQuery("select math_cube(b) from rangerauthz2.macro_testing");
+                        Assert.fail("macro deleted already");
+                }
+                catch(SQLException ex){
+                        //expected
+                }
+
+                statement.execute("DROP TABLE rangerauthz2.macro_testing");
+                statement.execute("DROP DATABASE rangerauthz2");
+
+                statement.close();
+                connection.close();
+        }
+
+        @Test
+        public void testCreateDropFunction() throws Exception {
+                String initialUrl = "jdbc:hive2://localhost:" + port;
+                Connection connection = DriverManager.getConnection(initialUrl, "admin", "admin");
+                Statement statement = connection.createStatement();
+
+                statement.execute("CREATE DATABASE IF NOT EXISTS rangerauthz3");
+                statement.close();
+                connection.close();
+
+                String url = "jdbc:hive2://localhost:" + port + "/rangerauthz3";
+                connection = DriverManager.getConnection(url, "admin", "admin");
+                statement = connection.createStatement();
+                statement.execute("CREATE TABLE if not exists rangerauthz3.function_testing (a DOUBLE, b DOUBLE)");
+                statement.execute("insert into rangerauthz3.function_testing (a, b) values (4.54845, 5.5487)");
+                ResultSet resultSet2 = statement.executeQuery("select round(b) from rangerauthz3.function_testing");
+
+                if (resultSet2.next()) {
+                        Assert.assertEquals(6, resultSet2.getInt(1));
+                } else {
+                        Assert.fail("No Resultset Found");
+                }
+
+                statement.execute("DROP TABLE rangerauthz3.function_testing");
+                statement.execute("DROP DATABASE rangerauthz3");
+
+                statement.close();
+                connection.close();
+        }
+
+        @Test
+        public void testGrantrevoke() throws Exception {
+                String initialUrl = "jdbc:hive2://localhost:" + port;
+                Connection connection = DriverManager.getConnection(initialUrl, "admin", "admin");
+                Statement statement = connection.createStatement();
+                statement.execute("CREATE DATABASE IF NOT EXISTS rangerauthzx");
+                statement.execute("use rangerauthzx");
+                statement.execute("CREATE TABLE rangerauthzx.tbl1 (a INT, b INT)");
+                statement.close();
+                connection.close();
+
+                String url = "jdbc:hive2://localhost:" + port;
+                connection = DriverManager.getConnection(url, "dave", "dave");
+                statement = connection.createStatement();
+                try{
+                statement.execute("use rangerauthzx");
+                statement.execute("grant select ON TABLE rangerauthzx.tbl1 to USER jane with grant option");
+                Assert.fail("access should not have been granted");
+                }
+                catch(SQLException ex){
+                        //expected
+                }
+
+                connection = DriverManager.getConnection(url, "da_test_user", "da_test_user");
+                statement = connection.createStatement();
+                try{
+                statement.execute("use rangerauthzx");
+                statement.execute("grant select ON TABLE rangerauthzx.tbl1 to USER jane with grant option");
+                }
+                catch(SQLException ex){
+                        Assert.fail("access should have been granted to da_test_user");
+                }
+                statement.close();
+                connection.close();
+
+                connection = DriverManager.getConnection(url, "admin", "admin");
+                statement = connection.createStatement();
+                statement.execute("DROP TABLE rangerauthzx.tbl1");
+        }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/35ff7ee7/hive-agent/src/test/resources/hive-policies.json
----------------------------------------------------------------------
diff --git a/hive-agent/src/test/resources/hive-policies.json b/hive-agent/src/test/resources/hive-policies.json
index 924c746..dd71424 100644
--- a/hive-agent/src/test/resources/hive-policies.json
+++ b/hive-agent/src/test/resources/hive-policies.json
@@ -99,6 +99,88 @@
       "isEnabled": true,
       "version": 2
     },
+     {
+      "service": "HIVETest",
+      "name": "Delagate admin-allowed on rangerauthzx",
+      "policyType": 0,
+      "isAuditEnabled": true,
+      "resources": {
+        "database": {
+          "values": [
+            "rangerauthzx"
+          ],
+          "isExcludes": false,
+          "isRecursive": false
+        },
+        "column": {
+          "values": [
+            "*"
+          ],
+          "isExcludes": false,
+          "isRecursive": false
+        },
+        "table": {
+          "values": [
+            "*"
+          ],
+          "isExcludes": false,
+          "isRecursive": false
+        }
+      },
+      "policyItems": [
+        {
+          "accesses": [
+            {
+              "type": "select",
+              "isAllowed": true
+            },
+            {
+              "type": "update",
+              "isAllowed": true
+            },
+            {
+              "type": "create",
+              "isAllowed": true
+            },
+            {
+              "type": "drop",
+              "isAllowed": true
+            },
+            {
+              "type": "alter",
+              "isAllowed": true
+            },
+            {
+              "type": "index",
+              "isAllowed": true
+            },
+            {
+              "type": "lock",
+              "isAllowed": true
+            },
+            {
+              "type": "all",
+              "isAllowed": true
+            }
+          ],
+          "users": [
+            "da_test_user"
+          ],
+          "groups": [],
+          "conditions": [],
+          "delegateAdmin": true
+        }
+      ],
+      "denyPolicyItems": [],
+      "allowExceptions": [],
+      "denyExceptions": [],
+      "dataMaskPolicyItems": [],
+      "rowFilterPolicyItems": [],
+      "id": 8,
+      "isEnabled": true,
+      "version": 2
+    }
+    ,
     {
       "service": "HIVETest",
       "name": "all - database, udf",