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.yarn.security.client;
020
021import java.io.IOException;
022
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceAudience.Public;
025import org.apache.hadoop.classification.InterfaceStability.Unstable;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.io.Text;
028import org.apache.hadoop.security.token.Token;
029import org.apache.hadoop.security.token.TokenRenewer;
030import org.apache.hadoop.yarn.client.api.TimelineClient;
031import org.apache.hadoop.yarn.exceptions.YarnException;
032
033@Public
034@Unstable
035public class TimelineDelegationTokenIdentifier extends YARNDelegationTokenIdentifier {
036
037  public static final Text KIND_NAME = new Text("TIMELINE_DELEGATION_TOKEN");
038
039  public TimelineDelegationTokenIdentifier() {
040
041  }
042
043  /**
044   * Create a new timeline delegation token identifier
045   *
046   * @param owner the effective username of the token owner
047   * @param renewer the username of the renewer
048   * @param realUser the real username of the token owner
049   */
050  public TimelineDelegationTokenIdentifier(Text owner, Text renewer,
051      Text realUser) {
052    super(owner, renewer, realUser);
053  }
054
055  @Override
056  public Text getKind() {
057    return KIND_NAME;
058  }
059
060  @InterfaceAudience.Private
061  public static class Renewer extends TokenRenewer {
062
063    @Override
064    public boolean handleKind(Text kind) {
065      return KIND_NAME.equals(kind);
066    }
067
068    @Override
069    public boolean isManaged(Token<?> token) throws IOException {
070      return true;
071    }
072
073    @SuppressWarnings("unchecked")
074    @Override
075    public long renew(Token<?> token, Configuration conf) throws IOException,
076        InterruptedException {
077      TimelineClient client = TimelineClient.createTimelineClient();
078      try {
079        client.init(conf);
080        client.start();
081        return client.renewDelegationToken(
082            (Token<TimelineDelegationTokenIdentifier>) token);
083      } catch (YarnException e) {
084        throw new IOException(e);
085      } finally {
086        client.stop();
087      }
088    }
089
090    @SuppressWarnings("unchecked")
091    @Override
092    public void cancel(Token<?> token, Configuration conf) throws IOException,
093        InterruptedException {
094      TimelineClient client = TimelineClient.createTimelineClient();
095      try {
096        client.init(conf);
097        client.start();
098        client.cancelDelegationToken(
099            (Token<TimelineDelegationTokenIdentifier>) token);
100      } catch (YarnException e) {
101        throw new IOException(e);
102      } finally {
103        client.stop();
104      }
105    }
106  }
107
108}