Java Code Examples for com.google.common.io.Resources#copy()

The following examples show how to use com.google.common.io.Resources#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenerationTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * When running Java tests in a build, there is no formal guarantee that these resources
 * be available as filesystem files. This copies a resource to a file in the temp dir,
 * and returns the new path.
 * The given resource name is relative to the class to which this method belongs
 * (which might be a subclass of GenerationTest, in a different package).
 */
public String getResourceAsFile(String resourceName) throws IOException {
  URL url;
  try {
    url = getClass().getResource(resourceName).toURI().toURL();
  } catch (URISyntaxException e) {
    throw new IOException(e);
  }
  File file = new File(tempDir + "/resources/"
      + getClass().getPackage().getName().replace('.', File.separatorChar)
      + File.separatorChar + resourceName);
  file.getParentFile().mkdirs();
  OutputStream ostream = new FileOutputStream(file);
  Resources.copy(url, ostream);
  return file.getPath();
}
 
Example 2
Source File: ClasspathExtension.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(File extensionsDir) throws IOException {
  if (!FileHandler.isZipped(loadFrom)) {
    throw new WebDriverException("Will only install zipped extensions for now");
  }

  File holdingPen = new File(extensionsDir, "webdriver-staging");
  FileHandler.createDir(holdingPen);

  File extractedXpi = new File(holdingPen, loadFrom);
  File parentDir = extractedXpi.getParentFile();
  if (!parentDir.exists()) {
    parentDir.mkdirs();
  }

  URL resourceUrl = Resources.getResource(loadResourcesUsing, loadFrom);

  try (OutputStream stream = new FileOutputStream(extractedXpi)){
    Resources.copy(resourceUrl, stream);
  }
  new FileExtension(extractedXpi).writeTo(extensionsDir);
}
 
Example 3
Source File: EncryptionTestUtils.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
public static Map<String, File> configureTestKeyStore(File baseDir,
      File keyStoreFile) throws IOException {
    Map<String, File> result = Maps.newHashMap();

    if (System.getProperty("java.vendor").contains("IBM")) {
      Resources.copy(Resources.getResource("ibm-test.keystore"),
          new FileOutputStream(keyStoreFile));
    } else {
      Resources.copy(Resources.getResource("sun-test.keystore"),
          new FileOutputStream(keyStoreFile));
    }
    /*
    Commands below:
    keytool -genseckey -alias key-0 -keypass keyPassword -keyalg AES \
      -keysize 128 -validity 9000 -keystore src/test/resources/test.keystore \
      -storetype jceks -storepass keyStorePassword
    keytool -genseckey -alias key-1 -keyalg AES -keysize 128 -validity 9000 \
      -keystore src/test/resources/test.keystore -storetype jceks \
      -storepass keyStorePassword
     */
//  key-0 has own password, key-1 used key store password
    result.put("key-0",
        TestUtils.writeStringToFile(baseDir, "key-0", "keyPassword"));
    result.put("key-1", null);
    return result;
  }
 
Example 4
Source File: PolicyFiles.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public static void copyToDir(File dest, String... resources)
    throws FileNotFoundException, IOException {
  for(String resource : resources) {
    LOGGER.debug("Copying " + resource + " to " + dest);
    Resources.copy(Resources.getResource(resource), new FileOutputStream(new File(dest, resource)));
  }
}
 
Example 5
Source File: TestPrivilegeAtTransform.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
  dataDir = context.getDataDir();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  policyFile = PolicyFile.setAdminOnServer1(ADMINGROUP);
}
 
Example 6
Source File: TestExportImportPrivileges.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  LOGGER.info("TestExportImportPrivileges setup");
  policyFile = super.setupPolicy();
  super.setup();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
}
 
Example 7
Source File: TestRuntimeMetadataRetrieval.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  policyFile = super.setupPolicy();
  super.setup();
  dataDir = context.getDataDir();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
}
 
Example 8
Source File: WixDeploymentSetupBuilder.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void createLogback ( final Element comp, final EquinoxAppService eas, final File resourceBase ) throws Exception
{
    final Element file = createElement ( comp, "File" ); //$NON-NLS-1$
    final String serviceName = makeServiceName ( eas );
    file.setAttribute ( "Id", "logback.xml_" + serviceName ); //$NON-NLS-1$ //$NON-NLS-2$
    file.setAttribute ( "Source", String.format ( "resources\\apps\\%s\\logback.xml", eas.getName () ) ); //$NON-NLS-1$ //$NON-NLS-2$

    final File logback = new File ( resourceBase, "logback.xml" ); //$NON-NLS-1$
    try ( FileOutputStream out = new FileOutputStream ( logback ) )
    {
        Resources.copy ( Resources.getResource ( MsiHandler.class, "templates/msi/app.logback.xml" ), out ); //$NON-NLS-1$
    }
}
 
