Search in sources :

Example 26 with AuthenticationTokenIdentifier

use of org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier in project accumulo by apache.

the class AuthenticationTokenSecretManagerTest method testRolledManagerKey.

@Test
public void testRolledManagerKey() throws Exception {
    // start of the test
    long then = System.currentTimeMillis();
    long tokenLifetime = MINUTES.toMillis(1);
    AuthenticationTokenSecretManager secretManager = new AuthenticationTokenSecretManager(instanceId, tokenLifetime);
    // Add a current key
    AuthenticationKey authKey1 = new AuthenticationKey(1, then, then + tokenLifetime, keyGen.generateKey());
    secretManager.addKey(authKey1);
    String principal = "user@EXAMPLE.COM";
    Entry<Token<AuthenticationTokenIdentifier>, AuthenticationTokenIdentifier> pair = secretManager.generateToken(principal, cfg);
    Token<AuthenticationTokenIdentifier> token = pair.getKey();
    AuthenticationTokenIdentifier id = new AuthenticationTokenIdentifier();
    id.readFields(new DataInputStream(new ByteArrayInputStream(token.getIdentifier())));
    long now = System.currentTimeMillis();
    secretManager.addKey(new AuthenticationKey(2, now, now + tokenLifetime, keyGen.generateKey()));
    // Should succeed -- the SecretManager still has authKey1
    secretManager.retrievePassword(id);
    // Remove authKey1
    secretManager.removeKey(authKey1.getKeyId());
    // Should fail -- authKey1 (presumably) expired, cannot authenticate
    assertThrows(InvalidToken.class, () -> secretManager.retrievePassword(id));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationTokenIdentifier(org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier) InvalidToken(org.apache.hadoop.security.token.SecretManager.InvalidToken) Token(org.apache.hadoop.security.token.Token) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 27 with AuthenticationTokenIdentifier

use of org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier in project accumulo by apache.

the class AuthenticationTokenSecretManagerTest method testGenerateToken.

@Test
public void testGenerateToken() throws Exception {
    // start of the test
    long then = System.currentTimeMillis();
    // 1 minute
    long tokenLifetime = MINUTES.toMillis(1);
    AuthenticationTokenSecretManager secretManager = new AuthenticationTokenSecretManager(instanceId, tokenLifetime);
    // Add a current key
    secretManager.addKey(new AuthenticationKey(1, then, then + tokenLifetime, keyGen.generateKey()));
    String principal = "user@EXAMPLE.COM";
    Entry<Token<AuthenticationTokenIdentifier>, AuthenticationTokenIdentifier> pair = secretManager.generateToken(principal, cfg);
    assertNotNull(pair);
    Token<AuthenticationTokenIdentifier> token = pair.getKey();
    assertNotNull(token);
    assertEquals(AuthenticationTokenIdentifier.TOKEN_KIND, token.getKind());
    // Reconstitute the token identifier (will happen when clients are involved)
    AuthenticationTokenIdentifier id = new AuthenticationTokenIdentifier();
    id.readFields(new DataInputStream(new ByteArrayInputStream(token.getIdentifier())));
    long now = System.currentTimeMillis();
    // Issue date should be after the test started, but before we deserialized the token
    assertTrue("Issue date did not fall within the expected upper bound. Expected less than " + now + ", but was " + id.getIssueDate(), id.getIssueDate() <= now);
    assertTrue("Issue date did not fall within the expected lower bound. Expected greater than " + then + ", but was " + id.getIssueDate(), id.getIssueDate() >= then);
    // Expiration is the token lifetime plus the issue date
    assertEquals(id.getIssueDate() + tokenLifetime, id.getExpirationDate());
    // Verify instance ID
    assertEquals(instanceId, id.getInstanceId());
    // The returned id should be the same as the reconstructed id
    assertEquals(pair.getValue(), id);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationTokenIdentifier(org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier) InvalidToken(org.apache.hadoop.security.token.SecretManager.InvalidToken) Token(org.apache.hadoop.security.token.Token) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 28 with AuthenticationTokenIdentifier

use of org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier in project accumulo by apache.

the class AuthenticationTokenSecretManagerTest method testExpiredPasswordsThrowError.

@Test
public void testExpiredPasswordsThrowError() throws Exception {
    // start of the test
    long then = System.currentTimeMillis();
    // 500ms lifetime
    long tokenLifetime = 500;
    AuthenticationTokenSecretManager secretManager = new AuthenticationTokenSecretManager(instanceId, tokenLifetime);
    // Add a current key
    secretManager.addKey(new AuthenticationKey(1, then, then + tokenLifetime, keyGen.generateKey()));
    String principal = "user@EXAMPLE.COM";
    Entry<Token<AuthenticationTokenIdentifier>, AuthenticationTokenIdentifier> pair = secretManager.generateToken(principal, cfg);
    Token<AuthenticationTokenIdentifier> token = pair.getKey();
    // Add a small buffer to make sure we move past the expiration of 0 for the token.
    Thread.sleep(1000);
    // Reconstitute the token identifier (will happen when clients are involved)
    AuthenticationTokenIdentifier id = new AuthenticationTokenIdentifier();
    id.readFields(new DataInputStream(new ByteArrayInputStream(token.getIdentifier())));
    assertThrows(InvalidToken.class, () -> secretManager.retrievePassword(id));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationTokenIdentifier(org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier) InvalidToken(org.apache.hadoop.security.token.SecretManager.InvalidToken) Token(org.apache.hadoop.security.token.Token) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 29 with AuthenticationTokenIdentifier

use of org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier in project accumulo by apache.

the class SaslDigestCallbackHandlerTest method testIdentifierSerialization.

@Test
public void testIdentifierSerialization() throws IOException {
    var tAuthIdentifier = createTAuthIdentifier("user", 1, 100L, 1000L, "instanceid");
    var identifier = new AuthenticationTokenIdentifier(tAuthIdentifier);
    byte[] serialized = identifier.getBytes();
    String name = handler.encodeIdentifier(serialized);
    byte[] reserialized = handler.decodeIdentifier(name);
    assertArrayEquals(serialized, reserialized);
    AuthenticationTokenIdentifier copy = new AuthenticationTokenIdentifier();
    copy.readFields(new DataInputStream(new ByteArrayInputStream(reserialized)));
    assertEquals(identifier, copy);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AuthenticationTokenIdentifier(org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

AuthenticationTokenIdentifier (org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier)29 ByteArrayInputStream (java.io.ByteArrayInputStream)13 DataInputStream (java.io.DataInputStream)13 DelegationTokenImpl (org.apache.accumulo.core.clientImpl.DelegationTokenImpl)11 Token (org.apache.hadoop.security.token.Token)11 Test (org.junit.jupiter.api.Test)11 TAuthenticationTokenIdentifier (org.apache.accumulo.core.securityImpl.thrift.TAuthenticationTokenIdentifier)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 DelegationTokenConfig (org.apache.accumulo.core.client.admin.DelegationTokenConfig)6 KerberosToken (org.apache.accumulo.core.client.security.tokens.KerberosToken)6 InvalidToken (org.apache.hadoop.security.token.SecretManager.InvalidToken)6 AccumuloException (org.apache.accumulo.core.client.AccumuloException)5 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)5 AuthenticationToken (org.apache.accumulo.core.client.security.tokens.AuthenticationToken)5 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)4 DelegationToken (org.apache.accumulo.core.client.security.tokens.DelegationToken)4 PasswordToken (org.apache.accumulo.core.client.security.tokens.PasswordToken)4 ClientContext (org.apache.accumulo.core.clientImpl.ClientContext)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3