Skip to content

Commit 0b82f55

Browse files
authored
Fail fast on invalid entitlement patches (#128071) (#128216)
* Fail fast on invalid entitlement patches * Don't peel off `PolicyParserException` * Just catch Exception
1 parent 659d7bd commit 0b82f55

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public static Map<String, Policy> createPluginPolicies(
8181
return pluginPolicies;
8282
}
8383

84+
/**
85+
* @throws PolicyParserException if the supplied policy is formatted incorrectly
86+
* @throws IllegalStateException for any other error parsing the patch, such as nonexistent module names
87+
*/
8488
public static Policy parseEncodedPolicyIfExists(
8589
String encodedPolicy,
8690
String version,
@@ -106,11 +110,8 @@ public static Policy parseEncodedPolicyIfExists(
106110
version
107111
);
108112
}
109-
} catch (Exception ex) {
110-
logger.warn(
111-
Strings.format("Found a policy patch with invalid content. The patch will not be applied. Layer [%s]", layerName),
112-
ex
113-
);
113+
} catch (Exception e) {
114+
throw new IllegalStateException("Unable to parse policy patch for layer [" + layerName + "]", e);
114115
}
115116
}
116117
return null;

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void testNoPatchWithVersionMismatch() {
135135

136136
public void testNoPatchWithValidationError() {
137137

138+
// Nonexistent module names
138139
var policyPatch = """
139140
versions:
140141
- 9.0.0
@@ -150,13 +151,15 @@ public void testNoPatchWithValidationError() {
150151
StandardCharsets.UTF_8
151152
);
152153

153-
var policy = PolicyUtils.parseEncodedPolicyIfExists(base64EncodedPolicy, "9.0.0", true, "test-plugin", Set.of());
154-
155-
assertThat(policy, nullValue());
154+
assertThrows(
155+
IllegalStateException.class,
156+
() -> PolicyUtils.parseEncodedPolicyIfExists(base64EncodedPolicy, "9.0.0", true, "test-plugin", Set.of())
157+
);
156158
}
157159

158160
public void testNoPatchWithParsingError() {
159161

162+
// no <version> or <policy> field
160163
var policyPatch = """
161164
entitlement-module-name:
162165
- load_native_libraries
@@ -168,9 +171,10 @@ public void testNoPatchWithParsingError() {
168171
StandardCharsets.UTF_8
169172
);
170173

171-
var policy = PolicyUtils.parseEncodedPolicyIfExists(base64EncodedPolicy, "9.0.0", true, "test-plugin", Set.of());
172-
173-
assertThat(policy, nullValue());
174+
assertThrows(
175+
IllegalStateException.class,
176+
() -> PolicyUtils.parseEncodedPolicyIfExists(base64EncodedPolicy, "9.0.0", true, "test-plugin", Set.of())
177+
);
174178
}
175179

176180
public void testMergeScopes() {

0 commit comments

Comments
 (0)