com.mycompany.app.TestStagingDirectoryPermissions.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.app.TestStagingDirectoryPermissions.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.mycompany.app;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.MiniMRClientCluster;
import org.apache.hadoop.mapred.MiniMRClientClusterFactory;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.RunningJob;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

// Verify the permissions of the job.xml in a default, non-secured cluster
public class TestStagingDirectoryPermissions {
    static class MySleepMapper implements Mapper<IntWritable, Text, NullWritable, NullWritable> {

        private JobConf job;

        public void map(IntWritable key, Text value, OutputCollector<NullWritable, NullWritable> output,
                Reporter reporter) throws IOException {
            System.out.println("snoooze...");
            try {
                Thread.sleep(10 * 1000l);
            } catch (InterruptedException e) {
                throw new IOException(e);
            }
        }

        public void close() throws IOException {
            // Nothing to do
        }

        public void configure(JobConf job) {
            this.job = job;
        }
    }

    public Path path(String p) {
        return new Path(p);
    }

    public Path path(Path p, String s) {
        return new Path(p, s);
    }

    @Test
    public void perms() throws IOException, InterruptedException {
        MiniDFSCluster minidfs = null;
        FileSystem fs = null;
        MiniMRClientCluster minimr = null;
        try {
            Configuration conf = new Configuration(true);
            conf.set("fs.permission.umask-mode", "0077");
            minidfs = new MiniDFSCluster.Builder(conf).build();
            minidfs.waitActive();

            fs = minidfs.getFileSystem();
            conf.set(FileSystem.FS_DEFAULT_NAME_KEY, fs.getUri().toString());
            Path p = path("/in");
            fs.mkdirs(p);

            FSDataOutputStream os = fs.create(new Path(p, "input.txt"));
            os.write("hello!".getBytes("UTF-8"));
            os.close();

            String user = UserGroupInformation.getCurrentUser().getUserName();
            Path home = new Path("/User/" + user);
            fs.mkdirs(home);
            minimr = MiniMRClientClusterFactory.create(this.getClass(), 1, conf);
            JobConf job = new JobConf(minimr.getConfig());

            job.setJobName("PermsTest");
            JobClient client = new JobClient(job);
            FileInputFormat.addInputPath(job, p);
            FileOutputFormat.setOutputPath(job, path("/out"));
            job.setInputFormat(TextInputFormat.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);

            job.setMapperClass(MySleepMapper.class);

            job.setNumReduceTasks(1);
            RunningJob submittedJob = client.submitJob(job);

            // Sleep for a bit to let localization finish
            System.out.println("Sleeping...");
            Thread.sleep(3 * 1000l);
            System.out.println("Done sleeping...");
            assertFalse(UserGroupInformation.isSecurityEnabled());

            Path stagingRoot = path("/tmp/hadoop-yarn/staging/" + user + "/.staging/");
            assertTrue(fs.exists(stagingRoot));
            assertEquals(1, fs.listStatus(stagingRoot).length);
            Path staging = fs.listStatus(stagingRoot)[0].getPath();
            Path jobXml = path(staging + "/job.xml");

            assertTrue(fs.exists(jobXml));

            FileStatus fileStatus = fs.getFileStatus(jobXml);
            System.out.println("job.xml permission = " + fileStatus.getPermission());
            assertTrue(fileStatus.getPermission().getOtherAction().implies(FsAction.READ));
            assertTrue(fileStatus.getPermission().getGroupAction().implies(FsAction.READ));

            submittedJob.waitForCompletion();
        } finally {
            if (minimr != null) {
                minimr.stop();
            }
            if (fs != null) {
                fs.close();
            }
            if (minidfs != null) {
                minidfs.shutdown(true);
            }
        }
    }
}