Example 9
Source File: TestHaEnd2End.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
  super.setupAdmin();
  super.setup();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  PolicyFile.setAdminOnServer1(ADMINGROUP);
}
 
Example 10
Source File: TestSandboxOps.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  policyFile = super.setupPolicy();
  super.setup();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  loadData = "server=server1->uri=file://" + dataFile.getPath();
}
 
Example 11
Source File: TestDbEndToEnd.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
  super.setupAdmin();
  super.setup();
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  PolicyFile.setAdminOnServer1(ADMINGROUP);
}
 
Example 12
Source File: TestGeolocationProcessor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  tempDir = Files.createTempDir();
  countryDb = new File(tempDir, "GeoLite2-Country.mmdb");
  cityDb = new File(tempDir, "GeoLite2-City.mmdb");
  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(countryDb));
  Resources.copy(Resources.getResource("GeoLite2-Country.mmdb"), out);
  out.flush();
  out.close();
  out = new BufferedOutputStream(new FileOutputStream(cityDb));
  Resources.copy(Resources.getResource("GeoLite2-City.mmdb"), out);
  out.flush();
  out.close();

}
 
Example 13
Source File: TestMovingToProduction.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  File dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  policyFile = PolicyFile.setAdminOnServer1(ADMINGROUP)
      .setUserGroupMapping(StaticUserGroup.getStaticMapping());
  writePolicyFile(policyFile);
}
 
Example 14
Source File: TestPolicyImportExport.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void prepareForImport(String resorceName) throws Exception {
  File importFile = new File(dataDir, resorceName);
  FileOutputStream to = new FileOutputStream(importFile);
  Resources.copy(Resources.getResource(resorceName), to);
  to.close();
  configTool.setImportPolicyFilePath(importFile.getAbsolutePath());
}
 
Example 15
Source File: TestSentryOnFailureHookLoading.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnFailureHookLoading() throws Exception {

  // Do not run this test if run with external HiveServer2
  // This test checks for a static member, which will not
  // be set if HiveServer2 and the test run in different JVMs
  String hiveServer2Type = System.getProperty(
      HiveServerFactory.HIVESERVER2_TYPE);
  if (hiveServer2Type != null &&
      !HiveServerFactory.isInternalServer(HiveServerFactory.HiveServer2Type
          .valueOf(hiveServer2Type.trim()))) {
    return;
  }

  context = createContext(testProperties);

  File dataDir = context.getDataDir();
  //copy data file to test dir
  File dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();

  policyFile
      .addRolesToGroup(USERGROUP1, "all_db1", "load_data")
      .addPermissionsToRole("all_db1", "server=server1->db=DB_1")
      .setUserGroupMapping(StaticUserGroup.getStaticMapping())
      .write(context.getPolicyFile());

  // setup db objects needed by the test
  Connection connection = context.createConnection(ADMIN1);
  Statement statement = context.createStatement(connection);
  statement.execute("DROP DATABASE IF EXISTS DB_1 CASCADE");
  statement.execute("DROP DATABASE IF EXISTS DB_2 CASCADE");
  statement.execute("CREATE DATABASE DB_1");
  statement.execute("CREATE DATABASE DB_2");
  statement.close();
  connection.close();

  // test execution
  connection = context.createConnection(USER1_1);
  statement = context.createStatement(connection);

  //negative test case: user can't drop another user's database
  assertFalse(DummySentryOnFailureHook.invoked);
    try {
    statement.execute("DROP DATABASE DB_2 CASCADE");
    Assert.fail("Expected SQL exception");
  } catch (SQLException e) {
    assertTrue(DummySentryOnFailureHook.invoked);
  }

  statement.close();
  connection.close();

  //test cleanup
  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  statement.execute("DROP DATABASE DB_1 CASCADE");
  statement.execute("DROP DATABASE DB_2 CASCADE");
  statement.close();
  connection.close();
  context.close();
}
 
Example 16
Source File: TestDatabaseProvider.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
/**
 * Revoke select on table after:
 *  - grant all on table
 *  - grant select on table
 *  - grant insert on table
 */
