Skip to content

Commit 1357446

Browse files
committed
Lambdas instead of booleans
1 parent 7b234c9 commit 1357446

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.core;
11+
12+
/**
13+
* A {@link java.util.function.Supplier}-like interface which allows throwing checked exceptions.
14+
*/
15+
@FunctionalInterface
16+
public interface CheckedSupplier<T, E extends Exception> {
17+
T get() throws E;
18+
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/EntitlementBootstrap.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.sun.tools.attach.AttachNotSupportedException;
1515
import com.sun.tools.attach.VirtualMachine;
1616

17+
import org.elasticsearch.core.CheckedConsumer;
18+
import org.elasticsearch.core.CheckedSupplier;
1719
import org.elasticsearch.core.SuppressForbidden;
1820
import org.elasticsearch.entitlement.initialization.EntitlementInitialization;
1921
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
@@ -146,33 +148,31 @@ private static String findAgentJar() {
146148
* @throws IllegalStateException if the entitlements system can't prevent an unauthorized action of our choosing
147149
*/
148150
private static void selfTest() {
149-
ensureCannotStartProcess(false);
150-
ensureCannotStartProcess(true);
151-
ensureCanCreateTempFile(false);
152-
ensureCanCreateTempFile(true);
151+
ensureCannotStartProcess(ProcessBuilder::start);
152+
ensureCanCreateTempFile(() -> Files.createTempFile(null, null));
153+
154+
// Try again with reflection
155+
ensureCannotStartProcess(pb -> {
156+
try {
157+
var start = ProcessBuilder.class.getMethod("start");
158+
start.invoke(pb);
159+
} catch (InvocationTargetException e) {
160+
throw (Exception)e.getCause();
161+
}
162+
});
163+
ensureCanCreateTempFile(() -> (Path) Files.class.getMethod("createTempFile", String.class, String.class, FileAttribute[].class)
164+
.invoke(null, null, null, new FileAttribute<?>[0]));
153165
}
154166

155-
private static void ensureCannotStartProcess(boolean useReflection) {
167+
private static void ensureCannotStartProcess(CheckedConsumer<ProcessBuilder, ?> startProcess) {
156168
try {
157169
// The command doesn't matter; it doesn't even need to exist
158-
ProcessBuilder builder = new ProcessBuilder("");
159-
if (useReflection) {
160-
try {
161-
var start = ProcessBuilder.class.getMethod("start");
162-
start.invoke(builder);
163-
} catch (InvocationTargetException e) {
164-
throw e.getCause();
165-
}
166-
} else {
167-
builder.start();
168-
}
170+
startProcess.accept(new ProcessBuilder(""));
169171
} catch (NotEntitledException e) {
170172
logger.debug("Success: Entitlement protection correctly prevented process creation");
171173
return;
172-
} catch (IOException e) {
174+
} catch (Exception e) {
173175
throw new IllegalStateException("Failed entitlement protection self-test", e);
174-
} catch (Throwable e) {
175-
throw new IllegalStateException("Error during entitlement protection self-test", e);
176176
}
177177
throw new IllegalStateException("Entitlement protection self-test was incorrectly permitted");
178178
}
@@ -181,15 +181,9 @@ private static void ensureCannotStartProcess(boolean useReflection) {
181181
* Originally {@code Security.selfTest}.
182182
*/
183183
@SuppressForbidden(reason = "accesses jvm default tempdir as a self-test")
184-
private static void ensureCanCreateTempFile(boolean useReflection) {
184+
private static void ensureCanCreateTempFile(CheckedSupplier<Path, ?> createTempFile) {
185185
try {
186-
Path p;
187-
if (useReflection) {
188-
p = (Path) Files.class.getMethod("createTempFile", String.class, String.class, FileAttribute[].class)
189-
.invoke(null, null, null, new FileAttribute<?>[0]);
190-
} else {
191-
p = Files.createTempFile(null, null);
192-
}
186+
Path p = createTempFile.get();
193187
p.toFile().deleteOnExit();
194188

195189
// Make an effort to clean up the file immediately; also, deleteOnExit leaves the file if the JVM exits abnormally.

0 commit comments

Comments
 (0)