Example #1
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        public static void StartContainer(NodeManager nm, ContainerId cId, FileContext localFS
                                          , FilePath scriptFileDir, FilePath processStartFile)
        {
            FilePath scriptFile = CreateUnhaltingScriptFile(cId, scriptFileDir, processStartFile
                                                            );
            ContainerLaunchContext containerLaunchContext = recordFactory.NewRecordInstance <ContainerLaunchContext
                                                                                             >();
            NodeId nodeId = BuilderUtils.NewNodeId(Sharpen.Extensions.GetAddressByName("localhost"
                                                                                       ).ToString(), 12345);
            URL localResourceUri = ConverterUtils.GetYarnUrlFromPath(localFS.MakeQualified(new
                                                                                           Path(scriptFile.GetAbsolutePath())));
            LocalResource localResource = recordFactory.NewRecordInstance <LocalResource>();

            localResource.SetResource(localResourceUri);
            localResource.SetSize(-1);
            localResource.SetVisibility(LocalResourceVisibility.Application);
            localResource.SetType(LocalResourceType.File);
            localResource.SetTimestamp(scriptFile.LastModified());
            string destinationFile = "dest_file";
            IDictionary <string, LocalResource> localResources = new Dictionary <string, LocalResource
                                                                                 >();

            localResources[destinationFile] = localResource;
            containerLaunchContext.SetLocalResources(localResources);
            IList <string> commands = Arrays.AsList(Shell.GetRunScriptCommand(scriptFile));

            containerLaunchContext.SetCommands(commands);
            IPEndPoint containerManagerBindAddress = NetUtils.CreateSocketAddrForHost("127.0.0.1"
                                                                                      , 12345);
            UserGroupInformation currentUser = UserGroupInformation.CreateRemoteUser(cId.ToString
                                                                                         ());

            Org.Apache.Hadoop.Security.Token.Token <NMTokenIdentifier> nmToken = ConverterUtils
                                                                                 .ConvertFromYarn(nm.GetNMContext().GetNMTokenSecretManager().CreateNMToken(cId.GetApplicationAttemptId
                                                                                                                                                                (), nodeId, user), containerManagerBindAddress);
            currentUser.AddToken(nmToken);
            ContainerManagementProtocol containerManager = currentUser.DoAs(new _PrivilegedAction_229
                                                                                ());
            StartContainerRequest scRequest = StartContainerRequest.NewInstance(containerLaunchContext
                                                                                , TestContainerManager.CreateContainerToken(cId, 0, nodeId, user, nm.GetNMContext
                                                                                                                                ().GetContainerTokenSecretManager()));
            IList <StartContainerRequest> list = new AList <StartContainerRequest>();

            list.AddItem(scRequest);
            StartContainersRequest allRequests = StartContainersRequest.NewInstance(list);

            containerManager.StartContainers(allRequests);
            IList <ContainerId> containerIds = new AList <ContainerId>();

            containerIds.AddItem(cId);
            GetContainerStatusesRequest request = GetContainerStatusesRequest.NewInstance(containerIds
                                                                                          );
            ContainerStatus containerStatus = containerManager.GetContainerStatuses(request).
                                              GetContainerStatuses()[0];

            NUnit.Framework.Assert.AreEqual(ContainerState.Running, containerStatus.GetState(
                                                ));
        }
