Skip to content

Commit 64d9bbd

Browse files
committed
HADOOP-18136. Verify FileUtils.unTar() handling of missing .tar files.
Adds a tarhardened command to check for this
1 parent 99b7a63 commit 64d9bbd

4 files changed

Lines changed: 137 additions & 3 deletions

File tree

src/main/java/help.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static void main(String[] args) {
4444
mkcsv.help();
4545
pathcapability.help();
4646
storediag.help();
47+
tarhardened.help();
4748
tlsinfo.help();
4849

4950
// extras must not refer to the optional classes.

src/main/java/org/apache/hadoop/fs/store/commands/TLSInfo.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
import static org.apache.hadoop.fs.store.diag.OptionSets.TLS_SYSPROPS;
3737

3838
/**
39-
* Print the local hostname.
40-
*
41-
* Prints some performance numbers at the end.
39+
* Print TLS info.
4240
*/
4341
public class TLSInfo extends DiagnosticsEntryPoint {
4442

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.fs.store.commands;
20+
21+
import java.io.File;
22+
import java.util.List;
23+
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import org.apache.hadoop.fs.FileUtil;
28+
import org.apache.hadoop.fs.store.diag.DiagnosticsEntryPoint;
29+
import org.apache.hadoop.util.ToolRunner;
30+
31+
import static org.apache.hadoop.fs.store.CommonParameters.DEFINE;
32+
import static org.apache.hadoop.fs.store.CommonParameters.VERBOSE;
33+
import static org.apache.hadoop.fs.store.CommonParameters.XMLFILE;
34+
import static org.apache.hadoop.fs.store.StoreExitCodes.E_USAGE;
35+
36+
/**
37+
* Checks to see if the tar command is hardened by taking a command line param and trying
38+
* to untar it.
39+
*/
40+
public class TarHardened extends DiagnosticsEntryPoint {
41+
42+
private static final Logger LOG = LoggerFactory.getLogger(TarHardened.class);
43+
44+
public static final String USAGE
45+
= "Usage: tarhardened [filename]\n"
46+
+ optusage(DEFINE, "key=value", "Define a property")
47+
+ optusage(XMLFILE, "file", "XML config file to load")
48+
+ optusage(VERBOSE, "verbose output");
49+
50+
public TarHardened() {
51+
createCommandFormat(1, 1, VERBOSE);
52+
addValueOptions(XMLFILE, DEFINE);
53+
}
54+
55+
@Override
56+
public int run(String[] args) throws Exception {
57+
List<String> paths = parseArgs(args);
58+
if (paths.size() > 1) {
59+
// too many entries
60+
errorln(USAGE);
61+
return E_USAGE;
62+
}
63+
String filename;
64+
if (paths.isEmpty()) {
65+
File tar = File.createTempFile("tarhardened", ".tgz");
66+
tar.delete();
67+
filename = tar.getAbsolutePath() + "; true";
68+
} else {
69+
filename = paths.get(0);
70+
}
71+
File tmpdir = File.createTempFile("tarhardened-dir", "");
72+
tmpdir.delete();
73+
final File source = new File(filename);
74+
println("Attempting to untar file with name \"%s\"", source);
75+
FileUtil.unTar(source, tmpdir);
76+
println("untar operation reported success");
77+
println();
78+
return 0;
79+
}
80+
81+
/**
82+
* Execute the command, return the result or throw an exception,
83+
* as appropriate.
84+
* @param args argument varags.
85+
* @return return code
86+
* @throws Exception failure
87+
*/
88+
public static int exec(String... args) throws Exception {
89+
return ToolRunner.run(new TarHardened(), args);
90+
}
91+
92+
/**
93+
* Main entry point. Calls {@code System.exit()} on all execution paths.
94+
* @param args argument list
95+
*/
96+
public static void main(String[] args) {
97+
try {
98+
exit(exec(args), "");
99+
} catch (Throwable e) {
100+
exitOnThrowable(e);
101+
}
102+
}
103+
104+
}

src/main/java/tarhardened.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import org.apache.hadoop.fs.store.commands.Command;
20+
import org.apache.hadoop.fs.store.commands.TarHardened;
21+
22+
public class tarhardened extends Command {
23+
24+
public static void main(String[] args) throws Exception {
25+
TarHardened.main(args);
26+
}
27+
28+
public static void help() {
29+
printCommand("tarhardened\t", "Validate tar hardening");
30+
}
31+
}

0 commit comments

Comments
 (0)