Example usage for org.apache.hadoop.security UserGroupInformation reset

List of usage examples for org.apache.hadoop.security UserGroupInformation reset

Introduction

In this page you can find the example usage for org.apache.hadoop.security UserGroupInformation reset.

Prototype

@InterfaceAudience.Private
    @VisibleForTesting
    public static void reset() 

Source Link

Usage

From source file:com.github.sakserv.minicluster.impl.KdcLocalClusterHBaseIntegrationTest.java

License:Apache License

@Test
public void testHBase() throws Exception {
    UserGroupInformation.loginUserFromKeytab(kdcLocalCluster.getKrbPrincipalWithRealm("hbase"),
            kdcLocalCluster.getKeytabForPrincipal("hbase"));

    assertTrue(UserGroupInformation.isSecurityEnabled());
    assertTrue(UserGroupInformation.isLoginKeytabBased());

    Configuration configuration = hbaseLocalCluster.getHbaseConfiguration();
    configuration.set("hbase.client.retries.number", "1");
    configuration.set("hbase.client.pause", "1000");
    configuration.set("zookeeper.recovery.retry", "1");

    // Write data
    try (Connection connection = ConnectionFactory.createConnection(configuration)) {
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf("test-kdc");
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }// www  . ja v  a  2s . c  om
        admin.createTable(new HTableDescriptor(tableName).addFamily(new HColumnDescriptor("cf")));

        try (BufferedMutator mutator = connection.getBufferedMutator(tableName)) {
            mutator.mutate(new Put(Bytes.toBytes("key")).addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"),
                    Bytes.toBytes("azerty")));
        }
    }

    // Log out
    LOG.info("Logout...");
    UserGroupInformation.getLoginUser().logoutUserFromKeytab();
    UserGroupInformation.reset();

    try {
        Configuration unauthenticatedConfiguration = HBaseConfiguration.create();
        hbaseLocalCluster.configure(unauthenticatedConfiguration);
        unauthenticatedConfiguration.set("hbase.client.retries.number", "1");
        unauthenticatedConfiguration.set("hbase.client.pause", "1000");
        unauthenticatedConfiguration.set("zookeeper.recovery.retry", "1");

        UserGroupInformation.setConfiguration(unauthenticatedConfiguration);
        try (Connection connection = ConnectionFactory.createConnection(unauthenticatedConfiguration)) {
            Admin admin = connection.getAdmin();

            TableName tableName = TableName.valueOf("test-kdc2");
            if (admin.tableExists(tableName)) {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }

            try (BufferedMutator mutator = connection.getBufferedMutator(tableName)) {
                mutator.mutate(new Put(Bytes.toBytes("key")).addColumn(Bytes.toBytes("cf"),
                        Bytes.toBytes("col1"), Bytes.toBytes("azerty")));
            }
        }
        fail();
    } catch (RetriesExhaustedException e) {
        LOG.info("Alright, this is expected!", e);
        assertTrue(e.getCause() instanceof IOException);
        System.out.println("Not authenticated!");
    }
}

From source file:com.github.sakserv.minicluster.impl.KdcLocalClusterHdfsIntegrationTest.java

License:Apache License

@Test
public void testHdfs() throws Exception {
    FileSystem hdfsFsHandle = hdfsLocalCluster.getHdfsFileSystemHandle();

    UserGroupInformation.loginUserFromKeytab(kdcLocalCluster.getKrbPrincipalWithRealm("hdfs"),
            kdcLocalCluster.getKeytabForPrincipal("hdfs"));

    assertTrue(UserGroupInformation.isSecurityEnabled());
    assertTrue(UserGroupInformation.isLoginKeytabBased());

    // Write a file to HDFS containing the test string
    FSDataOutputStream writer = hdfsFsHandle
            .create(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY)));
    writer.writeUTF(propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY));
    writer.close();/*from  ww  w. j a  v a2  s  . c om*/

    // Read the file and compare to test string
    FSDataInputStream reader = hdfsFsHandle
            .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY)));
    assertEquals(reader.readUTF(), propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY));
    reader.close();

    // Log out
    UserGroupInformation.getLoginUser().logoutUserFromKeytab();

    UserGroupInformation.reset();

    try {
        Configuration conf = new Configuration();
        UserGroupInformation.setConfiguration(conf);
        FileSystem.get(hdfsFsHandle.getUri(), conf)
                .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY)));
        fail();
    } catch (AccessControlException e) {
        LOG.info("Not authenticated!");
    }
}