Example #2
0
        // To avoid using cached client
        /// <exception cref="System.Exception"/>
        public virtual void TestAMRMMasterKeysUpdate()
        {
            AtomicReference <AMRMTokenSecretManager> spySecretMgrRef = new AtomicReference <AMRMTokenSecretManager
                                                                                            >();
            MockRM rm = new _MockRM_349(this, spySecretMgrRef, conf);

            // Skip the login.
            rm.Start();
            MockNM nm  = rm.RegisterNode("127.0.0.1:1234", 8000);
            RMApp  app = rm.SubmitApp(200);
            MockAM am  = MockRM.LaunchAndRegisterAM(app, rm, nm);
            AMRMTokenSecretManager spySecretMgr = spySecretMgrRef.Get();
            // Do allocate. Should not update AMRMToken
            AllocateResponse response = am.Allocate(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord
                                                    <AllocateRequest>());

            NUnit.Framework.Assert.IsNull(response.GetAMRMToken());
            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> oldToken = rm.GetRMContext
                                                                                        ().GetRMApps()[app.GetApplicationId()].GetRMAppAttempt(am.GetApplicationAttemptId
                                                                                                                                                   ()).GetAMRMToken();
            // roll over the master key
            // Do allocate again. the AM should get the latest AMRMToken
            rm.GetRMContext().GetAMRMTokenSecretManager().RollMasterKey();
            response = am.Allocate(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <AllocateRequest
                                                                                  >());
            NUnit.Framework.Assert.IsNotNull(response.GetAMRMToken());
            Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> amrmToken = ConverterUtils
                                                                                     .ConvertFromYarn(response.GetAMRMToken(), new Text(response.GetAMRMToken().GetService
                                                                                                                                            ()));
            NUnit.Framework.Assert.AreEqual(amrmToken.DecodeIdentifier().GetKeyId(), rm.GetRMContext
                                                ().GetAMRMTokenSecretManager().GetMasterKey().GetMasterKey().GetKeyId());
            // Do allocate again with the same old token and verify the RM sends
            // back the last generated token instead of generating it again.
            Org.Mockito.Mockito.Reset(spySecretMgr);
            UserGroupInformation ugi = UserGroupInformation.CreateUserForTesting(am.GetApplicationAttemptId
                                                                                     ().ToString(), new string[0]);

            ugi.AddTokenIdentifier(oldToken.DecodeIdentifier());
            response = am.DoAllocateAs(ugi, Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <AllocateRequest
                                                                                           >());
            NUnit.Framework.Assert.IsNotNull(response.GetAMRMToken());
            Org.Mockito.Mockito.Verify(spySecretMgr, Org.Mockito.Mockito.Never()).CreateAndGetAMRMToken
                (Matchers.IsA <ApplicationAttemptId>());
            // Do allocate again with the updated token and verify we do not
            // receive a new token to use.
            response = am.Allocate(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <AllocateRequest
                                                                                  >());
            NUnit.Framework.Assert.IsNull(response.GetAMRMToken());
            // Activate the next master key. Since there is new master key generated
            // in AMRMTokenSecretManager. The AMRMToken will not get updated for AM
            rm.GetRMContext().GetAMRMTokenSecretManager().ActivateNextMasterKey();
            response = am.Allocate(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <AllocateRequest
                                                                                  >());
            NUnit.Framework.Assert.IsNull(response.GetAMRMToken());
            rm.Stop();
        }
Example #3
0
 /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
 /// <exception cref="System.IO.IOException"/>
 public override Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> GetAMRMToken
     (ApplicationId appId)
 {
     Org.Apache.Hadoop.Yarn.Api.Records.Token token = GetApplicationReport(appId).GetAMRMToken
                                                          ();
     Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> amrmToken = null;
     if (token != null)
     {
         amrmToken = ConverterUtils.ConvertFromYarn(token, (Text)null);
     }
     return(amrmToken);
 }
Example #4
0
        internal virtual Org.Apache.Hadoop.Security.Token.Token <object> GetDelegationTokenFromHS
            (MRClientProtocol hsProxy)
        {
            GetDelegationTokenRequest request = recordFactory.NewRecordInstance <GetDelegationTokenRequest
                                                                                 >();

            request.SetRenewer(Master.GetMasterPrincipal(conf));
            Org.Apache.Hadoop.Yarn.Api.Records.Token mrDelegationToken;
            mrDelegationToken = hsProxy.GetDelegationToken(request).GetDelegationToken();
            return(ConverterUtils.ConvertFromYarn(mrDelegationToken, hsProxy.GetConnectAddress
                                                      ()));
        }