@Test
public void testRevokeSELECTOnTable() throws Exception{

  //copy data file to test dir
  File dataDir = context.getDataDir();
  File dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();

  Connection connection = context.createConnection(ADMIN1);
  Statement statement = context.createStatement(connection);
  statement.execute("CREATE ROLE user_role");

  statement.execute("DROP DATABASE IF EXISTS " + DB1 + " CASCADE");
  statement.execute("CREATE DATABASE " + DB1);
  statement.execute("USE " + DB1);

  statement.execute("DROP TABLE IF EXISTS t1");
  statement.execute("CREATE TABLE t1 (c1 string)");

  statement.execute("GRANT ALL ON TABLE t1 TO ROLE user_role");
  statement.execute("GRANT SELECT ON TABLE t1 TO ROLE user_role");
  statement.execute("GRANT INSERT ON TABLE t1 TO ROLE user_role");
  statement.execute("GRANT ALL ON URI 'file://" + dataFile.getPath() + "' TO ROLE user_role");
  statement.execute("GRANT ROLE user_role TO GROUP " + USERGROUP1);
  statement.close();
  connection.close();

  connection = context.createConnection(USER1_1);
  statement = context.createStatement(connection);
  // Ensure everything works
  statement.execute("SELECT * FROM " + DB1 + ".t1");
  statement.execute("LOAD DATA LOCAL INPATH '" + dataFile.getPath() + "' INTO TABLE " + DB1 + ".t1");

  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  assertResultSize(statement.executeQuery("SHOW GRANT ROLE user_role"), 2);
  statement.close();
  connection.close();

  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  statement.execute("USE " + DB1);
  statement.execute("REVOKE SELECT ON TABLE t1 from ROLE user_role");
  statement.close();
  connection.close();

  connection = context.createConnection(USER1_1);
  statement = context.createStatement(connection);
  // Ensure select not allowed
  try {
    statement.execute("SELECT * FROM " + DB1 + ".t1");
    assertTrue("SELECT should not be allowed !!", false);
  } catch (SQLException se) {
    // Ignore
  }

  // Ensure insert allowed
  statement.execute("LOAD DATA LOCAL INPATH '" + dataFile.getPath() + "' INTO TABLE " + DB1 + ".t1");
  statement.close();
  connection.close();

  // This removes the ALL and SELECT privileges
  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  assertResultSize(statement.executeQuery("SHOW GRANT ROLE user_role"), 2);
  statement.close();
  connection.close();

}
 
Example 17
Source File: TestViewPrivileges.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitioned() throws Exception {
  // copy data file to test dir
  File dataDir = context.getDataDir();
  File dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();

  String viewName = "view1";
  String db = "db1";
  String tabName = "tab1";
  policyFile
      .addPermissionsToRole("view", "server=server1->db=" + db + "->table=" + viewName)
      .addRolesToGroup(USERGROUP1, "view")
      .setUserGroupMapping(StaticUserGroup.getStaticMapping());
  policyFile.write(context.getPolicyFile());

  //admin creates a view
  Connection conn = context.createConnection(ADMIN1);
  Statement stmt = context.createStatement(conn);
  stmt.execute("DROP DATABASE IF EXISTS " + db + " CASCADE");
  stmt.execute("CREATE DATABASE " + db);

  stmt.execute("use " + db);
  stmt.execute("create table " + tabName + " (id int) partitioned by (part string)");
  stmt.execute("load data local inpath '" + dataFile + "' into table " + tabName + " PARTITION (part=\"a\")");
  stmt.execute("load data local inpath '" + dataFile + "' into table " + tabName + " PARTITION (part=\"b\")");
  ResultSet res = stmt.executeQuery("select count(*) from " + tabName);
  Assert.assertThat(res, notNullValue());
  while(res.next()) {
    Assume.assumeTrue(res.getInt(1) == Integer.valueOf(1000));
  }
  stmt.execute("create view " + viewName + " as select * from " + tabName + " where id<100");
  res = stmt.executeQuery("select count(*) from " + viewName);
  Assert.assertThat(res, notNullValue());
  int rowsInView = 0;
  while(res.next()) {
    rowsInView = res.getInt(1);
  }
  stmt.close();
  conn.close();

  Connection userConn = context.createConnection(USER1_1);
  Statement userStmt = context.createStatement(userConn);
  userStmt.execute("use " + db);
  res = userStmt.executeQuery("select count(*) from " + viewName);
  Assert.assertThat(res, notNullValue());
  while(res.next()) {
    Assert.assertThat(res.getInt(1), is(rowsInView));
  }
  userStmt.close();
  userConn.close();

  // user2 hasn't the privilege for the view
  userConn = context.createConnection(USER2_1);
  userStmt = context.createStatement(userConn);
  try {
    userStmt.executeQuery("select count(*) from " + viewName);
    Assert.fail("Expected SQL exception");
  } catch (SQLException e) {
    // ignore the exception
  }
  userStmt.close();
  userConn.close();
}
 
