Skip to content

Commit 4eefc6d

Browse files
authored
Convert a few entitlements to records (#118705)
* Convert a few entitlements to records * Make SetHttpsConnectionPropertiesEntitlement a record and fix tests
1 parent 9452a85 commit 4eefc6d

File tree

8 files changed

+23
-85
lines changed

8 files changed

+23
-85
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ private static Policy loadPluginPolicy(Path pluginRoot, boolean isModular, Strin
128128
final Policy policy = parsePolicyIfExists(pluginName, policyFile, isExternalPlugin);
129129

130130
// TODO: should this check actually be part of the parser?
131-
for (Scope scope : policy.scopes) {
132-
if (moduleNames.contains(scope.moduleName) == false) {
131+
for (Scope scope : policy.scopes()) {
132+
if (moduleNames.contains(scope.moduleName()) == false) {
133133
throw new IllegalStateException(
134134
Strings.format(
135135
"Invalid module name in policy: plugin [%s] does not have module [%s]; available modules [%s]; policy file [%s]",
136136
pluginName,
137-
scope.moduleName,
137+
scope.moduleName(),
138138
String.join(", ", moduleNames),
139139
policyFile
140140
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12-
public class CreateClassLoaderEntitlement implements Entitlement {
12+
public record CreateClassLoaderEntitlement() implements Entitlement {
1313
@ExternalEntitlement
14-
public CreateClassLoaderEntitlement() {}
14+
public CreateClassLoaderEntitlement {}
1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
/**
1313
* Internal policy type (not-parseable -- not available to plugins).
1414
*/
15-
public class ExitVMEntitlement implements Entitlement {}
15+
public record ExitVMEntitlement() implements Entitlement {}

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,15 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12-
import java.util.Collections;
1312
import java.util.List;
1413
import java.util.Objects;
1514

1615
/**
1716
* A holder for scoped entitlements.
1817
*/
19-
public class Policy {
20-
21-
public final String name;
22-
public final List<Scope> scopes;
23-
18+
public record Policy(String name, List<Scope> scopes) {
2419
public Policy(String name, List<Scope> scopes) {
2520
this.name = Objects.requireNonNull(name);
26-
this.scopes = Collections.unmodifiableList(Objects.requireNonNull(scopes));
27-
}
28-
29-
@Override
30-
public boolean equals(Object o) {
31-
if (this == o) return true;
32-
if (o == null || getClass() != o.getClass()) return false;
33-
Policy policy = (Policy) o;
34-
return Objects.equals(name, policy.name) && Objects.equals(scopes, policy.scopes);
35-
}
36-
37-
@Override
38-
public int hashCode() {
39-
return Objects.hash(name, scopes);
40-
}
41-
42-
@Override
43-
public String toString() {
44-
return "Policy{" + "name='" + name + '\'' + ", scopes=" + scopes + '}';
21+
this.scopes = List.copyOf(scopes);
4522
}
4623
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public PolicyManager(
9999
}
100100

101101
private static Map<String, List<Entitlement>> buildScopeEntitlementsMap(Policy policy) {
102-
return policy.scopes.stream().collect(Collectors.toUnmodifiableMap(scope -> scope.moduleName, scope -> scope.entitlements));
102+
return policy.scopes().stream().collect(Collectors.toUnmodifiableMap(scope -> scope.moduleName(), scope -> scope.entitlements()));
103103
}
104104

105105
public void checkStartProcess(Class<?> callerClass) {

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,17 @@
99

1010
package org.elasticsearch.entitlement.runtime.policy;
1111

12-
import java.util.Collections;
1312
import java.util.List;
1413
import java.util.Objects;
1514

1615
/**
1716
* A holder for entitlements within a single scope.
1817
*/
19-
public class Scope {
20-
21-
public final String moduleName;
22-
public final List<Entitlement> entitlements;
18+
public record Scope(String moduleName, List<Entitlement> entitlements) {
2319

2420
public Scope(String moduleName, List<Entitlement> entitlements) {
25-
this.moduleName = moduleName;
26-
this.entitlements = Collections.unmodifiableList(Objects.requireNonNull(entitlements));
27-
}
28-
29-
@Override
30-
public boolean equals(Object o) {
31-
if (this == o) return true;
32-
if (o == null || getClass() != o.getClass()) return false;
33-
Scope scope = (Scope) o;
34-
return Objects.equals(moduleName, scope.moduleName) && Objects.equals(entitlements, scope.entitlements);
21+
this.moduleName = Objects.requireNonNull(moduleName);
22+
this.entitlements = List.copyOf(entitlements);
3523
}
3624

37-
@Override
38-
public int hashCode() {
39-
return Objects.hash(moduleName, entitlements);
40-
}
41-
42-
@Override
43-
public String toString() {
44-
return "Scope{" + "name='" + moduleName + '\'' + ", entitlements=" + entitlements + '}';
45-
}
4625
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* An Entitlement to allow setting properties to a single Https connection after this has been created
1414
*/
15-
public class SetHttpsConnectionPropertiesEntitlement implements Entitlement {
15+
public record SetHttpsConnectionPropertiesEntitlement() implements Entitlement {
1616
@ExternalEntitlement(esModulesOnly = false)
17-
public SetHttpsConnectionPropertiesEntitlement() {}
17+
public SetHttpsConnectionPropertiesEntitlement {}
1818
}

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.List;
1818

19-
import static org.elasticsearch.test.LambdaMatchers.transformedMatch;
20-
import static org.hamcrest.Matchers.both;
21-
import static org.hamcrest.Matchers.contains;
2219
import static org.hamcrest.Matchers.equalTo;
23-
import static org.hamcrest.Matchers.instanceOf;
2420

2521
public class PolicyParserTests extends ESTestCase {
2622

@@ -39,58 +35,44 @@ public void testGetEntitlementTypeName() {
3935
public void testPolicyBuilder() throws IOException {
4036
Policy parsedPolicy = new PolicyParser(PolicyParserTests.class.getResourceAsStream("test-policy.yaml"), "test-policy.yaml", false)
4137
.parsePolicy();
42-
Policy builtPolicy = new Policy(
38+
Policy expected = new Policy(
4339
"test-policy.yaml",
4440
List.of(new Scope("entitlement-module-name", List.of(new FileEntitlement("test/path/to/file", List.of("read", "write")))))
4541
);
46-
assertEquals(parsedPolicy, builtPolicy);
42+
assertEquals(expected, parsedPolicy);
4743
}
4844

4945
public void testPolicyBuilderOnExternalPlugin() throws IOException {
5046
Policy parsedPolicy = new PolicyParser(PolicyParserTests.class.getResourceAsStream("test-policy.yaml"), "test-policy.yaml", true)
5147
.parsePolicy();
52-
Policy builtPolicy = new Policy(
48+
Policy expected = new Policy(
5349
"test-policy.yaml",
5450
List.of(new Scope("entitlement-module-name", List.of(new FileEntitlement("test/path/to/file", List.of("read", "write")))))
5551
);
56-
assertEquals(parsedPolicy, builtPolicy);
52+
assertEquals(expected, parsedPolicy);
5753
}
5854

5955
public void testParseCreateClassloader() throws IOException {
6056
Policy parsedPolicy = new PolicyParser(new ByteArrayInputStream("""
6157
entitlement-module-name:
6258
- create_class_loader
6359
""".getBytes(StandardCharsets.UTF_8)), "test-policy.yaml", false).parsePolicy();
64-
Policy builtPolicy = new Policy(
60+
Policy expected = new Policy(
6561
"test-policy.yaml",
6662
List.of(new Scope("entitlement-module-name", List.of(new CreateClassLoaderEntitlement())))
6763
);
68-
assertThat(
69-
parsedPolicy.scopes,
70-
contains(
71-
both(transformedMatch((Scope scope) -> scope.moduleName, equalTo("entitlement-module-name"))).and(
72-
transformedMatch(scope -> scope.entitlements, contains(instanceOf(CreateClassLoaderEntitlement.class)))
73-
)
74-
)
75-
);
64+
assertEquals(expected, parsedPolicy);
7665
}
7766

7867
public void testParseSetHttpsConnectionProperties() throws IOException {
7968
Policy parsedPolicy = new PolicyParser(new ByteArrayInputStream("""
8069
entitlement-module-name:
8170
- set_https_connection_properties
8271
""".getBytes(StandardCharsets.UTF_8)), "test-policy.yaml", true).parsePolicy();
83-
Policy builtPolicy = new Policy(
72+
Policy expected = new Policy(
8473
"test-policy.yaml",
85-
List.of(new Scope("entitlement-module-name", List.of(new CreateClassLoaderEntitlement())))
86-
);
87-
assertThat(
88-
parsedPolicy.scopes,
89-
contains(
90-
both(transformedMatch((Scope scope) -> scope.moduleName, equalTo("entitlement-module-name"))).and(
91-
transformedMatch(scope -> scope.entitlements, contains(instanceOf(SetHttpsConnectionPropertiesEntitlement.class)))
92-
)
93-
)
74+
List.of(new Scope("entitlement-module-name", List.of(new SetHttpsConnectionPropertiesEntitlement())))
9475
);
76+
assertEquals(expected, parsedPolicy);
9577
}
9678
}

0 commit comments

Comments
 (0)