示例#1
0
 /// <exception cref="System.IO.IOException"/>
 private string GetTokenOwner <_T0>(Org.Apache.Hadoop.Security.Token.Token <_T0> token
                                    )
     where _T0 : TokenIdentifier
 {
     // webhdfs doesn't register properly with the class loader
     Org.Apache.Hadoop.Security.Token.Token <object> clone = new Org.Apache.Hadoop.Security.Token.Token
                                                                 (token);
     clone.SetKind(DelegationTokenIdentifier.HdfsDelegationKind);
     return(clone.DecodeIdentifier().GetUser().GetUserName());
 }
示例#2
0
        public virtual void TestReadWriteStorage <T>()
            where T : TokenIdentifier
        {
            // create tokenStorage Object
            Credentials ts = new Credentials();

            Org.Apache.Hadoop.Security.Token.Token <T> token1 = new Org.Apache.Hadoop.Security.Token.Token
                                                                    ();
            Org.Apache.Hadoop.Security.Token.Token <T> token2 = new Org.Apache.Hadoop.Security.Token.Token
                                                                    ();
            Text service1 = new Text("service1");
            Text service2 = new Text("service2");
            ICollection <Text> services = new AList <Text>();

            services.AddItem(service1);
            services.AddItem(service2);
            token1.SetService(service1);
            token2.SetService(service2);
            ts.AddToken(new Text("sometoken1"), token1);
            ts.AddToken(new Text("sometoken2"), token2);
            // create keys and put it in
            KeyGenerator kg              = KeyGenerator.GetInstance(DefaultHmacAlgorithm);
            string       alias           = "alias";
            IDictionary <Text, byte[]> m = new Dictionary <Text, byte[]>(10);

            for (int i = 0; i < 10; i++)
            {
                Key key = kg.GenerateKey();
                m[new Text(alias + i)] = key.GetEncoded();
                ts.AddSecretKey(new Text(alias + i), key.GetEncoded());
            }
            // create file to store
            FilePath         tmpFileName = new FilePath(tmpDir, "tokenStorageTest");
            DataOutputStream dos         = new DataOutputStream(new FileOutputStream(tmpFileName));

            ts.Write(dos);
            dos.Close();
            // open and read it back
            DataInputStream dis = new DataInputStream(new FileInputStream(tmpFileName));

            ts = new Credentials();
            ts.ReadFields(dis);
            dis.Close();
            // get the tokens and compare the services
            ICollection <Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> > list = ts.GetAllTokens
                                                                                               ();

            Assert.Equal("getAllTokens should return collection of size 2"
                         , list.Count, 2);
            bool foundFirst  = false;
            bool foundSecond = false;

            foreach (Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> token in list)
            {
                if (token.GetService().Equals(service1))
                {
                    foundFirst = true;
                }
                if (token.GetService().Equals(service2))
                {
                    foundSecond = true;
                }
            }
            Assert.True("Tokens for services service1 and service2 must be present"
                        , foundFirst && foundSecond);
            // compare secret keys
            int mapLen = m.Count;

            Assert.Equal("wrong number of keys in the Storage", mapLen, ts
                         .NumberOfSecretKeys());
            foreach (Text a in m.Keys)
            {
                byte[] kTS    = ts.GetSecretKey(a);
                byte[] kLocal = m[a];
                Assert.True("keys don't match for " + a, WritableComparator.CompareBytes
                                (kTS, 0, kTS.Length, kLocal, 0, kLocal.Length) == 0);
            }
            tmpFileName.Delete();
        }