Example #5
0
        private MRClientProtocol GetMRClientProtocol(Token token, IPEndPoint hsAddress, string
                                                     user, Configuration conf)
        {
            UserGroupInformation ugi = UserGroupInformation.CreateRemoteUser(user);

            ugi.AddToken(ConverterUtils.ConvertFromYarn(token, hsAddress));
            YarnRPC          rpc      = YarnRPC.Create(conf);
            MRClientProtocol hsWithDT = ugi.DoAs(new _PrivilegedAction_288(rpc, hsAddress, conf
                                                                           ));

            return(hsWithDT);
        }
Example #6
0
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.Exception"/>
 public virtual Org.Apache.Hadoop.Security.Token.Token GetDelegationToken(Text renewer
                                                                          )
 {
     try
     {
         return(ConverterUtils.ConvertFromYarn(client.GetRMDelegationToken(renewer), GetRMDelegationTokenService
                                                   ()));
     }
     catch (YarnException e)
     {
         throw new IOException(e);
     }
 }
Example #7
0
        private ApplicationClientProtocol GetClientRMProtocolWithDT(Org.Apache.Hadoop.Yarn.Api.Records.Token
                                                                    token, IPEndPoint rmAddress, string user, Configuration conf)
        {
            // Maybe consider converting to Hadoop token, serialize de-serialize etc
            // before trying to renew the token.
            UserGroupInformation ugi = UserGroupInformation.CreateRemoteUser(user);

            ugi.AddToken(ConverterUtils.ConvertFromYarn(token, rmAddress));
            YarnRPC rpc = YarnRPC.Create(conf);
            ApplicationClientProtocol clientRMWithDT = ugi.DoAs(new _PrivilegedAction_464(rpc
                                                                                          , rmAddress, conf));

            return(clientRMWithDT);
        }
Example #8
0
        protected internal virtual ContainerManagementProtocol GetContainerManagementProtocolProxy
            (YarnRPC rpc, Org.Apache.Hadoop.Yarn.Api.Records.Token nmToken, NodeId nodeId, string
            user)
        {
            ContainerManagementProtocol proxy;
            UserGroupInformation        ugi = UserGroupInformation.CreateRemoteUser(user);
            IPEndPoint addr = NetUtils.CreateSocketAddr(nodeId.GetHost(), nodeId.GetPort());

            if (nmToken != null)
            {
                ugi.AddToken(ConverterUtils.ConvertFromYarn(nmToken, addr));
            }
            proxy = NMProxy.CreateNMProxy <ContainerManagementProtocol>(conf, ugi, rpc, addr);
            return(proxy);
        }
Example #9
0
        // Protected. For tests.
        protected internal virtual ContainerManagementProtocol GetContainerMgrProxy(ContainerId
                                                                                    containerId)
        {
            NodeId     node = masterContainer.GetNodeId();
            IPEndPoint containerManagerConnectAddress = NetUtils.CreateSocketAddrForHost(node
                                                                                         .GetHost(), node.GetPort());
            YarnRPC rpc = GetYarnRPC();
            UserGroupInformation currentUser = UserGroupInformation.CreateRemoteUser(containerId
                                                                                     .GetApplicationAttemptId().ToString());
            string user = rmContext.GetRMApps()[containerId.GetApplicationAttemptId().GetApplicationId
                                                    ()].GetUser();
            Token token = rmContext.GetNMTokenSecretManager().CreateNMToken(containerId.GetApplicationAttemptId
                                                                                (), node, user);

            currentUser.AddToken(ConverterUtils.ConvertFromYarn(token, containerManagerConnectAddress
                                                                ));
            return(NMProxy.CreateNMProxy <ContainerManagementProtocol>(conf, currentUser, rpc,
                                                                       containerManagerConnectAddress));
        }
