Skip to content

Commit 9b1e178

Browse files
committed
PR comments
1 parent 714fbda commit 9b1e178

File tree

4 files changed

+27
-75
lines changed

4 files changed

+27
-75
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static void bootstrap(
122122
suppressFailureLogClasses
123123
);
124124
exportInitializationToAgent();
125-
loadAgent(findAgentJar());
125+
loadAgent(findAgentJar(), EntitlementInitialization.class.getName());
126126
}
127127

128128
private static Path getUserHome() {
@@ -134,11 +134,11 @@ private static Path getUserHome() {
134134
}
135135

136136
@SuppressForbidden(reason = "The VirtualMachine API is the only way to attach a java agent dynamically")
137-
private static void loadAgent(String agentPath) {
137+
static void loadAgent(String agentPath, String entitlementInitializationClassName) {
138138
try {
139139
VirtualMachine vm = VirtualMachine.attach(Long.toString(ProcessHandle.current().pid()));
140140
try {
141-
vm.loadAgent(agentPath, EntitlementInitialization.class.getName());
141+
vm.loadAgent(agentPath, entitlementInitializationClassName);
142142
} finally {
143143
vm.detach();
144144
}

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public class EntitlementInitialization {
3434

3535
private static final Module ENTITLEMENTS_MODULE = PolicyManager.class.getModule();
3636

37-
private static ElasticsearchEntitlementChecker manager;
37+
private static ElasticsearchEntitlementChecker checker;
3838

3939
// Note: referenced by bridge reflectively
4040
public static EntitlementChecker checker() {
41-
return manager;
41+
return checker;
4242
}
4343

4444
/**
@@ -61,18 +61,7 @@ public static EntitlementChecker checker() {
6161
* @param inst the JVM instrumentation class instance
6262
*/
6363
public static void initialize(Instrumentation inst) throws Exception {
64-
manager = initChecker();
65-
66-
var verifyBytecode = Booleans.parseBoolean(System.getProperty("es.entitlements.verify_bytecode", "false"));
67-
if (verifyBytecode) {
68-
ensureClassesSensitiveToVerificationAreInitialized();
69-
}
70-
71-
DynamicInstrumentation.initialize(
72-
inst,
73-
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
74-
verifyBytecode
75-
);
64+
checker = initChecker(inst, createPolicyManager());
7665
}
7766

7867
private static PolicyManager createPolicyManager() {
@@ -112,9 +101,7 @@ private static void ensureClassesSensitiveToVerificationAreInitialized() {
112101
}
113102
}
114103

115-
private static ElasticsearchEntitlementChecker initChecker() {
116-
final PolicyManager policyManager = createPolicyManager();
117-
104+
static ElasticsearchEntitlementChecker initChecker(Instrumentation inst, PolicyManager policyManager) throws Exception {
118105
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
119106
ElasticsearchEntitlementChecker.class,
120107
Runtime.version().feature()
@@ -126,10 +113,25 @@ private static ElasticsearchEntitlementChecker initChecker() {
126113
} catch (NoSuchMethodException e) {
127114
throw new AssertionError("entitlement impl is missing no arg constructor", e);
128115
}
116+
117+
ElasticsearchEntitlementChecker checker;
129118
try {
130-
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
119+
checker = (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
131120
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
132121
throw new AssertionError(e);
133122
}
123+
124+
var verifyBytecode = Booleans.parseBoolean(System.getProperty("es.entitlements.verify_bytecode", "false"));
125+
if (verifyBytecode) {
126+
ensureClassesSensitiveToVerificationAreInitialized();
127+
}
128+
129+
DynamicInstrumentation.initialize(
130+
inst,
131+
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
132+
verifyBytecode
133+
);
134+
135+
return checker;
134136
}
135137
}

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementBootstrap.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@
99

1010
package org.elasticsearch.entitlement.bootstrap;
1111

12-
import com.sun.tools.attach.AgentInitializationException;
13-
import com.sun.tools.attach.AgentLoadException;
14-
import com.sun.tools.attach.AttachNotSupportedException;
15-
import com.sun.tools.attach.VirtualMachine;
16-
17-
import org.elasticsearch.core.SuppressForbidden;
1812
import org.elasticsearch.entitlement.initialization.TestEntitlementInitialization;
1913
import org.elasticsearch.logging.LogManager;
2014
import org.elasticsearch.logging.Logger;
2115

22-
import java.io.IOException;
23-
2416
class TestEntitlementBootstrap {
2517

2618
private static final Logger logger = LogManager.getLogger(TestEntitlementBootstrap.class);
@@ -30,20 +22,6 @@ class TestEntitlementBootstrap {
3022
*/
3123
public static void bootstrap() {
3224
logger.debug("Loading entitlement agent");
33-
loadAgent(EntitlementBootstrap.findAgentJar());
34-
}
35-
36-
@SuppressForbidden(reason = "The VirtualMachine API is the only way to attach a java agent dynamically")
37-
private static void loadAgent(String agentPath) {
38-
try {
39-
VirtualMachine vm = VirtualMachine.attach(Long.toString(ProcessHandle.current().pid()));
40-
try {
41-
vm.loadAgent(agentPath, TestEntitlementInitialization.class.getName());
42-
} finally {
43-
vm.detach();
44-
}
45-
} catch (AttachNotSupportedException | IOException | AgentLoadException | AgentInitializationException e) {
46-
throw new IllegalStateException("Unable to attach entitlement agent", e);
47-
}
25+
EntitlementBootstrap.loadAgent(EntitlementBootstrap.findAgentJar(), TestEntitlementInitialization.class.getName());
4826
}
4927
}

test/framework/src/main/java/org/elasticsearch/entitlement/initialization/TestEntitlementInitialization.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.lang.instrument.Instrumentation;
25-
import java.lang.reflect.Constructor;
26-
import java.lang.reflect.InvocationTargetException;
2725
import java.net.URL;
2826
import java.util.ArrayList;
2927
import java.util.HashMap;
@@ -37,20 +35,15 @@
3735
*/
3836
public class TestEntitlementInitialization {
3937

40-
private static ElasticsearchEntitlementChecker manager;
38+
private static ElasticsearchEntitlementChecker checker;
4139

4240
// Note: referenced by bridge reflectively
4341
public static EntitlementChecker checker() {
44-
return manager;
42+
return checker;
4543
}
4644

4745
public static void initialize(Instrumentation inst) throws Exception {
48-
manager = initChecker();
49-
DynamicInstrumentation.initialize(
50-
inst,
51-
EntitlementCheckerUtils.getVersionSpecificCheckerClass(EntitlementChecker.class, Runtime.version().feature()),
52-
false
53-
);
46+
checker = EntitlementInitialization.initChecker(inst, createPolicyManager());
5447
}
5548

5649
private record TestPluginData(String pluginName, boolean isModular, boolean isExternalPlugin) {}
@@ -125,25 +118,4 @@ private static PolicyManager createPolicyManager() {
125118
Set.of()
126119
);
127120
}
128-
129-
private static ElasticsearchEntitlementChecker initChecker() {
130-
final PolicyManager policyManager = createPolicyManager();
131-
132-
final Class<?> clazz = EntitlementCheckerUtils.getVersionSpecificCheckerClass(
133-
ElasticsearchEntitlementChecker.class,
134-
Runtime.version().feature()
135-
);
136-
137-
Constructor<?> constructor;
138-
try {
139-
constructor = clazz.getConstructor(PolicyManager.class);
140-
} catch (NoSuchMethodException e) {
141-
throw new AssertionError("entitlement impl is missing no arg constructor", e);
142-
}
143-
try {
144-
return (ElasticsearchEntitlementChecker) constructor.newInstance(policyManager);
145-
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
146-
throw new AssertionError(e);
147-
}
148-
}
149121
}

0 commit comments

Comments
 (0)