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    
019    package org.apache.hadoop.yarn.security;
020    
021    import java.io.DataInput;
022    import java.io.DataOutput;
023    import java.io.IOException;
024    
025    import org.apache.hadoop.classification.InterfaceAudience;
026    import org.apache.hadoop.classification.InterfaceAudience.Private;
027    import org.apache.hadoop.classification.InterfaceAudience.Public;
028    import org.apache.hadoop.classification.InterfaceStability.Evolving;
029    import org.apache.hadoop.io.Text;
030    import org.apache.hadoop.security.UserGroupInformation;
031    import org.apache.hadoop.security.token.Token;
032    import org.apache.hadoop.security.token.TokenIdentifier;
033    import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
034    import org.apache.hadoop.yarn.api.records.ApplicationId;
035    
036    /**
037     * AMRMTokenIdentifier is the TokenIdentifier to be used by
038     * ApplicationMasters to authenticate to the ResourceManager.
039     */
040    @Public
041    @Evolving
042    public class AMRMTokenIdentifier extends TokenIdentifier {
043    
044      public static final Text KIND_NAME = new Text("YARN_AM_RM_TOKEN");
045    
046      private ApplicationAttemptId applicationAttemptId;
047    
048      public AMRMTokenIdentifier() {
049      }
050    
051      public AMRMTokenIdentifier(ApplicationAttemptId appAttemptId) {
052        this();
053        this.applicationAttemptId = appAttemptId;
054      }
055    
056      @Private
057      public ApplicationAttemptId getApplicationAttemptId() {
058        return this.applicationAttemptId;
059      }
060    
061      @Override
062      public void write(DataOutput out) throws IOException {
063        ApplicationId appId = this.applicationAttemptId.getApplicationId();
064        out.writeLong(appId.getClusterTimestamp());
065        out.writeInt(appId.getId());
066        out.writeInt(this.applicationAttemptId.getAttemptId());
067      }
068    
069      @Override
070      public void readFields(DataInput in) throws IOException {
071        long clusterTimeStamp = in.readLong();
072        int appId = in.readInt();
073        int attemptId = in.readInt();
074        ApplicationId applicationId =
075            ApplicationId.newInstance(clusterTimeStamp, appId);
076        this.applicationAttemptId =
077            ApplicationAttemptId.newInstance(applicationId, attemptId);
078      }
079    
080      @Override
081      public Text getKind() {
082        return KIND_NAME;
083      }
084    
085      @Override
086      public UserGroupInformation getUser() {
087        if (this.applicationAttemptId == null
088            || "".equals(this.applicationAttemptId.toString())) {
089          return null;
090        }
091        return UserGroupInformation.createRemoteUser(this.applicationAttemptId
092            .toString());
093      }
094    
095      // TODO: Needed?
096      @InterfaceAudience.Private
097      public static class Renewer extends Token.TrivialRenewer {
098        @Override
099        protected Text getKind() {
100          return KIND_NAME;
101        }
102      }
103    }