Skip to content

Commit 986236a

Browse files
committed
Add ComponentKind enum
1 parent a3fd00b commit 986236a

File tree

4 files changed

+87
-66
lines changed

4 files changed

+87
-66
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,66 @@
5858
import static java.util.zip.ZipFile.OPEN_DELETE;
5959
import static java.util.zip.ZipFile.OPEN_READ;
6060
import static org.elasticsearch.entitlement.bridge.Util.NO_CLASS;
61+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.APM_AGENT;
62+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.PLUGIN;
63+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.SERVER;
64+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.UNKNOWN;
6165

6266
public class PolicyManager {
6367
/**
6468
* Use this if you don't have a {@link ModuleEntitlements} in hand.
6569
*/
6670
private static final Logger generalLogger = LogManager.getLogger(PolicyManager.class);
6771

68-
public static final String UNKNOWN_COMPONENT_NAME = "(unknown)";
69-
public static final String SERVER_COMPONENT_NAME = "(server)";
70-
public static final String APM_AGENT_COMPONENT_NAME = "(APM agent)";
71-
7272
static final Class<?> DEFAULT_FILESYSTEM_CLASS = PathUtils.getDefaultFileSystem().getClass();
7373

7474
static final Set<String> MODULES_EXCLUDED_FROM_SYSTEM_MODULES = Set.of("java.desktop");
7575

7676
/**
7777
* Identifies a particular entitlement {@link Scope} within a {@link Policy}.
78-
* @param componentName
79-
* @param moduleName
8078
*/
81-
public record PolicyScope(String componentName, String moduleName) {
79+
public record PolicyScope(ComponentKind kind, String componentName, String moduleName) {
8280
public PolicyScope {
81+
requireNonNull(kind);
8382
requireNonNull(componentName);
8483
requireNonNull(moduleName);
84+
assert kind.componentName == null || kind.componentName.equals(componentName);
85+
}
86+
87+
public static PolicyScope unknown(String moduleName) {
88+
return new PolicyScope(UNKNOWN, UNKNOWN.componentName, moduleName);
89+
}
90+
91+
public static PolicyScope server(String moduleName) {
92+
return new PolicyScope(SERVER, SERVER.componentName, moduleName);
93+
}
94+
95+
public static PolicyScope apmAgent(String moduleName) {
96+
return new PolicyScope(APM_AGENT, APM_AGENT.componentName, moduleName);
97+
}
98+
99+
public static PolicyScope plugin(String componentName, String moduleName) {
100+
return new PolicyScope(PLUGIN, componentName, moduleName);
101+
}
102+
}
103+
104+
public enum ComponentKind {
105+
UNKNOWN("(unknown)"),
106+
SERVER("(server)"),
107+
APM_AGENT("(APM agent)"),
108+
PLUGIN(null);
109+
110+
/**
111+
* If this kind corresponds to a single component, this is that component's name;
112+
* otherwise null.
113+
*/
114+
public final String componentName;
115+
116+
ComponentKind(String componentName) {
117+
this.componentName = componentName;
85118
}
86119
}
87120

88-
/**
89-
* @param componentName the plugin name; or else one of the special component names
90-
* like {@link #SERVER_COMPONENT_NAME} or {@link #APM_AGENT_COMPONENT_NAME}.
91-
*/
92121
record ModuleEntitlements(
93122
String componentName,
94123
Map<Class<? extends Entitlement>, List<Entitlement>> entitlementsByType,
@@ -215,9 +244,9 @@ public PolicyManager(
215244

216245
List<ExclusiveFileEntitlement> exclusiveFileEntitlements = new ArrayList<>();
217246
for (var e : serverEntitlements.entrySet()) {
218-
validateEntitlementsPerModule(SERVER_COMPONENT_NAME, e.getKey(), e.getValue(), exclusiveFileEntitlements);
247+
validateEntitlementsPerModule(SERVER.componentName, e.getKey(), e.getValue(), exclusiveFileEntitlements);
219248
}
220-
validateEntitlementsPerModule(APM_AGENT_COMPONENT_NAME, ALL_UNNAMED, apmAgentEntitlements, exclusiveFileEntitlements);
249+
validateEntitlementsPerModule(APM_AGENT.componentName, ALL_UNNAMED, apmAgentEntitlements, exclusiveFileEntitlements);
221250
for (var p : pluginsEntitlements.entrySet()) {
222251
for (var m : p.getValue().entrySet()) {
223252
validateEntitlementsPerModule(p.getKey(), m.getKey(), m.getValue(), exclusiveFileEntitlements);
@@ -614,31 +643,29 @@ private ModuleEntitlements computeEntitlements(Class<?> requestingClass) {
614643
var componentName = policyScope.componentName();
615644
var moduleName = policyScope.moduleName();
616645

617-
switch (componentName) {
618-
case SERVER_COMPONENT_NAME -> {
646+
switch (policyScope.kind()) {
647+
case SERVER -> {
619648
return getModuleScopeEntitlements(
620649
serverEntitlements,
621650
moduleName,
622-
SERVER_COMPONENT_NAME,
651+
SERVER.componentName,
623652
getComponentPathFromClass(requestingClass)
624653
);
625654
}
626-
case APM_AGENT_COMPONENT_NAME -> {
655+
case APM_AGENT -> {
627656
// The APM agent is the only thing running non-modular in the system classloader
628657
return policyEntitlements(
629-
APM_AGENT_COMPONENT_NAME,
658+
APM_AGENT.componentName,
630659
getComponentPathFromClass(requestingClass),
631660
ALL_UNNAMED,
632661
apmAgentEntitlements
633662
);
634663
}
635-
case UNKNOWN_COMPONENT_NAME -> {
636-
return defaultEntitlements(UNKNOWN_COMPONENT_NAME, null, moduleName);
664+
case UNKNOWN -> {
665+
return defaultEntitlements(UNKNOWN.componentName, null, moduleName);
637666
}
638667
default -> {
639-
// Must be a plugin
640-
assert componentName.startsWith("(") == false
641-
: "Parentheses indicate a special component name that isn't a plugin: " + componentName;
668+
assert policyScope.kind() == PLUGIN;
642669
var pluginEntitlements = pluginsEntitlements.get(componentName);
643670
if (pluginEntitlements == null) {
644671
return defaultEntitlements(componentName, sourcePaths.get(componentName), moduleName);

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/PolicyManagerTests.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939

4040
import static java.util.Map.entry;
4141
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ALL_UNNAMED;
42-
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.APM_AGENT_COMPONENT_NAME;
43-
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.SERVER_COMPONENT_NAME;
42+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.SERVER;
4443
import static org.hamcrest.Matchers.aMapWithSize;
4544
import static org.hamcrest.Matchers.allOf;
4645
import static org.hamcrest.Matchers.containsString;
@@ -91,7 +90,7 @@ public void testGetEntitlementsThrowsOnMissingPluginUnnamedModule() {
9190
createEmptyTestServerPolicy(),
9291
List.of(),
9392
Map.of("plugin1", createPluginPolicy("plugin.module")),
94-
c -> new PolicyScope("plugin1", moduleName(c)),
93+
c -> PolicyScope.plugin("plugin1", moduleName(c)),
9594
Map.of("plugin1", plugin1SourcePath),
9695
NO_ENTITLEMENTS_MODULE,
9796
TEST_PATH_LOOKUP,
@@ -120,7 +119,7 @@ public void testGetEntitlementsThrowsOnMissingPolicyForPlugin() {
120119
createEmptyTestServerPolicy(),
121120
List.of(),
122121
Map.of(),
123-
c -> new PolicyScope("plugin1", moduleName(c)),
122+
c -> PolicyScope.plugin("plugin1", moduleName(c)),
124123
Map.of("plugin1", plugin1SourcePath),
125124
NO_ENTITLEMENTS_MODULE,
126125
TEST_PATH_LOOKUP,
@@ -149,7 +148,7 @@ public void testGetEntitlementsFailureIsCached() {
149148
createEmptyTestServerPolicy(),
150149
List.of(),
151150
Map.of(),
152-
c -> new PolicyScope("plugin1", moduleName(c)),
151+
c -> PolicyScope.plugin("plugin1", moduleName(c)),
153152
Map.of("plugin1", plugin1SourcePath),
154153
NO_ENTITLEMENTS_MODULE,
155154
TEST_PATH_LOOKUP,
@@ -187,7 +186,7 @@ public void testGetEntitlementsReturnsEntitlementsForPluginUnnamedModule() {
187186
createEmptyTestServerPolicy(),
188187
List.of(),
189188
Map.ofEntries(entry("plugin2", createPluginPolicy(ALL_UNNAMED))),
190-
c -> new PolicyScope("plugin2", moduleName(c)),
189+
c -> PolicyScope.plugin("plugin2", moduleName(c)),
191190
Map.of("plugin2", Path.of("modules", "plugin2")),
192191
NO_ENTITLEMENTS_MODULE,
193192
TEST_PATH_LOOKUP,
@@ -206,7 +205,7 @@ public void testGetEntitlementsReturnsDefaultOnMissingPolicyForServer() throws C
206205
createTestServerPolicy("example"),
207206
List.of(),
208207
Map.of(),
209-
c -> new PolicyScope(SERVER_COMPONENT_NAME, moduleName(c)),
208+
c -> PolicyScope.server(moduleName(c)),
210209
Map.of(),
211210
NO_ENTITLEMENTS_MODULE,
212211
TEST_PATH_LOOKUP,
@@ -222,12 +221,12 @@ public void testGetEntitlementsReturnsDefaultOnMissingPolicyForServer() throws C
222221

223222
assertEquals(
224223
"No policy for this module in server",
225-
policyManager.defaultEntitlements(SERVER_COMPONENT_NAME, mockServerSourcePath, httpserverModuleName),
224+
policyManager.defaultEntitlements(SERVER.componentName, mockServerSourcePath, httpserverModuleName),
226225
policyManager.getEntitlements(mockServerClass)
227226
);
228227

229228
assertEquals(
230-
Map.of(requestingModule, policyManager.defaultEntitlements(SERVER_COMPONENT_NAME, mockServerSourcePath, httpserverModuleName)),
229+
Map.of(requestingModule, policyManager.defaultEntitlements(SERVER.componentName, mockServerSourcePath, httpserverModuleName)),
231230
policyManager.moduleEntitlementsMap
232231
);
233232
}
@@ -238,7 +237,7 @@ public void testGetEntitlementsReturnsEntitlementsForServerModule() throws Class
238237
createTestServerPolicy(httpserverModuleName),
239238
List.of(),
240239
Map.of(),
241-
c -> new PolicyScope(SERVER_COMPONENT_NAME, moduleName(c)),
240+
c -> PolicyScope.server(moduleName(c)),
242241
Map.of(),
243242
NO_ENTITLEMENTS_MODULE,
244243
TEST_PATH_LOOKUP,
@@ -263,7 +262,7 @@ public void testGetEntitlementsReturnsEntitlementsForPluginModule() throws IOExc
263262
createEmptyTestServerPolicy(),
264263
List.of(),
265264
Map.of("mock-plugin", createPluginPolicy("org.example.plugin")),
266-
c -> new PolicyScope("mock-plugin", moduleName(c)),
265+
c -> PolicyScope.plugin("mock-plugin", moduleName(c)),
267266
Map.of("mock-plugin", Path.of("modules", "mock-plugin")),
268267
NO_ENTITLEMENTS_MODULE,
269268
TEST_PATH_LOOKUP,
@@ -283,7 +282,7 @@ public void testGetEntitlementsResultIsCached() {
283282
createEmptyTestServerPolicy(),
284283
List.of(),
285284
Map.ofEntries(entry("plugin2", createPluginPolicy(ALL_UNNAMED))),
286-
c -> new PolicyScope("plugin2", moduleName(c)),
285+
c -> PolicyScope.plugin("plugin2", moduleName(c)),
287286
Map.of("plugin2", Path.of("modules", "plugin2")),
288287
NO_ENTITLEMENTS_MODULE,
289288
TEST_PATH_LOOKUP,
@@ -347,8 +346,8 @@ public void testAgentsEntitlements() throws IOException, ClassNotFoundException
347346
List.of(new CreateClassLoaderEntitlement()),
348347
Map.of(),
349348
c -> c.getPackageName().startsWith(TEST_AGENTS_PACKAGE_NAME)
350-
? new PolicyScope(APM_AGENT_COMPONENT_NAME, "test.agent.module")
351-
: new PolicyScope("test", "test.plugin.module"),
349+
? PolicyScope.apmAgent("test.agent.module")
350+
: PolicyScope.plugin("test", "test.plugin.module"),
352351
Map.of(),
353352
NO_ENTITLEMENTS_MODULE,
354353
TEST_PATH_LOOKUP,
@@ -377,7 +376,7 @@ public void testDuplicateEntitlements() {
377376
),
378377
List.of(),
379378
Map.of(),
380-
c -> new PolicyScope("test", moduleName(c)),
379+
c -> PolicyScope.plugin("test", moduleName(c)),
381380
Map.of(),
382381
NO_ENTITLEMENTS_MODULE,
383382
TEST_PATH_LOOKUP,
@@ -395,7 +394,7 @@ public void testDuplicateEntitlements() {
395394
createEmptyTestServerPolicy(),
396395
List.of(new CreateClassLoaderEntitlement(), new CreateClassLoaderEntitlement()),
397396
Map.of(),
398-
c -> new PolicyScope("test", moduleName(c)),
397+
c -> PolicyScope.plugin("test", moduleName(c)),
399398
Map.of(),
400399
NO_ENTITLEMENTS_MODULE,
401400
TEST_PATH_LOOKUP,
@@ -433,7 +432,7 @@ public void testDuplicateEntitlements() {
433432
)
434433
)
435434
),
436-
c -> new PolicyScope("plugin1", moduleName(c)),
435+
c -> PolicyScope.plugin("plugin1", moduleName(c)),
437436
Map.of("plugin1", Path.of("modules", "plugin1")),
438437
NO_ENTITLEMENTS_MODULE,
439438
TEST_PATH_LOOKUP,
@@ -485,7 +484,7 @@ public void testFilesEntitlementsWithExclusive() {
485484
)
486485
)
487486
),
488-
c -> new PolicyScope("", moduleName(c)),
487+
c -> PolicyScope.plugin("", moduleName(c)),
489488
Map.of("plugin1", Path.of("modules", "plugin1"), "plugin2", Path.of("modules", "plugin2")),
490489
NO_ENTITLEMENTS_MODULE,
491490
TEST_PATH_LOOKUP,
@@ -538,7 +537,7 @@ public void testFilesEntitlementsWithExclusive() {
538537
)
539538
)
540539
),
541-
c -> new PolicyScope("", moduleName(c)),
540+
c -> PolicyScope.plugin("", moduleName(c)),
542541
Map.of(),
543542
NO_ENTITLEMENTS_MODULE,
544543
TEST_PATH_LOOKUP,
@@ -564,7 +563,7 @@ public void testPluginResolverOverridesAgents() {
564563
createEmptyTestServerPolicy(),
565564
List.of(new CreateClassLoaderEntitlement()),
566565
Map.of(),
567-
c -> new PolicyScope("test", moduleName(c)), // Insist that the class is in a plugin
566+
c -> PolicyScope.plugin("test", moduleName(c)), // Insist that the class is in a plugin
568567
Map.of(),
569568
NO_ENTITLEMENTS_MODULE,
570569
TEST_PATH_LOOKUP,
@@ -586,7 +585,7 @@ private static PolicyManager policyManager(Module entitlementsModule) {
586585
createEmptyTestServerPolicy(),
587586
List.of(),
588587
Map.of(),
589-
c -> new PolicyScope("test", moduleName(c)),
588+
c -> PolicyScope.plugin("test", moduleName(c)),
590589
Map.of(),
591590
entitlementsModule,
592591
TEST_PATH_LOOKUP,

server/src/main/java/org/elasticsearch/bootstrap/ScopeResolver.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import java.util.stream.Stream;
1818

1919
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ALL_UNNAMED;
20-
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.APM_AGENT_COMPONENT_NAME;
21-
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.SERVER_COMPONENT_NAME;
22-
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.UNKNOWN_COMPONENT_NAME;
2320

2421
public class ScopeResolver {
2522
private final Map<Module, String> pluginNameByModule;
@@ -57,17 +54,17 @@ public PolicyScope resolveClassToScope(Class<?> clazz) {
5754
var module = clazz.getModule();
5855
var scopeName = getScopeName(module);
5956
if (isServerModule(module)) {
60-
return new PolicyScope(SERVER_COMPONENT_NAME, scopeName);
57+
return PolicyScope.server(scopeName);
6158
}
6259
String pluginName = pluginNameByModule.get(module);
6360
if (pluginName != null) {
64-
return new PolicyScope(pluginName, scopeName);
61+
return PolicyScope.plugin(pluginName, scopeName);
6562
}
6663
if (module.isNamed() == false && clazz.getPackageName().startsWith(apmAgentPackageName)) {
6764
// The APM agent is the only thing running non-modular in the system classloader
68-
return new PolicyScope(APM_AGENT_COMPONENT_NAME, ALL_UNNAMED);
65+
return PolicyScope.apmAgent(ALL_UNNAMED);
6966
}
70-
return new PolicyScope(UNKNOWN_COMPONENT_NAME, scopeName);
67+
return PolicyScope.unknown(scopeName);
7168
}
7269

7370
private static boolean isServerModule(Module requestingModule) {

0 commit comments

Comments
 (0)