Skip to content

Commit 1c96c3f

Browse files
authored
[TEST] Fix random RoleDescriptor compat with v6.7 (#82242) (#82363)
allowRestrictedIndices was only added in ES6.7 (see #37577). For randomised testing we should not set that field if we are generating a role descriptor that will be serialized into an older version stream. Resolves: #82216
1 parent 4e6e4ea commit 1c96c3f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void testToString() {
121121
}
122122

123123
public void testToXContentRoundtrip() throws Exception {
124-
final RoleDescriptor descriptor = randomRoleDescriptor();
124+
final RoleDescriptor descriptor = randomRoleDescriptor(Version.CURRENT);
125125
final XContentType xContentType = randomFrom(XContentType.values());
126126
final BytesReference xContentValue = toShuffledXContent(descriptor, xContentType, ToXContent.EMPTY_PARAMS, false);
127127
final RoleDescriptor parsed = RoleDescriptor.parse(descriptor.getName(), xContentValue, false, xContentType);
@@ -234,7 +234,7 @@ public void testSerialization() throws Exception {
234234
BytesStreamOutput output = new BytesStreamOutput();
235235
output.setVersion(version);
236236

237-
final RoleDescriptor descriptor = randomRoleDescriptor();
237+
final RoleDescriptor descriptor = randomRoleDescriptor(version);
238238
descriptor.writeTo(output);
239239
final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables());
240240
StreamInput streamInput = new NamedWriteableAwareStreamInput(
@@ -388,13 +388,16 @@ public void testIsEmpty() {
388388
}
389389
}
390390

391-
private RoleDescriptor randomRoleDescriptor() {
391+
/**
392+
* @param compatibleVersion Return a role descriptor that is compatible with the features that existed in this version of Elasticsearch
393+
*/
394+
private RoleDescriptor randomRoleDescriptor(Version compatibleVersion) {
392395
final RoleDescriptor.IndicesPrivileges[] indexPrivileges = new RoleDescriptor.IndicesPrivileges[randomIntBetween(0, 3)];
393396
for (int i = 0; i < indexPrivileges.length; i++) {
394397
final RoleDescriptor.IndicesPrivileges.Builder builder = RoleDescriptor.IndicesPrivileges.builder()
395398
.privileges(randomSubsetOf(randomIntBetween(1, 4), IndexPrivilege.names()))
396399
.indices(generateRandomStringArray(5, randomIntBetween(3, 9), false, false))
397-
.allowRestrictedIndices(randomBoolean());
400+
.allowRestrictedIndices(compatibleVersion.onOrAfter(Version.V_6_7_0) && randomBoolean());
398401
if (randomBoolean()) {
399402
builder.query(
400403
randomBoolean()

0 commit comments

Comments
 (0)