Skip to content

Commit b61ffc4

Browse files
prdoylemridula-s109
authored andcommitted
Initial TestPolicyManager implementation (elastic#128700)
* Initial TestPolicyManager implementation * The forbidden APIs check is not messing around
1 parent cb651cf commit b61ffc4

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,21 @@ boolean isTriviallyAllowed(Class<?> requestingClass) {
376376
generalLogger.debug("Entitlement trivially allowed from outermost frame");
377377
return true;
378378
}
379-
if (SYSTEM_LAYER_MODULES.contains(requestingClass.getModule())) {
379+
if (isTrustedSystemClass(requestingClass)) {
380380
generalLogger.debug("Entitlement trivially allowed from system module [{}]", requestingClass.getModule().getName());
381381
return true;
382382
}
383383
generalLogger.trace("Entitlement not trivially allowed");
384384
return false;
385385
}
386386

387+
/**
388+
* The main decision point for what counts as a trusted built-in JDK class.
389+
*/
390+
protected boolean isTrustedSystemClass(Class<?> requestingClass) {
391+
return SYSTEM_LAYER_MODULES.contains(requestingClass.getModule());
392+
}
393+
387394
@Override
388395
public String toString() {
389396
return "PolicyManager{" + "serverEntitlements=" + serverEntitlements + ", pluginsEntitlements=" + pluginsEntitlements + '}';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.entitlement.runtime.policy.Policy;
2121
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
2222
import org.elasticsearch.entitlement.runtime.policy.PolicyParser;
23+
import org.elasticsearch.entitlement.runtime.policy.TestPolicyManager;
2324
import org.elasticsearch.plugins.PluginDescriptor;
2425

2526
import java.io.IOException;
@@ -109,15 +110,14 @@ private static PolicyManager createPolicyManager(PathLookup pathLookup) throws I
109110

110111
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
111112

112-
PolicyManager policyManager = new PolicyManager(
113+
return new TestPolicyManager(
113114
HardcodedEntitlements.serverPolicy(null, null),
114115
HardcodedEntitlements.agentEntitlements(),
115116
pluginPolicies,
116117
scopeResolver,
117118
Map.of(),
118119
pathLookup
119120
);
120-
throw new IllegalStateException("Not yet implemented!");
121121
}
122122

123123
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.entitlement.runtime.policy;
11+
12+
import java.nio.file.Path;
13+
import java.util.stream.Stream;
14+
15+
public class TestPathLookup implements PathLookup {
16+
@Override
17+
public Path pidFile() {
18+
return null;
19+
}
20+
21+
@Override
22+
public Stream<Path> getBaseDirPaths(BaseDir baseDir) {
23+
return Stream.empty();
24+
}
25+
26+
@Override
27+
public Stream<Path> resolveRelativePaths(BaseDir baseDir, Path relativePath) {
28+
return Stream.empty();
29+
}
30+
31+
@Override
32+
public Stream<Path> resolveSettingPaths(BaseDir baseDir, String settingName) {
33+
return Stream.empty();
34+
}
35+
36+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.entitlement.runtime.policy;
11+
12+
import org.elasticsearch.entitlement.runtime.policy.entitlements.Entitlement;
13+
14+
import java.nio.file.Path;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.function.Function;
18+
19+
public class TestPolicyManager extends PolicyManager {
20+
public TestPolicyManager(
21+
Policy serverPolicy,
22+
List<Entitlement> apmAgentEntitlements,
23+
Map<String, Policy> pluginPolicies,
24+
Function<Class<?>, PolicyScope> scopeResolver,
25+
Map<String, Path> sourcePaths,
26+
PathLookup pathLookup
27+
) {
28+
super(serverPolicy, apmAgentEntitlements, pluginPolicies, scopeResolver, sourcePaths, pathLookup);
29+
}
30+
31+
/**
32+
* Called between tests so each test is not affected by prior tests
33+
*/
34+
public void reset() {
35+
super.moduleEntitlementsMap.clear();
36+
}
37+
38+
@Override
39+
protected boolean isTrustedSystemClass(Class<?> requestingClass) {
40+
ClassLoader loader = requestingClass.getClassLoader();
41+
return loader == null || loader == ClassLoader.getPlatformClassLoader();
42+
}
43+
44+
@Override
45+
boolean isTriviallyAllowed(Class<?> requestingClass) {
46+
return isTestFrameworkClass(requestingClass) || isEntitlementClass(requestingClass) || super.isTriviallyAllowed(requestingClass);
47+
}
48+
49+
private boolean isEntitlementClass(Class<?> requestingClass) {
50+
return requestingClass.getPackageName().startsWith("org.elasticsearch.entitlement")
51+
&& (requestingClass.getName().contains("Test") == false);
52+
}
53+
54+
private boolean isTestFrameworkClass(Class<?> requestingClass) {
55+
String packageName = requestingClass.getPackageName();
56+
return packageName.startsWith("org.junit") || packageName.startsWith("org.gradle");
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.entitlement.runtime.policy;
11+
12+
import org.elasticsearch.entitlement.runtime.policy.PolicyManager.PolicyScope;
13+
import org.elasticsearch.test.ESTestCase;
14+
import org.junit.Before;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.concurrent.atomic.AtomicInteger;
19+
20+
import static org.elasticsearch.entitlement.runtime.policy.PolicyManager.ComponentKind.PLUGIN;
21+
22+
public class TestPolicyManagerTests extends ESTestCase {
23+
TestPolicyManager policyManager;
24+
25+
@Before
26+
public void setupPolicyManager() {
27+
AtomicInteger scopeCounter = new AtomicInteger(0);
28+
policyManager = new TestPolicyManager(
29+
new Policy("empty", List.of()),
30+
List.of(),
31+
Map.of(),
32+
c -> new PolicyScope(PLUGIN, "example-plugin" + scopeCounter.incrementAndGet(), "org.example.module"),
33+
Map.of(),
34+
new TestPathLookup()
35+
);
36+
}
37+
38+
public void testReset() {
39+
assertTrue(policyManager.moduleEntitlementsMap.isEmpty());
40+
assertEquals("example-plugin1", policyManager.getEntitlements(getClass()).componentName());
41+
assertEquals("example-plugin1", policyManager.getEntitlements(getClass()).componentName());
42+
assertFalse(policyManager.moduleEntitlementsMap.isEmpty());
43+
44+
policyManager.reset();
45+
46+
assertTrue(policyManager.moduleEntitlementsMap.isEmpty());
47+
assertEquals("example-plugin2", policyManager.getEntitlements(getClass()).componentName());
48+
assertEquals("example-plugin2", policyManager.getEntitlements(getClass()).componentName());
49+
assertFalse(policyManager.moduleEntitlementsMap.isEmpty());
50+
}
51+
52+
public void testIsTriviallyAllowed() {
53+
assertTrue(policyManager.isTriviallyAllowed(String.class));
54+
assertTrue(policyManager.isTriviallyAllowed(org.junit.Before.class));
55+
assertTrue(policyManager.isTriviallyAllowed(PolicyManager.class));
56+
57+
assertFalse(policyManager.isTriviallyAllowed(getClass()));
58+
}
59+
}

0 commit comments

Comments
 (0)