Skip to content

Commit bd19d18

Browse files
test cloud API key authentication serialization
1 parent 30dc57d commit bd19d18

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationSerializationTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,52 @@ public void testWriteToWithCrossClusterAccessThrowsOnUnsupportedVersion() throws
115115
}
116116
}
117117

118+
public void testWriteToAndReadFromWithCloudApiKeyAuthentication() throws Exception {
119+
final Authentication authentication = Authentication.newCloudApiKeyAuthentication(
120+
AuthenticationResult.success(new User(randomAlphanumericOfLength(5), "superuser"), Map.of()),
121+
randomAlphanumericOfLength(10)
122+
);
123+
124+
assertThat(authentication.isCloudApiKey(), is(true));
125+
126+
BytesStreamOutput output = new BytesStreamOutput();
127+
authentication.writeTo(output);
128+
final Authentication readFrom = new Authentication(output.bytes().streamInput());
129+
assertThat(readFrom.isCloudApiKey(), is(true));
130+
131+
assertThat(readFrom, not(sameInstance(authentication)));
132+
assertThat(readFrom, equalTo(authentication));
133+
}
134+
135+
public void testWriteToWithCloudApiKeyThrowsOnUnsupportedVersion() {
136+
final Authentication authentication = Authentication.newCloudApiKeyAuthentication(
137+
AuthenticationResult.success(new User(randomAlphanumericOfLength(5), "superuser"), Map.of()),
138+
randomAlphanumericOfLength(10)
139+
);
140+
141+
try (BytesStreamOutput out = new BytesStreamOutput()) {
142+
final TransportVersion version = TransportVersionUtils.randomVersionBetween(
143+
random(),
144+
TransportVersions.V_8_0_0,
145+
TransportVersionUtils.getPreviousVersion(TransportVersions.SECURITY_CLOUD_API_KEY_REALM_AND_TYPE)
146+
);
147+
out.setTransportVersion(version);
148+
149+
final var ex = expectThrows(IllegalArgumentException.class, () -> authentication.writeTo(out));
150+
assertThat(
151+
ex.getMessage(),
152+
containsString(
153+
"versions of Elasticsearch before ["
154+
+ TransportVersions.SECURITY_CLOUD_API_KEY_REALM_AND_TYPE.toReleaseVersion()
155+
+ "] can't handle cloud API key authentication and attempted to send to ["
156+
+ out.getTransportVersion().toReleaseVersion()
157+
+ "]"
158+
)
159+
);
160+
}
161+
162+
}
163+
118164
public void testSystemUserReadAndWrite() throws Exception {
119165
BytesStreamOutput output = new BytesStreamOutput();
120166

0 commit comments

Comments
 (0)