Example #10
0
        /// <exception cref="System.Exception"/>
        public virtual void TestNMProxyRetry()
        {
            containerManager.Start();
            containerManager.SetBlockNewContainerRequests(false);
            StartContainersRequest allRequests = Records.NewRecord <StartContainersRequest>();
            ApplicationId          appId       = ApplicationId.NewInstance(1, 1);
            ApplicationAttemptId   attemptId   = ApplicationAttemptId.NewInstance(appId, 1);
            Token nmToken = context.GetNMTokenSecretManager().CreateNMToken(attemptId, context
                                                                            .GetNodeId(), user);
            IPEndPoint address = conf.GetSocketAddr(YarnConfiguration.NmBindHost, YarnConfiguration
                                                    .NmAddress, YarnConfiguration.DefaultNmAddress, YarnConfiguration.DefaultNmPort);

            Org.Apache.Hadoop.Security.Token.Token <NMTokenIdentifier> token = ConverterUtils.
                                                                               ConvertFromYarn(nmToken, SecurityUtil.BuildTokenService(address));
            UserGroupInformation ugi = UserGroupInformation.CreateRemoteUser(user);

            ugi.AddToken(token);
            ContainerManagementProtocol proxy = NMProxy.CreateNMProxy <ContainerManagementProtocol
                                                                       >(conf, ugi, YarnRPC.Create(conf), address);

            retryCount = 0;
            shouldThrowNMNotYetReadyException = false;
            proxy.StartContainers(allRequests);
            NUnit.Framework.Assert.AreEqual(5, retryCount);
            retryCount = 0;
            shouldThrowNMNotYetReadyException = false;
            proxy.StopContainers(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <StopContainersRequest
                                                                                >());
            NUnit.Framework.Assert.AreEqual(5, retryCount);
            retryCount = 0;
            shouldThrowNMNotYetReadyException = false;
            proxy.GetContainerStatuses(Org.Apache.Hadoop.Yarn.Util.Records.NewRecord <GetContainerStatusesRequest
                                                                                      >());
            NUnit.Framework.Assert.AreEqual(5, retryCount);
            retryCount = 0;
            shouldThrowNMNotYetReadyException = true;
            proxy.StartContainers(allRequests);
            NUnit.Framework.Assert.AreEqual(5, retryCount);
        }
