Search in sources :

Example 6 with DataInputByteBuffer

use of org.apache.hadoop.io.DataInputByteBuffer in project hadoop by apache.

the class TestTaskID method testWrite.

/**
   * Test of write method, of class TaskID.
   */
@Test
public void testWrite() throws Exception {
    JobID jobId = new JobID("1234", 1);
    TaskID taskId = new TaskID(jobId, TaskType.JOB_SETUP, 0);
    DataOutputByteBuffer out = new DataOutputByteBuffer();
    taskId.write(out);
    DataInputByteBuffer in = new DataInputByteBuffer();
    byte[] buffer = new byte[4];
    in.reset(out.getData());
    assertEquals("The write() method did not write the expected task ID", 0, in.readInt());
    assertEquals("The write() method did not write the expected job ID", 1, in.readInt());
    assertEquals("The write() method did not write the expected job " + "identifier length", 4, WritableUtils.readVInt(in));
    in.readFully(buffer, 0, 4);
    assertEquals("The write() method did not write the expected job " + "identifier length", "1234", new String(buffer));
    assertEquals("The write() method did not write the expected task type", TaskType.JOB_SETUP, WritableUtils.readEnum(in, TaskType.class));
}
Also used : DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) DataOutputByteBuffer(org.apache.hadoop.io.DataOutputByteBuffer) Test(org.junit.Test)

Example 7 with DataInputByteBuffer

use of org.apache.hadoop.io.DataInputByteBuffer in project hadoop by apache.

the class TestYarnClient method testAutomaticTimelineDelegationTokenLoading.

@Test
public void testAutomaticTimelineDelegationTokenLoading() throws Exception {
    Configuration conf = new YarnConfiguration();
    conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, true);
    SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS, conf);
    TimelineDelegationTokenIdentifier timelineDT = new TimelineDelegationTokenIdentifier();
    final Token<TimelineDelegationTokenIdentifier> dToken = new Token<TimelineDelegationTokenIdentifier>(timelineDT.getBytes(), new byte[0], timelineDT.getKind(), new Text());
    // create a mock client
    YarnClientImpl client = spy(new YarnClientImpl() {

        @Override
        TimelineClient createTimelineClient() throws IOException, YarnException {
            timelineClient = mock(TimelineClient.class);
            when(timelineClient.getDelegationToken(any(String.class))).thenReturn(dToken);
            return timelineClient;
        }

        @Override
        protected void serviceStart() throws Exception {
            rmClient = mock(ApplicationClientProtocol.class);
        }

        @Override
        protected void serviceStop() throws Exception {
        }

        @Override
        public ApplicationReport getApplicationReport(ApplicationId appId) {
            ApplicationReport report = mock(ApplicationReport.class);
            when(report.getYarnApplicationState()).thenReturn(YarnApplicationState.RUNNING);
            return report;
        }

        @Override
        public boolean isSecurityEnabled() {
            return true;
        }
    });
    client.init(conf);
    client.start();
    try {
        // when i == 1, timeline DT doesn't exist, need to get one more
        for (int i = 0; i < 2; ++i) {
            ApplicationSubmissionContext context = mock(ApplicationSubmissionContext.class);
            ApplicationId applicationId = ApplicationId.newInstance(0, i + 1);
            when(context.getApplicationId()).thenReturn(applicationId);
            DataOutputBuffer dob = new DataOutputBuffer();
            Credentials credentials = new Credentials();
            if (i == 0) {
                credentials.addToken(client.timelineService, dToken);
            }
            credentials.writeTokenStorageToStream(dob);
            ByteBuffer tokens = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
            ContainerLaunchContext clc = ContainerLaunchContext.newInstance(null, null, null, null, tokens, null);
            when(context.getAMContainerSpec()).thenReturn(clc);
            client.submitApplication(context);
            if (i == 0) {
                // GetTimelineDelegationToken shouldn't be called
                verify(client, never()).getTimelineDelegationToken();
            }
            // In either way, token should be there
            credentials = new Credentials();
            DataInputByteBuffer dibb = new DataInputByteBuffer();
            tokens = clc.getTokens();
            if (tokens != null) {
                dibb.reset(tokens);
                credentials.readTokenStorageStream(dibb);
                tokens.rewind();
            }
            Collection<Token<? extends TokenIdentifier>> dTokens = credentials.getAllTokens();
            Assert.assertEquals(1, dTokens.size());
            Assert.assertEquals(dToken, dTokens.iterator().next());
        }
    } finally {
        client.stop();
    }
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) TimelineDelegationTokenIdentifier(org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier) CapacitySchedulerConfiguration(org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) TimelineDelegationTokenIdentifier(org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) ByteBuffer(java.nio.ByteBuffer) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) ApplicationNotFoundException(org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException) ApplicationIdNotProvidedException(org.apache.hadoop.yarn.exceptions.ApplicationIdNotProvidedException) IOException(java.io.IOException) ContainerNotFoundException(org.apache.hadoop.yarn.exceptions.ContainerNotFoundException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) TimelineClient(org.apache.hadoop.yarn.client.api.TimelineClient) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ApplicationSubmissionContext(org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Example 8 with DataInputByteBuffer

use of org.apache.hadoop.io.DataInputByteBuffer in project hadoop by apache.

the class BuilderUtils method parseTokensConf.

public static Configuration parseTokensConf(ApplicationSubmissionContext context) throws IOException {
    ByteBuffer tokensConf = context.getAMContainerSpec().getTokensConf();
    if (tokensConf == null) {
        return null;
    }
    DataInputByteBuffer dibb = new DataInputByteBuffer();
    dibb.reset(tokensConf);
    Configuration appConf = new Configuration(false);
    appConf.readFields(dibb);
    tokensConf.rewind();
    return appConf;
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) ByteBuffer(java.nio.ByteBuffer) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer)

