Skip to content

Commit 3686202

Browse files
committed
Remove Version.V_6_x_x constants use in security
This removes some use of the v6 constants in various parts of security. Mostly this is BWC testing code, which is no longer needed as ES8 will not need to maintain compatibility with ES6. Relates: elastic#41164
1 parent f0fac9f commit 3686202

File tree

14 files changed

+33
-208
lines changed

14 files changed

+33
-208
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.role;
77

8-
import org.elasticsearch.Version;
98
import org.elasticsearch.action.ActionRequest;
109
import org.elasticsearch.action.ActionRequestValidationException;
1110
import org.elasticsearch.action.support.WriteRequest;
@@ -168,10 +167,8 @@ public void readFrom(StreamInput in) throws IOException {
168167
for (int i = 0; i < indicesSize; i++) {
169168
indicesPrivileges.add(new RoleDescriptor.IndicesPrivileges(in));
170169
}
171-
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
172-
applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::new);
173-
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
174-
}
170+
applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::new);
171+
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
175172
runAs = in.readStringArray();
176173
refreshPolicy = RefreshPolicy.readFrom(in);
177174
metadata = in.readMap();
@@ -186,10 +183,8 @@ public void writeTo(StreamOutput out) throws IOException {
186183
for (RoleDescriptor.IndicesPrivileges index : indicesPrivileges) {
187184
index.writeTo(out);
188185
}
189-
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
190-
out.writeList(applicationPrivileges);
191-
ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges);
192-
}
186+
out.writeList(applicationPrivileges);
187+
ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges);
193188
out.writeStringArray(runAs);
194189
refreshPolicy.writeTo(out);
195190
out.writeMap(metadata);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.user;
77

8-
import org.elasticsearch.Version;
98
import org.elasticsearch.action.ActionResponse;
109
import org.elasticsearch.common.io.stream.StreamInput;
1110
import org.elasticsearch.common.io.stream.StreamOutput;
1211
import org.elasticsearch.xpack.core.security.authc.Authentication;
13-
import org.elasticsearch.xpack.core.security.user.User;
1412

1513
import java.io.IOException;
1614

@@ -31,23 +29,13 @@ public Authentication authentication() {
3129
@Override
3230
public void writeTo(StreamOutput out) throws IOException {
3331
super.writeTo(out);
34-
if (out.getVersion().before(Version.V_6_6_0)) {
35-
User.writeTo(authentication.getUser(), out);
36-
} else {
37-
authentication.writeTo(out);
38-
}
32+
authentication.writeTo(out);
3933
}
4034

4135
@Override
4236
public void readFrom(StreamInput in) throws IOException {
4337
super.readFrom(in);
44-
if (in.getVersion().before(Version.V_6_6_0)) {
45-
final User user = User.readFrom(in);
46-
final Authentication.RealmRef unknownRealm = new Authentication.RealmRef("__unknown", "__unknown", "__unknown");
47-
authentication = new Authentication(user, unknownRealm, unknownRealm);
48-
} else {
49-
authentication = new Authentication(in);
50-
}
38+
authentication = new Authentication(in);
5139
}
5240

5341
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponse.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.user;
77

8-
import org.elasticsearch.Version;
98
import org.elasticsearch.action.ActionResponse;
109
import org.elasticsearch.common.Strings;
1110
import org.elasticsearch.common.bytes.BytesReference;
@@ -145,11 +144,7 @@ public Indices(StreamInput in) throws IOException {
145144
return new FieldPermissionsDefinition.FieldGrantExcludeGroup(grant, exclude);
146145
}));
147146
queries = Collections.unmodifiableSet(in.readSet(StreamInput::readBytesReference));
148-
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
149-
this.allowRestrictedIndices = in.readBoolean();
150-
} else {
151-
this.allowRestrictedIndices = false;
152-
}
147+
this.allowRestrictedIndices = in.readBoolean();
153148
}
154149

