001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.security;
019    
020    import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION;
021    
022    import java.io.IOException;
023    import java.lang.reflect.UndeclaredThrowableException;
024    import java.security.AccessControlContext;
025    import java.security.AccessController;
026    import java.security.Principal;
027    import java.security.PrivilegedAction;
028    import java.security.PrivilegedActionException;
029    import java.security.PrivilegedExceptionAction;
030    import java.util.ArrayList;
031    import java.util.Arrays;
032    import java.util.Collection;
033    import java.util.Collections;
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Map;
037    import java.util.Set;
038    
039    import javax.security.auth.Subject;
040    import javax.security.auth.callback.CallbackHandler;
041    import javax.security.auth.kerberos.KerberosKey;
042    import javax.security.auth.kerberos.KerberosPrincipal;
043    import javax.security.auth.kerberos.KerberosTicket;
044    import javax.security.auth.login.AppConfigurationEntry;
045    import javax.security.auth.login.LoginContext;
046    import javax.security.auth.login.LoginException;
047    import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
048    import javax.security.auth.spi.LoginModule;
049    
050    import org.apache.commons.logging.Log;
051    import org.apache.commons.logging.LogFactory;
052    import org.apache.hadoop.classification.InterfaceAudience;
053    import org.apache.hadoop.classification.InterfaceStability;
054    import org.apache.hadoop.conf.Configuration;
055    import org.apache.hadoop.fs.Path;
056    import org.apache.hadoop.metrics2.annotation.Metric;
057    import org.apache.hadoop.metrics2.annotation.Metrics;
058    import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
059    import org.apache.hadoop.metrics2.lib.MutableRate;
060    import org.apache.hadoop.security.authentication.util.KerberosName;
061    import org.apache.hadoop.security.token.Token;
062    import org.apache.hadoop.security.token.TokenIdentifier;
063    import org.apache.hadoop.util.Shell;
064    
065    import com.sun.security.auth.NTUserPrincipal;
066    import com.sun.security.auth.UnixPrincipal;
067    import com.sun.security.auth.module.Krb5LoginModule;
068    
069    /**
070     * User and group information for Hadoop.
071     * This class wraps around a JAAS Subject and provides methods to determine the
072     * user's username and groups. It supports both the Windows, Unix and Kerberos 
073     * login modules.
074     */
075    @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
076    @InterfaceStability.Evolving
077    public class UserGroupInformation {
078      private static final Log LOG =  LogFactory.getLog(UserGroupInformation.class);
079      /**
080       * Percentage of the ticket window to use before we renew ticket.
081       */
082      private static final float TICKET_RENEW_WINDOW = 0.80f;
083      static final String HADOOP_USER_NAME = "HADOOP_USER_NAME";
084      
085      /** 
086       * UgiMetrics maintains UGI activity statistics
087       * and publishes them through the metrics interfaces.
088       */
089      @Metrics(about="User and group related metrics", context="ugi")
090      static class UgiMetrics {
091        @Metric("Rate of successful kerberos logins and latency (milliseconds)")
092        MutableRate loginSuccess;
093        @Metric("Rate of failed kerberos logins and latency (milliseconds)")
094        MutableRate loginFailure;
095    
096        static UgiMetrics create() {
097          return DefaultMetricsSystem.instance().register(new UgiMetrics());
098        }
099      }
100      
101      /**
102       * A login module that looks at the Kerberos, Unix, or Windows principal and
103       * adds the corresponding UserName.
104       */
105      @InterfaceAudience.Private
106      public static class HadoopLoginModule implements LoginModule {
107        private Subject subject;
108    
109        @Override
110        public boolean abort() throws LoginException {
111          return true;
112        }
113    
114        private <T extends Principal> T getCanonicalUser(Class<T> cls) {
115          for(T user: subject.getPrincipals(cls)) {
116            return user;
117          }
118          return null;
119        }
120    
121        @Override
122        public boolean commit() throws LoginException {
123          if (LOG.isDebugEnabled()) {
124            LOG.debug("hadoop login commit");
125          }
126          // if we already have a user, we are done.
127          if (!subject.getPrincipals(User.class).isEmpty()) {
128            if (LOG.isDebugEnabled()) {
129              LOG.debug("using existing subject:"+subject.getPrincipals());
130            }
131            return true;
132          }
133          Principal user = null;
134          // if we are using kerberos, try it out
135          if (useKerberos) {
136            user = getCanonicalUser(KerberosPrincipal.class);
137            if (LOG.isDebugEnabled()) {
138              LOG.debug("using kerberos user:"+user);
139            }
140          }
141          //If we don't have a kerberos user and security is disabled, check
142          //if user is specified in the environment or properties
143          if (!isSecurityEnabled() && (user == null)) {
144            String envUser = System.getenv(HADOOP_USER_NAME);
145            if (envUser == null) {
146              envUser = System.getProperty(HADOOP_USER_NAME);
147            }
148            user = envUser == null ? null : new User(envUser);
149          }
150          // use the OS user
151          if (user == null) {
152            user = getCanonicalUser(OS_PRINCIPAL_CLASS);
153            if (LOG.isDebugEnabled()) {
154              LOG.debug("using local user:"+user);
155            }
156          }
157          // if we found the user, add our principal
158          if (user != null) {
159            subject.getPrincipals().add(new User(user.getName()));
160            return true;
161          }
162          LOG.error("Can't find user in " + subject);
163          throw new LoginException("Can't find user name");
164        }
165    
166        @Override
167        public void initialize(Subject subject, CallbackHandler callbackHandler,
168                               Map<String, ?> sharedState, Map<String, ?> options) {
169          this.subject = subject;
170        }
171    
172        @Override
173        public boolean login() throws LoginException {
174          if (LOG.isDebugEnabled()) {
175            LOG.debug("hadoop login");
176          }
177          return true;
178        }
179    
180        @Override
181        public boolean logout() throws LoginException {
182          if (LOG.isDebugEnabled()) {
183            LOG.debug("hadoop logout");
184          }
185          return true;
186        }
187      }
188    
189      /** Metrics to track UGI activity */
190      static UgiMetrics metrics = UgiMetrics.create();
191      /** Are the static variables that depend on configuration initialized? */
192      private static boolean isInitialized = false;
193      /** Should we use Kerberos configuration? */
194      private static boolean useKerberos;
195      /** Server-side groups fetching service */
196      private static Groups groups;
197      /** The configuration to use */
198      private static Configuration conf;
199    
200      
201      /** Leave 10 minutes between relogin attempts. */
202      private static final long MIN_TIME_BEFORE_RELOGIN = 10 * 60 * 1000L;
203      
204      /**Environment variable pointing to the token cache file*/
205      public static final String HADOOP_TOKEN_FILE_LOCATION = 
206        "HADOOP_TOKEN_FILE_LOCATION";
207      
208      /** 
209       * A method to initialize the fields that depend on a configuration.
210       * Must be called before useKerberos or groups is used.
211       */
212      private static synchronized void ensureInitialized() {
213        if (!isInitialized) {
214            initialize(new Configuration(), KerberosName.hasRulesBeenSet());
215        }
216      }
217    
218      /**
219       * Initialize UGI and related classes.
220       * @param conf the configuration to use
221       */
222      private static synchronized void initialize(Configuration conf, boolean skipRulesSetting) {
223        initUGI(conf);
224        // give the configuration on how to translate Kerberos names
225        try {
226          if (!skipRulesSetting) {
227            HadoopKerberosName.setConfiguration(conf);
228          }
229        } catch (IOException ioe) {
230          throw new RuntimeException("Problem with Kerberos auth_to_local name " +
231              "configuration", ioe);
232        }
233      }
234      
235      /**
236       * Set the configuration values for UGI.
237       * @param conf the configuration to use
238       */
239      private static synchronized void initUGI(Configuration conf) {
240        String value = conf.get(HADOOP_SECURITY_AUTHENTICATION);
241        if (value == null || "simple".equals(value)) {
242          useKerberos = false;
243        } else if ("kerberos".equals(value)) {
244          useKerberos = true;
245        } else {
246          throw new IllegalArgumentException("Invalid attribute value for " +
247                                             HADOOP_SECURITY_AUTHENTICATION + 
248                                             " of " + value);
249        }
250        // If we haven't set up testing groups, use the configuration to find it
251        if (!(groups instanceof TestingGroups)) {
252          groups = Groups.getUserToGroupsMappingService(conf);
253        }
254        isInitialized = true;
255        UserGroupInformation.conf = conf;
256      }
257    
258      /**
259       * Set the static configuration for UGI.
260       * In particular, set the security authentication mechanism and the
261       * group look up service.
262       * @param conf the configuration to use
263       */
264      public static void setConfiguration(Configuration conf) {
265        initialize(conf, false);
266      }
267      
268      /**
269       * Determine if UserGroupInformation is using Kerberos to determine
270       * user identities or is relying on simple authentication
271       * 
272       * @return true if UGI is working in a secure environment
273       */
274      public static boolean isSecurityEnabled() {
275        ensureInitialized();
276        return useKerberos;
277      }
278      
279      /**
280       * Information about the logged in user.
281       */
282      private static UserGroupInformation loginUser = null;
283      private static String keytabPrincipal = null;
284      private static String keytabFile = null;
285    
286      private final Subject subject;
287      // All non-static fields must be read-only caches that come from the subject.
288      private final User user;
289      private final boolean isKeytab;
290      private final boolean isKrbTkt;
291      
292      private static final String OS_LOGIN_MODULE_NAME;
293      private static final Class<? extends Principal> OS_PRINCIPAL_CLASS;
294      private static final boolean windows = 
295                               System.getProperty("os.name").startsWith("Windows");
296      static {
297        if (windows) {
298          OS_LOGIN_MODULE_NAME = "com.sun.security.auth.module.NTLoginModule";
299          OS_PRINCIPAL_CLASS = NTUserPrincipal.class;
300        } else {
301          OS_LOGIN_MODULE_NAME = "com.sun.security.auth.module.UnixLoginModule";
302          OS_PRINCIPAL_CLASS = UnixPrincipal.class;
303        }
304      }
305      
306      private static class RealUser implements Principal {
307        private final UserGroupInformation realUser;
308        
309        RealUser(UserGroupInformation realUser) {
310          this.realUser = realUser;
311        }
312        
313        public String getName() {
314          return realUser.getUserName();
315        }
316        
317        public UserGroupInformation getRealUser() {
318          return realUser;
319        }
320        
321        @Override
322        public boolean equals(Object o) {
323          if (this == o) {
324            return true;
325          } else if (o == null || getClass() != o.getClass()) {
326            return false;
327          } else {
328            return realUser.equals(((RealUser) o).realUser);
329          }
330        }
331        
332        @Override
333        public int hashCode() {
334          return realUser.hashCode();
335        }
336        
337        @Override
338        public String toString() {
339          return realUser.toString();
340        }
341      }
342      
343      /**
344       * A JAAS configuration that defines the login modules that we want
345       * to use for login.
346       */
347      private static class HadoopConfiguration 
348          extends javax.security.auth.login.Configuration {
349        private static final String SIMPLE_CONFIG_NAME = "hadoop-simple";
350        private static final String USER_KERBEROS_CONFIG_NAME = 
351          "hadoop-user-kerberos";
352        private static final String KEYTAB_KERBEROS_CONFIG_NAME = 
353          "hadoop-keytab-kerberos";
354    
355        private static final Map<String, String> BASIC_JAAS_OPTIONS =
356          new HashMap<String,String>();
357        static {
358          String jaasEnvVar = System.getenv("HADOOP_JAAS_DEBUG");
359          if (jaasEnvVar != null && "true".equalsIgnoreCase(jaasEnvVar)) {
360            BASIC_JAAS_OPTIONS.put("debug", "true");
361          }
362        }
363        
364        private static final AppConfigurationEntry OS_SPECIFIC_LOGIN =
365          new AppConfigurationEntry(OS_LOGIN_MODULE_NAME,
366                                    LoginModuleControlFlag.REQUIRED,
367                                    BASIC_JAAS_OPTIONS);
368        private static final AppConfigurationEntry HADOOP_LOGIN =
369          new AppConfigurationEntry(HadoopLoginModule.class.getName(),
370                                    LoginModuleControlFlag.REQUIRED,
371                                    BASIC_JAAS_OPTIONS);
372        private static final Map<String,String> USER_KERBEROS_OPTIONS = 
373          new HashMap<String,String>();
374        static {
375          USER_KERBEROS_OPTIONS.put("doNotPrompt", "true");
376          USER_KERBEROS_OPTIONS.put("useTicketCache", "true");
377          USER_KERBEROS_OPTIONS.put("renewTGT", "true");
378          String ticketCache = System.getenv("KRB5CCNAME");
379          if (ticketCache != null) {
380            USER_KERBEROS_OPTIONS.put("ticketCache", ticketCache);
381          }
382          USER_KERBEROS_OPTIONS.putAll(BASIC_JAAS_OPTIONS);
383        }
384        private static final AppConfigurationEntry USER_KERBEROS_LOGIN =
385          new AppConfigurationEntry(Krb5LoginModule.class.getName(),
386                                    LoginModuleControlFlag.OPTIONAL,
387                                    USER_KERBEROS_OPTIONS);
388        private static final Map<String,String> KEYTAB_KERBEROS_OPTIONS = 
389          new HashMap<String,String>();
390        static {
391          KEYTAB_KERBEROS_OPTIONS.put("doNotPrompt", "true");
392          KEYTAB_KERBEROS_OPTIONS.put("useKeyTab", "true");
393          KEYTAB_KERBEROS_OPTIONS.put("storeKey", "true");
394          KEYTAB_KERBEROS_OPTIONS.put("refreshKrb5Config", "true");
395          KEYTAB_KERBEROS_OPTIONS.putAll(BASIC_JAAS_OPTIONS);      
396        }
397        private static final AppConfigurationEntry KEYTAB_KERBEROS_LOGIN =
398          new AppConfigurationEntry(Krb5LoginModule.class.getName(),
399                                    LoginModuleControlFlag.REQUIRED,
400                                    KEYTAB_KERBEROS_OPTIONS);
401        
402        private static final AppConfigurationEntry[] SIMPLE_CONF = 
403          new AppConfigurationEntry[]{OS_SPECIFIC_LOGIN, HADOOP_LOGIN};
404    
405        private static final AppConfigurationEntry[] USER_KERBEROS_CONF =
406          new AppConfigurationEntry[]{OS_SPECIFIC_LOGIN, USER_KERBEROS_LOGIN,
407                                      HADOOP_LOGIN};
408    
409        private static final AppConfigurationEntry[] KEYTAB_KERBEROS_CONF =
410          new AppConfigurationEntry[]{KEYTAB_KERBEROS_LOGIN, HADOOP_LOGIN};
411    
412        @Override
413        public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
414          if (SIMPLE_CONFIG_NAME.equals(appName)) {
415            return SIMPLE_CONF;
416          } else if (USER_KERBEROS_CONFIG_NAME.equals(appName)) {
417            return USER_KERBEROS_CONF;
418          } else if (KEYTAB_KERBEROS_CONFIG_NAME.equals(appName)) {
419            KEYTAB_KERBEROS_OPTIONS.put("keyTab", keytabFile);
420            KEYTAB_KERBEROS_OPTIONS.put("principal", keytabPrincipal);
421            return KEYTAB_KERBEROS_CONF;
422          }
423          return null;
424        }
425      }
426      
427      private static LoginContext
428      newLoginContext(String appName, Subject subject) throws LoginException {
429        // Temporarily switch the thread's ContextClassLoader to match this
430        // class's classloader, so that we can properly load HadoopLoginModule
431        // from the JAAS libraries.
432        Thread t = Thread.currentThread();
433        ClassLoader oldCCL = t.getContextClassLoader();
434        t.setContextClassLoader(HadoopLoginModule.class.getClassLoader());
435        try {
436          return new LoginContext(appName, subject, null, new HadoopConfiguration());
437        } finally {
438          t.setContextClassLoader(oldCCL);
439        }
440      }
441    
442      private LoginContext getLogin() {
443        return user.getLogin();
444      }
445      
446      private void setLogin(LoginContext login) {
447        user.setLogin(login);
448      }
449    
450      /**
451       * Create a UserGroupInformation for the given subject.
452       * This does not change the subject or acquire new credentials.
453       * @param subject the user's subject
454       */
455      UserGroupInformation(Subject subject) {
456        this.subject = subject;
457        this.user = subject.getPrincipals(User.class).iterator().next();
458        this.isKeytab = !subject.getPrivateCredentials(KerberosKey.class).isEmpty();
459        this.isKrbTkt = !subject.getPrivateCredentials(KerberosTicket.class).isEmpty();
460      }
461      
462      /**
463       * checks if logged in using kerberos
464       * @return true if the subject logged via keytab or has a Kerberos TGT
465       */
466      public boolean hasKerberosCredentials() {
467        return isKeytab || isKrbTkt;
468      }
469    
470      /**
471       * Return the current user, including any doAs in the current stack.
472       * @return the current user
473       * @throws IOException if login fails
474       */
475      public synchronized
476      static UserGroupInformation getCurrentUser() throws IOException {
477        AccessControlContext context = AccessController.getContext();
478        Subject subject = Subject.getSubject(context);
479        if (subject == null || subject.getPrincipals(User.class).isEmpty()) {
480          return getLoginUser();
481        } else {
482          return new UserGroupInformation(subject);
483        }
484      }
485    
486      /**
487       * Get the currently logged in user.
488       * @return the logged in user
489       * @throws IOException if login fails
490       */
491      public synchronized 
492      static UserGroupInformation getLoginUser() throws IOException {
493        if (loginUser == null) {
494          try {
495            Subject subject = new Subject();
496            LoginContext login;
497            if (isSecurityEnabled()) {
498              login = newLoginContext(HadoopConfiguration.USER_KERBEROS_CONFIG_NAME,
499                  subject);
500            } else {
501              login = newLoginContext(HadoopConfiguration.SIMPLE_CONFIG_NAME, 
502                  subject);
503            }
504            login.login();
505            loginUser = new UserGroupInformation(subject);
506            loginUser.setLogin(login);
507            loginUser.setAuthenticationMethod(isSecurityEnabled() ?
508                                              AuthenticationMethod.KERBEROS :
509                                              AuthenticationMethod.SIMPLE);
510            loginUser = new UserGroupInformation(login.getSubject());
511            String fileLocation = System.getenv(HADOOP_TOKEN_FILE_LOCATION);
512            if (fileLocation != null && isSecurityEnabled()) {
513              // load the token storage file and put all of the tokens into the
514              // user.
515              Credentials cred = Credentials.readTokenStorageFile(
516                  new Path("file:///" + fileLocation), conf);
517              for (Token<?> token: cred.getAllTokens()) {
518                loginUser.addToken(token);
519              }
520            }
521            loginUser.spawnAutoRenewalThreadForUserCreds();
522          } catch (LoginException le) {
523            throw new IOException("failure to login", le);
524          }
525          if (LOG.isDebugEnabled()) {
526            LOG.debug("UGI loginUser:"+loginUser);
527          }
528        }
529        return loginUser;
530      }
531    
532      /**
533       * Is this user logged in from a keytab file?
534       * @return true if the credentials are from a keytab file.
535       */
536      public boolean isFromKeytab() {
537        return isKeytab;
538      }
539      
540      /**
541       * Get the Kerberos TGT
542       * @return the user's TGT or null if none was found
543       */
544      private synchronized KerberosTicket getTGT() {
545        Set<KerberosTicket> tickets = subject
546            .getPrivateCredentials(KerberosTicket.class);
547        for (KerberosTicket ticket : tickets) {
548          if (SecurityUtil.isOriginalTGT(ticket)) {
549            if (LOG.isDebugEnabled()) {
550              LOG.debug("Found tgt " + ticket);
551            }
552            return ticket;
553          }
554        }
555        return null;
556      }
557      
558      private long getRefreshTime(KerberosTicket tgt) {
559        long start = tgt.getStartTime().getTime();
560        long end = tgt.getEndTime().getTime();
561        return start + (long) ((end - start) * TICKET_RENEW_WINDOW);
562      }
563    
564      /**Spawn a thread to do periodic renewals of kerberos credentials*/
565      private void spawnAutoRenewalThreadForUserCreds() {
566        if (isSecurityEnabled()) {
567          //spawn thread only if we have kerb credentials
568          if (user.getAuthenticationMethod() == AuthenticationMethod.KERBEROS &&
569              !isKeytab) {
570            Thread t = new Thread(new Runnable() {
571              
572              public void run() {
573                String cmd = conf.get("hadoop.kerberos.kinit.command",
574                                      "kinit");
575                KerberosTicket tgt = getTGT();
576                if (tgt == null) {
577                  return;
578                }
579                long nextRefresh = getRefreshTime(tgt);
580                while (true) {
581                  try {
582                    long now = System.currentTimeMillis();
583                    if(LOG.isDebugEnabled()) {
584                      LOG.debug("Current time is " + now);
585                      LOG.debug("Next refresh is " + nextRefresh);
586                    }
587                    if (now < nextRefresh) {
588                      Thread.sleep(nextRefresh - now);
589                    }
590                    Shell.execCommand(cmd, "-R");
591                    if(LOG.isDebugEnabled()) {
592                      LOG.debug("renewed ticket");
593                    }
594                    reloginFromTicketCache();
595                    tgt = getTGT();
596                    if (tgt == null) {
597                      LOG.warn("No TGT after renewal. Aborting renew thread for " +
598                               getUserName());
599                      return;
600                    }
601                    nextRefresh = Math.max(getRefreshTime(tgt),
602                                           now + MIN_TIME_BEFORE_RELOGIN);
603                  } catch (InterruptedException ie) {
604                    LOG.warn("Terminating renewal thread");
605                    return;
606                  } catch (IOException ie) {
607                    LOG.warn("Exception encountered while running the" +
608                        " renewal command. Aborting renew thread. " + ie);
609                    return;
610                  }
611                }
612              }
613            });
614            t.setDaemon(true);
615            t.setName("TGT Renewer for " + getUserName());
616            t.start();
617          }
618        }
619      }
620      /**
621       * Log a user in from a keytab file. Loads a user identity from a keytab
622       * file and logs them in. They become the currently logged-in user.
623       * @param user the principal name to load from the keytab
624       * @param path the path to the keytab file
625       * @throws IOException if the keytab file can't be read
626       */
627      public synchronized
628      static void loginUserFromKeytab(String user,
629                                      String path
630                                      ) throws IOException {
631        if (!isSecurityEnabled())
632          return;
633    
634        keytabFile = path;
635        keytabPrincipal = user;
636        Subject subject = new Subject();
637        LoginContext login; 
638        long start = 0;
639        try {
640          login = 
641            newLoginContext(HadoopConfiguration.KEYTAB_KERBEROS_CONFIG_NAME, subject);
642          start = System.currentTimeMillis();
643          login.login();
644          metrics.loginSuccess.add(System.currentTimeMillis() - start);
645          loginUser = new UserGroupInformation(subject);
646          loginUser.setLogin(login);
647          loginUser.setAuthenticationMethod(AuthenticationMethod.KERBEROS);
648        } catch (LoginException le) {
649          if (start > 0) {
650            metrics.loginFailure.add(System.currentTimeMillis() - start);
651          }
652          throw new IOException("Login failure for " + user + " from keytab " + 
653                                path, le);
654        }
655        LOG.info("Login successful for user " + keytabPrincipal
656            + " using keytab file " + keytabFile);
657      }
658      
659      /**
660       * Re-login a user from keytab if TGT is expired or is close to expiry.
661       * 
662       * @throws IOException
663       */
664      public synchronized void checkTGTAndReloginFromKeytab() throws IOException {
665        if (!isSecurityEnabled()
666            || user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS
667            || !isKeytab)
668          return;
669        KerberosTicket tgt = getTGT();
670        if (tgt != null && System.currentTimeMillis() < getRefreshTime(tgt)) {
671          return;
672        }
673        reloginFromKeytab();
674      }
675    
676      /**
677       * Re-Login a user in from a keytab file. Loads a user identity from a keytab
678       * file and logs them in. They become the currently logged-in user. This
679       * method assumes that {@link #loginUserFromKeytab(String, String)} had 
680       * happened already.
681       * The Subject field of this UserGroupInformation object is updated to have
682       * the new credentials.
683       * @throws IOException on a failure
684       */
685      public synchronized void reloginFromKeytab()
686      throws IOException {
687        if (!isSecurityEnabled() ||
688             user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS ||
689             !isKeytab)
690          return;
691        
692        long now = System.currentTimeMillis();
693        if (!hasSufficientTimeElapsed(now)) {
694          return;
695        }
696    
697        KerberosTicket tgt = getTGT();
698        //Return if TGT is valid and is not going to expire soon.
699        if (tgt != null && now < getRefreshTime(tgt)) {
700          return;
701        }
702        
703        LoginContext login = getLogin();
704        if (login == null || keytabFile == null) {
705          throw new IOException("loginUserFromKeyTab must be done first");
706        }
707        
708        long start = 0;
709        // register most recent relogin attempt
710        user.setLastLogin(now);
711        try {
712          LOG.info("Initiating logout for " + getUserName());
713          synchronized (UserGroupInformation.class) {
714            // clear up the kerberos state. But the tokens are not cleared! As per
715            // the Java kerberos login module code, only the kerberos credentials
716            // are cleared
717            login.logout();
718            // login and also update the subject field of this instance to
719            // have the new credentials (pass it to the LoginContext constructor)
720            login = newLoginContext(
721                HadoopConfiguration.KEYTAB_KERBEROS_CONFIG_NAME, getSubject());
722            LOG.info("Initiating re-login for " + keytabPrincipal);
723            start = System.currentTimeMillis();
724            login.login();
725            metrics.loginSuccess.add(System.currentTimeMillis() - start);
726            setLogin(login);
727          }
728        } catch (LoginException le) {
729          if (start > 0) {
730            metrics.loginFailure.add(System.currentTimeMillis() - start);
731          }
732          throw new IOException("Login failure for " + keytabPrincipal + 
733              " from keytab " + keytabFile, le);
734        } 
735      }
736    
737      /**
738       * Re-Login a user in from the ticket cache.  This
739       * method assumes that login had happened already.
740       * The Subject field of this UserGroupInformation object is updated to have
741       * the new credentials.
742       * @throws IOException on a failure
743       */
744      public synchronized void reloginFromTicketCache()
745      throws IOException {
746        if (!isSecurityEnabled() || 
747            user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS ||
748            !isKrbTkt)
749          return;
750        LoginContext login = getLogin();
751        if (login == null) {
752          throw new IOException("login must be done first");
753        }
754        long now = System.currentTimeMillis();
755        if (!hasSufficientTimeElapsed(now)) {
756          return;
757        }
758        // register most recent relogin attempt
759        user.setLastLogin(now);
760        try {
761          LOG.info("Initiating logout for " + getUserName());
762          //clear up the kerberos state. But the tokens are not cleared! As per 
763          //the Java kerberos login module code, only the kerberos credentials
764          //are cleared
765          login.logout();
766          //login and also update the subject field of this instance to 
767          //have the new credentials (pass it to the LoginContext constructor)
768          login = 
769            newLoginContext(HadoopConfiguration.USER_KERBEROS_CONFIG_NAME, 
770                getSubject());
771          LOG.info("Initiating re-login for " + getUserName());
772          login.login();
773          setLogin(login);
774        } catch (LoginException le) {
775          throw new IOException("Login failure for " + getUserName(), le);
776        } 
777      }
778    
779    
780      /**
781       * Log a user in from a keytab file. Loads a user identity from a keytab
782       * file and login them in. This new user does not affect the currently
783       * logged-in user.
784       * @param user the principal name to load from the keytab
785       * @param path the path to the keytab file
786       * @throws IOException if the keytab file can't be read
787       */
788      public synchronized
789      static UserGroupInformation loginUserFromKeytabAndReturnUGI(String user,
790                                      String path
791                                      ) throws IOException {
792        if (!isSecurityEnabled())
793          return UserGroupInformation.getCurrentUser();
794        String oldKeytabFile = null;
795        String oldKeytabPrincipal = null;
796    
797        long start = 0;
798        try {
799          oldKeytabFile = keytabFile;
800          oldKeytabPrincipal = keytabPrincipal;
801          keytabFile = path;
802          keytabPrincipal = user;
803          Subject subject = new Subject();
804          
805          LoginContext login = 
806            newLoginContext(HadoopConfiguration.KEYTAB_KERBEROS_CONFIG_NAME, subject); 
807           
808          start = System.currentTimeMillis();
809          login.login();
810          metrics.loginSuccess.add(System.currentTimeMillis() - start);
811          UserGroupInformation newLoginUser = new UserGroupInformation(subject);
812          newLoginUser.setLogin(login);
813          newLoginUser.setAuthenticationMethod(AuthenticationMethod.KERBEROS);
814          
815          return newLoginUser;
816        } catch (LoginException le) {
817          if (start > 0) {
818            metrics.loginFailure.add(System.currentTimeMillis() - start);
819          }
820          throw new IOException("Login failure for " + user + " from keytab " + 
821                                path, le);
822        } finally {
823          if(oldKeytabFile != null) keytabFile = oldKeytabFile;
824          if(oldKeytabPrincipal != null) keytabPrincipal = oldKeytabPrincipal;
825        }
826      }
827    
828      private boolean hasSufficientTimeElapsed(long now) {
829        if (now - user.getLastLogin() < MIN_TIME_BEFORE_RELOGIN ) {
830          LOG.warn("Not attempting to re-login since the last re-login was " +
831              "attempted less than " + (MIN_TIME_BEFORE_RELOGIN/1000) + " seconds"+
832              " before.");
833          return false;
834        }
835        return true;
836      }
837      
838      /**
839       * Did the login happen via keytab
840       * @return true or false
841       */
842      public synchronized static boolean isLoginKeytabBased() throws IOException {
843        return getLoginUser().isKeytab;
844      }
845    
846      /**
847       * Create a user from a login name. It is intended to be used for remote
848       * users in RPC, since it won't have any credentials.
849       * @param user the full user principal name, must not be empty or null
850       * @return the UserGroupInformation for the remote user.
851       */
852      public static UserGroupInformation createRemoteUser(String user) {
853        if (user == null || "".equals(user)) {
854          throw new IllegalArgumentException("Null user");
855        }
856        Subject subject = new Subject();
857        subject.getPrincipals().add(new User(user));
858        UserGroupInformation result = new UserGroupInformation(subject);
859        result.setAuthenticationMethod(AuthenticationMethod.SIMPLE);
860        return result;
861      }
862    
863      /**
864       * existing types of authentications' methods
865       */
866      @InterfaceStability.Evolving
867      public static enum AuthenticationMethod {
868        SIMPLE,
869        KERBEROS,
870        TOKEN,
871        CERTIFICATE,
872        KERBEROS_SSL,
873        PROXY;
874      }
875    
876      /**
877       * Create a proxy user using username of the effective user and the ugi of the
878       * real user.
879       * @param user
880       * @param realUser
881       * @return proxyUser ugi
882       */
883      public static UserGroupInformation createProxyUser(String user,
884          UserGroupInformation realUser) {
885        if (user == null || "".equals(user)) {
886          throw new IllegalArgumentException("Null user");
887        }
888        if (realUser == null) {
889          throw new IllegalArgumentException("Null real user");
890        }
891        Subject subject = new Subject();
892        Set<Principal> principals = subject.getPrincipals();
893        principals.add(new User(user));
894        principals.add(new RealUser(realUser));
895        UserGroupInformation result =new UserGroupInformation(subject);
896        result.setAuthenticationMethod(AuthenticationMethod.PROXY);
897        return result;
898      }
899    
900      /**
901       * get RealUser (vs. EffectiveUser)
902       * @return realUser running over proxy user
903       */
904      public UserGroupInformation getRealUser() {
905        for (RealUser p: subject.getPrincipals(RealUser.class)) {
906          return p.getRealUser();
907        }
908        return null;
909      }
910    
911    
912      
913      /**
914       * This class is used for storing the groups for testing. It stores a local
915       * map that has the translation of usernames to groups.
916       */
917      private static class TestingGroups extends Groups {
918        private final Map<String, List<String>> userToGroupsMapping = 
919          new HashMap<String,List<String>>();
920        private Groups underlyingImplementation;
921        
922        private TestingGroups(Groups underlyingImplementation) {
923          super(new org.apache.hadoop.conf.Configuration());
924          this.underlyingImplementation = underlyingImplementation;
925        }
926        
927        @Override
928        public List<String> getGroups(String user) throws IOException {
929          List<String> result = userToGroupsMapping.get(user);
930          
931          if (result == null) {
932            result = underlyingImplementation.getGroups(user);
933          }
934    
935          return result;
936        }
937    
938        private void setUserGroups(String user, String[] groups) {
939          userToGroupsMapping.put(user, Arrays.asList(groups));
940        }
941      }
942    
943      /**
944       * Create a UGI for testing HDFS and MapReduce
945       * @param user the full user principal name
946       * @param userGroups the names of the groups that the user belongs to
947       * @return a fake user for running unit tests
948       */
949      @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
950      public static UserGroupInformation createUserForTesting(String user, 
951                                                              String[] userGroups) {
952        ensureInitialized();
953        UserGroupInformation ugi = createRemoteUser(user);
954        // make sure that the testing object is setup
955        if (!(groups instanceof TestingGroups)) {
956          groups = new TestingGroups(groups);
957        }
958        // add the user groups
959        ((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups);
960        return ugi;
961      }
962    
963    
964      /**
965       * Create a proxy user UGI for testing HDFS and MapReduce
966       * 
967       * @param user
968       *          the full user principal name for effective user
969       * @param realUser
970       *          UGI of the real user
971       * @param userGroups
972       *          the names of the groups that the user belongs to
973       * @return a fake user for running unit tests
974       */
975      @InterfaceAudience.LimitedPrivate( { "HDFS", "MapReduce" })
976      public static UserGroupInformation createProxyUserForTesting(String user,
977          UserGroupInformation realUser, String[] userGroups) {
978        ensureInitialized();
979        UserGroupInformation ugi = createProxyUser(user, realUser);
980        // make sure that the testing object is setup
981        if (!(groups instanceof TestingGroups)) {
982          groups = new TestingGroups(groups);
983        }
984        // add the user groups
985        ((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups);
986        return ugi;
987      }
988      
989      /**
990       * Get the user's login name.
991       * @return the user's name up to the first '/' or '@'.
992       */
993      public String getShortUserName() {
994        for (User p: subject.getPrincipals(User.class)) {
995          return p.getShortName();
996        }
997        return null;
998      }
999    
1000      /**
1001       * Get the user's full principal name.
1002       * @return the user's full principal name.
1003       */
1004      public String getUserName() {
1005        return user.getName();
1006      }
1007    
1008      /**
1009       * Add a TokenIdentifier to this UGI. The TokenIdentifier has typically been
1010       * authenticated by the RPC layer as belonging to the user represented by this
1011       * UGI.
1012       * 
1013       * @param tokenId
1014       *          tokenIdentifier to be added
1015       * @return true on successful add of new tokenIdentifier
1016       */
1017      public synchronized boolean addTokenIdentifier(TokenIdentifier tokenId) {
1018        return subject.getPublicCredentials().add(tokenId);
1019      }
1020    
1021      /**
1022       * Get the set of TokenIdentifiers belonging to this UGI
1023       * 
1024       * @return the set of TokenIdentifiers belonging to this UGI
1025       */
1026      public synchronized Set<TokenIdentifier> getTokenIdentifiers() {
1027        return subject.getPublicCredentials(TokenIdentifier.class);
1028      }
1029      
1030      /**
1031       * Add a token to this UGI
1032       * 
1033       * @param token Token to be added
1034       * @return true on successful add of new token
1035       */
1036      public synchronized boolean addToken(Token<? extends TokenIdentifier> token) {
1037        return subject.getPrivateCredentials().add(token);
1038      }
1039      
1040      /**
1041       * Obtain the collection of tokens associated with this user.
1042       * 
1043       * @return an unmodifiable collection of tokens associated with user
1044       */
1045      public synchronized
1046      Collection<Token<? extends TokenIdentifier>> getTokens() {
1047        Set<Object> creds = subject.getPrivateCredentials();
1048        List<Token<?>> result = new ArrayList<Token<?>>(creds.size());
1049        for(Object o: creds) {
1050          if (o instanceof Token<?>) {
1051            result.add((Token<?>) o);
1052          }
1053        }
1054        return Collections.unmodifiableList(result);
1055      }
1056    
1057      /**
1058       * Get the group names for this user.
1059       * @return the list of users with the primary group first. If the command
1060       *    fails, it returns an empty list.
1061       */
1062      public synchronized String[] getGroupNames() {
1063        ensureInitialized();
1064        try {
1065          List<String> result = groups.getGroups(getShortUserName());
1066          return result.toArray(new String[result.size()]);
1067        } catch (IOException ie) {
1068          LOG.warn("No groups available for user " + getShortUserName());
1069          return new String[0];
1070        }
1071      }
1072      
1073      /**
1074       * Return the username.
1075       */
1076      @Override
1077      public String toString() {
1078        StringBuilder sb = new StringBuilder(getUserName());
1079        sb.append(" (auth:"+getAuthenticationMethod()+")");
1080        if (getRealUser() != null) {
1081          sb.append(" via ").append(getRealUser().toString());
1082        }
1083        return sb.toString();
1084      }
1085    
1086      /**
1087       * Sets the authentication method in the subject
1088       * 
1089       * @param authMethod
1090       */
1091      public synchronized 
1092      void setAuthenticationMethod(AuthenticationMethod authMethod) {
1093        user.setAuthenticationMethod(authMethod);
1094      }
1095    
1096      /**
1097       * Get the authentication method from the subject
1098       * 
1099       * @return AuthenticationMethod in the subject, null if not present.
1100       */
1101      public synchronized AuthenticationMethod getAuthenticationMethod() {
1102        return user.getAuthenticationMethod();
1103      }
1104      
1105      /**
1106       * Returns the authentication method of a ugi. If the authentication method is
1107       * PROXY, returns the authentication method of the real user.
1108       * 
1109       * @param ugi
1110       * @return AuthenticationMethod
1111       */
1112      public static AuthenticationMethod getRealAuthenticationMethod(
1113          UserGroupInformation ugi) {
1114        AuthenticationMethod authMethod = ugi.getAuthenticationMethod();
1115        if (authMethod == AuthenticationMethod.PROXY) {
1116          authMethod = ugi.getRealUser().getAuthenticationMethod();
1117        }
1118        return authMethod;
1119      }
1120    
1121      /**
1122       * Compare the subjects to see if they are equal to each other.
1123       */
1124      @Override
1125      public boolean equals(Object o) {
1126        if (o == this) {
1127          return true;
1128        } else if (o == null || getClass() != o.getClass()) {
1129          return false;
1130        } else {
1131          return subject == ((UserGroupInformation) o).subject;
1132        }
1133      }
1134    
1135      /**
1136       * Return the hash of the subject.
1137       */
1138      @Override
1139      public int hashCode() {
1140        return System.identityHashCode(subject);
1141      }
1142    
1143      /**
1144       * Get the underlying subject from this ugi.
1145       * @return the subject that represents this user.
1146       */
1147      protected Subject getSubject() {
1148        return subject;
1149      }
1150    
1151      /**
1152       * Run the given action as the user.
1153       * @param <T> the return type of the run method
1154       * @param action the method to execute
1155       * @return the value from the run method
1156       */
1157      public <T> T doAs(PrivilegedAction<T> action) {
1158        logPrivilegedAction(subject, action);
1159        return Subject.doAs(subject, action);
1160      }
1161      
1162      /**
1163       * Run the given action as the user, potentially throwing an exception.
1164       * @param <T> the return type of the run method
1165       * @param action the method to execute
1166       * @return the value from the run method
1167       * @throws IOException if the action throws an IOException
1168       * @throws Error if the action throws an Error
1169       * @throws RuntimeException if the action throws a RuntimeException
1170       * @throws InterruptedException if the action throws an InterruptedException
1171       * @throws UndeclaredThrowableException if the action throws something else
1172       */
1173      public <T> T doAs(PrivilegedExceptionAction<T> action
1174                        ) throws IOException, InterruptedException {
1175        try {
1176          logPrivilegedAction(subject, action);
1177          return Subject.doAs(subject, action);
1178        } catch (PrivilegedActionException pae) {
1179          Throwable cause = pae.getCause();
1180          LOG.error("PriviledgedActionException as:"+this+" cause:"+cause);
1181          if (cause instanceof IOException) {
1182            throw (IOException) cause;
1183          } else if (cause instanceof Error) {
1184            throw (Error) cause;
1185          } else if (cause instanceof RuntimeException) {
1186            throw (RuntimeException) cause;
1187          } else if (cause instanceof InterruptedException) {
1188            throw (InterruptedException) cause;
1189          } else {
1190            throw new UndeclaredThrowableException(pae,"Unknown exception in doAs");
1191          }
1192        }
1193      }
1194    
1195      private void logPrivilegedAction(Subject subject, Object action) {
1196        if (LOG.isDebugEnabled()) {
1197          // would be nice if action included a descriptive toString()
1198          String where = new Throwable().getStackTrace()[2].toString();
1199          LOG.debug("PrivilegedAction as:"+this+" from:"+where);
1200        }
1201      }
1202    
1203      private void print() throws IOException {
1204        System.out.println("User: " + getUserName());
1205        System.out.print("Group Ids: ");
1206        System.out.println();
1207        String[] groups = getGroupNames();
1208        System.out.print("Groups: ");
1209        for(int i=0; i < groups.length; i++) {
1210          System.out.print(groups[i] + " ");
1211        }
1212        System.out.println();    
1213      }
1214    
1215      /**
1216       * A test method to print out the current user's UGI.
1217       * @param args if there are two arguments, read the user from the keytab
1218       * and print it out.
1219       * @throws Exception
1220       */
1221      public static void main(String [] args) throws Exception {
1222      System.out.println("Getting UGI for current user");
1223        UserGroupInformation ugi = getCurrentUser();
1224        ugi.print();
1225        System.out.println("UGI: " + ugi);
1226        System.out.println("Auth method " + ugi.user.getAuthenticationMethod());
1227        System.out.println("Keytab " + ugi.isKeytab);
1228        System.out.println("============================================================");
1229        
1230        if (args.length == 2) {
1231          System.out.println("Getting UGI from keytab....");
1232          loginUserFromKeytab(args[0], args[1]);
1233          getCurrentUser().print();
1234          System.out.println("Keytab: " + ugi);
1235          System.out.println("Auth method " + loginUser.user.getAuthenticationMethod());
1236          System.out.println("Keytab " + loginUser.isKeytab);
1237        }
1238      }
1239    
1240    }