Java Code Examples for org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager.DelegationTokenInformation

The following examples show how to use org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager.DelegationTokenInformation. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: NNAnalytics   Source File: TestTokenExtractor.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() {
  Mockito.doNothing().when(fsn).writeLock();
  Mockito.doNothing().when(fsn).writeUnlock();

  DelegationTokenIdentifier dtId =
      new DelegationTokenIdentifier(
          new Text("mockOwner"), new Text("mockGroup"), new Text("mockRealUser/[email protected]"));
  DelegationTokenInformation dtInfo = Mockito.mock(DelegationTokenInformation.class);
  dtsm.currentTokens.put(dtId, dtInfo);

  TokenExtractor extractor = new TokenExtractor(dtsm, fsn);
  Map<String, Long> tokenLastLogins = extractor.getTokenLastLogins();

  assertThat(tokenLastLogins.size(), is(2));
  assertThat(tokenLastLogins.containsKey("mockRealUser"), is(true));
  assertThat(tokenLastLogins.containsKey("mockOwner"), is(true));
}
 
Example 2
Source Project: NNAnalytics   Source File: TokenExtractor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extract the last seen DelegationTokens from FSNamesystem.
 *
 * @return map of user names to last timestamp of token seen
 */
public Map<String, Long> getTokenLastLogins() {
  if (fsn == null || dtsm == null) {
    return new HashMap<String, Long>() {
      {
        put("hdfs", System.currentTimeMillis());
        put("n/a", -1L);
      }
    };
  }
  Map<String, Long> lastLogins = new HashMap<>();
  fsn.writeLock();
  try {
    Set<Map.Entry<DelegationTokenIdentifier, DelegationTokenInformation>> entries =
        dtsm.currentTokens.entrySet();
    for (Map.Entry<DelegationTokenIdentifier, DelegationTokenInformation> entry : entries) {
      Text owner = entry.getKey().getOwner();
      Text realUser = entry.getKey().getRealUser();
      String ownerStr = new KerberosName(owner.toString()).getServiceName();
      long time = entry.getKey().getIssueDate();
      lastLogins.put(ownerStr, time);
      if ((realUser != null) && (!realUser.toString().isEmpty()) && !realUser.equals(owner)) {
        String realUserStr = new KerberosName(realUser.toString()).getServiceName();
        lastLogins.put(realUserStr, time);
      }
    }
    return lastLogins;
  } finally {
    fsn.writeUnlock();
  }
}
 
Example 3
Source Project: hadoop   Source File: TestDelegationToken.java    License: Apache License 2.0 4 votes vote down vote up
public Map<TestDelegationTokenIdentifier, DelegationTokenInformation> getAllTokens() {
  return currentTokens;
}
 
Example 4
Source Project: hadoop   Source File: TestDelegationToken.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParallelDelegationTokenCreation() throws Exception {
  final TestDelegationTokenSecretManager dtSecretManager = 
      new TestDelegationTokenSecretManager(2000, 24 * 60 * 60 * 1000, 
          7 * 24 * 60 * 60 * 1000, 2000);
  try {
    dtSecretManager.startThreads();
    int numThreads = 100;
    final int numTokensPerThread = 100;
    class tokenIssuerThread implements Runnable {

      @Override
      public void run() {
        for(int i =0;i <numTokensPerThread; i++) {
          generateDelegationToken(dtSecretManager, "auser", "arenewer");
          try {
            Thread.sleep(250); 
          } catch (Exception e) {
          }
        }
      }
    }
    Thread[] issuers = new Thread[numThreads];
    for (int i =0; i <numThreads; i++) {
      issuers[i] = new Daemon(new tokenIssuerThread());
      issuers[i].start();
    }
    for (int i =0; i <numThreads; i++) {
      issuers[i].join();
    }
    Map<TestDelegationTokenIdentifier, DelegationTokenInformation> tokenCache = dtSecretManager
        .getAllTokens();
    Assert.assertEquals(numTokensPerThread*numThreads, tokenCache.size());
    Iterator<TestDelegationTokenIdentifier> iter = tokenCache.keySet().iterator();
    while (iter.hasNext()) {
      TestDelegationTokenIdentifier id = iter.next();
      DelegationTokenInformation info = tokenCache.get(id);
      Assert.assertTrue(info != null);
      DelegationKey key = dtSecretManager.getKey(id);
      Assert.assertTrue(key != null);
      byte[] storedPassword = dtSecretManager.retrievePassword(id);
      byte[] password = dtSecretManager.createPassword(id, key);
      Assert.assertTrue(Arrays.equals(password, storedPassword));
      //verify by secret manager api
      dtSecretManager.verifyToken(id, password);
    }
  } finally {
    dtSecretManager.stopThreads();
  }
}
 
Example 5
Source Project: big-c   Source File: TestDelegationToken.java    License: Apache License 2.0 4 votes vote down vote up
public Map<TestDelegationTokenIdentifier, DelegationTokenInformation> getAllTokens() {
  return currentTokens;
}
 
Example 6
Source Project: big-c   Source File: TestDelegationToken.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParallelDelegationTokenCreation() throws Exception {
  final TestDelegationTokenSecretManager dtSecretManager = 
      new TestDelegationTokenSecretManager(2000, 24 * 60 * 60 * 1000, 
          7 * 24 * 60 * 60 * 1000, 2000);
  try {
    dtSecretManager.startThreads();
    int numThreads = 100;
    final int numTokensPerThread = 100;
    class tokenIssuerThread implements Runnable {

      @Override
      public void run() {
        for(int i =0;i <numTokensPerThread; i++) {
          generateDelegationToken(dtSecretManager, "auser", "arenewer");
          try {
            Thread.sleep(250); 
          } catch (Exception e) {
          }
        }
      }
    }
    Thread[] issuers = new Thread[numThreads];
    for (int i =0; i <numThreads; i++) {
      issuers[i] = new Daemon(new tokenIssuerThread());
      issuers[i].start();
    }
    for (int i =0; i <numThreads; i++) {
      issuers[i].join();
    }
    Map<TestDelegationTokenIdentifier, DelegationTokenInformation> tokenCache = dtSecretManager
        .getAllTokens();
    Assert.assertEquals(numTokensPerThread*numThreads, tokenCache.size());
    Iterator<TestDelegationTokenIdentifier> iter = tokenCache.keySet().iterator();
    while (iter.hasNext()) {
      TestDelegationTokenIdentifier id = iter.next();
      DelegationTokenInformation info = tokenCache.get(id);
      Assert.assertTrue(info != null);
      DelegationKey key = dtSecretManager.getKey(id);
      Assert.assertTrue(key != null);
      byte[] storedPassword = dtSecretManager.retrievePassword(id);
      byte[] password = dtSecretManager.createPassword(id, key);
      Assert.assertTrue(Arrays.equals(password, storedPassword));
      //verify by secret manager api
      dtSecretManager.verifyToken(id, password);
    }
  } finally {
    dtSecretManager.stopThreads();
  }
}