Skip to content

Commit b702dcd

Browse files
committed
Move PolicyManager instantiation to initPhase2.
This is a precursor for instantating a different PolicyManager for tests.
1 parent 4a34f0f commit b702dcd

File tree

3 files changed

+33
-79
lines changed

3 files changed

+33
-79
lines changed

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

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
import com.sun.tools.attach.AttachNotSupportedException;
1515
import com.sun.tools.attach.VirtualMachine;
1616

17-
import org.elasticsearch.core.Nullable;
1817
import org.elasticsearch.core.PathUtils;
1918
import org.elasticsearch.core.SuppressForbidden;
2019
import org.elasticsearch.entitlement.initialization.EntitlementInitialization;
2120
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
22-
import org.elasticsearch.entitlement.runtime.policy.PathLookupImpl;
2321
import org.elasticsearch.entitlement.runtime.policy.Policy;
2422
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
2523
import org.elasticsearch.logging.LogManager;
@@ -30,8 +28,6 @@
3028
import java.nio.file.Path;
3129
import java.util.Map;
3230
import java.util.Set;
33-
import java.util.function.Function;
34-
import java.util.stream.Stream;
3531

3632
import static java.util.Objects.requireNonNull;
3733

@@ -42,19 +38,19 @@
4238
*/
4339
public class EntitlementBootstrap {
4440

41+
/**
42+
* A place to stash objects that the agent will need during its initialization
43+
*/
4544
public record BootstrapArgs(
46-
@Nullable Policy serverPolicyPatch,
45+
PolicyManager policyManager,
4746
Map<String, Policy> pluginPolicies,
48-
Function<Class<?>, PolicyManager.PolicyScope> scopeResolver,
4947
PathLookup pathLookup,
50-
Map<String, Path> sourcePaths,
5148
Set<Package> suppressFailureLogPackages
5249
) {
5350
public BootstrapArgs {
51+
requireNonNull(policyManager);
5452
requireNonNull(pluginPolicies);
55-
requireNonNull(scopeResolver);
5653
requireNonNull(pathLookup);
57-
requireNonNull(sourcePaths);
5854
requireNonNull(suppressFailureLogPackages);
5955
}
6056
}
@@ -71,68 +67,27 @@ public static BootstrapArgs bootstrapArgs() {
7167
* <p>
7268
* (Note: when we reference Elasticsearch "plugins" here, we generally also include Elasticsearch "modules".)
7369
*
74-
* @param serverPolicyPatch a policy with additional entitlements to patch the embedded server layer policy
75-
* @param pluginPolicies a map holding policies for plugins, by plugin name.
76-
* @param scopeResolver a functor to map a Java Class to the component and module it belongs to.
77-
* @param settingResolver a functor to resolve a setting name pattern for one or more Elasticsearch settings.
78-
* @param dataDirs data directories for Elasticsearch
79-
* @param sharedRepoDirs shared repository directories for Elasticsearch
80-
* @param configDir the config directory for Elasticsearch
81-
* @param libDir the lib directory for Elasticsearch
82-
* @param modulesDir the directory where Elasticsearch modules are
83-
* @param pluginsDir the directory where plugins are installed for Elasticsearch
84-
* @param sourcePaths a map holding the path to each plugin or module jars, by plugin name.
85-
* @param tempDir the temp directory for Elasticsearch
86-
* @param logsDir the log directory for Elasticsearch
87-
* @param pidFile path to a pid file for Elasticsearch, or {@code null} if one was not specified
88-
* @param suppressFailureLogPackages packages for which we do not need or want to log Entitlements failures
70+
* @param policyManager
71+
* @param pluginPolicies a map holding policies for plugins, by plugin name.
72+
* @param pathLookup
73+
* @param suppressFailureLogPackages packages for which we do not need or want to log Entitlements failures
8974
*/
9075
public static void bootstrap(
91-
Policy serverPolicyPatch,
76+
PolicyManager policyManager,
9277
Map<String, Policy> pluginPolicies,
93-
Function<Class<?>, PolicyManager.PolicyScope> scopeResolver,
94-
Function<String, Stream<String>> settingResolver,
95-
Path[] dataDirs,
96-
Path[] sharedRepoDirs,
97-
Path configDir,
98-
Path libDir,
99-
Path modulesDir,
100-
Path pluginsDir,
101-
Map<String, Path> sourcePaths,
102-
Path logsDir,
103-
Path tempDir,
104-
Path pidFile,
78+
PathLookup pathLookup,
10579
Set<Package> suppressFailureLogPackages
10680
) {
10781
logger.debug("Loading entitlement agent");
10882
if (EntitlementBootstrap.bootstrapArgs != null) {
10983
throw new IllegalStateException("plugin data is already set");
11084
}
111-
EntitlementBootstrap.bootstrapArgs = new BootstrapArgs(
112-
serverPolicyPatch,
113-
pluginPolicies,
114-
scopeResolver,
115-
new PathLookupImpl(
116-
getUserHome(),
117-
configDir,
118-
dataDirs,
119-
sharedRepoDirs,
120-
libDir,
121-
modulesDir,
122-
pluginsDir,
123-
logsDir,
124-
tempDir,
125-
pidFile,
126-
settingResolver
127-
),
128-
sourcePaths,
129-
suppressFailureLogPackages
130-
);
85+
EntitlementBootstrap.bootstrapArgs = new BootstrapArgs(policyManager, pluginPolicies, pathLookup, suppressFailureLogPackages);
13186
exportInitializationToAgent();
13287
loadAgent(findAgentJar());
13388
}
13489

135-
private static Path getUserHome() {
90+
public static Path getUserHome() {
13691
String userHome = System.getProperty("user.home");
13792
if (userHome == null) {
13893
throw new IllegalStateException("user.home system property is required");

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import org.elasticsearch.core.Booleans;
1313
import org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap;
14-
import org.elasticsearch.entitlement.bootstrap.HardcodedEntitlements;
1514
import org.elasticsearch.entitlement.bridge.EntitlementChecker;
1615
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
1716
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
@@ -124,19 +123,11 @@ private static PolicyCheckerImpl createPolicyChecker() {
124123

125124
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
126125

127-
PolicyManager policyManager = new PolicyManager(
128-
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
129-
HardcodedEntitlements.agentEntitlements(),
130-
pluginPolicies,
131-
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
132-
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
133-
pathLookup
134-
);
135126
return new PolicyCheckerImpl(
136127
bootstrapArgs.suppressFailureLogPackages(),
137128
ENTITLEMENTS_MODULE,
138-
policyManager,
139-
bootstrapArgs.pathLookup()
129+
bootstrapArgs.policyManager(),
130+
pathLookup
140131
);
141132
}
142133

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.elasticsearch.core.IOUtils;
3434
import org.elasticsearch.core.SuppressForbidden;
3535
import org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap;
36+
import org.elasticsearch.entitlement.bootstrap.HardcodedEntitlements;
3637
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
38+
import org.elasticsearch.entitlement.runtime.policy.PathLookupImpl;
3739
import org.elasticsearch.entitlement.runtime.policy.Policy;
3840
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
3941
import org.elasticsearch.entitlement.runtime.policy.PolicyUtils;
@@ -75,6 +77,7 @@
7577
import java.util.stream.Collectors;
7678
import java.util.stream.Stream;
7779

80+
import static org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap.getUserHome;
7881
import static org.elasticsearch.nativeaccess.WindowsFunctions.ConsoleCtrlHandler.CTRL_CLOSE_EVENT;
7982

8083
/**
@@ -247,25 +250,30 @@ private static void initPhase2(Bootstrap bootstrap) throws IOException {
247250
pluginsLoader = PluginsLoader.createPluginsLoader(modulesBundles, pluginsBundles, findPluginsWithNativeAccess(pluginPolicies));
248251

249252
var scopeResolver = ScopeResolver.create(pluginsLoader.pluginLayers(), APM_AGENT_PACKAGE_NAME);
250-
Map<String, Path> sourcePaths = Stream.concat(modulesBundles.stream(), pluginsBundles.stream())
251-
.collect(Collectors.toUnmodifiableMap(bundle -> bundle.pluginDescriptor().getName(), PluginBundle::getDir));
252-
EntitlementBootstrap.bootstrap(
253-
serverPolicyPatch,
254-
pluginPolicies,
255-
scopeResolver::resolveClassToScope,
256-
nodeEnv.settings()::getValues,
253+
PathLookupImpl pathLookup = new PathLookupImpl(
254+
getUserHome(),
255+
nodeEnv.configDir(),
257256
nodeEnv.dataDirs(),
258257
nodeEnv.repoDirs(),
259-
nodeEnv.configDir(),
260258
nodeEnv.libDir(),
261259
nodeEnv.modulesDir(),
262260
nodeEnv.pluginsDir(),
263-
sourcePaths,
264261
nodeEnv.logsDir(),
265262
nodeEnv.tmpDir(),
266263
args.pidFile(),
267-
Set.of(EntitlementSelfTester.class.getPackage())
264+
nodeEnv.settings()::getValues
265+
);
266+
Map<String, Path> sourcePaths = Stream.concat(modulesBundles.stream(), pluginsBundles.stream())
267+
.collect(Collectors.toUnmodifiableMap(bundle -> bundle.pluginDescriptor().getName(), PluginBundle::getDir));
268+
PolicyManager policyManager = new PolicyManager(
269+
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), serverPolicyPatch),
270+
HardcodedEntitlements.agentEntitlements(),
271+
pluginPolicies,
272+
scopeResolver::resolveClassToScope,
273+
sourcePaths,
274+
pathLookup
268275
);
276+
EntitlementBootstrap.bootstrap(policyManager, pluginPolicies, pathLookup, Set.of(EntitlementSelfTester.class.getPackage()));
269277
EntitlementSelfTester.entitlementSelfTest();
270278

271279
bootstrap.setPluginsLoader(pluginsLoader);

0 commit comments

Comments
 (0)