Example 9 with DataInputByteBuffer

use of org.apache.hadoop.io.DataInputByteBuffer in project hadoop by apache.

the class BuilderUtils method parseCredentials.

public static Credentials parseCredentials(ApplicationSubmissionContext application) throws IOException {
    Credentials credentials = new Credentials();
    DataInputByteBuffer dibb = new DataInputByteBuffer();
    ByteBuffer tokens = application.getAMContainerSpec().getTokens();
    if (tokens != null) {
        dibb.reset(tokens);
        credentials.readTokenStorageStream(dibb);
        tokens.rewind();
    }
    return credentials;
}
Also used : DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) ByteBuffer(java.nio.ByteBuffer) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) Credentials(org.apache.hadoop.security.Credentials)

Example 10 with DataInputByteBuffer

use of org.apache.hadoop.io.DataInputByteBuffer in project hadoop by apache.

the class ApplicationAttemptStateDataPBImpl method convertCredentialsFromByteBuffer.

private static Credentials convertCredentialsFromByteBuffer(ByteBuffer appAttemptTokens) {
    DataInputByteBuffer dibb = new DataInputByteBuffer();
    try {
        Credentials credentials = null;
        if (appAttemptTokens != null) {
            credentials = new Credentials();
            appAttemptTokens.rewind();
            dibb.reset(appAttemptTokens);
            credentials.readTokenStorageStream(dibb);
        }
        return credentials;
    } catch (IOException e) {
        LOG.error("Failed to convert Credentials from ByteBuffer.");
        assert false;
        return null;
    } finally {
        IOUtils.closeStream(dibb);
    }
}
Also used : DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) IOException(java.io.IOException) Credentials(org.apache.hadoop.security.Credentials)

Aggregations

DataInputByteBuffer (org.apache.hadoop.io.DataInputByteBuffer)21 Credentials (org.apache.hadoop.security.Credentials)12 ByteBuffer (java.nio.ByteBuffer)10 Token (org.apache.hadoop.security.token.Token)8 Test (org.junit.Test)8 Text (org.apache.hadoop.io.Text)5 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)4 IOException (java.io.IOException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Configuration (org.apache.hadoop.conf.Configuration)3 DelegationTokenIdentifier (org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier)3 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)3 DataOutputByteBuffer (org.apache.hadoop.io.DataOutputByteBuffer)3 InvalidToken (org.apache.hadoop.security.token.SecretManager.InvalidToken)3 ApplicationAccessType (org.apache.hadoop.yarn.api.records.ApplicationAccessType)3 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)3 NodeHeartbeatResponse (org.apache.hadoop.yarn.server.api.protocolrecords.NodeHeartbeatResponse)3 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)3 MockRM (org.apache.hadoop.yarn.server.resourcemanager.MockRM)3 TestSecurityMockRM (org.apache.hadoop.yarn.server.resourcemanager.TestRMRestart.TestSecurityMockRM)3