|
6 | 6 | */ |
7 | 7 | package org.elasticsearch.xpack.idp.action; |
8 | 8 |
|
| 9 | +import org.elasticsearch.TransportVersion; |
| 10 | +import org.elasticsearch.TransportVersions; |
9 | 11 | import org.elasticsearch.action.ActionRequestValidationException; |
10 | 12 | import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
11 | 14 | import org.elasticsearch.test.ESTestCase; |
| 15 | +import org.elasticsearch.test.TransportVersionUtils; |
12 | 16 | import org.elasticsearch.xpack.idp.saml.support.SamlInitiateSingleSignOnAttributes; |
13 | 17 |
|
14 | 18 | import java.util.Arrays; |
|
23 | 27 |
|
24 | 28 | public class SamlInitiateSingleSignOnRequestTests extends ESTestCase { |
25 | 29 |
|
26 | | - public void testSerialization() throws Exception { |
| 30 | + public void testSerializationCurrentVersion() throws Exception { |
27 | 31 | final SamlInitiateSingleSignOnRequest request = new SamlInitiateSingleSignOnRequest(); |
28 | 32 | request.setSpEntityId("https://kibana_url"); |
29 | 33 | 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 | + ); |
30 | 42 | assertThat("An invalid request is not guaranteed to serialize correctly", request.validate(), nullValue()); |
31 | 43 | 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 | + } |
32 | 53 | request.writeTo(out); |
33 | 54 |
|
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 | + } |
39 | 100 | } |
40 | 101 |
|
41 | 102 | public void testValidation() { |
|
0 commit comments