Example #11
0
            protected internal virtual ContainerManagementProtocol NewProxy(YarnRPC rpc, string
                                                                            containerManagerBindAddr, ContainerId containerId, Token token)
            {
                if (token == null)
                {
                    throw new SecretManager.InvalidToken("No NMToken sent for " + containerManagerBindAddr
                                                         );
                }
                IPEndPoint cmAddr = NetUtils.CreateSocketAddr(containerManagerBindAddr);

                ContainerManagementProtocolProxy.Log.Info("Opening proxy : " + containerManagerBindAddr
                                                          );
                // the user in createRemoteUser in this context has to be ContainerID
                UserGroupInformation user = UserGroupInformation.CreateRemoteUser(containerId.GetApplicationAttemptId
                                                                                      ().ToString());

                Org.Apache.Hadoop.Security.Token.Token <NMTokenIdentifier> nmToken = ConverterUtils
                                                                                     .ConvertFromYarn(token, cmAddr);
                user.AddToken(nmToken);
                return(NMProxy.CreateNMProxy <ContainerManagementProtocol>(this._enclosing.conf, user
                                                                           , rpc, cmAddr));
            }
 /// <exception cref="System.IO.IOException"/>
 private NMTokenIdentifier GetNMTokenId(Org.Apache.Hadoop.Yarn.Api.Records.Token token
                                        )
 {
     Org.Apache.Hadoop.Security.Token.Token <NMTokenIdentifier> convertedToken = ConverterUtils
                                                                                 .ConvertFromYarn(token, (Text)null);
     return(convertedToken.DecodeIdentifier());
 }
        /// <exception cref="System.IO.IOException"/>
        private MRClientProtocol GetProxy()
        {
            if (realProxy != null)
            {
                return(realProxy);
            }
            // Possibly allow nulls through the PB tunnel, otherwise deal with an exception
            // and redirect to the history server.
            ApplicationReport application = null;

            try
            {
                application = rm.GetApplicationReport(appId);
            }
            catch (ApplicationNotFoundException)
            {
                application = null;
            }
            catch (YarnException e2)
            {
                throw new IOException(e2);
            }
            if (application != null)
            {
                trackingUrl = application.GetTrackingUrl();
            }
            IPEndPoint serviceAddr = null;

            while (application == null || YarnApplicationState.Running == application.GetYarnApplicationState
                       ())
            {
                if (application == null)
                {
                    Log.Info("Could not get Job info from RM for job " + jobId + ". Redirecting to job history server."
                             );
                    return(CheckAndGetHSProxy(null, JobState.New));
                }
                try
                {
                    if (application.GetHost() == null || string.Empty.Equals(application.GetHost()))
                    {
                        Log.Debug("AM not assigned to Job. Waiting to get the AM ...");
                        Sharpen.Thread.Sleep(2000);
                        Log.Debug("Application state is " + application.GetYarnApplicationState());
                        application = rm.GetApplicationReport(appId);
                        continue;
                    }
                    else
                    {
                        if (Unavailable.Equals(application.GetHost()))
                        {
                            if (!amAclDisabledStatusLogged)
                            {
                                Log.Info("Job " + jobId + " is running, but the host is unknown." + " Verify user has VIEW_JOB access."
                                         );
                                amAclDisabledStatusLogged = true;
                            }
                            return(GetNotRunningJob(application, JobState.Running));
                        }
                    }
                    if (!conf.GetBoolean(MRJobConfig.JobAmAccessDisabled, false))
                    {
                        UserGroupInformation newUgi = UserGroupInformation.CreateRemoteUser(UserGroupInformation
                                                                                            .GetCurrentUser().GetUserName());
                        serviceAddr = NetUtils.CreateSocketAddrForHost(application.GetHost(), application
                                                                       .GetRpcPort());
                        if (UserGroupInformation.IsSecurityEnabled())
                        {
                            Token clientToAMToken = application.GetClientToAMToken();
                            Org.Apache.Hadoop.Security.Token.Token <ClientToAMTokenIdentifier> token = ConverterUtils
                                                                                                       .ConvertFromYarn(clientToAMToken, serviceAddr);
                            newUgi.AddToken(token);
                        }
                        Log.Debug("Connecting to " + serviceAddr);
                        IPEndPoint finalServiceAddr = serviceAddr;
                        realProxy = newUgi.DoAs(new _PrivilegedExceptionAction_202(this, finalServiceAddr
                                                                                   ));
                    }
                    else
                    {
                        if (!amAclDisabledStatusLogged)
                        {
                            Log.Info("Network ACL closed to AM for job " + jobId + ". Not going to try to reach the AM."
                                     );
                            amAclDisabledStatusLogged = true;
                        }
                        return(GetNotRunningJob(null, JobState.Running));
                    }
                    return(realProxy);
                }
                catch (IOException)
                {
                    //possibly the AM has crashed
                    //there may be some time before AM is restarted
                    //keep retrying by getting the address from RM
                    Log.Info("Could not connect to " + serviceAddr + ". Waiting for getting the latest AM address..."
                             );
                    try
                    {
                        Sharpen.Thread.Sleep(2000);
                    }
                    catch (Exception e1)
                    {
                        Log.Warn("getProxy() call interruped", e1);
                        throw new YarnRuntimeException(e1);
                    }
                    try
                    {
                        application = rm.GetApplicationReport(appId);
                    }
                    catch (YarnException e1)
                    {
                        throw new IOException(e1);
                    }
                    if (application == null)
                    {
                        Log.Info("Could not get Job info from RM for job " + jobId + ". Redirecting to job history server."
                                 );
                        return(CheckAndGetHSProxy(null, JobState.Running));
                    }
                }
                catch (Exception e)
                {
                    Log.Warn("getProxy() call interruped", e);
                    throw new YarnRuntimeException(e);
                }
                catch (YarnException e)
                {
                    throw new IOException(e);
                }
            }
            string user = application.GetUser();

            if (user == null)
            {
                throw new IOException("User is not set in the application report");
            }
            if (application.GetYarnApplicationState() == YarnApplicationState.New || application
                .GetYarnApplicationState() == YarnApplicationState.NewSaving || application.GetYarnApplicationState
                    () == YarnApplicationState.Submitted || application.GetYarnApplicationState() ==
                YarnApplicationState.Accepted)
            {
                realProxy = null;
                return(GetNotRunningJob(application, JobState.New));
            }
            if (application.GetYarnApplicationState() == YarnApplicationState.Failed)
            {
                realProxy = null;
                return(GetNotRunningJob(application, JobState.Failed));
            }
            if (application.GetYarnApplicationState() == YarnApplicationState.Killed)
            {
                realProxy = null;
                return(GetNotRunningJob(application, JobState.Killed));
            }
            //History server can serve a job only if application
            //succeeded.
            if (application.GetYarnApplicationState() == YarnApplicationState.Finished)
            {
                Log.Info("Application state is completed. FinalApplicationStatus=" + application.
                         GetFinalApplicationStatus().ToString() + ". Redirecting to job history server");
                realProxy = CheckAndGetHSProxy(application, JobState.Succeeded);
            }
            return(realProxy);
        }
        // Test the DT mast key in the state-store when the mast key is being rolled.
        /// <exception cref="System.Exception"/>
        public virtual void TestRMDTMasterKeyStateOnRollingMasterKey()
        {
            MemoryRMStateStore memStore = new MemoryRMStateStore();

            memStore.Init(conf);
            RMStateStore.RMState rmState = memStore.GetState();
            IDictionary <RMDelegationTokenIdentifier, long> rmDTState = rmState.GetRMDTSecretManagerState
                                                                            ().GetTokenState();
            ICollection <DelegationKey> rmDTMasterKeyState = rmState.GetRMDTSecretManagerState
                                                                 ().GetMasterKeyState();
            MockRM rm1 = new TestRMDelegationTokens.MyMockRM(this, conf, memStore);

            rm1.Start();
            // on rm start, two master keys are created.
            // One is created at RMDTSecretMgr.startThreads.updateCurrentKey();
            // the other is created on the first run of
            // tokenRemoverThread.rollMasterKey()
            RMDelegationTokenSecretManager dtSecretManager = rm1.GetRMContext().GetRMDelegationTokenSecretManager
                                                                 ();

            // assert all master keys are saved
            NUnit.Framework.Assert.AreEqual(dtSecretManager.GetAllMasterKeys(), rmDTMasterKeyState
                                            );
            ICollection <DelegationKey> expiringKeys = new HashSet <DelegationKey>();

            Sharpen.Collections.AddAll(expiringKeys, dtSecretManager.GetAllMasterKeys());
            // request to generate a RMDelegationToken
            GetDelegationTokenRequest request = Org.Mockito.Mockito.Mock <GetDelegationTokenRequest
                                                                          >();

            Org.Mockito.Mockito.When(request.GetRenewer()).ThenReturn("renewer1");
            GetDelegationTokenResponse response = rm1.GetClientRMService().GetDelegationToken
                                                      (request);

            Org.Apache.Hadoop.Yarn.Api.Records.Token delegationToken = response.GetRMDelegationToken
                                                                           ();
            Org.Apache.Hadoop.Security.Token.Token <RMDelegationTokenIdentifier> token1 = ConverterUtils
                                                                                          .ConvertFromYarn(delegationToken, (Text)null);
            RMDelegationTokenIdentifier dtId1 = token1.DecodeIdentifier();

            // For all keys that still remain in memory, we should have them stored
            // in state-store also.
            while (((TestRMDelegationTokens.TestRMDelegationTokenSecretManager)dtSecretManager
                    ).numUpdatedKeys.Get() < 3)
            {
                ((TestRMDelegationTokens.TestRMDelegationTokenSecretManager)dtSecretManager).CheckCurrentKeyInStateStore
                    (rmDTMasterKeyState);
                Sharpen.Thread.Sleep(100);
            }
            // wait for token to expire and remove from state-store
            // rollMasterKey is called every 1 second.
            int count = 0;

            while (rmDTState.Contains(dtId1) && count < 100)
            {
                Sharpen.Thread.Sleep(100);
                count++;
            }
            rm1.Stop();
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestClientTokenRace()
        {
            conf.Set(CommonConfigurationKeysPublic.HadoopSecurityAuthentication, "kerberos");
            UserGroupInformation.SetConfiguration(conf);
            ContainerManagementProtocol containerManager = Org.Mockito.Mockito.Mock <ContainerManagementProtocol
                                                                                     >();
            StartContainersResponse mockResponse = Org.Mockito.Mockito.Mock <StartContainersResponse
                                                                             >();

            Org.Mockito.Mockito.When(containerManager.StartContainers((StartContainersRequest
                                                                       )Matchers.Any())).ThenReturn(mockResponse);
            DrainDispatcher dispatcher = new DrainDispatcher();
            MockRM          rm         = new _MockRMWithCustomAMLauncher_433(dispatcher, conf, containerManager
                                                                             );

            rm.Start();
            // Submit an app
            RMApp app = rm.SubmitApp(1024);
            // Set up a node.
            MockNM nm1 = rm.RegisterNode("localhost:1234", 3072);

            nm1.NodeHeartbeat(true);
            dispatcher.Await();
            nm1.NodeHeartbeat(true);
            dispatcher.Await();
            ApplicationAttemptId appAttempt = app.GetCurrentAppAttempt().GetAppAttemptId();
            MockAM mockAM = new MockAM(rm.GetRMContext(), rm.GetApplicationMasterService(), app
                                       .GetCurrentAppAttempt().GetAppAttemptId());
            UserGroupInformation appUgi = UserGroupInformation.CreateRemoteUser(appAttempt.ToString
                                                                                    ());
            RegisterApplicationMasterResponse response = appUgi.DoAs(new _PrivilegedAction_469
                                                                         (mockAM));
            // Get the app-report.
            GetApplicationReportRequest request = Org.Apache.Hadoop.Yarn.Util.Records.NewRecord
                                                  <GetApplicationReportRequest>();

            request.SetApplicationId(app.GetApplicationId());
            GetApplicationReportResponse reportResponse = rm.GetClientRMService().GetApplicationReport
                                                              (request);
            ApplicationReport appReport = reportResponse.GetApplicationReport();

            Org.Apache.Hadoop.Yarn.Api.Records.Token originalClientToAMToken = appReport.GetClientToAMToken
                                                                                   ();
            // ClientToAMToken master key should have been received on register
            // application master response.
            ByteBuffer clientMasterKey = response.GetClientToAMTokenMasterKey();

            NUnit.Framework.Assert.IsNotNull(clientMasterKey);
            NUnit.Framework.Assert.IsTrue(((byte[])clientMasterKey.Array()).Length > 0);
            // Start the AM with the correct shared-secret.
            ApplicationAttemptId appAttemptId = app.GetAppAttempts().Keys.GetEnumerator().Next
                                                    ();

            NUnit.Framework.Assert.IsNotNull(appAttemptId);
            TestClientToAMTokens.CustomAM am = new TestClientToAMTokens.CustomAM(appAttemptId
                                                                                 , null);
            am.Init(conf);
            am.Start();
            // Now the real test!
            // Set up clients to be able to pick up correct tokens.
            SecurityUtil.SetSecurityInfoProviders(new TestClientToAMTokens.CustomSecurityInfo
                                                      ());
            Org.Apache.Hadoop.Security.Token.Token <ClientToAMTokenIdentifier> token = ConverterUtils
                                                                                       .ConvertFromYarn(originalClientToAMToken, am.address);
            // Schedule the key to be set after a significant delay
            Timer     timer     = new Timer();
            TimerTask timerTask = new _TimerTask_516(am, clientMasterKey);

            timer.Schedule(timerTask, 250);
            // connect should pause waiting for the master key to arrive
            VerifyValidToken(conf, am, token);
            am.Stop();
            rm.Stop();
        }
        public virtual void TestClientToAMTokens()
        {
            conf.Set(CommonConfigurationKeysPublic.HadoopSecurityAuthentication, "kerberos");
            UserGroupInformation.SetConfiguration(conf);
            ContainerManagementProtocol containerManager = Org.Mockito.Mockito.Mock <ContainerManagementProtocol
                                                                                     >();
            StartContainersResponse mockResponse = Org.Mockito.Mockito.Mock <StartContainersResponse
                                                                             >();

            Org.Mockito.Mockito.When(containerManager.StartContainers((StartContainersRequest
                                                                       )Matchers.Any())).ThenReturn(mockResponse);
            DrainDispatcher dispatcher = new DrainDispatcher();
            MockRM          rm         = new _MockRMWithCustomAMLauncher_192(dispatcher, conf, containerManager
                                                                             );

            rm.Start();
            // Submit an app
            RMApp app = rm.SubmitApp(1024);
            // Set up a node.
            MockNM nm1 = rm.RegisterNode("localhost:1234", 3072);

            nm1.NodeHeartbeat(true);
            dispatcher.Await();
            nm1.NodeHeartbeat(true);
            dispatcher.Await();
            ApplicationAttemptId appAttempt = app.GetCurrentAppAttempt().GetAppAttemptId();
            MockAM mockAM = new MockAM(rm.GetRMContext(), rm.GetApplicationMasterService(), app
                                       .GetCurrentAppAttempt().GetAppAttemptId());
            UserGroupInformation appUgi = UserGroupInformation.CreateRemoteUser(appAttempt.ToString
                                                                                    ());
            RegisterApplicationMasterResponse response = appUgi.DoAs(new _PrivilegedAction_229
                                                                         (mockAM));
            // Get the app-report.
            GetApplicationReportRequest request = Org.Apache.Hadoop.Yarn.Util.Records.NewRecord
                                                  <GetApplicationReportRequest>();

            request.SetApplicationId(app.GetApplicationId());
            GetApplicationReportResponse reportResponse = rm.GetClientRMService().GetApplicationReport
                                                              (request);
            ApplicationReport appReport = reportResponse.GetApplicationReport();

            Org.Apache.Hadoop.Yarn.Api.Records.Token originalClientToAMToken = appReport.GetClientToAMToken
                                                                                   ();
            // ClientToAMToken master key should have been received on register
            // application master response.
            NUnit.Framework.Assert.IsNotNull(response.GetClientToAMTokenMasterKey());
            NUnit.Framework.Assert.IsTrue(((byte[])response.GetClientToAMTokenMasterKey().Array
                                               ()).Length > 0);
            // Start the AM with the correct shared-secret.
            ApplicationAttemptId appAttemptId = app.GetAppAttempts().Keys.GetEnumerator().Next
                                                    ();

            NUnit.Framework.Assert.IsNotNull(appAttemptId);
            TestClientToAMTokens.CustomAM am = new TestClientToAMTokens.CustomAM(appAttemptId
                                                                                 , ((byte[])response.GetClientToAMTokenMasterKey().Array()));
            am.Init(conf);
            am.Start();
            // Now the real test!
            // Set up clients to be able to pick up correct tokens.
            SecurityUtil.SetSecurityInfoProviders(new TestClientToAMTokens.CustomSecurityInfo
                                                      ());
            // Verify denial for unauthenticated user
            try
            {
                TestClientToAMTokens.CustomProtocol client = (TestClientToAMTokens.CustomProtocol
                                                              )RPC.GetProxy <TestClientToAMTokens.CustomProtocol>(1L, am.address, conf);
                client.Ping();
                NUnit.Framework.Assert.Fail("Access by unauthenticated user should fail!!");
            }
            catch (Exception)
            {
                NUnit.Framework.Assert.IsFalse(am.pinged);
            }
            Org.Apache.Hadoop.Security.Token.Token <ClientToAMTokenIdentifier> token = ConverterUtils
                                                                                       .ConvertFromYarn(originalClientToAMToken, am.address);
            // Verify denial for a malicious user with tampered ID
            VerifyTokenWithTamperedID(conf, am, token);
            // Verify denial for a malicious user with tampered user-name
            VerifyTokenWithTamperedUserName(conf, am, token);
            // Now for an authenticated user
            VerifyValidToken(conf, am, token);
            // Verify for a new version token
            VerifyNewVersionToken(conf, am, token, rm);
            am.Stop();
            rm.Stop();
        }