MetastoreAuthzObjectFilter { public List<T> filterDatabases(String username, List<T> dbNames) { if (dbNames == null) { return Collections.emptyList(); } List<T> filteredDatabases = Lists.newArrayList(); for (T dbName : dbNames) { String objName = extractor.getDatabaseName(dbName); if (Strings.isEmpty(objName) || authorizeDatabase(username, objName)) { filteredDatabases.add(dbName); } } return filteredDatabases; } MetastoreAuthzObjectFilter(HiveAuthzBinding authzBinding, ObjectExtractor extractor); static HiveAuthzPrivileges getListDatabasesPrivileges(); HiveAuthzPrivileges getListTablePrivileges(); List<T> filterDatabases(String username, List<T> dbNames); List<T> filterTables(String username, List<T> tables);  }
@Test public void testFilterDatabaseStrings() { final String USER1 = "user1"; MetastoreAuthzObjectFilter filter = new MetastoreAuthzObjectFilter(mockBinding, DB_NAME_EXTRACTOR); assertThat(filter).isNotNull(); assertThat(filter.filterDatabases(USER1, null)).isNotNull().isEmpty(); assertThat(filter.filterDatabases(USER1, Collections.emptyList())).isNotNull().isEmpty(); assertThat(filter.filterDatabases(USER1, Arrays.asList(null, "", null, null, ""))) .containsExactly(null, "", null, null, ""); restrictDatabaseNamesOnBinding(USER1, Arrays.asList("db1", "db3", "db5")); assertThat(filter.filterDatabases(USER1, Arrays.asList("db1", "db2", "db3", "db4", "db5"))) .containsExactly("db2", "db4"); }
@Test public void testFilterDatabaseHiveObjects() { final String USER1 = "user1"; MetastoreAuthzObjectFilter filter = new MetastoreAuthzObjectFilter(mockBinding, HIVE_OBJECT_EXTRACTOR); assertThat(filter).isNotNull(); assertThat(filter.filterDatabases(USER1, null)).isNotNull().isEmpty(); assertThat(filter.filterDatabases(USER1, Collections.emptyList())).isNotNull().isEmpty(); assertThat(filter.filterDatabases(USER1, Arrays.asList(null, null))) .containsExactly(null, null); restrictDatabaseNamesOnBinding(USER1, Arrays.asList("db1", "db3", "db5")); assertThat(filter.filterDatabases(USER1, createHivePrivilegeDatabaseList("db1", "db2", "db3", "db4", "db5"))) .extracting(HiveObjectExtractor.objectName()).containsExactly("db2", "db4"); }
@Test public void testFilterDatabasesRestrictDefaultDatabase() { final String USER1 = "user1"; MetastoreAuthzObjectFilter filter; restrictDatabaseNamesOnBinding(USER1, Arrays.asList("db1", "default")); restrictDefaultDatabase(true); filter = new MetastoreAuthzObjectFilter<String>(mockBinding, DB_NAME_EXTRACTOR); assertThat(filter.filterDatabases(USER1, Arrays.asList("db1", "default", "db4"))) .containsExactly("db4"); restrictDefaultDatabase(false); filter = new MetastoreAuthzObjectFilter<String>(mockBinding, DB_NAME_EXTRACTOR); assertThat(filter.filterDatabases(USER1, Arrays.asList("db1", "default", "db4"))) .containsExactly("default", "db4"); restrictDefaultDatabase(true); filter = new MetastoreAuthzObjectFilter(mockBinding, HIVE_OBJECT_EXTRACTOR); assertThat(filter.filterDatabases(USER1, createHivePrivilegeDatabaseList("db1", "default", "db4"))) .extracting(HiveObjectExtractor.objectName()).containsExactly("db4"); restrictDefaultDatabase(false); filter = new MetastoreAuthzObjectFilter(mockBinding, HIVE_OBJECT_EXTRACTOR); assertThat(filter.filterDatabases(USER1, createHivePrivilegeDatabaseList("db1", "default", "db4"))) .extracting(HiveObjectExtractor.objectName()).containsExactly("default", "db4"); }