Skip to content

Commit

Permalink
Fix build after Lucene upgrade and breaking XContentFactory changes (o…
Browse files Browse the repository at this point in the history
…pensearch-project#3069)

There are multiple PRs in core affecting the security plugin that the
security plugin needs to adapt to.

- opensearch-project/OpenSearch#7792
- opensearch-project/OpenSearch#8826
- opensearch-project/OpenSearch#8668

I am opening a Draft PR that includes a fix for the Lucene-related test
failures which was caused by
opensearch-project/OpenSearch#7792

Resolves: opensearch-project#3064

Signed-off-by: Craig Perkins <cwperx@amazon.com>
  • Loading branch information
cwperks authored Jul 31, 2023
1 parent d3488e8 commit 08d1734
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
Expand Down Expand Up @@ -300,7 +299,7 @@ public Map<String, RequestContentValidator.DataType> allowedKeys() {
}

private JsonNode xContentToJsonNode(final ToXContent toXContent) throws IOException {
try (final var xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON)) {
try (final var xContentBuilder = XContentFactory.jsonBuilder()) {
toXContent.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
return DefaultObjectMapper.readTree(Strings.toString(xContentBuilder));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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();
Expand All @@ -133,7 +133,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();
Expand Down

0 comments on commit 08d1734

Please sign in to comment.