Skip to content

Commit 04a9f69

Browse files
author
David Roberts
committed
Revert "Use a private directory for temporary files"
This reverts commit fdbc53b.
1 parent fdbc53b commit 04a9f69

File tree

6 files changed

+5
-178
lines changed

6 files changed

+5
-178
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,11 @@
5656
import java.io.PrintStream;
5757
import java.io.UnsupportedEncodingException;
5858
import java.net.URISyntaxException;
59-
import java.nio.file.Files;
6059
import java.nio.file.Path;
6160
import java.security.NoSuchAlgorithmException;
62-
import java.util.Comparator;
6361
import java.util.List;
6462
import java.util.Collections;
65-
import java.util.Set;
6663
import java.util.concurrent.CountDownLatch;
67-
import java.util.stream.Collectors;
6864

6965
/**
7066
* Internal startup code.
@@ -189,7 +185,6 @@ public void run() {
189185
IOUtils.close(node, spawner);
190186
LoggerContext context = (LoggerContext) LogManager.getContext(false);
191187
Configurator.shutdown(context);
192-
cleanTmpFiles(environment.tmpFile());
193188
} catch (IOException ex) {
194189
throw new ElasticsearchException("failed to stop node", ex);
195190
}
@@ -267,17 +262,9 @@ private void start() throws NodeValidationException {
267262
keepAliveThread.start();
268263
}
269264

270-
/**
271-
* This is used for graceful shutdown on Windows when triggered by
272-
* CTRL-C or the service control manager.
273-
*/
274265
static void stop() throws IOException {
275266
try {
276267
IOUtils.close(INSTANCE.node, INSTANCE.spawner);
277-
// TODO: should the logger be shut down here like it is for the shutdown hook?
278-
if (INSTANCE.node != null) {
279-
cleanTmpFiles(INSTANCE.node.getEnvironment().tmpFile());
280-
}
281268
} finally {
282269
INSTANCE.keepAliveLatch.countDown();
283270
}
@@ -408,10 +395,4 @@ private static void checkLucene() {
408395
}
409396
}
410397

411-
static void cleanTmpFiles(Path tmpFile) throws IOException {
412-
List<Path> toDelete = Files.walk(tmpFile).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
413-
for (Path path : toDelete) {
414-
Files.delete(path);
415-
}
416-
}
417398
}

core/src/main/java/org/elasticsearch/bootstrap/Security.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static void addFilePermissions(Permissions policy, Environment environment) thro
276276
addDirectoryPath(policy, Environment.PATH_HOME_SETTING.getKey(), environment.pluginsFile(), "read,readlink");
277277
addDirectoryPath(policy, "path.conf'", environment.configFile(), "read,readlink");
278278
// read-write dirs
279-
addDirectoryPath(policy, "java.io.tmpdir", environment.systemTmpFile(), "read,readlink,write,delete");
279+
addDirectoryPath(policy, "java.io.tmpdir", environment.tmpFile(), "read,readlink,write,delete");
280280
addDirectoryPath(policy, Environment.PATH_LOGS_SETTING.getKey(), environment.logsFile(), "read,readlink,write,delete");
281281
if (environment.sharedDataFile() != null) {
282282
addDirectoryPath(policy, Environment.PATH_SHARED_DATA_SETTING.getKey(), environment.sharedDataFile(),

core/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.settings.Setting;
2626
import org.elasticsearch.common.settings.Setting.Property;
2727
import org.elasticsearch.common.settings.Settings;
28-
import org.elasticsearch.common.util.set.Sets;
2928

3029
import java.io.IOException;
3130
import java.net.MalformedURLException;
@@ -34,12 +33,9 @@
3433
import java.nio.file.FileStore;
3534
import java.nio.file.Files;
3635
import java.nio.file.Path;
37-
import java.nio.file.attribute.PosixFilePermission;
38-
import java.nio.file.attribute.PosixFilePermissions;
3936
import java.util.Collections;
4037
import java.util.List;
4138
import java.util.Objects;
42-
import java.util.Set;
4339
import java.util.function.Function;
4440

4541
/**
@@ -87,38 +83,7 @@ public class Environment {
8783
private final Path pidFile;
8884

8985
/** Path to the temporary file directory used by the JDK */
90-
private static final Path systemTmpFile = PathUtils.get(System.getProperty("java.io.tmpdir"));
91-
92-
/**
93-
* Path to the temporary file directory used by Elasticsearch.
94-
* This will be a sub-directory that is created under the JDK temporary directory.
95-
*/
96-
private static final Path tmpFile;
97-
98-
/**
99-
* Creates a secure temporary directory whose contents can only be seen by the user
100-
* that Elasticsearch is running as. This is static so that only one such directory
101-
* is created, even when many instances of {@link Environment} are created.
102-
*/
103-
static {
104-
Path createdTmpFile;
105-
try {
106-
try {
107-
// On POSIX file systems everyone can see the contents of the /tmp directory, so create a
108-
// sub-directory under it that only the user running Elasticsearch can list the contents of.
109-
Set<PosixFilePermission> attrs = Sets.newHashSet(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE,
110-
PosixFilePermission.OWNER_EXECUTE);
111-
createdTmpFile = Files.createTempDirectory(systemTmpFile, "elasticsearch", PosixFilePermissions.asFileAttribute(attrs));
112-
} catch (UnsupportedOperationException e) {
113-
// Assume this isn't a POSIX file system. On Windows each user's %TEMP% directory is visible only to
114-
// them and administrators, so the exact permissions of this sub-directory are less important.
115-
createdTmpFile = Files.createTempDirectory(systemTmpFile, "elasticsearch");
116-
}
117-
} catch (IOException e) {
118-
throw new IllegalStateException("Unable to create secure temp directory", e);
119-
}
120-
tmpFile = createdTmpFile;
121-
}
86+
private final Path tmpFile = PathUtils.get(System.getProperty("java.io.tmpdir"));
12287

12388
public Environment(Settings settings) {
12489
this(settings, null);
@@ -324,14 +289,6 @@ public Path pidFile() {
324289
}
325290

326291
/** Path to the default temp directory used by the JDK */
327-
public Path systemTmpFile() {
328-
return systemTmpFile;
329-
}
330-
331-
/**
332-
* Path to the temporary file directory used by Elasticsearch.
333-
* This will be a sub-directory that is created under the JDK temporary directory.
334-
*/
335292
public Path tmpFile() {
336293
return tmpFile;
337294
}
@@ -360,7 +317,4 @@ public static void assertEquivalent(Environment actual, Environment expected) {
360317
private static void assertEquals(Object actual, Object expected, String name) {
361318
assert Objects.deepEquals(actual, expected) : "actual " + name + " [" + actual + "] is different than [ " + expected + "]";
362319
}
363-
364-
// does nothing, just easy way to make sure the class is loaded.
365-
public static void ensureClassLoaded() {}
366320
}

core/src/test/java/org/elasticsearch/bootstrap/BootstrapTests.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.common.settings.SecureSettings;
2525
import org.elasticsearch.common.settings.SecureString;
2626
import org.elasticsearch.common.settings.Settings;
27-
import org.elasticsearch.common.util.set.Sets;
2827
import org.elasticsearch.env.Environment;
2928
import org.elasticsearch.test.ESTestCase;
3029
import org.junit.After;
@@ -34,15 +33,12 @@
3433
import java.nio.file.FileSystem;
3534
import java.nio.file.Files;
3635
import java.nio.file.Path;
37-
import java.nio.file.attribute.PosixFilePermission;
38-
import java.nio.file.attribute.PosixFilePermissions;
3936
import java.util.ArrayList;
4037
import java.util.List;
41-
import java.util.Set;
4238

4339
public class BootstrapTests extends ESTestCase {
44-
private Environment env;
45-
private List<FileSystem> fileSystems = new ArrayList<>();
40+
Environment env;
41+
List<FileSystem> fileSystems = new ArrayList<>();
4642

4743
@After
4844
public void closeMockFileSystems() throws IOException {
@@ -70,24 +66,4 @@ public void testLoadSecureSettings() throws Exception {
7066
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
7167
}
7268
}
73-
74-
public void testCleanTmpFiles() throws IOException {
75-
Path testBaseDir = createTempDir();
76-
Path createdTmpFile;
77-
try {
78-
Set<PosixFilePermission> attrs = Sets.newHashSet(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE,
79-
PosixFilePermission.OWNER_EXECUTE);
80-
createdTmpFile = Files.createTempDirectory(testBaseDir, "BootstrapTests", PosixFilePermissions.asFileAttribute(attrs));
81-
} catch (UnsupportedOperationException e) {
82-
// Assume this isn't a POSIX file system
83-
createdTmpFile = Files.createTempDirectory(testBaseDir, "BootstrapTests");
84-
}
85-
assertTrue(Files.isDirectory(createdTmpFile));
86-
Files.createTempFile(createdTmpFile, "some", "junk");
87-
Files.createTempFile(createdTmpFile, "more", "junk");
88-
Path nested = Files.createTempDirectory(createdTmpFile, "nested");
89-
Files.createTempFile(nested, "nested", "junk");
90-
Bootstrap.cleanTmpFiles(createdTmpFile);
91-
assertFalse(Files.exists(createdTmpFile));
92-
}
9369
}

qa/evil-tests/src/test/java/org/elasticsearch/env/EnvironmentEvilTests.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import org.elasticsearch.common.io.FileSystemUtils;
2929
import org.elasticsearch.common.io.PathUtils;
3030
import org.elasticsearch.common.network.IfConfig;
31-
import org.elasticsearch.env.Environment;
3231
import org.elasticsearch.plugins.PluginInfo;
3332
import org.junit.Assert;
3433

34+
import java.io.FilePermission;
3535
import java.io.InputStream;
3636
import java.net.SocketPermission;
3737
import java.net.URL;
@@ -72,9 +72,6 @@ public class BootstrapForTesting {
7272
"please set ${java.io.tmpdir} in pom.xml"));
7373
try {
7474
Security.ensureDirectoryExists(javaTmpDir);
75-
// This ensures that the secure temp directory under java.io.tmpdir
76-
// exists before we install any mock file systems
77-
Environment.ensureClassLoaded();
7875
} catch (Exception e) {
7976
throw new RuntimeException("unable to create test temp directory", e);
8077
}

0 commit comments

Comments
 (0)