Example 18
Source File: TestDatabaseProvider.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
/**
 * SENTRY-299
 *
 * 1. Create 2 Roles (user_role & user_role2)
 * 2. Create a Table t1
 * 3. grant ALL on t1 to user_role
 * 4. grant INSERT on t1 to user_role2
 * 5. Revoke INSERT on t1 from user_role
 *     - This would imply user_role can still SELECT
 *     - But user_role should NOT be allowed to LOAD
 * 6. Ensure Presense of another role will still enforce the revoke
 * @throws Exception
 */

@Test
public void testRevokeFailAnotherRoleExist() throws Exception {

  //copy data file to test dir
  File dataDir = context.getDataDir();
  File dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();


  Connection connection = context.createConnection(ADMIN1);
  Statement statement = context.createStatement(connection);
  statement.execute("CREATE ROLE user_role");
  statement.execute("CREATE ROLE user_role2");

  statement.execute("DROP DATABASE IF EXISTS " + DB1 + " CASCADE");
  statement.execute("CREATE DATABASE " + DB1);
  statement.execute("USE " + DB1);
  statement.execute("DROP TABLE IF EXISTS t1");
  statement.execute("CREATE TABLE t1 (c1 string)");
  statement.execute("GRANT ALL ON TABLE t1 TO ROLE user_role");
  statement.execute("GRANT ALL ON TABLE t1 TO ROLE user_role2");
  statement.execute("GRANT ALL ON URI \"file://" + dataFile.getPath()
      + "\" TO ROLE user_role");
  statement.execute("GRANT ALL ON URI 'file://" + dataFile.getPath() + "' TO ROLE user_role2");
  statement.execute("GRANT INSERT ON TABLE t1 TO ROLE user_role2");
  statement.execute("GRANT ROLE user_role TO GROUP " + USERGROUP1);
  statement.execute("GRANT ROLE user_role2 TO GROUP " + USERGROUP2);
  statement.close();
  connection.close();

  connection = context.createConnection(USER1_1);
  statement = context.createStatement(connection);
  statement.execute("SELECT * FROM " + DB1 + ".t1");
  statement.close();
  connection.close();

  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  ResultSet resultSet = statement.executeQuery("SHOW GRANT ROLE user_role");
  assertResultSize(resultSet, 2);
  statement.close();
  connection.close();

  // Revoke ALL on Db
  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  statement.execute("USE " + DB1);
  statement.execute("REVOKE INSERT ON TABLE t1 from ROLE user_role");
  statement.close();
  connection.close();

  connection = context.createConnection(USER1_1);
  statement = context.createStatement(connection);
  // This Should pass
  statement.execute("SELECT * FROM " + DB1 + ".t1");

  try {
    statement.execute("LOAD DATA LOCAL INPATH '" + dataFile.getPath() + "' INTO TABLE " + DB1 + ".t1");
    assertTrue("INSERT Should Not be allowed since we Revoked INSERT privileges on the table !!", false);
  } catch (Exception e) {

  } finally {
    statement.close();
    connection.close();
  }

  // user_role2 can still insert into table
  connection = context.createConnection(USER2_1);
  statement = context.createStatement(connection);
  statement.execute("LOAD DATA LOCAL INPATH '" + dataFile.getPath() + "' INTO TABLE " + DB1 + ".t1");
  statement.close();
  connection.close();

  // Grant changed from ALL to SELECT
  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  resultSet = statement.executeQuery("SHOW GRANT ROLE user_role");
  assertResultSize(resultSet, 2);
  statement.close();
  connection.close();

}
 
Example 19
Source File: TestPrivilegesAtTableScope.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
/***
 * Verify truncate partitioned permissions for different users with different
 * privileges
 * @throws Exception
 */
