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.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.TokenIdentifier; 032 import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; 033 import org.apache.hadoop.yarn.api.records.ApplicationId; 034 import org.apache.hadoop.yarn.api.records.NodeId; 035 036 @Public 037 @Evolving 038 public class NMTokenIdentifier extends TokenIdentifier { 039 040 private static Log LOG = LogFactory.getLog(NMTokenIdentifier.class); 041 042 public static final Text KIND = new Text("NMToken"); 043 044 private ApplicationAttemptId appAttemptId; 045 private NodeId nodeId; 046 private String appSubmitter; 047 private int keyId; 048 049 public NMTokenIdentifier(ApplicationAttemptId appAttemptId, NodeId nodeId, 050 String applicationSubmitter, int masterKeyId) { 051 this.appAttemptId = appAttemptId; 052 this.nodeId = nodeId; 053 this.appSubmitter = applicationSubmitter; 054 this.keyId = masterKeyId; 055 } 056 057 /** 058 * Default constructor needed by RPC/Secret manager 059 */ 060 public NMTokenIdentifier() { 061 } 062 063 public ApplicationAttemptId getApplicationAttemptId() { 064 return appAttemptId; 065 } 066 067 public NodeId getNodeId() { 068 return nodeId; 069 } 070 071 public String getApplicationSubmitter() { 072 return appSubmitter; 073 } 074 075 public int getKeyId() { 076 return keyId; 077 } 078 079 @Override 080 public void write(DataOutput out) throws IOException { 081 LOG.debug("Writing NMTokenIdentifier to RPC layer: " + this); 082 ApplicationId applicationId = appAttemptId.getApplicationId(); 083 out.writeLong(applicationId.getClusterTimestamp()); 084 out.writeInt(applicationId.getId()); 085 out.writeInt(appAttemptId.getAttemptId()); 086 out.writeUTF(this.nodeId.toString()); 087 out.writeUTF(this.appSubmitter); 088 out.writeInt(this.keyId); 089 } 090 091 @Override 092 public void readFields(DataInput in) throws IOException { 093 appAttemptId = 094 ApplicationAttemptId.newInstance( 095 ApplicationId.newInstance(in.readLong(), in.readInt()), 096 in.readInt()); 097 String[] hostAddr = in.readUTF().split(":"); 098 nodeId = NodeId.newInstance(hostAddr[0], Integer.parseInt(hostAddr[1])); 099 appSubmitter = in.readUTF(); 100 keyId = in.readInt(); 101 } 102 103 @Override 104 public Text getKind() { 105 return KIND; 106 } 107 108 @Override 109 public UserGroupInformation getUser() { 110 return UserGroupInformation.createRemoteUser(appAttemptId.toString()); 111 } 112 }