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
019package org.apache.hadoop.hdfs.security.token.delegation;
020
021import java.io.ByteArrayInputStream;
022import java.io.DataInputStream;
023import java.io.IOException;
024import java.util.Collections;
025import java.util.Map;
026
027import org.apache.commons.collections.map.LRUMap;
028import org.apache.hadoop.classification.InterfaceAudience;
029import org.apache.hadoop.hdfs.web.WebHdfsConstants;
030import org.apache.hadoop.io.Text;
031import org.apache.hadoop.security.UserGroupInformation;
032import org.apache.hadoop.security.token.Token;
033import org.apache.hadoop.security.token.TokenIdentifier;
034import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
035
036import com.google.common.annotations.VisibleForTesting;
037
038/**
039 * A delegation token identifier that is specific to HDFS.
040 */
041@InterfaceAudience.Private
042public class DelegationTokenIdentifier
043    extends AbstractDelegationTokenIdentifier {
044  public static final Text HDFS_DELEGATION_KIND =
045      new Text("HDFS_DELEGATION_TOKEN");
046
047  @SuppressWarnings("unchecked")
048  private static Map<TokenIdentifier, UserGroupInformation> ugiCache =
049      Collections.synchronizedMap(new LRUMap(64));
050
051  @VisibleForTesting
052  public void clearCache() {
053    ugiCache.clear();
054  }
055
056  /**
057   * Create an empty delegation token identifier for reading into.
058   */
059  public DelegationTokenIdentifier() {
060  }
061
062  /**
063   * Create a new delegation token identifier
064   * @param owner the effective username of the token owner
065   * @param renewer the username of the renewer
066   * @param realUser the real username of the token owner
067   */
068  public DelegationTokenIdentifier(Text owner, Text renewer, Text realUser) {
069    super(owner, renewer, realUser);
070  }
071
072  @Override
073  public Text getKind() {
074    return HDFS_DELEGATION_KIND;
075  }
076
077  @Override
078  public UserGroupInformation getUser() {
079    UserGroupInformation ugi = ugiCache.get(this);
080    if (ugi == null) {
081      ugi = super.getUser();
082      ugiCache.put(this, ugi);
083    }
084    return ugi;
085  }
086
087  @Override
088  public String toString() {
089    return getKind() + " token " + getSequenceNumber()
090        + " for " + getUser().getShortUserName();
091  }
092
093  /** @return a string representation of the token */
094  public static String stringifyToken(final Token<?> token) throws IOException {
095    DelegationTokenIdentifier ident = new DelegationTokenIdentifier();
096    ByteArrayInputStream buf = new ByteArrayInputStream(token.getIdentifier());
097    DataInputStream in = new DataInputStream(buf);
098    ident.readFields(in);
099
100    if (token.getService().getLength() > 0) {
101      return ident + " on " + token.getService();
102    } else {
103      return ident.toString();
104    }
105  }
106
107  public static class WebHdfsDelegationTokenIdentifier
108      extends DelegationTokenIdentifier {
109    public WebHdfsDelegationTokenIdentifier() {
110      super();
111    }
112    @Override
113    public Text getKind() {
114      return WebHdfsConstants.WEBHDFS_TOKEN_KIND;
115    }
116  }
117
118  public static class SWebHdfsDelegationTokenIdentifier
119      extends WebHdfsDelegationTokenIdentifier {
120    public SWebHdfsDelegationTokenIdentifier() {
121      super();
122    }
123    @Override
124    public Text getKind() {
125      return WebHdfsConstants.SWEBHDFS_TOKEN_KIND;
126    }
127  }
128}