From 73d553f2ceed4c2bc1134b19fb2fbf078b02da90 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Mon, 31 Jul 2023 09:20:23 -0400 Subject: [PATCH] Fix build after Lucene upgrade and breaking XContentFactory changes (#3069) There are multiple PRs in core affecting the security plugin that the security plugin needs to adapt to. - https://github.com/opensearch-project/OpenSearch/pull/7792 - https://github.com/opensearch-project/OpenSearch/pull/8826 - https://github.com/opensearch-project/OpenSearch/pull/8668 I am opening a Draft PR that includes a fix for the Lucene-related test failures which was caused by https://github.com/opensearch-project/OpenSearch/pull/7792 Resolves: https://github.com/opensearch-project/security/issues/3064 Signed-off-by: Craig Perkins (cherry picked from commit 08d17343c0821f7b7528a9559fc3384af5aa2c5b) --- .../configuration/DlsFlsFilterLeafReader.java | 25 +++++++++++++++++++ .../security/support/ConfigHelper.java | 2 +- .../security/tools/SecurityAdmin.java | 2 +- .../security/test/helper/file/FileHelper.java | 4 +-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/security/configuration/DlsFlsFilterLeafReader.java b/src/main/java/org/opensearch/security/configuration/DlsFlsFilterLeafReader.java index 508c3dd9b4..0966a3f3ac 100644 --- a/src/main/java/org/opensearch/security/configuration/DlsFlsFilterLeafReader.java +++ b/src/main/java/org/opensearch/security/configuration/DlsFlsFilterLeafReader.java @@ -46,6 +46,7 @@ import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.StoredFieldVisitor; +import org.apache.lucene.index.StoredFields; import org.apache.lucene.index.TermState; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; @@ -473,6 +474,24 @@ public void close() throws IOException { } } + private class DlsFlsStoredFields extends StoredFields { + private final StoredFields in; + + public DlsFlsStoredFields(StoredFields storedFields) { + this.in = storedFields; + } + + @Override + public void document(final int docID, StoredFieldVisitor visitor) throws IOException { + visitor = getDlsFlsVisitor(visitor); + try { + in.document(docID, visitor); + } finally { + finishVisitor(visitor); + } + } + } + @Override protected StoredFieldsReader doGetSequentialStoredFieldsReader(final StoredFieldsReader reader) { return new DlsFlsStoredFieldsReader(reader); @@ -1284,6 +1303,12 @@ public TermState termState() throws IOException { } + @Override + public StoredFields storedFields() throws IOException { + ensureOpen(); + return new DlsFlsStoredFields(in.storedFields()); + } + private String getRuntimeActionName() { return (String) threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_ACTION_NAME); } diff --git a/src/main/java/org/opensearch/security/support/ConfigHelper.java b/src/main/java/org/opensearch/security/support/ConfigHelper.java index 92bf069028..434a8a02fa 100644 --- a/src/main/java/org/opensearch/security/support/ConfigHelper.java +++ b/src/main/java/org/opensearch/security/support/ConfigHelper.java @@ -141,7 +141,7 @@ public static BytesReference readXContent(final Reader reader, final MediaType m BytesReference retVal; XContentParser parser = null; try { - parser = XContentFactory.xContent(mediaType).createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, reader); + parser = mediaType.xContent().createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); diff --git a/src/main/java/org/opensearch/security/tools/SecurityAdmin.java b/src/main/java/org/opensearch/security/tools/SecurityAdmin.java index 3cf1428d05..8e3ceb85f3 100644 --- a/src/main/java/org/opensearch/security/tools/SecurityAdmin.java +++ b/src/main/java/org/opensearch/security/tools/SecurityAdmin.java @@ -1194,7 +1194,7 @@ private static BytesReference readXContent(final String content, final MediaType BytesReference retVal; XContentParser parser = null; try { - parser = XContentFactory.xContent(mediaType).createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, content); + parser = mediaType.xContent().createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, content); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); diff --git a/src/test/java/org/opensearch/security/test/helper/file/FileHelper.java b/src/test/java/org/opensearch/security/test/helper/file/FileHelper.java index 2bf66a265b..b23c59b861 100644 --- a/src/test/java/org/opensearch/security/test/helper/file/FileHelper.java +++ b/src/test/java/org/opensearch/security/test/helper/file/FileHelper.java @@ -104,7 +104,7 @@ public static BytesReference readYamlContent(final String file) { XContentParser parser = null; try { - parser = XContentFactory.xContent(XContentType.YAML) + parser = XContentType.YAML.xContent() .createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, new StringReader(loadFile(file))); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); @@ -127,7 +127,7 @@ public static BytesReference readYamlContentFromString(final String yaml) { XContentParser parser = null; try { - parser = XContentFactory.xContent(XContentType.YAML) + parser = XContentType.YAML.xContent() .createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, new StringReader(yaml)); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder();