Skip to content

Commit 3d71f74

Browse files
authored
Fail startup if entitlement instrumentation failed (#130051) (#130059)
Java class transformers swallow exceptions, so any instrumentation failures, for example due to a java version mismatch, will silently proceed with startup, which then will cryptically fail the entitlement self test. This commit logs exceptions that occur during instrumentation, as well as plumb through the fact that any occured so that bootstrap can fail rather than allow startup to proceed.
1 parent f0c0ac0 commit 3d71f74

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static void bootstrap(
9898
);
9999
exportInitializationToAgent();
100100
loadAgent(findAgentJar(), EntitlementInitialization.class.getName());
101+
102+
if (EntitlementInitialization.getError() != null) {
103+
throw EntitlementInitialization.getError();
104+
}
101105
}
102106

103107
private static Path getUserHome() {

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/DynamicInstrumentation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ static void initialize(Instrumentation inst, Class<?> checkerInterface, boolean
117117
// We should have failed already in the loop above, but just in case we did not, rethrow.
118118
throw e;
119119
}
120+
121+
if (transformer.hadErrors()) {
122+
throw new RuntimeException("Failed to transform JDK classes for entitlements");
123+
}
120124
}
121125

122126
private static Map<MethodKey, CheckMethod> getMethodsToInstrument(Class<?> checkerInterface) throws ClassNotFoundException,

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1717
import org.elasticsearch.entitlement.runtime.policy.PolicyCheckerImpl;
1818
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
19+
import org.elasticsearch.logging.LogManager;
20+
import org.elasticsearch.logging.Logger;
1921

2022
import java.lang.instrument.Instrumentation;
2123
import java.lang.reflect.Constructor;
2224
import java.lang.reflect.InvocationTargetException;
2325
import java.util.Set;
26+
import java.util.concurrent.atomic.AtomicReference;
2427

2528
import static java.util.Objects.requireNonNull;
2629

@@ -32,17 +35,26 @@
3235
* to begin injecting our instrumentation.
3336
*/
3437
public class EntitlementInitialization {
38+
private static final Logger logger = LogManager.getLogger(EntitlementInitialization.class);
3539

3640
private static final Module ENTITLEMENTS_MODULE = PolicyManager.class.getModule();
3741

3842
public static InitializeArgs initializeArgs;
3943
private static ElasticsearchEntitlementChecker checker;
44+
private static AtomicReference<RuntimeException> error = new AtomicReference<>();
4045

4146
// Note: referenced by bridge reflectively
4247
public static EntitlementChecker checker() {
4348
return checker;
4449
}
4550

51+
/**
52+
* Return any exception that occurred during initialization
53+
*/
54+
public static RuntimeException getError() {
55+
return error.get();
56+
}
57+
4658
/**
4759
* Initializes the Entitlement system:
4860
* <ol>
@@ -62,10 +74,16 @@ public static EntitlementChecker checker() {
6274
*
6375
* @param inst the JVM instrumentation class instance
6476
*/
65-
public static void initialize(Instrumentation inst) throws Exception {
66-
// the checker _MUST_ be set before _any_ instrumentation is done
67-
checker = initChecker(initializeArgs.policyManager());
68-
initInstrumentation(inst);
77+
public static void initialize(Instrumentation inst) {
78+
try {
79+
// the checker _MUST_ be set before _any_ instrumentation is done
80+
checker = initChecker(initializeArgs.policyManager());
81+
initInstrumentation(inst);
82+
} catch (Exception e) {
83+
// exceptions thrown within the agent will be swallowed, so capture it here
84+
// instead so that it can be retrieved by bootstrap
85+
error.set(new RuntimeException("Failed to initialize entitlements", e));
86+
}
6987
}
7088

7189
/**

libs/entitlement/src/main/java/org/elasticsearch/entitlement/instrumentation/Transformer.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99

1010
package org.elasticsearch.entitlement.instrumentation;
1111

12+
import org.elasticsearch.logging.LogManager;
13+
import org.elasticsearch.logging.Logger;
14+
1215
import java.lang.instrument.ClassFileTransformer;
1316
import java.security.ProtectionDomain;
1417
import java.util.Set;
18+
import java.util.concurrent.atomic.AtomicBoolean;
1519

1620
/**
1721
* A {@link ClassFileTransformer} that applies an {@link Instrumenter} to the appropriate classes.
1822
*/
1923
public class Transformer implements ClassFileTransformer {
24+
private static final Logger logger = LogManager.getLogger(Transformer.class);
2025
private final Instrumenter instrumenter;
2126
private final Set<String> classesToTransform;
27+
private final AtomicBoolean hadErrors = new AtomicBoolean(false);
2228

2329
private boolean verifyClasses;
2430

@@ -33,6 +39,10 @@ public void enableClassVerification() {
3339
this.verifyClasses = true;
3440
}
3541

42+
public boolean hadErrors() {
43+
return hadErrors.get();
44+
}
45+
3646
@Override
3747
public byte[] transform(
3848
ClassLoader loader,
@@ -42,13 +52,19 @@ public byte[] transform(
4252
byte[] classfileBuffer
4353
) {
4454
if (classesToTransform.contains(className)) {
45-
// System.out.println("Transforming " + className);
46-
return instrumenter.instrumentClass(className, classfileBuffer, verifyClasses);
55+
logger.debug("Transforming " + className);
56+
try {
57+
return instrumenter.instrumentClass(className, classfileBuffer, verifyClasses);
58+
} catch (Throwable t) {
59+
hadErrors.set(true);
60+
logger.error("Failed to instrument class " + className, t);
61+
// throwing an exception from a transformer results in the exception being swallowed,
62+
// effectively the same as returning null anyways, so we instead log it here completely
63+
return null;
64+
}
4765
} else {
48-
// System.out.println("Not transforming " + className);
66+
logger.trace("Not transforming " + className);
4967
return null;
5068
}
5169
}
52-
53-
// private static final Logger LOGGER = LogManager.getLogger(Transformer.class);
5470
}

0 commit comments

Comments
 (0)