Skip to content

Commit bdbb95b

Browse files
authored
[8.19] Encapsulate entitlements (#128637) (#128701)
* Encapsulate entitlements (#128637) * Rename and encapsulate InitializeArgs * Move ElasticsearchEntitlementChecker out of api package. It's an implementation detail that doesn't need to be exposed to the rest of the system. * Stub TestPathLookup (not yet implemented) * Move entitlement checkers to policy package. Maintain parity with main branch to ease backporting.
1 parent 5f7df00 commit bdbb95b

File tree

13 files changed

+108
-70
lines changed

13 files changed

+108
-70
lines changed

libs/entitlement/src/main/java/module-info.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
requires static org.elasticsearch.entitlement.bridge; // At runtime, this will be in java.base
2121

2222
exports org.elasticsearch.entitlement.runtime.api;
23-
exports org.elasticsearch.entitlement.runtime.policy;
24-
exports org.elasticsearch.entitlement.runtime.policy.entitlements to org.elasticsearch.server;
2523
exports org.elasticsearch.entitlement.instrumentation;
2624
exports org.elasticsearch.entitlement.bootstrap to org.elasticsearch.server;
2725
exports org.elasticsearch.entitlement.initialization to java.base;
2826

27+
// TODO: Most of the things in the policy package should be internal implementation details that are not exported.
28+
exports org.elasticsearch.entitlement.runtime.policy;
29+
exports org.elasticsearch.entitlement.runtime.policy.entitlements to org.elasticsearch.server;
30+
2931
uses org.elasticsearch.entitlement.instrumentation.InstrumentationService;
3032
}

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
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;
21-
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
2220
import org.elasticsearch.entitlement.runtime.policy.PathLookupImpl;
2321
import org.elasticsearch.entitlement.runtime.policy.Policy;
2422
import org.elasticsearch.entitlement.runtime.policy.PolicyManager;
@@ -33,35 +31,11 @@
3331
import java.util.function.Function;
3432
import java.util.stream.Stream;
3533

36-
import static java.util.Objects.requireNonNull;
37-
3834
public class EntitlementBootstrap {
3935

40-
public record BootstrapArgs(
41-
@Nullable Policy serverPolicyPatch,
42-
Map<String, Policy> pluginPolicies,
43-
Function<Class<?>, PolicyManager.PolicyScope> scopeResolver,
44-
PathLookup pathLookup,
45-
Map<String, Path> sourcePaths,
46-
Set<Package> suppressFailureLogPackages
47-
) {
48-
public BootstrapArgs {
49-
requireNonNull(pluginPolicies);
50-
requireNonNull(scopeResolver);
51-
requireNonNull(pathLookup);
52-
requireNonNull(sourcePaths);
53-
requireNonNull(suppressFailureLogPackages);
54-
}
55-
}
56-
57-
private static BootstrapArgs bootstrapArgs;
58-
59-
public static BootstrapArgs bootstrapArgs() {
60-
return bootstrapArgs;
61-
}
62-
6336
/**
64-
* Activates entitlement checking. Once this method returns, calls to methods protected by Entitlements from classes without a valid
37+
* Main entry point that activates entitlement checking. Once this method returns,
38+
* calls to methods protected by entitlements from classes without a valid
6539
* policy will throw {@link org.elasticsearch.entitlement.runtime.api.NotEntitledException}.
6640
*
6741
* @param serverPolicyPatch a policy with additional entitlements to patch the embedded server layer policy
@@ -98,10 +72,10 @@ public static void bootstrap(
9872
Set<Package> suppressFailureLogPackages
9973
) {
10074
logger.debug("Loading entitlement agent");
101-
if (EntitlementBootstrap.bootstrapArgs != null) {
102-
throw new IllegalStateException("plugin data is already set");
75+
if (EntitlementInitialization.initializeArgs != null) {
76+
throw new IllegalStateException("initialization data is already set");
10377
}
104-
EntitlementBootstrap.bootstrapArgs = new BootstrapArgs(
78+
EntitlementInitialization.initializeArgs = new EntitlementInitialization.InitializeArgs(
10579
serverPolicyPatch,
10680
pluginPolicies,
10781
scopeResolver,

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

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
package org.elasticsearch.entitlement.initialization;
1111

1212
import org.elasticsearch.core.Booleans;
13-
import org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap;
13+
import org.elasticsearch.core.Nullable;
1414
import org.elasticsearch.entitlement.bridge.EntitlementChecker;
15-
import org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker;
15+
import org.elasticsearch.entitlement.runtime.policy.ElasticsearchEntitlementChecker;
1616
import org.elasticsearch.entitlement.runtime.policy.PathLookup;
1717
import org.elasticsearch.entitlement.runtime.policy.Policy;
1818
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
@@ -22,8 +22,12 @@
2222
import java.lang.instrument.Instrumentation;
2323
import java.lang.reflect.Constructor;
2424
import java.lang.reflect.InvocationTargetException;
25+
import java.nio.file.Path;
2526
import java.util.Map;
2627
import java.util.Set;
28+
import java.util.function.Function;
29+
30+
import static java.util.Objects.requireNonNull;
2731

2832
/**
2933
* Called by the agent during {@code agentmain} to configure the entitlement system,
@@ -36,6 +40,7 @@ public class EntitlementInitialization {
3640

3741
private static final Module ENTITLEMENTS_MODULE = PolicyManager.class.getModule();
3842

43+
public static InitializeArgs initializeArgs;
3944
private static ElasticsearchEntitlementChecker checker;
4045

4146
// Note: referenced by bridge reflectively
@@ -66,29 +71,55 @@ public static void initialize(Instrumentation inst) throws Exception {
6671
checker = initChecker(inst, createPolicyManager());
6772
}
6873

74+
/**
75+
* Arguments to {@link #initialize}. Since that's called in a static context from the agent,
76+
* we have no way to pass arguments directly, so we stuff them in here.
77+
*
78+
* @param serverPolicyPatch
79+
* @param pluginPolicies
80+
* @param scopeResolver
81+
* @param pathLookup
82+
* @param sourcePaths
83+
* @param suppressFailureLogPackages
84+
*/
85+
public record InitializeArgs(
86+
@Nullable Policy serverPolicyPatch,
87+
Map<String, Policy> pluginPolicies,
88+
Function<Class<?>, PolicyManager.PolicyScope> scopeResolver,
89+
PathLookup pathLookup,
90+
Map<String, Path> sourcePaths,
91+
Set<Package> suppressFailureLogPackages
92+
) {
93+
public InitializeArgs {
94+
requireNonNull(pluginPolicies);
95+
requireNonNull(scopeResolver);
96+
requireNonNull(pathLookup);
97+
requireNonNull(sourcePaths);
98+
requireNonNull(suppressFailureLogPackages);
99+
}
100+
}
101+
69102
private static PolicyCheckerImpl createPolicyChecker(PolicyManager policyManager) {
70-
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
71103
return new PolicyCheckerImpl(
72-
bootstrapArgs.suppressFailureLogPackages(),
104+
initializeArgs.suppressFailureLogPackages(),
73105
ENTITLEMENTS_MODULE,
74106
policyManager,
75-
bootstrapArgs.pathLookup()
107+
initializeArgs.pathLookup()
76108
);
77109
}
78110

79111
private static PolicyManager createPolicyManager() {
80-
EntitlementBootstrap.BootstrapArgs bootstrapArgs = EntitlementBootstrap.bootstrapArgs();
81-
Map<String, Policy> pluginPolicies = bootstrapArgs.pluginPolicies();
82-
PathLookup pathLookup = bootstrapArgs.pathLookup();
112+
Map<String, Policy> pluginPolicies = initializeArgs.pluginPolicies();
113+
PathLookup pathLookup = initializeArgs.pathLookup();
83114

84115
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
85116

86117
return new PolicyManager(
87-
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), bootstrapArgs.serverPolicyPatch()),
118+
HardcodedEntitlements.serverPolicy(pathLookup.pidFile(), initializeArgs.serverPolicyPatch()),
88119
HardcodedEntitlements.agentEntitlements(),
89120
pluginPolicies,
90-
EntitlementBootstrap.bootstrapArgs().scopeResolver(),
91-
EntitlementBootstrap.bootstrapArgs().sourcePaths(),
121+
initializeArgs.scopeResolver(),
122+
initializeArgs.sourcePaths(),
92123
pathLookup
93124
);
94125
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
* <h2>Checks</h2>
190190
* <p>
191191
* The injected prologue calls a {@code check$} method on {@link org.elasticsearch.entitlement.bridge.EntitlementChecker}; its
192-
* implementation (normally on {@link org.elasticsearch.entitlement.runtime.api.ElasticsearchEntitlementChecker}, unless it is a
192+
* implementation (normally on {@link org.elasticsearch.entitlement.runtime.policy.ElasticsearchEntitlementChecker}, unless it is a
193193
* version-specific method) calls the appropriate methods on {@link org.elasticsearch.entitlement.runtime.policy.PolicyManager},
194194
* forwarding the caller class and a specific set of arguments. These methods all start with check, roughly matching an entitlement type
195195
* (e.g. {@link org.elasticsearch.entitlement.runtime.policy.PolicyChecker#checkInboundNetworkAccess},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
/**
11+
* The public API for the Entitlements system.
12+
* All other packages are implementation details that should use selective exports.
13+
*/
14+
package org.elasticsearch.entitlement.runtime.api;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.entitlement.runtime.api;
10+
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import jdk.nio.Channels;
1313

1414
import org.elasticsearch.core.SuppressForbidden;
1515
import org.elasticsearch.entitlement.bridge.EntitlementChecker;
16-
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1716

1817
import java.io.File;
1918
import java.io.FileDescriptor;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.entitlement.runtime.api;
10+
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.entitlement.bridge.Java19EntitlementChecker;
13-
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1413

1514
import java.lang.foreign.Addressable;
1615
import java.lang.foreign.FunctionDescriptor;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.entitlement.runtime.api;
10+
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.entitlement.bridge.Java20EntitlementChecker;
13-
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1413

1514
import java.lang.foreign.FunctionDescriptor;
1615
import java.lang.foreign.Linker;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.entitlement.runtime.api;
10+
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.entitlement.bridge.Java21EntitlementChecker;
13-
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1413

1514
import java.lang.foreign.AddressLayout;
1615
import java.lang.foreign.Arena;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.entitlement.runtime.api;
10+
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.entitlement.bridge.Java22EntitlementChecker;
13-
import org.elasticsearch.entitlement.runtime.policy.PolicyChecker;
1413

1514
public class Java22ElasticsearchEntitlementChecker extends Java21ElasticsearchEntitlementChecker implements Java22EntitlementChecker {
1615

0 commit comments

Comments
 (0)