155150
public Set<String> getIndices() {
@@ -254,9 +249,7 @@ public void writeTo(StreamOutput out) throws IOException {
254249
output.writeOptionalStringArray(fields.getExcludedFields());
255250
});
256251
out.writeCollection(queries, StreamOutput::writeBytesReference);
257-
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
258-
out.writeBoolean(allowRestrictedIndices);
259-
}
252+
out.writeBoolean(allowRestrictedIndices);
260253
}
261254
}
262255
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.user;
77

8-
import org.elasticsearch.Version;
98
import org.elasticsearch.action.ActionRequest;
109
import org.elasticsearch.action.ActionRequestValidationException;
1110
import org.elasticsearch.common.io.stream.StreamInput;
@@ -109,9 +108,7 @@ public void readFrom(StreamInput in) throws IOException {
109108
for (int i = 0; i < indexSize; i++) {
110109
indexPrivileges[i] = new RoleDescriptor.IndicesPrivileges(in);
111110
}
112-
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
113-
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
114-
}
111+
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
115112
}
116113

117114
@Override
@@ -123,9 +120,7 @@ public void writeTo(StreamOutput out) throws IOException {
123120
for (RoleDescriptor.IndicesPrivileges priv : indexPrivileges) {
124121
priv.writeTo(out);
125122
}
126-
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
127-
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
128-
}
123+
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
129124
}
130125

131126
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponse.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.user;
77

8-
import org.elasticsearch.Version;
98
import org.elasticsearch.action.ActionResponse;
109
import org.elasticsearch.common.io.stream.StreamInput;
1110
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -103,16 +102,10 @@ public int hashCode() {
103102
public void readFrom(StreamInput in) throws IOException {
104103
super.readFrom(in);
105104
completeMatch = in.readBoolean();
106-
if (in.getVersion().onOrAfter(Version.V_6_6_0 )) {
107-
cluster = in.readMap(StreamInput::readString, StreamInput::readBoolean);
108-
}
105+
cluster = in.readMap(StreamInput::readString, StreamInput::readBoolean);
109106
index = readResourcePrivileges(in);
110-
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
111-
application = in.readMap(StreamInput::readString, HasPrivilegesResponse::readResourcePrivileges);
112-
}
113-
if (in.getVersion().onOrAfter(Version.V_6_6_0)) {
114-
username = in.readString();
115-
}
107+
application = in.readMap(StreamInput::readString, HasPrivilegesResponse::readResourcePrivileges);
108+
username = in.readString();
116109
}
117110

118111
private static Set<ResourcePrivileges> readResourcePrivileges(StreamInput in) throws IOException {
@@ -130,16 +123,10 @@ private static Set<ResourcePrivileges> readResourcePrivileges(StreamInput in) th
130123
public void writeTo(StreamOutput out) throws IOException {
131124
super.writeTo(out);
132125
out.writeBoolean(completeMatch);
133-
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
134-
out.writeMap(cluster, StreamOutput::writeString, StreamOutput::writeBoolean);
135-
}
126+
out.writeMap(cluster, StreamOutput::writeString, StreamOutput::writeBoolean);
136127
writeResourcePrivileges(out, index);
137-
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
138-
out.writeMap(application, StreamOutput::writeString, HasPrivilegesResponse::writeResourcePrivileges);
139-
}
140-
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
141-
out.writeString(username);
142-
}
128+
out.writeMap(application, StreamOutput::writeString, HasPrivilegesResponse::writeResourcePrivileges);
129+
out.writeString(username);
143130
}
144131

145132
private static void writeResourcePrivileges(StreamOutput out, Set<ResourcePrivileges> privileges) throws IOException {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,8 @@ public Authentication(StreamInput in) throws IOException {
6060
this.lookedUpBy = null;
6161
}
6262
this.version = in.getVersion();
63-
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
64-
type = AuthenticationType.values()[in.readVInt()];
65-
metadata = in.readMap();
66-
} else {
67-
type = AuthenticationType.REALM;
68-
metadata = Collections.emptyMap();
69-
}
63+
type = AuthenticationType.values()[in.readVInt()];
64+
metadata = in.readMap();
7065
}
7166

