Introduce Insights API#1610
Conversation
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie <jkhanjob@gmail.com>
4883d42 to
77fcda9
Compare
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie <jkhanjob@gmail.com>
0eba385 to
49018e0
Compare
|
CI failed due to jacoco changes in build.gradle. Not sure how to fix. One naive way is to add correlation request, response, and Action in AD to avoid ml-commons dependency. |
src/main/java/org/opensearch/ad/rest/RestInsightsJobAction.java
Outdated
Show resolved
Hide resolved
src/main/java/org/opensearch/ad/ml/MLMetricsCorrelationInputBuilder.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Jackie <jkhanjob@gmail.com>
e665658 to
7525e0d
Compare
kaituo
left a comment
There was a problem hiding this comment.
CI failed due to:
* What went wrong:
Execution failed for task ':jacocoTestCoverageVerification'.
> A failure occurred while executing org.gradle.internal.jacoco.JacocoCoverageAction
> Rule violated for class org.opensearch.ad.InsightsJobProcessor.CorrelationPayload: lines covered ratio is 0.00, but expected minimum is 0.75
Rule violated for class org.opensearch.ad.InsightsJobProcessor: branches covered ratio is 0.28, but expected minimum is 0.60
Rule violated for class org.opensearch.ad.InsightsJobProcessor: lines covered ratio is 0.45, but expected minimum is 0.75
Rule violated for class org.opensearch.ad.ml.InsightsGenerator: branches covered ratio is 0.00, but expected minimum is 0.60
Rule violated for class org.opensearch.ad.ml.InsightsGenerator: lines covered ratio is 0.00, but expected minimum is 0.75
Rule violated for class org.opensearch.ad.indices.ADIndexManagement: branches covered ratio is 0.33, but expected minimum is 0.60
Rule violated for class org.opensearch.ad.indices.ADIndexManagement: lines covered ratio is 0.67, but expected minimum is 0.75
src/main/java/org/opensearch/ad/transport/InsightsJobTransportAction.java
Outdated
Show resolved
Hide resolved
src/main/java/org/opensearch/ad/transport/InsightsJobTransportAction.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Jackie <jkhanjob@gmail.com>
5c9dabd to
85db8bb
Compare
Signed-off-by: Jackie <jkhanjob@gmail.com>
05bba14 to
bb524d1
Compare
| "num_series": { | ||
| "type": "integer" | ||
| }, | ||
| "num_models": { | ||
| "type": "integer" |
There was a problem hiding this comment.
This comment is not addressed
Signed-off-by: Jackie <jkhanjob@gmail.com>
7271a7a to
55e645c
Compare
kaituo
left a comment
There was a problem hiding this comment.
Reviewed a few lines and got similar questions.
src/main/java/org/opensearch/ad/rest/handler/InsightsJobActionHandler.java
Outdated
Show resolved
Hide resolved
| // init insights-results index (customer-owned index). | ||
| // IMPORTANT: for public REST requests, the security plugin already populated user context in thread context. | ||
| // Injecting roles here can overwrite/misrepresent the authenticated user (e.g. empty roles), leading to 403. | ||
| indexManagement.initInsightsResultIndexIfAbsent(ActionListener.wrap(createIndexResponse -> { |
There was a problem hiding this comment.
If a user has no permission to create index, it is a security leak to create these user index.
There was a problem hiding this comment.
changed to use pluginClient because of seeing this error in security test:
SecureADRestIT > testInsightsApisUseSystemContextForJobIndex FAILED
org.opensearch.client.ResponseException: method [POST], host [https://127.0.0.1:38067], URI [/_plugins/_anomaly_detection/insights/_start], status line [HTTP/2.0 403 Forbidden]
{"error":{"root_cause":[{"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"}],"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"},"status":403}
The failure error is 403 no permissions for [indices:admin/create, indices:admin/aliases] and associated roles [], happening during job index creation when running as Alice inside of InsightsJobActionHandler. The handler is stashing thread context before initJobIndex(), the roles[] indicates somewhere we're running the index create call without the authenticated user and without any injected roles, so the security plugin treats it as "no roles", and denies indices:admin/cream/aliases. After investigating and exploring the repo, made the following changes:
- stop injecting roles for insights result index creation
- init
insights-resultsindex is triggered from the REST start call (insights/_start), so it runs on a thread where the security plugin has already set the real authenticated user context. In that situation we should not inject the job user, because we'd be overwriting a correct, already authenticated context, and risk injecting roles=[]. - but when search/write insights-results during job execution, that happens in the backend job thread, where there is no REST user context. There we do need job user injection (we already inject
queryCustomResultIndexwithInjectSecurityandwriteInsightsToIndexwithInjectSecurity)
- init
- use
pluginClientfor job index operations, especially the create job index step which needsindices:admin/create+indices:admin/aliases.- stash context only clears the thread context security headers (user/roles), so the request doesn't accidentally run as the caller.
localClient+stashed context, the request runs with no user/roles in headers. Under Security, that often becomes "anonymous/no backend roles", so system index reads can still be denied (the error we saw:associated roles[])pluginClient+stashed context, the request is executed as the plugin's system subject, gives it the ability to access system indices under security.
why we still stash context when using pluginClient?
even when using pluginClient, stashing is a safety measure to ensure no caller context leaks into the request, prevents accidental authZ behavior. The privilege comes from which client executes the request, not from stashing.
why we use localClient + stashContext at other places in this repo and it still works
- A lot of stashed context calls are for
cluster/state/metadata/monitoringstyle actions, or for indices that are not treated as protected system indices in that scenario. They're not actually doing system index access - They stash context, but later explicitly run as a user. Seeing example pattern in transport actions: stash early to avoid leaking caller context, then later restore or inject user/roles when doing customer-owned index operations. So it’s not “stashed-only” end-to-end.
why Insights needed pluginClient even with stashContext
For system indices, "no user" is not the same as system. pluginClient is different because it executes under the plugin's system subject. stashContext() is a safety mechanism (avoid leaking caller), but pluginClient is an authorization mechanism (run as system subject).
There was a problem hiding this comment.
pasting full stack trace of the failed security test before:
SecureADRestIT > testInsightsApisUseSystemContextForJobIndex FAILED
org.opensearch.client.ResponseException: method [POST], host [https://127.0.0.1:41231/], URI [/_plugins/_anomaly_detection/insights/_start], status line [HTTP/2.0 403 Forbidden]
{"error":{"root_cause":[{"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"}],"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"},"status":403}
at __randomizedtesting.SeedInfo.seed([ACF4EF0F436E5C7E:D7004542BB6F5CF4]:0)
at app//org.opensearch.client.RestClient.convertResponse(RestClient.java:494)
at app//org.opensearch.client.RestClient.performRequest(RestClient.java:383)
at app//org.opensearch.client.RestClient.performRequest(RestClient.java:358)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:236)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:209)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:198)
at app//org.opensearch.ad.rest.SecureADRestIT.testInsightsApisUseSystemContextForJobIndex(SecureADRestIT.java:1173)
Suite: Test class org.opensearch.ad.rest.SecureADRestIT
1> [2026-01-30T01:07:11,309][INFO ][o.o.a.r.SecureADRestIT ] [testValidateAnomalyDetector] before test
1> [2026-01-30T01:07:11,310][INFO ][o.o.a.r.SecureADRestIT ] [testValidateAnomalyDetector] initializing REST clients against [https://[::1]:39561, https://127.0.0.1:41231]/
1> [2026-01-30T01:07:32,886][WARN ][o.o.c.RestClient ] [testValidateAnomalyDetector]COMMONS-LOGGING request [PUT https://127.0.0.1:41231/_cluster/settings] returned 1 warnings: [299 OpenSearch-3.5.0-SNAPSHOT-2f29662a6de42208fded173a5ac78c0d486f8b0a "[opendistro.anomaly_detection.filter_by_backend_roles] setting was deprecated in OpenSearch and will be removed in a future release! See the breaking changes documentation for the next major version."]
1> [2026-01-30T01:08:03,824][INFO ][o.o.a.r.SecureADRestIT ] [testValidateAnomalyDetector] after test
1> [2026-01-30T01:08:03,827][INFO ][o.o.a.r.SecureADRestIT ] [testGetDetector] before test
1> [2026-01-30T01:08:42,945][INFO ][o.o.a.r.SecureADRestIT ] [testGetDetector] after test
1> [2026-01-30T01:08:42,951][INFO ][o.o.a.r.SecureADRestIT ] [testStartAndStopDetector] before test
1> [2026-01-30T01:09:29,540][INFO ][o.o.a.r.SecureADRestIT ] [testStartAndStopDetector] after test
2> REPRODUCE WITH: ./gradlew ':integTest' --tests 'org.opensearch.ad.rest.SecureADRestIT.testInsightsApisUseSystemContextForJobIndex' -Dtests.seed=ACF4EF0F436E5C7E -Dtests.security.manager=false -Dtests.jvm.argline="-XX:TieredStopAtLevel=1 -XX:ReservedCodeCacheSize=64m" -Dtests.locale=so -Dtests.timezone=Arctic/Longyearbyen -Druntime.java=21
2> org.opensearch.client.ResponseException: method [POST], host [https://127.0.0.1:41231/], URI [/_plugins/_anomaly_detection/insights/_start], status line [HTTP/2.0 403 Forbidden]
{"error":{"root_cause":[{"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"}],"type":"security_exception","reason":"no permissions for [indices:admin/create, indices:admin/aliases] and associated roles []"},"status":403}
at __randomizedtesting.SeedInfo.seed([ACF4EF0F436E5C7E:D7004542BB6F5CF4]:0)
at app//org.opensearch.client.RestClient.convertResponse(RestClient.java:494)
at app//org.opensearch.client.RestClient.performRequest(RestClient.java:383)
at app//org.opensearch.client.RestClient.performRequest(RestClient.java:358)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:236)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:209)
at app//org.opensearch.timeseries.TestHelpers.makeRequest(TestHelpers.java:198)
at app//org.opensearch.ad.rest.SecureADRestIT.testInsightsApisUseSystemContextForJobIndex(SecureADRestIT.java:1173)
1> [2026-01-30T01:09:29,543][INFO ][o.o.a.r.SecureADRestIT ] [testSearchDetector] before test
1> [2026-01-30T01:10:08,778][INFO ][o.o.a.r.SecureADRestIT ] [testSearchDetector] after test
1> [2026-01-30T01:10:08,781][INFO ][o.o.a.r.SecureADRestIT ] [testInsightsApisUseSystemContextForJobIndex] before test
1> [2026-01-30T01:10:46,507][INFO ][o.o.a.r.SecureADRestIT ] [testInsightsApisUseSystemContextForJobIndex] after test
1> [2026-01-30T01:10:46,522][INFO ][o.o.a.r.SecureADRestIT ] [testUpdateDetector] before test
1> [2026-01-30T01:11:27,504][INFO ][o.o.a.r.SecureADRestIT ] [testUpdateDetector] after test
1> [2026-01-30T01:11:27,507][INFO ][o.o.a.r.SecureADRestIT ] [testDeleteDetector] before test
1> [2026-01-30T01:12:03,725][INFO ][o.o.a.r.SecureADRestIT ] [testDeleteDetector] after test
1> [2026-01-30T01:12:03,730][INFO ][o.o.a.r.SecureADRestIT ] [testPreviewAnomalyDetector] before test
1> [2026-01-30T01:12:50,184][INFO ][o.o.a.r.SecureADRestIT ] [testPreviewAnomalyDetector] after test
1> [2026-01-30T01:12:50,187][INFO ][o.o.a.r.SecureADRestIT ] [testCreateAnomalyDetector] before test
1> [2026-01-30T01:14:04,836][INFO ][o.o.a.r.SecureADRestIT ] [testCreateAnomalyDetector] after test
2> NOTE: leaving temporary files on disk at: /__w/anomaly-detection/anomaly-detection/build/testrun/integTest/temp/org.opensearch.ad.rest.SecureADRestIT_ACF4EF0F436E5C7E-001
2> NOTE: test params are: codec=Asserting(Lucene103): {}, docValues:{}, maxPointsInLeafNode=711, maxMBSortInHeap=6.330024828563959, sim=Asserting(RandomSimilarity(queryNorm=true): {}), locale=so, timezone=Arctic/Longyearbyen
2> NOTE: Linux 6.11.0-1018-azure amd64/Eclipse Adoptium 21.0.10 (64-bit)/cpus=4,threads=1,free=192305584,total=536870912
2> NOTE: All tests run in this JVM: [DetectionResultEvalutationIT, HistoricalMissingSingleFeatureIT, MissingMultiFeatureIT, MissingSingleFeatureIT, MixedRealtimeHistoricalIT, PreviewMissingSingleFeatureIT, PreviewRuleIT, RealTimeRuleIT, SimpleRealTimeBatchIT, AnomalyDetectorRestApiIT, DataDependentADRestApiIT, HistoricalAnalysisRestApiIT, SecureADRestIT]
Tests with failures:
131 tests completed, 1 failed, 1 skipped
- org.opensearch.ad.rest.SecureADRestIT.testInsightsApisUseSystemContextForJobIndex
- ```
src/main/java/org/opensearch/ad/rest/handler/InsightsJobActionHandler.java
Outdated
Show resolved
Hide resolved
9cd762c to
a185cec
Compare
Signed-off-by: Jackie <jkhanjob@gmail.com>
a185cec to
fd0f1d9
Compare
Signed-off-by: Jackie <jkhanjob@gmail.com>
Signed-off-by: Jackie Han <jkhanjob@gmail.com>
Description
Related Issues
Resolves #[Issue number to be closed when this PR is merged]
Check List
--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.