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 */
018package org.apache.hadoop.security;
019
020import static org.apache.hadoop.security.UGIExceptionMessages.*;
021
022import java.io.IOException;
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceStability;
025
026/**
027 * Thrown when {@link UserGroupInformation} failed with an unrecoverable error,
028 * such as failure in kerberos login/logout, invalid subject etc.
029 *
030 * Caller should not retry when catching this exception.
031 */
032@InterfaceAudience.Public
033@InterfaceStability.Unstable
034public class KerberosAuthException extends IOException {
035  static final long serialVersionUID = 31L;
036
037  private String user;
038  private String principal;
039  private String keytabFile;
040  private String ticketCacheFile;
041  private String initialMessage;
042
043  public KerberosAuthException(String msg) {
044    super(msg);
045  }
046
047  public KerberosAuthException(Throwable cause) {
048    super(cause);
049  }
050
051  public KerberosAuthException(String initialMsg, Throwable cause) {
052    this(cause);
053    initialMessage = initialMsg;
054  }
055
056  public void setUser(final String u) {
057    user = u;
058  }
059
060  public void setPrincipal(final String p) {
061    principal = p;
062  }
063
064  public void setKeytabFile(final String k) {
065    keytabFile = k;
066  }
067
068  public void setTicketCacheFile(final String t) {
069    ticketCacheFile = t;
070  }
071
072  /** @return The initial message, or null if not set. */
073  public String getInitialMessage() {
074    return initialMessage;
075  }
076
077  /** @return The keytab file path, or null if not set. */
078  public String getKeytabFile() {
079    return keytabFile;
080  }
081
082  /** @return The principal, or null if not set. */
083  public String getPrincipal() {
084    return principal;
085  }
086
087  /** @return The ticket cache file path, or null if not set. */
088  public String getTicketCacheFile() {
089    return ticketCacheFile;
090  }
091
092  /** @return The user, or null if not set. */
093  public String getUser() {
094    return user;
095  }
096
097  @Override
098  public String getMessage() {
099    final StringBuilder sb = new StringBuilder();
100    if (initialMessage != null) {
101      sb.append(initialMessage);
102    }
103    if (user != null) {
104      sb.append(FOR_USER + user);
105    }
106    if (principal != null) {
107      sb.append(FOR_PRINCIPAL + principal);
108    }
109    if (keytabFile != null) {
110      sb.append(FROM_KEYTAB + keytabFile);
111    }
112    if (ticketCacheFile != null) {
113      sb.append(USING_TICKET_CACHE_FILE+ ticketCacheFile);
114    }
115    sb.append(" " + super.getMessage());
116    return sb.toString();
117  }
118}