7267
public User getUser() {
@@ -165,10 +160,8 @@ public void writeTo(StreamOutput out) throws IOException {
165160
} else {
166161
out.writeBoolean(false);
167162
}
168-
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
169-
out.writeVInt(type.ordinal());
170-
out.writeMap(metadata);
171-
}
163+
out.writeVInt(type.ordinal());
164+
out.writeMap(metadata);
172165
}
173166

174167
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.elasticsearch.xpack.core.security.authz;
77

88
import org.elasticsearch.ElasticsearchParseException;
9-
import org.elasticsearch.Version;
109
import org.elasticsearch.common.Nullable;
1110
import org.elasticsearch.common.ParseField;
1211
import org.elasticsearch.common.Strings;
@@ -122,13 +121,8 @@ public RoleDescriptor(StreamInput in) throws IOException {
122121
this.metadata = in.readMap();
123122
this.transientMetadata = in.readMap();
124123

125-
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
126-
this.applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
127-
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
128-
} else {
129-
this.applicationPrivileges = ApplicationResourcePrivileges.NONE;
130-
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.EMPTY_ARRAY;
131-
}
124+
this.applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
125+
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
132126
}
133127

134128
public String getName() {
@@ -264,10 +258,8 @@ public void writeTo(StreamOutput out) throws IOException {
264258
out.writeStringArray(runAs);
265259
out.writeMap(metadata);
266260
out.writeMap(transientMetadata);
267-
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
268-
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
269-
ConditionalClusterPrivileges.writeArray(out, getConditionalClusterPrivileges());
270-
}
261+
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
262+
ConditionalClusterPrivileges.writeArray(out, getConditionalClusterPrivileges());
271263
}
272264

273265
public static RoleDescriptor parse(String name, BytesReference source, boolean allow2xFormat, XContentType xContentType)
@@ -608,11 +600,7 @@ public IndicesPrivileges(StreamInput in) throws IOException {
608600
this.deniedFields = in.readOptionalStringArray();
609601
this.privileges = in.readStringArray();
610602
this.query = in.readOptionalBytesReference();
611-
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
612-
allowRestrictedIndices = in.readBoolean();
613-
} else {
614-
allowRestrictedIndices = false;
615-
}
603+
this.allowRestrictedIndices = in.readBoolean();
616604
}
617605

618606
@Override
@@ -622,9 +610,7 @@ public void writeTo(StreamOutput out) throws IOException {
622610
out.writeOptionalStringArray(deniedFields);
623611
out.writeStringArray(privileges);
624612
out.writeOptionalBytesReference(query);
625-
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
626-
out.writeBoolean(allowRestrictedIndices);
627-
}
613+
out.writeBoolean(allowRestrictedIndices);
628614
}
629615

