Skip to content

Commit 59f4cd1

Browse files
committed
setup entitlements in bootstrap for testing
1 parent f8b252b commit 59f4cd1

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.core.Booleans;
1717
import org.elasticsearch.core.PathUtils;
18+
import org.elasticsearch.entitlement.runtime.policy.Policy;
19+
import org.elasticsearch.entitlement.runtime.policy.PolicyManager.PolicyScope;
20+
import org.elasticsearch.entitlement.runtime.policy.PolicyParser;
21+
import org.elasticsearch.entitlement.runtime.policy.Scope;
1822
import org.elasticsearch.jdk.JarHell;
1923
import org.elasticsearch.xcontent.ConstructingObjectParser;
2024
import org.elasticsearch.xcontent.ParseField;
2125
import org.elasticsearch.xcontent.XContentParser;
2226
import org.elasticsearch.xcontent.XContentParserConfiguration;
2327
import org.elasticsearch.xcontent.json.JsonXContent;
2428

25-
import java.io.BufferedReader;
2629
import java.io.IOException;
2730
import java.io.InputStream;
2831
import java.io.UncheckedIOException;
2932
import java.net.URL;
3033
import java.nio.file.Files;
3134
import java.nio.file.Path;
3235
import java.util.Enumeration;
36+
import java.util.HashMap;
3337
import java.util.List;
38+
import java.util.Map;
3439
import java.util.Objects;
40+
import java.util.function.Function;
3541

3642
/**
3743
* Initializes natives and installs test security manager
@@ -47,17 +53,26 @@ public class BootstrapForTesting {
4753
// without making things complex???
4854

4955
private static final ConstructingObjectParser<TestBuildInfoLocation, Void> TEST_BUILD_INFO_LOCATION_PARSER =
50-
new ConstructingObjectParser<>("test_build_info_location", values -> new TestBuildInfoLocation((String)values[0], (String)values[1]));
56+
new ConstructingObjectParser<>(
57+
"test_build_info_location",
58+
values -> new TestBuildInfoLocation((String) values[0], (String) values[1])
59+
);
5160
@SuppressWarnings("unchecked")
52-
private static final ConstructingObjectParser<TestBuildInfo, Void> TEST_BUILD_INFO_PARSER =
53-
new ConstructingObjectParser<>("test_build_info", values -> new TestBuildInfo((String)values[0], (List<TestBuildInfoLocation>)values[1]));
61+
private static final ConstructingObjectParser<TestBuildInfo, Void> TEST_BUILD_INFO_PARSER = new ConstructingObjectParser<>(
62+
"test_build_info",
63+
values -> new TestBuildInfo((String) values[0], (List<TestBuildInfoLocation>) values[1])
64+
);
5465

5566
static {
56-
TEST_BUILD_INFO_LOCATION_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("module"));
5767
TEST_BUILD_INFO_LOCATION_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("representative_class"));
68+
TEST_BUILD_INFO_LOCATION_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("module"));
5869

5970
TEST_BUILD_INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("component"));
60-
TEST_BUILD_INFO_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), TEST_BUILD_INFO_LOCATION_PARSER::apply, new ParseField("locations"));
71+
TEST_BUILD_INFO_PARSER.declareObjectArray(
72+
ConstructingObjectParser.constructorArg(),
73+
TEST_BUILD_INFO_LOCATION_PARSER::apply,
74+
new ParseField("locations")
75+
);
6176

6277
// make sure java.io.tmpdir exists always (in case code uses it in a static initializer)
6378
Path javaTmpDir = PathUtils.get(
@@ -95,19 +110,52 @@ public class BootstrapForTesting {
95110
IfConfig.logIfNecessary();
96111

97112
try {
113+
Map<String, PolicyScope> locationToPolicyScope = new HashMap<>();
98114
Enumeration<URL> urls = BootstrapForTesting.class.getClassLoader().getResources("META-INF/plugin-test-build-info.json");
99-
System.out.println("HERE0: " + urls.hasMoreElements());
100115
while (urls.hasMoreElements()) {
101-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, urls.nextElement().openStream())) {
102-
TestBuildInfo testBuildInfo = TEST_BUILD_INFO_PARSER.parse(parser, null);
103-
System.out.println("HERE1: " + testBuildInfo);
116+
TestBuildInfo testBuildInfo;
117+
try (
118+
XContentParser parser = JsonXContent.jsonXContent.createParser(
119+
XContentParserConfiguration.EMPTY,
120+
urls.nextElement().openStream()
121+
)
122+
) {
123+
testBuildInfo = TEST_BUILD_INFO_PARSER.parse(parser, null);
124+
}
125+
String componentName = testBuildInfo.component();
126+
// TODO: look at descriptor file to determine if plugin is module, if not use unnamed
127+
URL policyURL = BootstrapForTesting.class.getClassLoader()
128+
.getResource("META-INF/es-plugins/" + componentName + "/entitlement-policy.yaml");
129+
if (policyURL == null) {
130+
continue;
131+
}
132+
Map<String, PolicyScope> moduleToScope = new HashMap<>();
133+
try (InputStream policyStream = policyURL.openStream()) {
134+
Policy policy = new PolicyParser(policyStream, componentName, false).parsePolicy();
135+
for (Scope parserScope : policy.scopes()) {
136+
moduleToScope.put(parserScope.moduleName(), PolicyScope.plugin(componentName, parserScope.moduleName()));
137+
}
138+
}
139+
for (TestBuildInfoLocation testBuildInfoLocation : testBuildInfo.locations()) {
140+
URL url = BootstrapForTesting.class.getClassLoader().getResource(testBuildInfoLocation.representativeClass());
141+
String externalForm = url.toExternalForm();
142+
if (externalForm.startsWith("jar:")) {
143+
int start = "jar:".length();
144+
int end = externalForm.indexOf('!');
145+
externalForm = externalForm.substring(start, end);
146+
}
147+
PolicyScope policyScope = moduleToScope.get(testBuildInfoLocation.module());
148+
if (policyScope == null) {
149+
continue;
150+
}
151+
locationToPolicyScope.put(externalForm, policyScope);
104152
}
105153
}
106-
System.out.println(
107-
"HERE2: " + BootstrapForTesting.class.getClassLoader().getResource("META-INF/plugin-test-build-info.json"));
108-
System.out.println(
109-
"HERE3: " + BootstrapForTesting.class.getResource("/META-INF/plugin-test-build-info.json"));
110-
System.out.println("HERE4: " + BootstrapForTesting.class.getProtectionDomain().getCodeSource().getLocation());
154+
System.out.println("LOCATION TO POLICY SCOPE:" + locationToPolicyScope);
155+
Function<Class<?>, PolicyScope> classToPolicyScope = c -> locationToPolicyScope.get(
156+
c.getProtectionDomain().getCodeSource().getLocation().toExternalForm()
157+
);
158+
System.out.println("TEST0:" + classToPolicyScope.apply(XContentParser.class));
111159
} catch (IOException ioe) {
112160
throw new UncheckedIOException(ioe);
113161
}

0 commit comments

Comments
 (0)