Search in sources :

Example 81 with Credentials

use of org.apache.hadoop.security.Credentials in project hadoop by apache.

the class TestCredentials method testReadWriteStorage.

@SuppressWarnings("unchecked")
@Test
public <T extends TokenIdentifier> void testReadWriteStorage() throws IOException, NoSuchAlgorithmException {
    // create tokenStorage Object
    Credentials ts = new Credentials();
    Token<T> token1 = new Token();
    Token<T> token2 = new Token();
    Text service1 = new Text("service1");
    Text service2 = new Text("service2");
    Collection<Text> services = new ArrayList<Text>();
    services.add(service1);
    services.add(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
    final KeyGenerator kg = KeyGenerator.getInstance(DEFAULT_HMAC_ALGORITHM);
    String alias = "alias";
    Map<Text, byte[]> m = new HashMap<Text, byte[]>(10);
    for (int i = 0; i < 10; i++) {
        Key key = kg.generateKey();
        m.put(new Text(alias + i), key.getEncoded());
        ts.addSecretKey(new Text(alias + i), key.getEncoded());
    }
    // create file to store
    File tmpFileName = new File(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
    Collection<Token<? extends TokenIdentifier>> list = ts.getAllTokens();
    assertEquals("getAllTokens should return collection of size 2", list.size(), 2);
    boolean foundFirst = false;
    boolean foundSecond = false;
    for (Token<? extends TokenIdentifier> token : list) {
        if (token.getService().equals(service1)) {
            foundFirst = true;
        }
        if (token.getService().equals(service2)) {
            foundSecond = true;
        }
    }
    assertTrue("Tokens for services service1 and service2 must be present", foundFirst && foundSecond);
    // compare secret keys
    int mapLen = m.size();
    assertEquals("wrong number of keys in the Storage", mapLen, ts.numberOfSecretKeys());
    for (Text a : m.keySet()) {
        byte[] kTS = ts.getSecretKey(a);
        byte[] kLocal = m.get(a);
        assertTrue("keys don't match for " + a, WritableComparator.compareBytes(kTS, 0, kTS.length, kLocal, 0, kLocal.length) == 0);
    }
    tmpFileName.delete();
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) KeyGenerator(javax.crypto.KeyGenerator) File(java.io.File) Credentials(org.apache.hadoop.security.Credentials) Key(java.security.Key) Test(org.junit.Test)

Example 82 with Credentials

use of org.apache.hadoop.security.Credentials in project hadoop by apache.

the class TestCredentials method testWritableProperties.

@Test
public /**
   * Verify the suitability of read/writeProto for use with Writable interface.
   */
void testWritableProperties() throws IOException, NoSuchAlgorithmException {
    String testname = "testWritableProperties";
    Text tok1 = new Text("token1");
    Text tok2 = new Text("token2");
    Text key1 = new Text("key1");
    Credentials ts = generateCredentials(tok1, tok2, key1);
    Text tok3 = new Text("token3");
    Text key2 = new Text("key2");
    Credentials ts2 = generateCredentials(tok1, tok3, key2);
    writeCredentialsProtos(ts, ts2, testname);
    List<Credentials> clist = readCredentialsProtos(testname);
    assertCredentials(testname, tok1, key1, ts, clist.get(0));
    assertCredentials(testname, tok2, key1, ts, clist.get(0));
    assertCredentials(testname, tok1, key2, ts2, clist.get(1));
    assertCredentials(testname, tok3, key2, ts2, clist.get(1));
}
Also used : Text(org.apache.hadoop.io.Text) Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Example 83 with Credentials

use of org.apache.hadoop.security.Credentials in project hadoop by apache.

the class TestCredentials method testBasicReadWriteStreamEmpty.

@Test
public void testBasicReadWriteStreamEmpty() throws IOException, NoSuchAlgorithmException {
    String testname = "testBasicReadWriteStreamEmpty";
    Credentials ts = new Credentials();
    writeCredentialsStream(ts, testname);
    Credentials ts2 = readCredentialsStream(testname);
    assertEquals("test empty tokens", 0, ts2.numberOfTokens());
    assertEquals("test empty keys", 0, ts2.numberOfSecretKeys());
}
Also used : Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Example 84 with Credentials

use of org.apache.hadoop.security.Credentials in project hadoop by apache.

the class TestCredentials method testBasicReadWriteProto.

@Test
public void testBasicReadWriteProto() throws IOException, NoSuchAlgorithmException {
    String testname = "testBasicReadWriteProto";
    Text tok1 = new Text("token1");
    Text tok2 = new Text("token2");
    Text key1 = new Text("key1");
    Credentials ts = generateCredentials(tok1, tok2, key1);
    writeCredentialsProto(ts, testname);
    Credentials ts2 = readCredentialsProto(testname);
    assertCredentials(testname, tok1, key1, ts, ts2);
    assertCredentials(testname, tok2, key1, ts, ts2);
}
Also used : Text(org.apache.hadoop.io.Text) Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Example 85 with Credentials

use of org.apache.hadoop.security.Credentials in project hadoop by apache.

the class TestCredentials method testBasicReadWriteProtoEmpty.

@Test
public void testBasicReadWriteProtoEmpty() throws IOException, NoSuchAlgorithmException {
    String testname = "testBasicReadWriteProtoEmpty";
    Credentials ts = new Credentials();
    writeCredentialsProto(ts, testname);
    Credentials ts2 = readCredentialsProto(testname);
    assertEquals("test empty tokens", 0, ts2.numberOfTokens());
    assertEquals("test empty keys", 0, ts2.numberOfSecretKeys());
}
Also used : Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Aggregations

Credentials (org.apache.hadoop.security.Credentials)238 Test (org.junit.Test)105 Token (org.apache.hadoop.security.token.Token)76 Text (org.apache.hadoop.io.Text)64 IOException (java.io.IOException)63 Path (org.apache.hadoop.fs.Path)50 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)48 ByteBuffer (java.nio.ByteBuffer)42 Configuration (org.apache.hadoop.conf.Configuration)41 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)37 HashMap (java.util.HashMap)34 InetSocketAddress (java.net.InetSocketAddress)30 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)30 ContainerId (org.apache.hadoop.yarn.api.records.ContainerId)28 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)27 File (java.io.File)25 ApplicationAttemptId (org.apache.hadoop.yarn.api.records.ApplicationAttemptId)24 ContainerLaunchContext (org.apache.hadoop.yarn.api.records.ContainerLaunchContext)23 JobConf (org.apache.hadoop.mapred.JobConf)20 LocalResource (org.apache.hadoop.yarn.api.records.LocalResource)19