630616
public static Builder builder() {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestTests.java

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.elasticsearch.test.ESTestCase;
2121
import org.elasticsearch.test.VersionUtils;
2222
import org.elasticsearch.xpack.core.XPackClientPlugin;
23-
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
2423
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
2524
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges;
2625

@@ -31,11 +30,9 @@
3130
import java.util.Map;
3231
import java.util.function.Supplier;
3332

34-
import static org.hamcrest.Matchers.arrayWithSize;
3533
import static org.hamcrest.Matchers.containsString;
3634
import static org.hamcrest.Matchers.equalTo;
3735
import static org.hamcrest.Matchers.hasItem;
38-
import static org.hamcrest.Matchers.iterableWithSize;
3936
import static org.hamcrest.Matchers.notNullValue;
4037
import static org.hamcrest.Matchers.nullValue;
4138

@@ -60,7 +57,7 @@ public void testSerialization() throws IOException {
6057

6158
final BytesStreamOutput out = new BytesStreamOutput();
6259
if (randomBoolean()) {
63-
final Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_7_0, Version.CURRENT);
60+
final Version version = VersionUtils.randomCompatibleVersion(random(), Version.CURRENT);
6461
logger.info("Serializing with version {}", version);
6562
out.setVersion(version);
6663
}
@@ -75,67 +72,6 @@ public void testSerialization() throws IOException {
7572
assertThat(copy.roleDescriptor(), equalTo(original.roleDescriptor()));
7673
}
7774

78-
public void testSerializationBetweenV64AndV66() throws IOException {
79-
final PutRoleRequest original = buildRandomRequest();
80-
81-
final BytesStreamOutput out = new BytesStreamOutput();
82-
final Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_4_0, Version.V_6_6_0);
83-
out.setVersion(version);
84-
original.writeTo(out);
85-
86-
final PutRoleRequest copy = new PutRoleRequest();
87-
final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables());
88-
StreamInput in = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(out.bytes())), registry);
89-
in.setVersion(version);
90-
copy.readFrom(in);
91-
92-
assertThat(copy.name(), equalTo(original.name()));
93-
assertThat(copy.cluster(), equalTo(original.cluster()));
94-
assertIndicesSerializedRestricted(copy.indices(), original.indices());
95-
assertThat(copy.runAs(), equalTo(original.runAs()));
96-
assertThat(copy.metadata(), equalTo(original.metadata()));
97-
assertThat(copy.getRefreshPolicy(), equalTo(original.getRefreshPolicy()));
98-
99-
assertThat(copy.applicationPrivileges(), equalTo(original.applicationPrivileges()));
100-
assertThat(copy.conditionalClusterPrivileges(), equalTo(original.conditionalClusterPrivileges()));
101-
}
102-
103-
public void testSerializationV60AndV32() throws IOException {
104-
final PutRoleRequest original = buildRandomRequest();
105-
106-
final BytesStreamOutput out = new BytesStreamOutput();
107-
final Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_0_0, Version.V_6_3_2);
108-
out.setVersion(version);
109-
original.writeTo(out);
110-
111-
final PutRoleRequest copy = new PutRoleRequest();
112-
final StreamInput in = out.bytes().streamInput();
113-
in.setVersion(version);
114-
copy.readFrom(in);
115-
116-
assertThat(copy.name(), equalTo(original.name()));
117-
assertThat(copy.cluster(), equalTo(original.cluster()));
118-
assertIndicesSerializedRestricted(copy.indices(), original.indices());
119-
assertThat(copy.runAs(), equalTo(original.runAs()));
120-
assertThat(copy.metadata(), equalTo(original.metadata()));
121-
assertThat(copy.getRefreshPolicy(), equalTo(original.getRefreshPolicy()));
122-
123-
assertThat(copy.applicationPrivileges(), iterableWithSize(0));
124-
assertThat(copy.conditionalClusterPrivileges(), arrayWithSize(0));
125-
}
126-
127-
private void assertIndicesSerializedRestricted(RoleDescriptor.IndicesPrivileges[] copy, RoleDescriptor.IndicesPrivileges[] original) {
128-
assertThat(copy.length, equalTo(original.length));
129-
for (int i = 0; i < copy.length; i++) {
130-
assertThat(copy[i].allowRestrictedIndices(), equalTo(false));
131-
assertThat(copy[i].getIndices(), equalTo(original[i].getIndices()));
132-
assertThat(copy[i].getPrivileges(), equalTo(original[i].getPrivileges()));
133-
assertThat(copy[i].getDeniedFields(), equalTo(original[i].getDeniedFields()));
134-
assertThat(copy[i].getGrantedFields(), equalTo(original[i].getGrantedFields()));
135-
assertThat(copy[i].getQuery(), equalTo(original[i].getQuery()));
136-
}
137-
}
138-
13975
private void assertSuccessfulValidation(PutRoleRequest request) {
14076
final ActionRequestValidationException exception = request.validate();
14177
assertThat(exception, nullValue());

0 commit comments

Comments
 (0)