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.DataInputStream;
023    import java.io.DataOutput;
024    import java.io.IOException;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
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.TokenIdentifier;
033    import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
034    import org.apache.hadoop.yarn.api.records.NodeId;
035    import org.apache.hadoop.yarn.api.records.impl.pb.ApplicationAttemptIdPBImpl;
036    import org.apache.hadoop.yarn.api.records.impl.pb.NodeIdPBImpl;
037    import org.apache.hadoop.yarn.proto.YarnSecurityTokenProtos.NMTokenIdentifierProto;
038    
039    import com.google.protobuf.TextFormat;
040    
041    @Public
042    @Evolving
043    public class NMTokenIdentifier extends TokenIdentifier {
044    
045      private static Log LOG = LogFactory.getLog(NMTokenIdentifier.class);
046    
047      public static final Text KIND = new Text("NMToken");
048      
049      private NMTokenIdentifierProto proto;
050    
051      public NMTokenIdentifier(ApplicationAttemptId appAttemptId, 
052          NodeId nodeId, String applicationSubmitter, int masterKeyId) {
053        NMTokenIdentifierProto.Builder builder = NMTokenIdentifierProto.newBuilder();
054        if (appAttemptId != null) {
055          builder.setAppAttemptId(
056              ((ApplicationAttemptIdPBImpl)appAttemptId).getProto());
057        }
058        if (nodeId != null) {
059          builder.setNodeId(((NodeIdPBImpl)nodeId).getProto());
060        }
061        builder.setAppSubmitter(applicationSubmitter);
062        builder.setKeyId(masterKeyId);
063        proto = builder.build();
064      }
065      
066      /**
067       * Default constructor needed by RPC/Secret manager
068       */
069      public NMTokenIdentifier() {
070      }
071    
072      public ApplicationAttemptId getApplicationAttemptId() {
073        if (!proto.hasAppAttemptId()) {
074          return null;
075        }
076        return new ApplicationAttemptIdPBImpl(proto.getAppAttemptId());
077      }
078      
079      public NodeId getNodeId() {
080        if (!proto.hasNodeId()) {
081          return null;
082        }
083        return new NodeIdPBImpl(proto.getNodeId());
084      }
085      
086      public String getApplicationSubmitter() {
087        return proto.getAppSubmitter();
088      }
089      
090      public int getKeyId() {
091        return proto.getKeyId();
092      }
093      
094      @Override
095      public void write(DataOutput out) throws IOException {
096        LOG.debug("Writing NMTokenIdentifier to RPC layer: " + this);
097        out.write(proto.toByteArray());
098      }
099    
100      @Override
101      public void readFields(DataInput in) throws IOException {
102        proto = NMTokenIdentifierProto.parseFrom((DataInputStream)in);
103      }
104    
105      @Override
106      public Text getKind() {
107        return KIND;
108      }
109    
110      @Override
111      public UserGroupInformation getUser() {
112        String appAttemptId = null;
113        if (proto.hasAppAttemptId()) {
114          appAttemptId = new ApplicationAttemptIdPBImpl(
115              proto.getAppAttemptId()).toString();
116        }
117        return UserGroupInformation.createRemoteUser(appAttemptId);
118      }
119      
120      public NMTokenIdentifierProto getProto() {
121        return proto;
122      }
123      
124      @Override
125      public int hashCode() {
126        return getProto().hashCode();
127      }
128    
129      @Override
130      public boolean equals(Object other) {
131        if (other == null)
132          return false;
133        if (other.getClass().isAssignableFrom(this.getClass())) {
134          return this.getProto().equals(this.getClass().cast(other).getProto());
135        }
136        return false;
137      }
138    
139      @Override
140      public String toString() {
141        return TextFormat.shortDebugString(getProto());
142      }
143    }