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.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.hadoop.classification.InterfaceAudience;
028    import org.apache.hadoop.classification.InterfaceAudience.Public;
029    import org.apache.hadoop.classification.InterfaceStability.Evolving;
030    import org.apache.hadoop.io.Text;
031    import org.apache.hadoop.security.UserGroupInformation;
032    import org.apache.hadoop.security.token.Token;
033    import org.apache.hadoop.security.token.TokenIdentifier;
034    import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
035    import org.apache.hadoop.yarn.api.records.ApplicationId;
036    import org.apache.hadoop.yarn.api.records.ContainerId;
037    import org.apache.hadoop.yarn.api.records.Priority;
038    import org.apache.hadoop.yarn.api.records.Resource;
039    
040    /**
041     * TokenIdentifier for a container. Encodes {@link ContainerId},
042     * {@link Resource} needed by the container and the target NMs host-address.
043     * 
044     */
045    @Public
046    @Evolving
047    public class ContainerTokenIdentifier extends TokenIdentifier {
048    
049      private static Log LOG = LogFactory.getLog(ContainerTokenIdentifier.class);
050    
051      public static final Text KIND = new Text("ContainerToken");
052    
053      private ContainerId containerId;
054      private String nmHostAddr;
055      private String appSubmitter;
056      private Resource resource;
057      private long expiryTimeStamp;
058      private int masterKeyId;
059      private long rmIdentifier;
060      private Priority priority;
061      private long creationTime;
062    
063      public ContainerTokenIdentifier(ContainerId containerID,
064          String hostName, String appSubmitter, Resource r, long expiryTimeStamp,
065          int masterKeyId, long rmIdentifier, Priority priority, long creationTime) {
066        this.containerId = containerID;
067        this.nmHostAddr = hostName;
068        this.appSubmitter = appSubmitter;
069        this.resource = r;
070        this.expiryTimeStamp = expiryTimeStamp;
071        this.masterKeyId = masterKeyId;
072        this.rmIdentifier = rmIdentifier;
073        this.priority = priority;
074        this.creationTime = creationTime;
075      }
076    
077      /**
078       * Default constructor needed by RPC layer/SecretManager.
079       */
080      public ContainerTokenIdentifier() {
081      }
082    
083      public ContainerId getContainerID() {
084        return this.containerId;
085      }
086    
087      public String getApplicationSubmitter() {
088        return this.appSubmitter;
089      }
090    
091      public String getNmHostAddress() {
092        return this.nmHostAddr;
093      }
094    
095      public Resource getResource() {
096        return this.resource;
097      }
098    
099      public long getExpiryTimeStamp() {
100        return this.expiryTimeStamp;
101      }
102    
103      public int getMasterKeyId() {
104        return this.masterKeyId;
105      }
106    
107      public Priority getPriority() {
108        return this.priority;
109      }
110    
111      public long getCreationTime() {
112        return this.creationTime;
113      }
114      /**
115       * Get the RMIdentifier of RM in which containers are allocated
116       * @return RMIdentifier
117       */
118      public long getRMIdentifer() {
119        return this.rmIdentifier;
120      }
121    
122      @Override
123      public void write(DataOutput out) throws IOException {
124        LOG.debug("Writing ContainerTokenIdentifier to RPC layer: " + this);
125        ApplicationAttemptId applicationAttemptId = this.containerId
126            .getApplicationAttemptId();
127        ApplicationId applicationId = applicationAttemptId.getApplicationId();
128        out.writeLong(applicationId.getClusterTimestamp());
129        out.writeInt(applicationId.getId());
130        out.writeInt(applicationAttemptId.getAttemptId());
131        out.writeInt(this.containerId.getId());
132        out.writeUTF(this.nmHostAddr);
133        out.writeUTF(this.appSubmitter);
134        out.writeInt(this.resource.getMemory());
135        out.writeInt(this.resource.getVirtualCores());
136        out.writeLong(this.expiryTimeStamp);
137        out.writeInt(this.masterKeyId);
138        out.writeLong(this.rmIdentifier);
139        out.writeInt(this.priority.getPriority());
140        out.writeLong(this.creationTime);
141      }
142    
143      @Override
144      public void readFields(DataInput in) throws IOException {
145        ApplicationId applicationId =
146            ApplicationId.newInstance(in.readLong(), in.readInt());
147        ApplicationAttemptId applicationAttemptId =
148            ApplicationAttemptId.newInstance(applicationId, in.readInt());
149        this.containerId =
150            ContainerId.newInstance(applicationAttemptId, in.readInt());
151        this.nmHostAddr = in.readUTF();
152        this.appSubmitter = in.readUTF();
153        int memory = in.readInt();
154        int vCores = in.readInt();
155        this.resource = Resource.newInstance(memory, vCores);
156        this.expiryTimeStamp = in.readLong();
157        this.masterKeyId = in.readInt();
158        this.rmIdentifier = in.readLong();
159        this.priority = Priority.newInstance(in.readInt());
160        this.creationTime = in.readLong();
161      }
162    
163      @Override
164      public Text getKind() {
165        return KIND;
166      }
167    
168      @Override
169      public UserGroupInformation getUser() {
170        return UserGroupInformation.createRemoteUser(this.containerId.toString());
171      }
172    
173      // TODO: Needed?
174      @InterfaceAudience.Private
175      public static class Renewer extends Token.TrivialRenewer {
176        @Override
177        protected Text getKind() {
178          return KIND;
179        }
180      }
181    }