public AMRMTokenIdentifierForTest(AMRMTokenIdentifier tokenIdentifier, string message
                                   )
 {
     builder = YarnSecurityTestAMRMTokenProtos.AMRMTokenIdentifierForTestProto.NewBuilder
                   ();
     builder.SetAppAttemptId(tokenIdentifier.GetProto().GetAppAttemptId());
     builder.SetKeyId(tokenIdentifier.GetKeyId());
     builder.SetMessage(message);
     proto = ((YarnSecurityTestAMRMTokenProtos.AMRMTokenIdentifierForTestProto)builder
              .Build());
     builder = null;
 }
Example #2
0
        public virtual void TestAMRMTokenUpdate()
        {
            Configuration        conf      = new Configuration();
            ApplicationAttemptId attemptId = ApplicationAttemptId.NewInstance(ApplicationId.NewInstance
                                                                                  (1, 1), 1);
            AMRMTokenIdentifier oldTokenId = new AMRMTokenIdentifier(attemptId, 1);
            AMRMTokenIdentifier newTokenId = new AMRMTokenIdentifier(attemptId, 2);

            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> oldToken = new Org.Apache.Hadoop.Security.Token.Token
                                                                                    <AMRMTokenIdentifier>(oldTokenId.GetBytes(), Sharpen.Runtime.GetBytesForString("oldpassword"
                                                                                                                                                                   ), oldTokenId.GetKind(), new Text());
            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> newToken = new Org.Apache.Hadoop.Security.Token.Token
                                                                                    <AMRMTokenIdentifier>(newTokenId.GetBytes(), Sharpen.Runtime.GetBytesForString("newpassword"
                                                                                                                                                                   ), newTokenId.GetKind(), new Text());
            TestLocalContainerAllocator.MockScheduler scheduler = new TestLocalContainerAllocator.MockScheduler
                                                                      ();
            scheduler.amToken = newToken;
            LocalContainerAllocator lca = new TestLocalContainerAllocator.StubbedLocalContainerAllocator
                                              (scheduler);

            lca.Init(conf);
            lca.Start();
            UserGroupInformation testUgi = UserGroupInformation.CreateUserForTesting("someuser"
                                                                                     , new string[0]);

            testUgi.AddToken(oldToken);
            testUgi.DoAs(new _PrivilegedExceptionAction_144(lca));
            lca.Close();
            // verify there is only one AMRM token in the UGI and it matches the
            // updated token from the RM
            int tokenCount = 0;

            Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> ugiToken = null;
            foreach (Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> token in testUgi
                     .GetTokens())
            {
                if (AMRMTokenIdentifier.KindName.Equals(token.GetKind()))
                {
                    ugiToken = token;
                    ++tokenCount;
                }
            }
            NUnit.Framework.Assert.AreEqual("too many AMRM tokens", 1, tokenCount);
            Assert.AssertArrayEquals("token identifier not updated", newToken.GetIdentifier()
                                     , ugiToken.GetIdentifier());
            Assert.AssertArrayEquals("token password not updated", newToken.GetPassword(), ugiToken
                                     .GetPassword());
            NUnit.Framework.Assert.AreEqual("AMRM token service not updated", new Text(ClientRMProxy
                                                                                       .GetAMRMTokenService(conf)), ugiToken.GetService());
        }
        // Obtain the needed AMRMTokenIdentifier from the remote-UGI. RPC layer
        // currently sets only the required id, but iterate through anyways just to be
        // sure.
        /// <exception cref="System.IO.IOException"/>
        private AMRMTokenIdentifier SelectAMRMTokenIdentifier(UserGroupInformation remoteUgi
                                                              )
        {
            AMRMTokenIdentifier           result   = null;
            ICollection <TokenIdentifier> tokenIds = remoteUgi.GetTokenIdentifiers();

            foreach (TokenIdentifier tokenId in tokenIds)
            {
                if (tokenId is AMRMTokenIdentifier)
                {
                    result = (AMRMTokenIdentifier)tokenId;
                    break;
                }
            }
            return(result);
        }
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        private AMRMTokenIdentifier AuthorizeRequest()
        {
            UserGroupInformation remoteUgi;

            try
            {
                remoteUgi = UserGroupInformation.GetCurrentUser();
            }
            catch (IOException e)
            {
                string msg = "Cannot obtain the user-name for authorizing ApplicationMaster. " +
                             "Got exception: " + StringUtils.StringifyException(e);
                Log.Warn(msg);
                throw RPCUtil.GetRemoteException(msg);
            }
            bool   tokenFound = false;
            string message    = string.Empty;
            AMRMTokenIdentifier appTokenIdentifier = null;

            try
            {
                appTokenIdentifier = SelectAMRMTokenIdentifier(remoteUgi);
                if (appTokenIdentifier == null)
                {
                    tokenFound = false;
                    message    = "No AMRMToken found for user " + remoteUgi.GetUserName();
                }
                else
                {
                    tokenFound = true;
                }
            }
            catch (IOException)
            {
                tokenFound = false;
                message    = "Got exception while looking for AMRMToken for user " + remoteUgi.GetUserName
                                 ();
            }
            if (!tokenFound)
            {
                Log.Warn(message);
                throw RPCUtil.GetRemoteException(message);
            }
            return(appTokenIdentifier);
        }
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        public virtual AllocateResponse Allocate(AllocateRequest request)
        {
            AMRMTokenIdentifier  amrmTokenIdentifier = AuthorizeRequest();
            ApplicationAttemptId appAttemptId        = amrmTokenIdentifier.GetApplicationAttemptId();
            ApplicationId        applicationId       = appAttemptId.GetApplicationId();

            this.amLivelinessMonitor.ReceivedPing(appAttemptId);
            /* check if its in cache */
            ApplicationMasterService.AllocateResponseLock Lock = responseMap[appAttemptId];
            if (Lock == null)
            {
                string message = "Application attempt " + appAttemptId + " doesn't exist in ApplicationMasterService cache.";
                Log.Error(message);
                throw new ApplicationAttemptNotFoundException(message);
            }
            lock (Lock)
            {
                AllocateResponse lastResponse = Lock.GetAllocateResponse();
                if (!HasApplicationMasterRegistered(appAttemptId))
                {
                    string message = "AM is not registered for known application attempt: " + appAttemptId
                                     + " or RM had restarted after AM registered . AM should re-register.";
                    Log.Info(message);
                    RMAuditLogger.LogFailure(this.rmContext.GetRMApps()[appAttemptId.GetApplicationId
                                                                            ()].GetUser(), RMAuditLogger.AuditConstants.AmAllocate, string.Empty, "ApplicationMasterService"
                                             , message, applicationId, appAttemptId);
                    throw new ApplicationMasterNotRegisteredException(message);
                }
                if ((request.GetResponseId() + 1) == lastResponse.GetResponseId())
                {
                    /* old heartbeat */
                    return(lastResponse);
                }
                else
                {
                    if (request.GetResponseId() + 1 < lastResponse.GetResponseId())
                    {
                        string message = "Invalid responseId in AllocateRequest from application attempt: "
                                         + appAttemptId + ", expect responseId to be " + (lastResponse.GetResponseId() +
                                                                                          1);
                        throw new InvalidApplicationMasterRequestException(message);
                    }
                }
                //filter illegal progress values
                float filteredProgress = request.GetProgress();
                if (float.IsNaN(filteredProgress) || filteredProgress == float.NegativeInfinity ||
                    filteredProgress < 0)
                {
                    request.SetProgress(0);
                }
                else
                {
                    if (filteredProgress > 1 || filteredProgress == float.PositiveInfinity)
                    {
                        request.SetProgress(1);
                    }
                }
                // Send the status update to the appAttempt.
                this.rmContext.GetDispatcher().GetEventHandler().Handle(new RMAppAttemptStatusupdateEvent
                                                                            (appAttemptId, request.GetProgress()));
                IList <ResourceRequest>  ask                = request.GetAskList();
                IList <ContainerId>      release            = request.GetReleaseList();
                ResourceBlacklistRequest blacklistRequest   = request.GetResourceBlacklistRequest();
                IList <string>           blacklistAdditions = (blacklistRequest != null) ? blacklistRequest.
                                                              GetBlacklistAdditions() : Sharpen.Collections.EmptyList;
                IList <string> blacklistRemovals = (blacklistRequest != null) ? blacklistRequest.GetBlacklistRemovals
                                                       () : Sharpen.Collections.EmptyList;
                RMApp app = this.rmContext.GetRMApps()[applicationId];
                // set label expression for Resource Requests if resourceName=ANY
                ApplicationSubmissionContext asc = app.GetApplicationSubmissionContext();
                foreach (ResourceRequest req in ask)
                {
                    if (null == req.GetNodeLabelExpression() && ResourceRequest.Any.Equals(req.GetResourceName
                                                                                               ()))
                    {
                        req.SetNodeLabelExpression(asc.GetNodeLabelExpression());
                    }
                }
                // sanity check
                try
                {
                    RMServerUtils.NormalizeAndValidateRequests(ask, rScheduler.GetMaximumResourceCapability
                                                                   (), app.GetQueue(), rScheduler, rmContext);
                }
                catch (InvalidResourceRequestException e)
                {
                    Log.Warn("Invalid resource ask by application " + appAttemptId, e);
                    throw;
                }
                try
                {
                    RMServerUtils.ValidateBlacklistRequest(blacklistRequest);
                }
                catch (InvalidResourceBlacklistRequestException e)
                {
                    Log.Warn("Invalid blacklist request by application " + appAttemptId, e);
                    throw;
                }
                // In the case of work-preserving AM restart, it's possible for the
                // AM to release containers from the earlier attempt.
                if (!app.GetApplicationSubmissionContext().GetKeepContainersAcrossApplicationAttempts
                        ())
                {
                    try
                    {
                        RMServerUtils.ValidateContainerReleaseRequest(release, appAttemptId);
                    }
                    catch (InvalidContainerReleaseException e)
                    {
                        Log.Warn("Invalid container release by application " + appAttemptId, e);
                        throw;
                    }
                }
                // Send new requests to appAttempt.
                Allocation allocation = this.rScheduler.Allocate(appAttemptId, ask, release, blacklistAdditions
                                                                 , blacklistRemovals);
                if (!blacklistAdditions.IsEmpty() || !blacklistRemovals.IsEmpty())
                {
                    Log.Info("blacklist are updated in Scheduler." + "blacklistAdditions: " + blacklistAdditions
                             + ", " + "blacklistRemovals: " + blacklistRemovals);
                }
                RMAppAttempt     appAttempt       = app.GetRMAppAttempt(appAttemptId);
                AllocateResponse allocateResponse = recordFactory.NewRecordInstance <AllocateResponse
                                                                                     >();
                if (!allocation.GetContainers().IsEmpty())
                {
                    allocateResponse.SetNMTokens(allocation.GetNMTokens());
                }
                // update the response with the deltas of node status changes
                IList <RMNode> updatedNodes = new AList <RMNode>();
                if (app.PullRMNodeUpdates(updatedNodes) > 0)
                {
                    IList <NodeReport> updatedNodeReports = new AList <NodeReport>();
                    foreach (RMNode rmNode in updatedNodes)
                    {
                        SchedulerNodeReport schedulerNodeReport = rScheduler.GetNodeReport(rmNode.GetNodeID
                                                                                               ());
                        Resource used          = BuilderUtils.NewResource(0, 0);
                        int      numContainers = 0;
                        if (schedulerNodeReport != null)
                        {
                            used          = schedulerNodeReport.GetUsedResource();
                            numContainers = schedulerNodeReport.GetNumContainers();
                        }
                        NodeId     nodeId = rmNode.GetNodeID();
                        NodeReport report = BuilderUtils.NewNodeReport(nodeId, rmNode.GetState(), rmNode.
                                                                       GetHttpAddress(), rmNode.GetRackName(), used, rmNode.GetTotalCapability(), numContainers
                                                                       , rmNode.GetHealthReport(), rmNode.GetLastHealthReportTime(), rmNode.GetNodeLabels
                                                                           ());
                        updatedNodeReports.AddItem(report);
                    }
                    allocateResponse.SetUpdatedNodes(updatedNodeReports);
                }
                allocateResponse.SetAllocatedContainers(allocation.GetContainers());
                allocateResponse.SetCompletedContainersStatuses(appAttempt.PullJustFinishedContainers
                                                                    ());
                allocateResponse.SetResponseId(lastResponse.GetResponseId() + 1);
                allocateResponse.SetAvailableResources(allocation.GetResourceLimit());
                allocateResponse.SetNumClusterNodes(this.rScheduler.GetNumClusterNodes());
                // add preemption to the allocateResponse message (if any)
                allocateResponse.SetPreemptionMessage(GeneratePreemptionMessage(allocation));
                // update AMRMToken if the token is rolled-up
                MasterKeyData nextMasterKey = this.rmContext.GetAMRMTokenSecretManager().GetNextMasterKeyData
                                                  ();
                if (nextMasterKey != null && nextMasterKey.GetMasterKey().GetKeyId() != amrmTokenIdentifier
                    .GetKeyId())
                {
                    RMAppAttemptImpl appAttemptImpl = (RMAppAttemptImpl)appAttempt;
                    Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> amrmToken = appAttempt
                                                                                             .GetAMRMToken();
                    if (nextMasterKey.GetMasterKey().GetKeyId() != appAttemptImpl.GetAMRMTokenKeyId())
                    {
                        Log.Info("The AMRMToken has been rolled-over. Send new AMRMToken back" + " to application: "
                                 + applicationId);
                        amrmToken = rmContext.GetAMRMTokenSecretManager().CreateAndGetAMRMToken(appAttemptId
                                                                                                );
                        appAttemptImpl.SetAMRMToken(amrmToken);
                    }
                    allocateResponse.SetAMRMToken(Org.Apache.Hadoop.Yarn.Api.Records.Token.NewInstance
                                                      (amrmToken.GetIdentifier(), amrmToken.GetKind().ToString(), amrmToken.GetPassword
                                                          (), amrmToken.GetService().ToString()));
                }

                /*
                 * As we are updating the response inside the lock object so we don't
                 * need to worry about unregister call occurring in between (which
                 * removes the lock object).
                 */
                Lock.SetAllocateResponse(allocateResponse);
                return(allocateResponse);
            }
        }
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        public virtual RegisterApplicationMasterResponse RegisterApplicationMaster(RegisterApplicationMasterRequest
                                                                                   request)
        {
            AMRMTokenIdentifier  amrmTokenIdentifier  = AuthorizeRequest();
            ApplicationAttemptId applicationAttemptId = amrmTokenIdentifier.GetApplicationAttemptId
                                                            ();
            ApplicationId appID = applicationAttemptId.GetApplicationId();

            ApplicationMasterService.AllocateResponseLock Lock = responseMap[applicationAttemptId
                                                                 ];
            if (Lock == null)
            {
                RMAuditLogger.LogFailure(this.rmContext.GetRMApps()[appID].GetUser(), RMAuditLogger.AuditConstants
                                         .RegisterAm, "Application doesn't exist in cache " + applicationAttemptId, "ApplicationMasterService"
                                         , "Error in registering application master", appID, applicationAttemptId);
                ThrowApplicationDoesNotExistInCacheException(applicationAttemptId);
            }
            // Allow only one thread in AM to do registerApp at a time.
            lock (Lock)
            {
                AllocateResponse lastResponse = Lock.GetAllocateResponse();
                if (HasApplicationMasterRegistered(applicationAttemptId))
                {
                    string message = "Application Master is already registered : " + appID;
                    Log.Warn(message);
                    RMAuditLogger.LogFailure(this.rmContext.GetRMApps()[appID].GetUser(), RMAuditLogger.AuditConstants
                                             .RegisterAm, string.Empty, "ApplicationMasterService", message, appID, applicationAttemptId
                                             );
                    throw new InvalidApplicationMasterRequestException(message);
                }
                this.amLivelinessMonitor.ReceivedPing(applicationAttemptId);
                RMApp app = this.rmContext.GetRMApps()[appID];
                // Setting the response id to 0 to identify if the
                // application master is register for the respective attemptid
                lastResponse.SetResponseId(0);
                Lock.SetAllocateResponse(lastResponse);
                Log.Info("AM registration " + applicationAttemptId);
                this.rmContext.GetDispatcher().GetEventHandler().Handle(new RMAppAttemptRegistrationEvent
                                                                            (applicationAttemptId, request.GetHost(), request.GetRpcPort(), request.GetTrackingUrl
                                                                                ()));
                RMAuditLogger.LogSuccess(app.GetUser(), RMAuditLogger.AuditConstants.RegisterAm,
                                         "ApplicationMasterService", appID, applicationAttemptId);
                // Pick up min/max resource from scheduler...
                RegisterApplicationMasterResponse response = recordFactory.NewRecordInstance <RegisterApplicationMasterResponse
                                                                                              >();
                response.SetMaximumResourceCapability(rScheduler.GetMaximumResourceCapability(app
                                                                                              .GetQueue()));
                response.SetApplicationACLs(app.GetRMAppAttempt(applicationAttemptId).GetSubmissionContext
                                                ().GetAMContainerSpec().GetApplicationACLs());
                response.SetQueue(app.GetQueue());
                if (UserGroupInformation.IsSecurityEnabled())
                {
                    Log.Info("Setting client token master key");
                    response.SetClientToAMTokenMasterKey(ByteBuffer.Wrap(rmContext.GetClientToAMTokenSecretManager
                                                                             ().GetMasterKey(applicationAttemptId).GetEncoded()));
                }
                // For work-preserving AM restart, retrieve previous attempts' containers
                // and corresponding NM tokens.
                if (app.GetApplicationSubmissionContext().GetKeepContainersAcrossApplicationAttempts
                        ())
                {
                    IList <Container> transferredContainers = ((AbstractYarnScheduler)rScheduler).GetTransferredContainers
                                                                  (applicationAttemptId);
                    if (!transferredContainers.IsEmpty())
                    {
                        response.SetContainersFromPreviousAttempts(transferredContainers);
                        IList <NMToken> nmTokens = new AList <NMToken>();
                        foreach (Container container in transferredContainers)
                        {
                            try
                            {
                                NMToken token = rmContext.GetNMTokenSecretManager().CreateAndGetNMToken(app.GetUser
                                                                                                            (), applicationAttemptId, container);
                                if (null != token)
                                {
                                    nmTokens.AddItem(token);
                                }
                            }
                            catch (ArgumentException e)
                            {
                                // if it's a DNS issue, throw UnknowHostException directly and
                                // that
                                // will be automatically retried by RMProxy in RPC layer.
                                if (e.InnerException is UnknownHostException)
                                {
                                    throw (UnknownHostException)e.InnerException;
                                }
                            }
                        }
                        response.SetNMTokensFromPreviousAttempts(nmTokens);
                        Log.Info("Application " + appID + " retrieved " + transferredContainers.Count + " containers from previous"
                                 + " attempts and " + nmTokens.Count + " NM tokens.");
                    }
                }
                response.SetSchedulerResourceTypes(rScheduler.GetSchedulingResourceTypes());
                return(response);
            }
        }