/*
* Copyright 2018 LinkedIn Corp.
*
* Licensed under the Apache License, Version 2.0 (the “License”); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package azkaban.utils;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
import org.apache.hadoop.security.authentication.client.AuthenticatedURL.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The util class for hadoop authentication.
 */
public class AuthenticationUtils {

  private static final Logger logger = LoggerFactory.getLogger(AuthenticationUtils.class);

  public static HttpURLConnection loginAuthenticatedURL(final URL url, final String keytabPrincipal,
      final String keytabPath) throws Exception {
    final List<URL> resources = new ArrayList<>();
    resources.add(url);

    final URLClassLoader ucl = new URLClassLoader(resources.toArray(new URL[resources.size()]));
    final Configuration conf = new Configuration();
    conf.setClassLoader(ucl);
    UserGroupInformation.setConfiguration(conf);

    logger.info(
        "Logging in URL: " + url.toString() + " using Principal: " + keytabPrincipal + ", Keytab: "
            + keytabPath);

    UserGroupInformation.loginUserFromKeytab(keytabPrincipal, keytabPath);

    final HttpURLConnection connection = UserGroupInformation.getLoginUser().doAs(
        (PrivilegedExceptionAction<HttpURLConnection>) () -> {
          final Token token = new Token();
          return new AuthenticatedURL().openConnection(url, token);
        });

    return connection;
  }
}

@@ -669,166 +669,38 @@
 ava.
-net.URLClassLoader;%0Aimport java.security.PrivilegedExceptionAction;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport org.apache.hadoop.conf.Configura
+security.PrivilegedExceptionAc
 tion
@@ -1305,266 +1305,352 @@
 n %7B%0A
+%0A
     
-final List%3CURL%3E resources = new ArrayList%3C%3E();%0A    resources.add(url);%0A%0A    final URLClassLoader ucl = new URLClassLoader(resources.toArray(new URL%5Bresources.size()%5D));%0A    final Configuration conf = new Configuration();%0A    conf.setClassLoader(ucl);%0A   
+logger.info(%0A        %22Logging in URL: %22 + url.toString() + %22 using Principal: %22 + keytabPrincipal + %22, Keytab: %22%0A            + keytabPath);%0A%0A    UserGroupInformation loginUser = UserGroupInformation.getLoginUser();%0A%0A    if (loginUser == null) %7B%0A      UserGroupInformation.loginUserFromKeytab(keytabPrincipal, keytabPath);%0A      loginUser =
  Use
@@ -1671,33 +1671,26 @@
 ion.
-setConfiguration(conf);%0A%0A
+getLoginUser();%0A  
     
@@ -1705,171 +1705,178 @@
 nfo(
-%0A        %22Logging in URL: %22 + url.toString() + %22 using Principal: %22 + keytabPrincipal + %22, Keytab: %22%0A            + keytabPath);%0A%0A
+%22Logged in with user %22 + loginUser);%0A    %7D else %7B%0A      logger.info(%22Login user (%22 + loginUser + %22) already created, refreshing tgt.%22);%0A 
     
-UserGroupInformation.
+ loginUser.checkTGTAndRe
 login
-User
 From
@@ -1886,37 +1886,16 @@
 tab(
-keytabPrincipal, keytabPath);
+);%0A    %7D
 %0A%0A  
@@ -1937,41 +1937,17 @@
 n = 
-UserGroupInformation.getL
+l
 oginUser
 ().d
@@ -1942,18 +1942,16 @@
 oginUser
-()
 .doAs(%0A 

Fix HDFS viewer authentication issue. (#2255)