1414import com .sun .tools .attach .AttachNotSupportedException ;
1515import com .sun .tools .attach .VirtualMachine ;
1616
17+ import org .elasticsearch .core .CheckedConsumer ;
18+ import org .elasticsearch .core .CheckedSupplier ;
1719import org .elasticsearch .core .SuppressForbidden ;
1820import org .elasticsearch .entitlement .initialization .EntitlementInitialization ;
1921import 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