From 636814de7eb58b6ac807055f68351e28d0245c81 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Mon, 11 Jul 2022 10:12:37 +1000 Subject: [PATCH] Fix RealmIdentifier XContent parser RealmIdentifier's constructor has type before name. But the parser had it reserved. This PR fixes the parser to have the same argument order as the constructor. It also adds relevant tests. Since the parser is only used internally for the User Profile feature which is not official released yet. This fix is a non-issue. --- .../core/security/authc/RealmConfig.java | 2 +- .../core/security/authc/RealmConfigTests.java | 16 +++++++++ .../core/security/authc/RealmDomainTests.java | 33 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmDomainTests.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java index e827fea69b527..fc3274e796f9a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java @@ -267,7 +267,7 @@ public int compareTo(RealmIdentifier other) { ); static { - REALM_IDENTIFIER_PARSER.declareString(constructorArg(), new ParseField("name")); REALM_IDENTIFIER_PARSER.declareString(constructorArg(), new ParseField("type")); + REALM_IDENTIFIER_PARSER.declareString(constructorArg(), new ParseField("name")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java index c14cd571d66a4..ec3719d070f6f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java @@ -11,10 +11,17 @@ import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import org.mockito.Mockito; +import java.io.IOException; + import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; public class RealmConfigTests extends ESTestCase { @@ -57,4 +64,13 @@ public void testWillNotFailWhenOrderIsMissingAndDisabled() { final RealmConfig realmConfig = new RealmConfig(realmIdentifier, settings, environment, threadContext); assertThat(realmConfig.enabled(), is(false)); } + + public void testXContentSerialization() throws IOException { + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + realmIdentifier.toXContent(builder, ToXContent.EMPTY_PARAMS); + try (XContentParser parser = createParser(builder)) { + assertThat(RealmConfig.REALM_IDENTIFIER_PARSER.parse(parser, null), equalTo(realmIdentifier)); + } + } + } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmDomainTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmDomainTests.java new file mode 100644 index 0000000000000..e2951cebb82d3 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmDomainTests.java @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.core.security.authc; + +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; + +import java.io.IOException; + +public class RealmDomainTests extends AbstractSerializingTestCase { + + @Override + protected RealmDomain doParseInstance(XContentParser parser) throws IOException { + return RealmDomain.REALM_DOMAIN_PARSER.parse(parser, null); + } + + @Override + protected Writeable.Reader instanceReader() { + return RealmDomain::readFrom; + } + + @Override + protected RealmDomain createTestInstance() { + return AuthenticationTestHelper.randomDomain(randomBoolean()); + } + +}