@Test
public void testTruncatePartitionedTable() throws Exception {
  File dataDir = context.getDataDir();
  // copy data file to test dir
  File dataFile = new File(dataDir, MULTI_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(MULTI_TYPE_DATA_FILE_NAME), to);
  to.close();

  policyFile.setUserGroupMapping(StaticUserGroup.getStaticMapping());
  writePolicyFile(policyFile);

  // create partitioned tables
  Connection connection = context.createConnection(ADMIN1);
  Statement statement = context.createStatement(connection);
  statement.execute("USE " + DB1);
  statement.execute("DROP TABLE if exists " + TBL1);
  statement.execute("CREATE TABLE " + TBL1 + " (i int) PARTITIONED BY (j int)");
  statement.execute("DROP TABLE if exists " + TBL2);
  statement.execute("CREATE TABLE " + TBL2 + " (i int) PARTITIONED BY (j int)");
  statement.execute("DROP TABLE if exists " + TBL3);
  statement.execute("CREATE TABLE " + TBL3 + " (i int) PARTITIONED BY (j int)");

  // verify admin can execute truncate empty partitioned table
  statement.execute("TRUNCATE TABLE " + TBL1);
  assertFalse(hasData(statement, TBL1));
  statement.close();
  connection.close();

  // add roles and grant permissions
  updatePolicyFile();

  // test truncate empty partitioned tables
  truncateTableTests(false);

  // add partitions to tables
  connection = context.createConnection(ADMIN1);
  statement = context.createStatement(connection);
  statement.execute("USE " + DB1);
  statement.execute("ALTER TABLE " + TBL1 + " ADD PARTITION (j=1) PARTITION (j=2)");
  statement.execute("ALTER TABLE " + TBL2 + " ADD PARTITION (j=1) PARTITION (j=2)");
  statement.execute("ALTER TABLE " + TBL3 + " ADD PARTITION (j=1) PARTITION (j=2)");

  // verify admin can execute truncate NOT empty partitioned table
  statement.execute("TRUNCATE TABLE " + TBL1 + " partition (j=1)");
  statement.execute("TRUNCATE TABLE " + TBL1);
  assertFalse(hasData(statement, TBL1));
  statement.close();
  connection.close();

  // test truncate NOT empty partitioned tables
  truncateTableTests(true);
}
 
Example 20
Source File: TestUriPermissions.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadPrivileges() throws Exception {
  dataFile = new File(dataDir, SINGLE_TYPE_DATA_FILE_NAME);
  FileOutputStream to = new FileOutputStream(dataFile);
  Resources.copy(Resources.getResource(SINGLE_TYPE_DATA_FILE_NAME), to);
  to.close();
  loadData = "server=server1->uri=file://" + dataFile.getPath();

  String tabName = "tab1";
  Connection userConn = null;
  Statement userStmt = null;

  // create dbs
  Connection adminCon = context.createConnection(ADMIN1);
  Statement adminStmt = context.createStatement(adminCon);
  adminStmt.execute("use default");
  adminStmt.execute("CREATE DATABASE " + DB1);
  adminStmt.execute("use " + DB1);
  adminStmt.execute("CREATE TABLE " + tabName + "(id int)");
  context.close();

  policyFile
          .addRolesToGroup(USERGROUP1, "db1_read", "db1_write", "data_read")
          .addRolesToGroup(USERGROUP2, "db1_write")
          .addPermissionsToRole("db1_write", "server=server1->db=" + DB1 + "->table=" + tabName + "->action=INSERT")
          .addPermissionsToRole("db1_read", "server=server1->db=" + DB1 + "->table=" + tabName + "->action=SELECT")
          .addPermissionsToRole("data_read", loadData);
  writePolicyFile(policyFile);

  // positive test, user1 has access to file being loaded
  userConn = context.createConnection(USER1_1);
  userStmt = context.createStatement(userConn);
  userStmt.execute("use " + DB1);
  userStmt.execute("load data local inpath 'file://" + dataFile.getPath() +
      "' into table " + tabName);
  userStmt.execute("select * from " + tabName + " limit 1");
  ResultSet res = userStmt.getResultSet();
  Assert.assertTrue("Table should have data after load", res.next());
  res.close();
  context.close();

  // Negative test, user2 doesn't have access to the file being loaded
  userConn = context.createConnection(USER2_1);
  userStmt = context.createStatement(userConn);
  userStmt.execute("use " + DB1);
  context.assertAuthzException(userStmt, "load data local inpath 'file://" + dataFile.getPath() +
      "' into table " + tabName);
  userStmt.close();
  userConn.close();
}