Skip to content

Commit deb2d40

Browse files
authored
Remove old InvalidateApiKeyRequest transport versions (#118290) (#125480)
1 parent 3c55bd6 commit deb2d40

File tree

2 files changed

+8
-90
lines changed

2 files changed

+8
-90
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/InvalidateApiKeyRequest.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
package org.elasticsearch.xpack.core.security.action.apikey;
99

10-
import org.elasticsearch.TransportVersions;
1110
import org.elasticsearch.action.ActionRequest;
1211
import org.elasticsearch.action.ActionRequestValidationException;
1312
import org.elasticsearch.common.Strings;
@@ -41,19 +40,10 @@ public InvalidateApiKeyRequest(StreamInput in) throws IOException {
4140
super(in);
4241
realmName = textOrNull(in.readOptionalString());
4342
userName = textOrNull(in.readOptionalString());
44-
if (in.getTransportVersion().onOrAfter(TransportVersions.V_7_10_0)) {
45-
ids = in.readOptionalStringArray();
46-
} else {
47-
final String id = in.readOptionalString();
48-
ids = Strings.hasText(id) ? new String[] { id } : null;
49-
}
43+
ids = in.readOptionalStringArray();
5044
validateIds(ids);
5145
name = textOrNull(in.readOptionalString());
52-
if (in.getTransportVersion().onOrAfter(TransportVersions.V_7_4_0)) {
53-
ownedByAuthenticatedUser = in.readOptionalBoolean();
54-
} else {
55-
ownedByAuthenticatedUser = false;
56-
}
46+
ownedByAuthenticatedUser = in.readOptionalBoolean();
5747
}
5848

5949
public InvalidateApiKeyRequest(
@@ -209,23 +199,9 @@ public void writeTo(StreamOutput out) throws IOException {
209199
super.writeTo(out);
210200
out.writeOptionalString(realmName);
211201
out.writeOptionalString(userName);
212-
if (out.getTransportVersion().onOrAfter(TransportVersions.V_7_10_0)) {
213-
out.writeOptionalStringArray(ids);
214-
} else {
215-
if (ids != null) {
216-
if (ids.length == 1) {
217-
out.writeOptionalString(ids[0]);
218-
} else {
219-
throw new IllegalArgumentException("a request with multi-valued field [ids] cannot be sent to an older version");
220-
}
221-
} else {
222-
out.writeOptionalString(null);
223-
}
224-
}
202+
out.writeOptionalStringArray(ids);
225203
out.writeOptionalString(name);
226-
if (out.getTransportVersion().onOrAfter(TransportVersions.V_7_4_0)) {
227-
out.writeOptionalBoolean(ownedByAuthenticatedUser);
228-
}
204+
out.writeOptionalBoolean(ownedByAuthenticatedUser);
229205
}
230206

231207
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/InvalidateApiKeyRequestTests.java

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import java.io.IOException;
2323
import java.util.function.Supplier;
2424

25-
import static org.elasticsearch.test.TransportVersionUtils.getPreviousVersion;
2625
import static org.elasticsearch.test.TransportVersionUtils.randomVersionBetween;
2726
import static org.hamcrest.Matchers.containsInAnyOrder;
2827
import static org.hamcrest.Matchers.containsString;
2928
import static org.hamcrest.Matchers.equalTo;
30-
import static org.hamcrest.Matchers.is;
3129
import static org.hamcrest.Matchers.nullValue;
3230

3331
public class InvalidateApiKeyRequestTests extends ESTestCase {
@@ -120,14 +118,10 @@ public void writeTo(StreamOutput out) throws IOException {
120118
super.writeTo(out);
121119
out.writeOptionalString(realm);
122120
out.writeOptionalString(user);
123-
if (out.getTransportVersion().onOrAfter(TransportVersions.V_7_10_0)) {
124-
if (Strings.hasText(apiKeyId)) {
125-
out.writeOptionalStringArray(new String[] { apiKeyId });
126-
} else {
127-
out.writeOptionalStringArray(null);
128-
}
121+
if (Strings.hasText(apiKeyId)) {
122+
out.writeOptionalStringArray(new String[] { apiKeyId });
129123
} else {
130-
out.writeOptionalString(apiKeyId);
124+
out.writeOptionalStringArray(null);
131125
}
132126
out.writeOptionalString(apiKeyName);
133127
out.writeOptionalBoolean(ownedByAuthenticatedUser);
@@ -160,20 +154,13 @@ public void writeTo(StreamOutput out) throws IOException {
160154
ByteArrayOutputStream bos = new ByteArrayOutputStream();
161155
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(bos)
162156
) {
163-
TransportVersion streamVersion = randomVersionBetween(
164-
random(),
165-
TransportVersions.V_7_4_0,
166-
getPreviousVersion(TransportVersions.V_7_10_0)
167-
);
168157
Dummy d = new Dummy(inputs[caseNo]);
169-
osso.setTransportVersion(streamVersion);
170158
d.writeTo(osso);
171159

172160
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
173161
InputStreamStreamInput issi = new InputStreamStreamInput(bis);
174-
issi.setTransportVersion(streamVersion);
175-
176162
InvalidateApiKeyRequest request = new InvalidateApiKeyRequest(issi);
163+
177164
ActionRequestValidationException ve = request.validate();
178165
assertNotNull(ve.getMessage(), ve);
179166
assertEquals(expectedErrorMessages[caseNo].length, ve.validationErrors().size());
@@ -186,36 +173,6 @@ public void testSerialization() throws IOException {
186173
final String apiKeyId = randomAlphaOfLength(5);
187174
final boolean ownedByAuthenticatedUser = true;
188175
InvalidateApiKeyRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, ownedByAuthenticatedUser);
189-
{
190-
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
191-
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
192-
out.setTransportVersion(randomVersionBetween(random(), TransportVersions.V_7_0_0, TransportVersions.V_7_3_0));
193-
invalidateApiKeyRequest.writeTo(out);
194-
195-
InputStreamStreamInput inputStreamStreamInput = new InputStreamStreamInput(new ByteArrayInputStream(outBuffer.toByteArray()));
196-
inputStreamStreamInput.setTransportVersion(
197-
randomVersionBetween(random(), TransportVersions.V_7_0_0, TransportVersions.V_7_3_0)
198-
);
199-
InvalidateApiKeyRequest requestFromInputStream = new InvalidateApiKeyRequest(inputStreamStreamInput);
200-
201-
assertThat(requestFromInputStream.getIds(), equalTo(invalidateApiKeyRequest.getIds()));
202-
// old version so the default for `ownedByAuthenticatedUser` is false
203-
assertThat(requestFromInputStream.ownedByAuthenticatedUser(), is(false));
204-
}
205-
{
206-
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
207-
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
208-
out.setTransportVersion(randomVersionBetween(random(), TransportVersions.V_7_4_0, TransportVersions.V_7_9_0));
209-
invalidateApiKeyRequest.writeTo(out);
210-
211-
InputStreamStreamInput inputStreamStreamInput = new InputStreamStreamInput(new ByteArrayInputStream(outBuffer.toByteArray()));
212-
inputStreamStreamInput.setTransportVersion(
213-
randomVersionBetween(random(), TransportVersions.V_7_4_0, TransportVersions.V_7_9_0)
214-
);
215-
InvalidateApiKeyRequest requestFromInputStream = new InvalidateApiKeyRequest(inputStreamStreamInput);
216-
217-
assertThat(requestFromInputStream, equalTo(invalidateApiKeyRequest));
218-
}
219176
{
220177
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
221178
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
@@ -232,21 +189,6 @@ public void testSerialization() throws IOException {
232189
}
233190
}
234191

235-
public void testSerializationWillThrowWhenMultipleIdsAndOldVersionStream() {
236-
final InvalidateApiKeyRequest invalidateApiKeyRequest = new InvalidateApiKeyRequest(
237-
randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)),
238-
randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)),
239-
randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)),
240-
false,
241-
new String[] { randomAlphaOfLength(12), randomAlphaOfLength(12) }
242-
);
243-
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
244-
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
245-
out.setTransportVersion(randomVersionBetween(random(), TransportVersions.V_7_4_0, getPreviousVersion(TransportVersions.V_7_10_0)));
246-
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> invalidateApiKeyRequest.writeTo(out));
247-
assertThat(e.getMessage(), containsString("a request with multi-valued field [ids] cannot be sent to an older version"));
248-
}
249-
250192
private static String randomNullOrEmptyString() {
251193
return randomFrom(new String[] { "", null });
252194
}

0 commit comments

Comments
 (0)