From 8063e1b64f880afb7dc54311f3110b162b913038 Mon Sep 17 00:00:00 2001 From: Ryan Liang <109499885+RyanL1997@users.noreply.github.com> Date: Tue, 25 Jul 2023 10:41:31 -0700 Subject: [PATCH] Adapt MediaType library change in core (#3050) * Adapt the MediaType changes in core Signed-off-by: Ryan Liang --- .../auth/http/saml/AuthTokenProcessorHandler.java | 2 +- .../security/auditlog/impl/AbstractAuditLog.java | 3 ++- .../security/auditlog/impl/AuditMessage.java | 5 +++-- .../security/auditlog/impl/RequestResolver.java | 11 ++++++----- .../dlic/rest/api/PatchableResourceApiAction.java | 2 +- .../auth/http/saml/HTTPSamlAuthenticatorTest.java | 7 +++---- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java b/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java index fe995b3287..3fee4a9444 100644 --- a/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java +++ b/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java @@ -192,7 +192,7 @@ private boolean handleLowLevel(RestRequest restRequest, RestChannel restChannel) XPathExpressionException, ParserConfigurationException, SAXException, SettingsException { try { - if (restRequest.getXContentType() != XContentType.JSON) { + if (restRequest.getMediaType() != XContentType.JSON) { throw new OpenSearchSecurityException( "/_opendistro/_security/api/authtoken expects content with type application/json", RestStatus.UNSUPPORTED_MEDIA_TYPE diff --git a/src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java b/src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java index d4cc756697..f884738f4b 100644 --- a/src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java +++ b/src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java @@ -48,6 +48,7 @@ import org.opensearch.common.settings.Settings; import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.xcontent.XContentHelper; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -657,7 +658,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index // originalResult.internalSourceRef())); // current source, normally not null or empty - msg.addTupleToRequestBody(new Tuple(XContentType.JSON, currentIndex.source())); + msg.addTupleToRequestBody(new Tuple(XContentType.JSON, currentIndex.source())); } } diff --git a/src/main/java/org/opensearch/security/auditlog/impl/AuditMessage.java b/src/main/java/org/opensearch/security/auditlog/impl/AuditMessage.java index f7e4255e03..34f8f1fef5 100644 --- a/src/main/java/org/opensearch/security/auditlog/impl/AuditMessage.java +++ b/src/main/java/org/opensearch/security/auditlog/impl/AuditMessage.java @@ -38,6 +38,7 @@ import org.opensearch.common.collect.Tuple; import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.xcontent.XContentHelper; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.common.Strings; @@ -205,7 +206,7 @@ void addSecurityConfigWriteDiffSource(final String diff, final String id) { // } // } - public void addTupleToRequestBody(Tuple xContentTuple) { + public void addTupleToRequestBody(Tuple xContentTuple) { if (xContentTuple != null) { try { auditInfo.put(REQUEST_BODY, XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1())); @@ -377,7 +378,7 @@ void addRestRequestInfo(final RestRequest request, final AuditConfig.Filter filt addRestMethod(request.method()); if (filter.shouldLogRequestBody() && request.hasContentOrSourceParam()) { try { - final Tuple xContentTuple = request.contentOrSourceParam(); + final Tuple xContentTuple = request.contentOrSourceParam(); final String requestBody = XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1()); if (path != null && requestBody != null diff --git a/src/main/java/org/opensearch/security/auditlog/impl/RequestResolver.java b/src/main/java/org/opensearch/security/auditlog/impl/RequestResolver.java index c37247e7db..198fbe6366 100644 --- a/src/main/java/org/opensearch/security/auditlog/impl/RequestResolver.java +++ b/src/main/java/org/opensearch/security/auditlog/impl/RequestResolver.java @@ -47,6 +47,7 @@ import org.opensearch.common.settings.Settings; import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.xcontent.XContentFactory; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.index.Index; @@ -381,7 +382,7 @@ private static void addIndicesSourceSafe( final String[] indices, final IndexNameExpressionResolver resolver, final ClusterService cs, - final XContentType xContentType, + final MediaType mediaType, final Object source, final Settings settings, boolean resolveIndices, @@ -423,14 +424,14 @@ private static void addIndicesSourceSafe( if (sourceIsSensitive && source != null) { if (!allIndicesMatcher.test(securityIndex)) { if (source instanceof BytesReference) { - msg.addTupleToRequestBody(convertSource(xContentType, (BytesReference) source)); + msg.addTupleToRequestBody(convertSource(mediaType, (BytesReference) source)); } else { msg.addMapToRequestBody((Map) source); } } } else if (source != null) { if (source instanceof BytesReference) { - msg.addTupleToRequestBody(convertSource(xContentType, (BytesReference) source)); + msg.addTupleToRequestBody(convertSource(mediaType, (BytesReference) source)); } else { msg.addMapToRequestBody((Map) source); } @@ -438,12 +439,12 @@ private static void addIndicesSourceSafe( } } - private static Tuple convertSource(XContentType type, BytesReference bytes) { + private static Tuple convertSource(MediaType type, BytesReference bytes) { if (type == null) { type = XContentType.JSON; } - return new Tuple(type, bytes); + return new Tuple(type, bytes); } private static String[] arrayOrEmpty(String[] array) { diff --git a/src/main/java/org/opensearch/security/dlic/rest/api/PatchableResourceApiAction.java b/src/main/java/org/opensearch/security/dlic/rest/api/PatchableResourceApiAction.java index 6050b9785e..f630a6bab7 100644 --- a/src/main/java/org/opensearch/security/dlic/rest/api/PatchableResourceApiAction.java +++ b/src/main/java/org/opensearch/security/dlic/rest/api/PatchableResourceApiAction.java @@ -71,7 +71,7 @@ public PatchableResourceApiAction( } private void handlePatch(RestChannel channel, final RestRequest request, final Client client) throws IOException { - if (request.getXContentType() != XContentType.JSON) { + if (request.getMediaType() != XContentType.JSON) { badRequestResponse(channel, "PATCH accepts only application/json"); return; } diff --git a/src/test/java/com/amazon/dlic/auth/http/saml/HTTPSamlAuthenticatorTest.java b/src/test/java/com/amazon/dlic/auth/http/saml/HTTPSamlAuthenticatorTest.java index eec2ff76b7..edec52bb33 100644 --- a/src/test/java/com/amazon/dlic/auth/http/saml/HTTPSamlAuthenticatorTest.java +++ b/src/test/java/com/amazon/dlic/auth/http/saml/HTTPSamlAuthenticatorTest.java @@ -45,7 +45,7 @@ import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.settings.Settings; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.rest.RestChannel; import org.opensearch.rest.RestRequest; @@ -992,7 +992,7 @@ public XContentBuilder newErrorBuilder() throws IOException { } @Override - public XContentBuilder newBuilder(XContentType xContentType, boolean useFiltering) throws IOException { + public XContentBuilder newBuilder(MediaType mediaType, boolean useFiltering) throws IOException { return null; } @@ -1018,8 +1018,7 @@ public void sendResponse(RestResponse response) { } @Override - public XContentBuilder newBuilder(XContentType xContentType, XContentType responseContentType, boolean useFiltering) - throws IOException { + public XContentBuilder newBuilder(MediaType mediaType, MediaType responseContentType, boolean useFiltering) throws IOException { return null; }