Skip to content

Commit 147c6e8

Browse files
committed
HADOOP-6472. add tokenCache option to GenericOptionsParser for passing
file with secret keys to a map reduce job. (boryas) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@895801 13f79535-47bb-0310-9956-ffa450edef68
1 parent fe8dad0 commit 147c6e8

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Trunk (unreleased changes)
7676

7777
HADOOP-6435. Make RPC.waitForProxy with timeout public. (Steve Loughran
7878
via tomwhite)
79+
80+
HADOOP-6472. add tokenCache option to GenericOptionsParser for passing
81+
file with secret keys to a map reduce job. (boryas)
7982

8083
OPTIMIZATIONS
8184

src/java/org/apache/hadoop/util/GenericOptionsParser.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ private static Options buildGeneralOptions(Options opts) {
233233
.withDescription("comma separated archives to be unarchived" +
234234
" on the compute machines.")
235235
.create("archives");
236+
237+
// file with security tokens
238+
Option tokensFile = OptionBuilder.withArgName("tokensFile")
239+
.hasArg()
240+
.withDescription("name of the file with the tokens")
241+
.create("tokenCacheFile");
236242

237243
opts.addOption(fs);
238244
opts.addOption(jt);
@@ -241,6 +247,7 @@ private static Options buildGeneralOptions(Options opts) {
241247
opts.addOption(libjars);
242248
opts.addOption(files);
243249
opts.addOption(archives);
250+
opts.addOption(tokensFile);
244251

245252
return opts;
246253
}
@@ -295,6 +302,19 @@ private void processGeneralOptions(Configuration conf,
295302
}
296303
}
297304
conf.setBoolean("mapred.used.genericoptionsparser", true);
305+
306+
// tokensFile
307+
if(line.hasOption("tokenCacheFile")) {
308+
String fileName = line.getOptionValue("tokenCacheFile");
309+
// check if the local file exists
310+
FileSystem localFs = FileSystem.getLocal(conf);
311+
Path p = new Path(fileName);
312+
if (!localFs.exists(p)) {
313+
throw new FileNotFoundException("File "+fileName+" does not exist.");
314+
}
315+
LOG.debug("setting conf tokensFile: " + fileName);
316+
conf.set("tokenCacheFile", localFs.makeQualified(p).toString());
317+
}
298318
}
299319

300320
/**

src/test/core/org/apache/hadoop/util/TestGenericOptionsParser.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@
1919

2020
import java.io.File;
2121
import java.io.FileNotFoundException;
22+
import java.io.IOException;
2223
import java.net.URI;
2324

25+
import junit.framework.TestCase;
26+
2427
import org.apache.hadoop.conf.Configuration;
2528
import org.apache.hadoop.fs.FileSystem;
2629
import org.apache.hadoop.fs.Path;
2730

28-
import junit.framework.TestCase;
29-
3031
public class TestGenericOptionsParser extends TestCase {
31-
private static File testDir =
32-
new File(System.getProperty("test.build.data", "/tmp"), "generic");
32+
File testDir;
33+
Configuration conf;
34+
FileSystem localFs;
35+
3336

3437
public void testFilesOption() throws Exception {
35-
Configuration conf = new Configuration();
3638
File tmpFile = new File(testDir, "tmpfile");
37-
FileSystem localFs = FileSystem.getLocal(conf);
3839
Path tmpPath = new Path(tmpFile.toString());
3940
localFs.create(tmpPath);
4041
String[] args = new String[2];
@@ -74,7 +75,62 @@ public void testFilesOption() throws Exception {
7475
th instanceof FileNotFoundException);
7576
files = conf2.get("tmpfiles");
7677
assertNull("files is not null", files);
77-
testDir.delete();
78+
}
79+
80+
@Override
81+
protected void setUp() throws Exception {
82+
super.setUp();
83+
conf = new Configuration();
84+
localFs = FileSystem.getLocal(conf);
85+
testDir = new File(System.getProperty("test.build.data", "/tmp"), "generic");
86+
if(testDir.exists())
87+
localFs.delete(new Path(testDir.toString()), true);
7888
}
7989

90+
@Override
91+
protected void tearDown() throws Exception {
92+
super.tearDown();
93+
if(testDir.exists()) {
94+
localFs.delete(new Path(testDir.toString()), true);
95+
}
96+
}
97+
98+
/**
99+
* testing -fileCache option
100+
* @throws IOException
101+
*/
102+
public void testTokenCacheOption() throws IOException {
103+
FileSystem localFs = FileSystem.getLocal(conf);
104+
105+
File tmpFile = new File(testDir, "tokenCacheFile");
106+
if(tmpFile.exists()) {
107+
tmpFile.delete();
108+
}
109+
String[] args = new String[2];
110+
// pass a files option
111+
args[0] = "-tokenCacheFile";
112+
args[1] = tmpFile.toString();
113+
114+
// test non existing file
115+
Throwable th = null;
116+
try {
117+
new GenericOptionsParser(conf, args);
118+
} catch (Exception e) {
119+
th = e;
120+
}
121+
assertNotNull(th);
122+
assertTrue("FileNotFoundException is not thrown",
123+
th instanceof FileNotFoundException);
124+
125+
// create file
126+
Path tmpPath = new Path(tmpFile.toString());
127+
localFs.create(tmpPath);
128+
new GenericOptionsParser(conf, args);
129+
String fileName = conf.get("tokenCacheFile");
130+
assertNotNull("files is null", fileName);
131+
assertEquals("files option does not match",
132+
localFs.makeQualified(tmpPath).toString(), fileName);
133+
134+
localFs.delete(new Path(testDir.getAbsolutePath()), true);
135+
}
80136
}

0 commit comments

Comments
 (0)