diff --git a/gradle/code-coverage.gradle b/gradle/code-coverage.gradle index 4ad1965b212ca..3ca6b1fe84ea7 100644 --- a/gradle/code-coverage.gradle +++ b/gradle/code-coverage.gradle @@ -15,7 +15,6 @@ repositories { maven { url = "https://ci.opensearch.org/ci/dbc/snapshots/lucene/" } - maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } } allprojects { diff --git a/plugins/query-insights/build.gradle b/plugins/query-insights/build.gradle index 99dfaea44e6e0..eabbd395bd3bd 100644 --- a/plugins/query-insights/build.gradle +++ b/plugins/query-insights/build.gradle @@ -14,24 +14,5 @@ opensearchplugin { classname 'org.opensearch.plugin.insights.QueryInsightsPlugin' } -dependencyLicenses.enabled = false - -ext { - opensearch_version = versions.opensearch - // 3.0.0-SNAPSHOT -> 3.0.0.0 - version_tokens = opensearch_version.tokenize('-') - common_utils_version = version_tokens[0] + '.0' - if (version_tokens.size() > 1) { - common_utils_version = common_utils_version + "-" + version_tokens[1] - } -} - -repositories { - mavenLocal() - mavenCentral() - maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } -} - dependencies { - implementation "org.opensearch:common-utils:${common_utils_version}" } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java index cec3b3d583319..380a1366d77d9 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java @@ -16,13 +16,12 @@ import org.opensearch.action.search.SearchRequestOperationsListener; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; -import org.opensearch.commons.authuser.User; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.plugin.insights.core.service.QueryInsightsService; import org.opensearch.plugin.insights.rules.model.Attribute; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; -import org.opensearch.plugin.insights.settings.QueryInsightsSettings; +import org.opensearch.plugin.insights.utils.ThreadContextParser; import org.opensearch.threadpool.ThreadPool; import java.util.Collections; @@ -149,15 +148,7 @@ public void onRequestEnd(final SearchPhaseContext context, final SearchRequestCo attributes.put(Attribute.INDICES, request.indices()); attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap()); // add user related information - Object userInfo = threadPool.getThreadContext().getTransient(QueryInsightsSettings.REQUEST_HEADER_USER_INFO); - if (userInfo != null) { - attributes.put(Attribute.USER, User.parse(userInfo.toString())); - } - // add remote ip address - Object remoteAddress = threadPool.getThreadContext().getTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS); - if (remoteAddress != null) { - attributes.put(Attribute.REMOTE_ADDRESS, remoteAddress.toString()); - } + attributes.putAll(ThreadContextParser.getUserInfoFromThreadContext(threadPool.getThreadContext())); SearchQueryRecord record = new SearchQueryRecord(request.getOrCreateAbsoluteStartMillis(), measurements, attributes); queryInsightsService.addRecord(record); } catch (Exception e) { diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java index fd16285d203f6..9b98f84afc4b4 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java @@ -49,9 +49,21 @@ public enum Attribute { */ REMOTE_ADDRESS, /** - * Information related to the user who sent this request + * Username of the user who sent this request */ - USER; + USER_NAME, + /** + * Backend roles of the user who sent this request + */ + USER_BACKEND_ROLES, + /** + * Roles of the user who sent this request + */ + USER_ROLES, + /** + * Tenant info of the user who sent this request + */ + USER_TENANT; /** * Read an Attribute from a StreamInput diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/ThreadContextParser.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/ThreadContextParser.java new file mode 100644 index 0000000000000..ca1bba1d07553 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/ThreadContextParser.java @@ -0,0 +1,64 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.plugin.insights.utils; + +import org.opensearch.common.util.concurrent.ThreadContext; +import org.opensearch.core.common.Strings; +import org.opensearch.plugin.insights.rules.model.Attribute; +import org.opensearch.plugin.insights.settings.QueryInsightsSettings; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * Helper class to parse information from the thread context + */ +public final class ThreadContextParser { + + private ThreadContextParser() {} + + /** + * Get User info from the thread context + * + * @param threadContext context of the thread + * @return Map of {@link Attribute} and the corresponding values + */ + public static Map getUserInfoFromThreadContext(ThreadContext threadContext) { + Map userInfoMap = new HashMap<>(); + if (threadContext == null) { + return userInfoMap; + } + Object userInfoObj = threadContext.getTransient(QueryInsightsSettings.REQUEST_HEADER_USER_INFO); + if (userInfoObj == null) { + return userInfoMap; + } + String userInfoStr = userInfoObj.toString(); + Object remoteAddressObj = threadContext.getTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS); + if (remoteAddressObj != null) { + userInfoMap.put(Attribute.REMOTE_ADDRESS, remoteAddressObj.toString()); + } + + String[] userInfo = userInfoStr.split("\\|"); + if ((userInfo.length == 0) || (Strings.isNullOrEmpty(userInfo[0]))) { + return userInfoMap; + } + userInfoMap.put(Attribute.USER_NAME, userInfo[0].trim()); + if ((userInfo.length > 1) && !Strings.isNullOrEmpty(userInfo[1])) { + userInfoMap.put(Attribute.USER_BACKEND_ROLES, Arrays.asList(userInfo[1].split(","))); + } + if ((userInfo.length > 2) && !Strings.isNullOrEmpty(userInfo[2])) { + userInfoMap.put(Attribute.USER_ROLES, Arrays.asList(userInfo[2].split(","))); + } + if ((userInfo.length > 3) && !Strings.isNullOrEmpty(userInfo[3])) { + userInfoMap.put(Attribute.USER_TENANT, userInfo[3].trim()); + } + return userInfoMap; + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/package-info.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/package-info.java new file mode 100644 index 0000000000000..220329a8d4bf4 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/utils/package-info.java @@ -0,0 +1,12 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** + * Utils for Query Insights Plugin + */ +package org.opensearch.plugin.insights.utils; diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java index ad173734798c1..74fd959050c68 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java @@ -16,8 +16,6 @@ import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.commons.InjectSecurity; -import org.opensearch.commons.authuser.User; import org.opensearch.plugin.insights.core.service.QueryInsightsService; import org.opensearch.plugin.insights.core.service.TopQueriesService; import org.opensearch.plugin.insights.rules.model.Attribute; @@ -60,7 +58,10 @@ public class QueryInsightsListenerTests extends OpenSearchTestCase { private final Settings.Builder settingsBuilder = Settings.builder(); private final Settings settings = settingsBuilder.build(); private final String remoteAddress = "1.2.3.4"; - private User user; + private final String userName = "user1"; + private final List userBackendRoles = List.of("bk-role1", "bk-role2"); + private final List userRoles = List.of("role1", "role2"); + private final String userTenant = "tenant1"; private ClusterService clusterService; @Before @@ -75,9 +76,10 @@ public void setup() { // inject user info ThreadContext threadContext = new ThreadContext(settings); - user = new User("user-1", List.of("role1", "role2"), List.of("role3", "role4"), List.of()); - InjectSecurity injector = new InjectSecurity("id", settings, threadContext); - injector.injectUserInfo(user); + threadContext.putTransient( + QueryInsightsSettings.REQUEST_HEADER_USER_INFO, + userName + '|' + String.join(",", userBackendRoles) + "|" + String.join(",", userRoles) + "|" + userTenant + ); threadContext.putTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS, remoteAddress); when(threadPool.getThreadContext()).thenReturn(threadContext); } @@ -120,7 +122,10 @@ public void testOnRequestEnd() { assertEquals(numberOfShards, attrs.get(Attribute.TOTAL_SHARDS)); assertEquals(indices, attrs.get(Attribute.INDICES)); assertEquals(phaseLatencyMap, attrs.get(Attribute.PHASE_LATENCY_MAP)); - assertEquals(user, attrs.get(Attribute.USER)); + assertEquals(userName, attrs.get(Attribute.USER_NAME)); + assertEquals(userBackendRoles, attrs.get(Attribute.USER_BACKEND_ROLES)); + assertEquals(userRoles, attrs.get(Attribute.USER_ROLES)); + assertEquals(userTenant, attrs.get(Attribute.USER_TENANT)); assertEquals(remoteAddress, attrs.get(Attribute.REMOTE_ADDRESS)); }