Skip to content

Commit 5072c44

Browse files
committed
Add transport-version checks
1 parent c88a58a commit 5072c44

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ static TransportVersion def(int id) {
273273
public static final TransportVersion INFERENCE_CUSTOM_SERVICE_ADDED = def(9_084_0_00);
274274
public static final TransportVersion ESQL_LIMIT_ROW_SIZE = def(9_085_0_00);
275275
public static final TransportVersion ESQL_REGEX_MATCH_WITH_CASE_INSENSITIVITY = def(9_086_0_00);
276+
public static final TransportVersion IDP_CUSTOM_SAML_ATTRIBUTES = def(9_087_0_00);
276277

277278
/*
278279
* STOP! READ THIS FIRST! No, really,

x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequest.java

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

9+
import org.elasticsearch.TransportVersions;
910
import org.elasticsearch.action.ActionRequestValidationException;
1011
import org.elasticsearch.action.LegacyActionRequest;
1112
import org.elasticsearch.common.Strings;
@@ -30,7 +31,9 @@ public SamlInitiateSingleSignOnRequest(StreamInput in) throws IOException {
3031
spEntityId = in.readString();
3132
assertionConsumerService = in.readString();
3233
samlAuthenticationState = in.readOptionalWriteable(SamlAuthenticationState::new);
33-
attributes = in.readOptionalWriteable(SamlInitiateSingleSignOnAttributes::new);
34+
if (in.getTransportVersion().onOrAfter(TransportVersions.IDP_CUSTOM_SAML_ATTRIBUTES)) {
35+
attributes = in.readOptionalWriteable(SamlInitiateSingleSignOnAttributes::new);
36+
}
3437
}
3538

3639
public SamlInitiateSingleSignOnRequest() {}
@@ -96,7 +99,9 @@ public void writeTo(StreamOutput out) throws IOException {
9699
out.writeString(spEntityId);
97100
out.writeString(assertionConsumerService);
98101
out.writeOptionalWriteable(samlAuthenticationState);
99-
out.writeOptionalWriteable(attributes);
102+
if (out.getTransportVersion().onOrAfter(TransportVersions.IDP_CUSTOM_SAML_ATTRIBUTES)) {
103+
out.writeOptionalWriteable(attributes);
104+
}
100105
}
101106

102107
@Override

x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequestTests.java

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
*/
77
package org.elasticsearch.xpack.idp.action;
88

9+
import org.elasticsearch.TransportVersion;
10+
import org.elasticsearch.TransportVersions;
911
import org.elasticsearch.action.ActionRequestValidationException;
1012
import org.elasticsearch.common.io.stream.BytesStreamOutput;
13+
import org.elasticsearch.common.io.stream.StreamInput;
1114
import org.elasticsearch.test.ESTestCase;
15+
import org.elasticsearch.test.TransportVersionUtils;
1216
import org.elasticsearch.xpack.idp.saml.support.SamlInitiateSingleSignOnAttributes;
1317

1418
import java.util.Arrays;
@@ -23,19 +27,76 @@
2327

2428
public class SamlInitiateSingleSignOnRequestTests extends ESTestCase {
2529

26-
public void testSerialization() throws Exception {
30+
public void testSerializationCurrentVersion() throws Exception {
2731
final SamlInitiateSingleSignOnRequest request = new SamlInitiateSingleSignOnRequest();
2832
request.setSpEntityId("https://kibana_url");
2933
request.setAssertionConsumerService("https://kibana_url/acs");
34+
request.setAttributes(
35+
new SamlInitiateSingleSignOnAttributes(
36+
Map.ofEntries(
37+
Map.entry("http://idp.elastic.co/attribute/custom1", List.of("foo")),
38+
Map.entry("http://idp.elastic.co/attribute/custom2", List.of("bar", "baz"))
39+
)
40+
)
41+
);
3042
assertThat("An invalid request is not guaranteed to serialize correctly", request.validate(), nullValue());
3143
final BytesStreamOutput out = new BytesStreamOutput();
44+
if (randomBoolean()) {
45+
out.setTransportVersion(
46+
TransportVersionUtils.randomVersionBetween(
47+
random(),
48+
TransportVersions.IDP_CUSTOM_SAML_ATTRIBUTES,
49+
TransportVersion.current()
50+
)
51+
);
52+
}
3253
request.writeTo(out);
3354

34-
final SamlInitiateSingleSignOnRequest request1 = new SamlInitiateSingleSignOnRequest(out.bytes().streamInput());
35-
assertThat(request1.getSpEntityId(), equalTo(request.getSpEntityId()));
36-
assertThat(request1.getAssertionConsumerService(), equalTo(request.getAssertionConsumerService()));
37-
final ActionRequestValidationException validationException = request1.validate();
38-
assertNull(validationException);
55+
try (StreamInput in = out.bytes().streamInput()) {
56+
in.setTransportVersion(out.getTransportVersion());
57+
final SamlInitiateSingleSignOnRequest request1 = new SamlInitiateSingleSignOnRequest(in);
58+
assertThat(request1.getSpEntityId(), equalTo(request.getSpEntityId()));
59+
assertThat(request1.getAssertionConsumerService(), equalTo(request.getAssertionConsumerService()));
60+
assertThat(request1.getAttributes(), equalTo(request.getAttributes()));
61+
final ActionRequestValidationException validationException = request1.validate();
62+
assertNull(validationException);
63+
}
64+
}
65+
66+
public void testSerializationOldTransportVersion() throws Exception {
67+
final SamlInitiateSingleSignOnRequest request = new SamlInitiateSingleSignOnRequest();
68+
request.setSpEntityId("https://kibana_url");
69+
request.setAssertionConsumerService("https://kibana_url/acs");
70+
if (randomBoolean()) {
71+
request.setAttributes(
72+
new SamlInitiateSingleSignOnAttributes(
73+
Map.ofEntries(
74+
Map.entry("http://idp.elastic.co/attribute/custom1", List.of("foo")),
75+
Map.entry("http://idp.elastic.co/attribute/custom2", List.of("bar", "baz"))
76+
)
77+
)
78+
);
79+
}
80+
assertThat("An invalid request is not guaranteed to serialize correctly", request.validate(), nullValue());
81+
final BytesStreamOutput out = new BytesStreamOutput();
82+
out.setTransportVersion(
83+
TransportVersionUtils.randomVersionBetween(
84+
random(),
85+
TransportVersions.MINIMUM_COMPATIBLE,
86+
TransportVersionUtils.getPreviousVersion(TransportVersions.IDP_CUSTOM_SAML_ATTRIBUTES)
87+
)
88+
);
89+
request.writeTo(out);
90+
91+
try (StreamInput in = out.bytes().streamInput()) {
92+
in.setTransportVersion(out.getTransportVersion());
93+
final SamlInitiateSingleSignOnRequest request1 = new SamlInitiateSingleSignOnRequest(in);
94+
assertThat(request1.getSpEntityId(), equalTo(request.getSpEntityId()));
95+
assertThat(request1.getAssertionConsumerService(), equalTo(request.getAssertionConsumerService()));
96+
assertThat(request1.getAttributes(), nullValue());
97+
final ActionRequestValidationException validationException = request1.validate();
98+
assertNull(validationException);
99+
}
39100
}
40101

41102
public void testValidation() {

0 commit comments

Comments
 (0)