Search in sources :

Example 1 with TokenIdentifier

use of org.apache.hadoop.security.token.TokenIdentifier in project flink by apache.

the class Utils method setTokensFor.

public static void setTokensFor(ContainerLaunchContext amContainer, List<Path> paths, Configuration conf) throws IOException {
    Credentials credentials = new Credentials();
    // for HDFS
    TokenCache.obtainTokensForNamenodes(credentials, paths.toArray(new Path[0]), conf);
    // for HBase
    obtainTokenForHBase(credentials, conf);
    // for user
    UserGroupInformation currUsr = UserGroupInformation.getCurrentUser();
    Collection<Token<? extends TokenIdentifier>> usrTok = currUsr.getTokens();
    for (Token<? extends TokenIdentifier> token : usrTok) {
        final Text id = new Text(token.getIdentifier());
        LOG.info("Adding user token " + id + " with " + token);
        credentials.addToken(id, token);
    }
    try (DataOutputBuffer dob = new DataOutputBuffer()) {
        credentials.writeTokenStorageToStream(dob);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Wrote tokens. Credentials buffer length: " + dob.getLength());
        }
        ByteBuffer securityTokens = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
        amContainer.setTokens(securityTokens);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) ByteBuffer(java.nio.ByteBuffer) Credentials(org.apache.hadoop.security.Credentials) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 2 with TokenIdentifier

use of org.apache.hadoop.security.token.TokenIdentifier in project hadoop by apache.

the class DataNode method checkReadAccess.

private void checkReadAccess(final ExtendedBlock block) throws IOException {
    // Make sure this node has registered for the block pool.
    try {
        getDNRegistrationForBP(block.getBlockPoolId());
    } catch (IOException e) {
        // if it has not registered with the NN, throw an exception back.
        throw new org.apache.hadoop.ipc.RetriableException("Datanode not registered. Try again later.");
    }
    if (isBlockTokenEnabled) {
        Set<TokenIdentifier> tokenIds = UserGroupInformation.getCurrentUser().getTokenIdentifiers();
        if (tokenIds.size() != 1) {
            throw new IOException("Can't continue since none or more than one " + "BlockTokenIdentifier is found.");
        }
        for (TokenIdentifier tokenId : tokenIds) {
            BlockTokenIdentifier id = (BlockTokenIdentifier) tokenId;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Got: " + id.toString());
            }
            blockPoolTokenSecretManager.checkAccess(id, null, block, BlockTokenIdentifier.AccessMode.READ);
        }
    }
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) BlockTokenIdentifier(org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier) BlockTokenIdentifier(org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier) IOException(java.io.IOException)

Example 3 with TokenIdentifier

use of org.apache.hadoop.security.token.TokenIdentifier in project hadoop by apache.

the class TestTokenAspect method testGetRemoteToken.

@Test
public void testGetRemoteToken() throws IOException, URISyntaxException {
    Configuration conf = new Configuration();
    DummyFs fs = spy(new DummyFs());
    Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234"));
    doReturn(token).when(fs).getDelegationToken(anyString());
    doReturn(token).when(fs).getRenewToken();
    fs.initialize(new URI("dummyfs://127.0.0.1:1234"), conf);
    fs.tokenAspect.ensureTokenInitialized();
    // Select a token, store and renew it
    verify(fs).setDelegationToken(token);
    assertNotNull(Whitebox.getInternalState(fs.tokenAspect, "dtRenewer"));
    assertNotNull(Whitebox.getInternalState(fs.tokenAspect, "action"));
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) Configuration(org.apache.hadoop.conf.Configuration) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) URI(java.net.URI) Test(org.junit.Test)

Example 4 with TokenIdentifier

use of org.apache.hadoop.security.token.TokenIdentifier in project hadoop by apache.

the class TestTokenAspect method testCachedInitialization.

@Test
public void testCachedInitialization() throws IOException, URISyntaxException {
    Configuration conf = new Configuration();
    DummyFs fs = spy(new DummyFs());
    Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234"));
    doReturn(token).when(fs).getDelegationToken(anyString());
    doReturn(token).when(fs).getRenewToken();
    fs.emulateSecurityEnabled = true;
    fs.initialize(new URI("dummyfs://127.0.0.1:1234"), conf);
    fs.tokenAspect.ensureTokenInitialized();
    verify(fs, times(1)).getDelegationToken(null);
    verify(fs, times(1)).setDelegationToken(token);
    // For the second iteration, the token should be cached.
    fs.tokenAspect.ensureTokenInitialized();
    verify(fs, times(1)).getDelegationToken(null);
    verify(fs, times(1)).setDelegationToken(token);
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) Configuration(org.apache.hadoop.conf.Configuration) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) URI(java.net.URI) Test(org.junit.Test)

Example 5 with TokenIdentifier

use of org.apache.hadoop.security.token.TokenIdentifier in project hadoop by apache.

the class TestTokenAspect method testInitWithUGIToken.

@Test
public void testInitWithUGIToken() throws IOException, URISyntaxException {
    Configuration conf = new Configuration();
    DummyFs fs = spy(new DummyFs());
    doReturn(null).when(fs).getDelegationToken(anyString());
    Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234"));
    fs.ugi.addToken(token);
    fs.ugi.addToken(new Token<TokenIdentifier>(new byte[0], new byte[0], new Text("Other token"), new Text("127.0.0.1:8021")));
    assertEquals("wrong tokens in user", 2, fs.ugi.getTokens().size());
    fs.emulateSecurityEnabled = true;
    fs.initialize(new URI("dummyfs://127.0.0.1:1234"), conf);
    fs.tokenAspect.ensureTokenInitialized();
    // Select a token from ugi (not from the remote host), store it but don't
    // renew it
    verify(fs).setDelegationToken(token);
    verify(fs, never()).getDelegationToken(anyString());
    assertNull(Whitebox.getInternalState(fs.tokenAspect, "dtRenewer"));
    assertNull(Whitebox.getInternalState(fs.tokenAspect, "action"));
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) Configuration(org.apache.hadoop.conf.Configuration) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) URI(java.net.URI) Test(org.junit.Test)

Aggregations

TokenIdentifier (org.apache.hadoop.security.token.TokenIdentifier)34 Token (org.apache.hadoop.security.token.Token)24 Text (org.apache.hadoop.io.Text)16 Credentials (org.apache.hadoop.security.Credentials)13 IOException (java.io.IOException)11 Test (org.junit.Test)11 Configuration (org.apache.hadoop.conf.Configuration)7 URI (java.net.URI)5 ByteBuffer (java.nio.ByteBuffer)5 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)5 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)5 HashMap (java.util.HashMap)4 AMRMTokenIdentifier (org.apache.hadoop.yarn.security.AMRMTokenIdentifier)4 InetSocketAddress (java.net.InetSocketAddress)3 TestTokenIdentifier (org.apache.hadoop.ipc.TestRpcBase.TestTokenIdentifier)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 AuthenticationToken (org.apache.accumulo.core.client.security.tokens.AuthenticationToken)2 MockFileSystem (org.apache.hadoop.fs.FileSystemTestHelper.MockFileSystem)2