<p>I have all 4 files needed to read from/write to HDFS in my resources folder and method to create hdfs object is as below .</p>
<pre><code>public  static FileSystem getHdfsOnPrem(String coreSiteXml, String hdfsSiteXml, String krb5confLoc, String keyTabLoc){
        // Setup the configuration object.
        try {
            Configuration config = new Configuration();
            config.addResource(new org.apache.hadoop.fs.Path(coreSiteXml));
            config.addResource(new org.apache.hadoop.fs.Path(hdfsSiteXml));
            config.set(&quot;hadoop.security.authentication&quot;, &quot;Kerberos&quot;);
            config.addResource(krb5confLoc);
            config.set(&quot;fs.hdfs.impl&quot;,org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
            config.set(&quot;fs.file.impl&quot;,org.apache.hadoop.fs.LocalFileSystem.class.getName());
            System.setProperty(&quot;java.security.krb5.conf&quot;, krb5confLoc);
            org.apache.hadoop.security.HadoopKerberosName.setConfiguration(config);
            UserGroupInformation.setConfiguration(config);
            UserGroupInformation.loginUserFromKeytab(&quot;my_username&quot;, keyTabLoc);
            return org.apache.hadoop.fs.FileSystem.get(config);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
</code></pre>
<p>It works when I run it in local and pass below as the paths</p>
<pre><code>C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\core-site.xml
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\hdfs-site.xml
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\krb5.conf
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\my_username.user.keytab
</code></pre>
<p>It runs fine when I run it in local but when I bundle it as JAR and run it in an env like kubernetes it throws below error (Since bundling as JAR I can read contents of resource files as stream but I need to pass in path for loginuserFromKeytab method)</p>
<pre><code>org.apache.hadoop.security.KerberosAuthException: failure to login: for principal: my_username from keytab file:/opt/spark-3.0.0/jars/foo-project-name!/my_username.user.keytab javax.security.auth.login.LoginException: Unable to obtain password from user
</code></pre>
<p>Any suggestions/pointers are appreciated.</p>

Venkatesh Gotimukul
<p>I suggest you use <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html#:%7E:text=JAAS%20can%20be%20used%20for,bean%2C%20or%20a%20servlet%3B%20and" rel="nofollow noreferrer">Jaas</a> config file instead of writing this code.  This helps to remove the security plumbing from your code and externalizes it.  &quot;Unable to obtain password &quot; would occur if the user that is running your app doesn't have permission to access the file.</p>
