Skip to content

Commit 42ae399

Browse files
authored
Fail fast on invalid entitlement patches (#128071) (#128215)
* Fail fast on invalid entitlement patches * Don't peel off `PolicyParserException` * Just catch Exception
1 parent 720e3b5 commit 42ae399

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
@@ -134,6 +134,7 @@ public void testNoPatchWithVersionMismatch() {
134134

135135
public void testNoPatchWithValidationError() {
136136

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

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

157159
public void testNoPatchWithParsingError() {
158160

161+
// no <version> or <policy> field
159162
var policyPatch = """
160163
entitlement-module-name:
161164
- load_native_libraries
@@ -167,9 +170,10 @@ public void testNoPatchWithParsingError() {
167170
StandardCharsets.UTF_8
168171
);
169172

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

175179
public void testMergeScopes() {

0 commit comments

Comments
 (0)