From 6cc90e655ec4209f7a3c30c4684af03cfc5a4b5a Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Mon, 7 Aug 2023 14:18:57 -0400 Subject: [PATCH] React to Strings changes from #9103 in core (#3110) ### Description React to changes from https://github.com/opensearch-project/OpenSearch/pull/9103 in core ### Check List - [ ] New functionality includes testing - [ ] New functionality has been documented - [ ] Commits are signed per the DCO using --signoff By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin). --------- Signed-off-by: Craig Perkins --- .../test/framework/TestSecurityConfig.java | 5 ++--- .../framework/cluster/LocalOpenSearchCluster.java | 2 +- .../test/framework/cluster/TestRestClient.java | 2 +- .../security/action/whoami/WhoAmIResponse.java | 6 +++--- .../security/auditlog/impl/AbstractAuditLog.java | 5 ++--- .../security/auditlog/impl/AuditMessage.java | 4 ++-- .../security/auditlog/impl/RequestResolver.java | 3 +-- .../configuration/ConfigurationRepository.java | 6 +++--- .../security/configuration/DlsFlsValveImpl.java | 12 +++++++----- .../org/opensearch/security/tools/SecurityAdmin.java | 7 ++++--- .../java/org/opensearch/security/ConfigTests.java | 2 +- .../auditlog/config/AuditConfigSerializeTest.java | 11 +++++------ .../org/opensearch/security/dlic/dlsfls/DlsTest.java | 2 +- .../security/dlic/dlsfls/FlsFlatTests.java | 3 +-- .../rest/validation/RequestContentValidatorTest.java | 3 +-- 15 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/integrationTest/java/org/opensearch/test/framework/TestSecurityConfig.java b/src/integrationTest/java/org/opensearch/test/framework/TestSecurityConfig.java index d44f10eba0..7b19d4f7f0 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/TestSecurityConfig.java +++ b/src/integrationTest/java/org/opensearch/test/framework/TestSecurityConfig.java @@ -52,7 +52,6 @@ import org.opensearch.action.index.IndexRequest; import org.opensearch.action.update.UpdateRequest; import org.opensearch.client.Client; -import org.opensearch.common.Strings; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.xcontent.ToXContentObject; @@ -680,7 +679,7 @@ private static String configToJson(CType configType, Map> getConfigurationsFromIndex( if (logComplianceEvent && auditLog.getComplianceConfig().isEnabled()) { CType configurationType = configTypes.iterator().next(); Map fields = new HashMap(); - fields.put(configurationType.toLCString(), Strings.toString(XContentType.JSON, retVal.get(configurationType))); + fields.put(configurationType.toLCString(), Strings.toString(MediaTypeRegistry.JSON, retVal.get(configurationType))); auditLog.logDocumentRead(this.securityIndex, configurationType.toLCString(), null, fields); } diff --git a/src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java b/src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java index de7ffe1fc2..cf2e77a25f 100644 --- a/src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java +++ b/src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java @@ -48,10 +48,10 @@ import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.Strings; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.common.Strings; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.index.query.ParsedQuery; import org.opensearch.core.rest.RestStatus; @@ -230,10 +230,10 @@ public boolean invoke( StringBuilder sb = new StringBuilder(); if (searchRequest.source() != null) { - sb.append(Strings.toString(XContentType.JSON, searchRequest.source()) + System.lineSeparator()); + sb.append(Strings.toString(MediaTypeRegistry.JSON, searchRequest.source()) + System.lineSeparator()); } - sb.append(Strings.toString(XContentType.JSON, af) + System.lineSeparator()); + sb.append(Strings.toString(MediaTypeRegistry.JSON, af) + System.lineSeparator()); LogManager.getLogger("debuglogger").error(sb.toString()); @@ -245,7 +245,9 @@ public boolean invoke( LogManager.getLogger("debuglogger") .error( "Shard requestcache enabled for " - + (searchRequest.source() == null ? "" : Strings.toString(XContentType.JSON, searchRequest.source())) + + (searchRequest.source() == null + ? "" + : Strings.toString(MediaTypeRegistry.JSON, searchRequest.source())) ); } diff --git a/src/main/java/org/opensearch/security/tools/SecurityAdmin.java b/src/main/java/org/opensearch/security/tools/SecurityAdmin.java index 7d8a8a7b0b..b6b1f2fe84 100644 --- a/src/main/java/org/opensearch/security/tools/SecurityAdmin.java +++ b/src/main/java/org/opensearch/security/tools/SecurityAdmin.java @@ -109,7 +109,7 @@ import org.opensearch.client.indices.GetIndexResponse; import org.opensearch.client.transport.NoNodeAvailableException; import org.opensearch.cluster.health.ClusterHealthStatus; -import org.opensearch.common.Strings; +import org.opensearch.core.common.Strings; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.common.collect.Tuple; import org.opensearch.common.settings.Settings; @@ -118,6 +118,7 @@ import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.core.xcontent.MediaType; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; @@ -1240,7 +1241,7 @@ private static String convertToYaml(String type, BytesReference bytes, boolean p builder.prettyPrint(); } builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML); - return Strings.toString(builder); + return builder.toString(); } } @@ -1267,7 +1268,7 @@ protected static void generateDiagnoseTrace(final RestHighLevelClient restHighLe try { sb.append("ClusterHealthRequest:" + System.lineSeparator()); ClusterHealthResponse nir = restHighLevelClient.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT); - sb.append(Strings.toString(XContentType.JSON, nir, true, true)); + sb.append(Strings.toString(MediaTypeRegistry.JSON, nir, true, true)); } catch (Exception e1) { sb.append(ExceptionsHelper.stackTrace(e1)); } diff --git a/src/test/java/org/opensearch/security/ConfigTests.java b/src/test/java/org/opensearch/security/ConfigTests.java index c64ca55318..7fc12ef92d 100644 --- a/src/test/java/org/opensearch/security/ConfigTests.java +++ b/src/test/java/org/opensearch/security/ConfigTests.java @@ -27,9 +27,9 @@ import org.junit.Assert; import org.junit.Test; -import org.opensearch.common.Strings; import org.opensearch.common.collect.Tuple; import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.common.Strings; import org.opensearch.security.securityconf.Migration; import org.opensearch.security.securityconf.impl.CType; import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration; diff --git a/src/test/java/org/opensearch/security/auditlog/config/AuditConfigSerializeTest.java b/src/test/java/org/opensearch/security/auditlog/config/AuditConfigSerializeTest.java index 8cc19fa0f5..33dad63e5f 100644 --- a/src/test/java/org/opensearch/security/auditlog/config/AuditConfigSerializeTest.java +++ b/src/test/java/org/opensearch/security/auditlog/config/AuditConfigSerializeTest.java @@ -24,7 +24,6 @@ import org.junit.Before; import org.junit.Test; -import org.opensearch.common.Strings; import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.xcontent.XContentBuilder; @@ -88,7 +87,7 @@ public void testDefaultSerialize() throws IOException { .endObject() .endObject(); - assertTrue(compareJson(Strings.toString(jsonBuilder), json)); + assertTrue(compareJson(jsonBuilder.toString(), json)); } @Test @@ -148,7 +147,7 @@ public void testDeserialize() throws IOException { .field("write_ignore_users", Collections.singletonList("test-user-3")) .endObject() .endObject(); - final String json = Strings.toString(jsonBuilder); + final String json = jsonBuilder.toString(); // act final AuditConfig auditConfig = objectMapper.readValue(json, AuditConfig.class); @@ -246,7 +245,7 @@ public void testSerialize() throws IOException { // act final String json = objectMapper.writeValueAsString(auditConfig); // assert - assertTrue(compareJson(Strings.toString(jsonBuilder), json)); + assertTrue(compareJson(jsonBuilder.toString(), json)); } @Test @@ -288,7 +287,7 @@ public void testNullSerialize() throws IOException { // act final String json = objectMapper.writeValueAsString(auditConfig); // assert - assertTrue(compareJson(Strings.toString(jsonBuilder), json)); + assertTrue(compareJson(jsonBuilder.toString(), json)); } @Test @@ -348,7 +347,7 @@ public void testCustomSettings() throws IOException { .field("write_log_diffs", false) .endObject() .endObject(); - final String json = Strings.toString(jsonBuilder); + final String json = jsonBuilder.toString(); // act final AuditConfig auditConfig = customObjectMapper.readValue(json, AuditConfig.class); diff --git a/src/test/java/org/opensearch/security/dlic/dlsfls/DlsTest.java b/src/test/java/org/opensearch/security/dlic/dlsfls/DlsTest.java index 48f63ec555..d43a804d47 100644 --- a/src/test/java/org/opensearch/security/dlic/dlsfls/DlsTest.java +++ b/src/test/java/org/opensearch/security/dlic/dlsfls/DlsTest.java @@ -20,8 +20,8 @@ import org.opensearch.action.search.SearchRequest; import org.opensearch.action.support.WriteRequest.RefreshPolicy; import org.opensearch.client.Client; -import org.opensearch.common.Strings; import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.common.Strings; import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse; public class DlsTest extends AbstractDlsFlsTest { diff --git a/src/test/java/org/opensearch/security/dlic/dlsfls/FlsFlatTests.java b/src/test/java/org/opensearch/security/dlic/dlsfls/FlsFlatTests.java index e4b3063138..4d3e2d1846 100644 --- a/src/test/java/org/opensearch/security/dlic/dlsfls/FlsFlatTests.java +++ b/src/test/java/org/opensearch/security/dlic/dlsfls/FlsFlatTests.java @@ -18,7 +18,6 @@ import org.opensearch.action.index.IndexRequest; import org.opensearch.action.support.WriteRequest.RefreshPolicy; import org.opensearch.client.Client; -import org.opensearch.common.Strings; import org.opensearch.common.xcontent.XContentType; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.security.test.DynamicSecurityConfig; @@ -65,7 +64,7 @@ protected void populateData(final Client tc) { } catch (final Exception e) { throw new RuntimeException(e); } - final String mappings = Strings.toString(builder); + final String mappings = builder.toString(); final Consumer createIndexWithMapping = (indexName) -> { final CreateIndexRequest createIndex = new CreateIndexRequest(indexName); diff --git a/src/test/java/org/opensearch/security/dlic/rest/validation/RequestContentValidatorTest.java b/src/test/java/org/opensearch/security/dlic/rest/validation/RequestContentValidatorTest.java index 55b8664188..a64de6ddeb 100644 --- a/src/test/java/org/opensearch/security/dlic/rest/validation/RequestContentValidatorTest.java +++ b/src/test/java/org/opensearch/security/dlic/rest/validation/RequestContentValidatorTest.java @@ -21,7 +21,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import org.opensearch.common.Strings; import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.common.bytes.BytesArray; @@ -301,7 +300,7 @@ public Map allowedKeys() { private JsonNode xContentToJsonNode(final ToXContent toXContent) throws IOException { try (final var xContentBuilder = XContentFactory.jsonBuilder()) { toXContent.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); - return DefaultObjectMapper.readTree(Strings.toString(xContentBuilder)); + return DefaultObjectMapper.readTree(xContentBuilder.toString()); } }