Skip to content

Commit

Permalink
refactor method updateClient from ClusterTest to ClientFixture
Browse files Browse the repository at this point in the history
  • Loading branch information
kingswanwho committed Feb 7, 2023
1 parent 9bad380 commit 8d3b329
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static void queryView(String viewName) throws Exception {

static void queryViewNotAuthorized(String viewName) throws Exception {
String query = String.format("SELECT rownum FROM %s.tmp.%s ORDER BY rownum LIMIT 1", MINI_DFS_STORAGE_PLUGIN_NAME, viewName);
errorMsgTestHelper(query, String.format(
client.errorMsgTestHelper(query, String.format(
"Not authorized to read view [%s] in schema [%s.tmp]", viewName, MINI_DFS_STORAGE_PLUGIN_NAME));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ public void showSchemas() throws Exception {

@Test
public void user0_showTables() throws Exception {
updateClient(org1Users[0]);
client.close();
client = client.updateClient(cluster, org1Users[0]);
showTablesHelper(db_general,
// Users are expected to see all tables in a database even if they don't have permissions to read from tables.
ImmutableList.of(
Expand All @@ -205,7 +206,8 @@ public void user0_showTables() throws Exception {
@Test
public void user0_allowed_g_student_user0() throws Exception {
// SELECT on "student_user0" table is granted to user "user0"
updateClient(org1Users[0]);
client.close();
client = client.updateClient(cluster, org1Users[0]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_student_user0));
}
Expand All @@ -219,10 +221,11 @@ public void user0_allowed_vw_student_user0() throws Exception {
public void user0_forbidden_g_voter_role0() throws Exception {
// SELECT on table "student_user0" is NOT granted to user "user0" directly or indirectly through role "role0" as
// user "user0" is not part of role "role0"
updateClient(org1Users[0]);
client.close();
client = client.updateClient(cluster, org1Users[0]);
run("USE " + hivePluginName + "." + db_general);
final String query = String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_voter_role0);
errorMsgTestHelper(query, "Principal [name=user0_1, type=USER] does not have following privileges for " +
client.errorMsgTestHelper(query, "Principal [name=user0_1, type=USER] does not have following privileges for " +
"operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=db_general.voter_role0]]\n");
}

Expand All @@ -233,19 +236,22 @@ public void user0_forbidden_vw_voter_role0() throws Exception {

@Test
public void user0_forbidden_v_student_u1g1_750() throws Exception {
updateClient(org1Users[0]);
client.close();
client = client.updateClient(cluster, org1Users[0]);
queryViewNotAuthorized(v_student_u1g1_750);
}

@Test
public void user0_allowed_v_student_u0g0_750() throws Exception {
updateClient(org1Users[0]);
client.close();
client = client.updateClient(cluster, org1Users[0]);
queryView(v_student_u0g0_750);
}

@Test
public void user1_showTables() throws Exception {
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
showTablesHelper(db_general,
// Users are expected to see all tables in a database even if they don't have permissions to read from tables.
ImmutableList.of(
Expand All @@ -261,10 +267,11 @@ public void user1_showTables() throws Exception {
@Test
public void user1_forbidden_g_student_user0() throws Exception {
// SELECT on table "student_user0" is NOT granted to user "user1"
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
run("USE " + hivePluginName + "." + db_general);
final String query = String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_student_user0);
errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
client.errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
"operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=db_general.student_user0]]\n");
}

Expand All @@ -276,7 +283,8 @@ public void user1_forbidden_vw_student_user0() throws Exception {
@Test
public void user1_allowed_g_voter_role0() throws Exception {
// SELECT on "voter_role0" table is granted to role "role0" and user "user1" is part the role "role0"
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_voter_role0));
}
Expand All @@ -290,42 +298,47 @@ public void user1_allowed_vw_voter_role0() throws Exception {
public void user1_allowed_g_voter_role0_but_forbidden_g_student_user2() throws Exception {
// SELECT on "voter_role0" table is granted to role "role0" and user "user1" is part the role "role0"
// SELECT on "student_user2" table is NOT granted to either role "role0" or user "user1"
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
run("USE " + hivePluginName + "." + db_general);
final String query =
String.format("SELECT * FROM %s v JOIN %s s on v.name = s.name limit 2;", g_voter_role0, g_student_user2);
errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
client.errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
"operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=db_general.student_user2]]");
}

@Test
public void user1_allowed_vw_voter_role0_but_forbidden_vw_student_user2() throws Exception {
// SELECT on "vw_voter_role0" table is granted to role "role0" and user "user1" is part the role "role0"
// SELECT on "vw_student_user2" table is NOT granted to either role "role0" or user "user1"
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
run("USE " + hivePluginName + "." + db_general);
final String query =
String.format("SELECT * FROM %s v JOIN %s s on v.name = s.name limit 2;", vw_voter_role0, vw_student_user2);
errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
client.errorMsgTestHelper(query, "Principal [name=user1_1, type=USER] does not have following privileges for " +
"operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=db_general.vw_student_user2]]");
}

@Test
public void user1_allowed_v_student_u0g0_750() throws Exception {
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
queryView(v_student_u0g0_750);
}

@Test
public void user1_allowed_v_student_u1g1_750() throws Exception {
updateClient(org1Users[1]);
client.close();
client = client.updateClient(cluster, org1Users[1]);
queryView(v_student_u1g1_750);
}

@Test
public void user2_allowed_g_voter_role0() throws Exception {
// SELECT on "voter_role0" table is granted to role "role0" and user "user2" is part the role "role0"
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_voter_role0));
}
Expand All @@ -338,7 +351,8 @@ public void user2_allowed_vw_voter_role0() throws Exception {
@Test
public void user2_allowed_g_student_user2() throws Exception {
// SELECT on "student_user2" table is granted to user "user2"
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s ORDER BY name LIMIT 2", g_student_user2));
}
Expand All @@ -352,27 +366,31 @@ public void user2_allowed_vw_student_user2() throws Exception {
public void user2_allowed_g_voter_role0_and_g_student_user2() throws Exception {
// SELECT on "voter_role0" table is granted to role "role0" and user "user2" is part the role "role0"
// SELECT on "student_user2" table is granted to user "user2"
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s v JOIN %s s on v.name = s.name limit 2;", g_voter_role0, g_student_user2));
}

@Test
public void user2_allowed_vw_voter_role0_and_vw_student_user2() throws Exception {
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
run("USE " + hivePluginName + "." + db_general);
run(String.format("SELECT * FROM %s v JOIN %s s on v.name = s.name limit 2;", vw_voter_role0, vw_student_user2));
}

@Test
public void user2_forbidden_v_student_u0g0_750() throws Exception {
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
queryViewNotAuthorized(v_student_u0g0_750);
}

@Test
public void user2_allowed_v_student_u1g1_750() throws Exception {
updateClient(org1Users[2]);
client.close();
client = client.updateClient(cluster, org1Users[2]);
queryView(v_student_u1g1_750);
}

Expand All @@ -385,7 +403,8 @@ public static void shutdown() throws Exception {
private static void queryHiveView(String usr, String viewName) throws Exception {
String query = String.format("SELECT COUNT(*) AS rownum FROM %s.%s.%s",
hivePluginName, db_general, viewName);
updateClient(usr);
client.close();
client = client.updateClient(cluster, usr);
client.testBuilder()
.sqlQuery(query)
.unOrdered()
Expand All @@ -400,8 +419,9 @@ private static void queryHiveViewNotAuthorized(String usr, String viewName) thro
"operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=db_general.%s]]\n",
usr, viewName);

updateClient(usr);
errorMsgTestHelper(query, expectedError);
client.close();
client = client.updateClient(cluster, usr);
client.errorMsgTestHelper(query, expectedError);
}

private static void createHiveView(Driver driver, String db, String viewName, String tblName) {
Expand Down

0 comments on commit 8d3b329

Please sign in to comment.