How to connect with hdfs via kerberos from OSGI bundles
<p>We are trying to connect with the HDFS using kerberos, from Karaf container by OSGI bundle. We have already installed the hadoop client in karaf using apache servicemix bundles.</p>

<pre><code>&lt;groupId&gt;org.apache.servicemix.bundles&lt;/groupId&gt;
            &lt;artifactId&gt;org.apache.servicemix.bundles.hadoop-client&lt;/artifactId&gt;
            &lt;version&gt;2.4.1_1&lt;/version&gt;
</code></pre>

<p>Pom file is attached below:</p>

<pre><code>&lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.3.7&lt;/version&gt;
                &lt;extensions&gt;true&lt;/extensions&gt;
                &lt;configuration&gt;
                    &lt;instructions&gt;
                        &lt;Bundle-Activator&gt;com.bdbizviz.hadoop.activator.PaHdfsActivator&lt;/Bundle-Activator&gt;
                        &lt;Bundle-SymbolicName&gt;${project.artifactId}&lt;/Bundle-SymbolicName&gt;
                        &lt;Bundle-Version&gt;${project.version}&lt;/Bundle-Version&gt;
                        &lt;Export-Package&gt;
                            &lt;!-- com.google.*, !org.apache.camel.model.dataformat, !org.apache.poi.ddf, 
                                !org.apache.xmlbeans, org.apache.commons.collections.*, org.apache.commons.configuration.*, 
                                org.apache.hadoop.hdfs*, org.apache.hadoop.hdfs.client*, org.apache.hadoop.hdfs.net*, 
                                org.apache.hadoop.hdfs.protocol.datatransfer*, org.apache.hadoop.hdfs.protocol.proto*, 
                                org.apache.hadoop.hdfs.protocolPB*, org.apache.hadoop.conf.*, org.apache.hadoop.io.*, 
                                org.apache.hadoop.fs.*, org.apache.hadoop.security.*, org.apache.hadoop.metrics2.*, 
                                org.apache.hadoop.util.*, org.apache.hadoop*; --&gt;
                            &lt;!-- org.apache.*; --&gt;
                        &lt;/Export-Package&gt;
                        &lt;Import-Package&gt;
                            org.apache.hadoop*,org.osgi.framework,*;resolution:=optional
                        &lt;/Import-Package&gt;
                        &lt;Include-Resource&gt;
                            {maven-resources},
                            @org.apache.servicemix.bundles.hadoop-client-2.4.1_1.jar!/coredefault.
                            xml,
                            @org.apache.servicemix.bundles.hadoop-client-2.4.1_1.jar!/hdfsdefault.
                            xml,
                            @org.apache.servicemix.bundles.hadoop-client-
                            2.4.1_1.jar!/mapred-default.xml,
                            @org.apache.servicemix.bundles.hadoop-client-
                            2.4.1_1.jar!/hadoop-metrics.properties
                        &lt;/Include-Resource&gt;
                        &lt;DynamicImport-Package&gt;*&lt;/DynamicImport-Package&gt;
                    &lt;/instructions&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.servicemix.bundles&lt;/groupId&gt;
            &lt;artifactId&gt;org.apache.servicemix.bundles.hadoop-client&lt;/artifactId&gt;
            &lt;version&gt;2.4.1_1&lt;/version&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;jdk.tools&lt;/groupId&gt;
                    &lt;artifactId&gt;jdk.tools&lt;/artifactId&gt;
                    &lt;!-- &lt;version&gt;1.7&lt;/version&gt; --&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;
</code></pre>

<p>Code Snippet:</p>

<pre><code>public class TestHdfs implements ITestHdfs{

    public void printName() throws IOException{

        /*

        Configuration config = new Configuration();
        config.set("fs.default.name", "hdfs://192.168.1.17:8020");
        config.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
        config.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
        try {
            fs = FileSystem.get(config);
            getHostnames(fs);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

        final Configuration config = new Configuration();
        config.set("fs.default.name", "hdfs://192.168.1.124:8020");
        config.set("fs.file.impl", LocalFileSystem.class.getName());
        config.set("fs.hdfs.impl", DistributedFileSystem.class.getName());
        config.set("hadoop.security.authentication", "KERBEROS");
        config.set("dfs.namenode.kerberos.principal.pattern",
                "hdfs/*@********.COM");

        System.setProperty("HADOOP_JAAS_DEBUG", "true");
        System.setProperty("sun.security.krb5.debug", "true");
        System.setProperty("java.net.preferIPv4Stack", "true");

        System.out.println("--------------status---:"
                + UserGroupInformation.isSecurityEnabled());
        UserGroupInformation.setConfiguration(config);
        // UserGroupInformation.loginUserFromKeytab(
        // "hdfs/hadoop1.********.com@********.COM",
        // "file:/home/kaushal/hdfs-hadoop1.keytab");

        UserGroupInformation app_ugi = UserGroupInformation
                .loginUserFromKeytabAndReturnUGI("hdfs/hadoop1.********.com@********.COM",
                        "C:\\Users\\desanth.pv\\Desktop\\hdfs-hadoop1.keytab");
        UserGroupInformation proxy_ugi = UserGroupInformation.createProxyUser(
                "ssdfsdfsdfsdfag", app_ugi);
        System.out.println("--------------status---:"
                + UserGroupInformation.isSecurityEnabled());
        /*ClassLoader tccl = Thread.currentThread()
                .getContextClassLoader();*/
        try {
            /*Thread.currentThread().setContextClassLoader(
                    getClass().getClassLoader());*/
            proxy_ugi.doAs(new PrivilegedExceptionAction() {

                @Override
                public Object run() throws Exception {
                    /*ClassLoader tccl = Thread.currentThread()
                            .getContextClassLoader();*/
                    try {
                        /*Thread.currentThread().setContextClassLoader(
                                getClass().getClassLoader());*/
                        System.out.println("desanth");
                        FileSystem fs = FileSystem.get(config);
                        DistributedFileSystem hdfs = (DistributedFileSystem) fs;
                        DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();

                        String[] names = new String[dataNodeStats.length];
                        for (int i = 0; i &lt; dataNodeStats.length; i++) {
                            names[i] = dataNodeStats[i].getHostName();
                            System.out.println((dataNodeStats[i].getHostName()));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //Thread.currentThread().setContextClassLoader(tccl);
                    }

                    return null;
                }
            });
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            /*Thread.currentThread().setContextClassLoader(tccl);*/

        }
    }



    public void getHostnames(FileSystem fs) throws IOException {
        DistributedFileSystem hdfs = (DistributedFileSystem) fs;
        DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();

        String[] names = new String[dataNodeStats.length];
        for (int i = 0; i &lt; dataNodeStats.length; i++) {
            names[i] = dataNodeStats[i].getHostName();
            System.out.println((dataNodeStats[i].getHostName()));
        }
    }
}
</code></pre>

<p>Error :</p>

<pre><code>Caused by: org.apache.hadoop.security.AccessControlException: Client cannot authenticate via:[TOKEN, KERBEROS]
[12:35:51 PM] Jayendra Parsai: java.io.IOException: Failed on local exception: java.io.IOException: org.apache.hadoop.security.AccessControlException: Client cannot authenticate via:[TOKEN, KERBEROS]; Host Details : local host is: "jayendra-dynabook-T451-34EW/127.0.1.1"; destination host is: "hadoop2.********.com":8020;
</code></pre>