Skip to content

Commit

Permalink
[Backport 2.x] Notification security fix (opensearch-project#861)
Browse files Browse the repository at this point in the history
* Notification security fix (opensearch-project#852)

* added injecting whole user object in threadContext before calling notification APIs so that backend roles are available to notification plugin

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>

* compile fix

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>

* refactored user_info injection to use InjectSecurity

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>

* ktlint fix

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>

---------

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>
(cherry picked from commit e0b7a5a)

* remove unneeded import

Signed-off-by: Ashish Agrawal <ashisagr@amazon.com>

---------

Signed-off-by: Ashish Agrawal <ashisagr@amazon.com>
Co-authored-by: Petar Dzepina <petar.dzepina@gmail.com>
Co-authored-by: Ashish Agrawal <ashisagr@amazon.com>
  • Loading branch information
3 people committed Jun 8, 2023
1 parent 24d7688 commit 821db45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
28 changes: 18 additions & 10 deletions alerting/src/main/kotlin/org/opensearch/alerting/MonitorRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,24 @@ abstract class MonitorRunner {
throw IllegalStateException("Message content missing in the Destination with id: ${action.destinationId}")
}
if (!dryrun) {
val roles = MonitorRunnerService.getRolesForMonitor(monitor)
withClosableContext(
InjectorContextElement(monitor.id, monitorCtx.settings!!, monitorCtx.threadPool!!.threadContext, roles)
) {
actionOutput[Action.MESSAGE_ID] = getConfigAndSendNotification(
action,
monitorCtx,
actionOutput[Action.SUBJECT],
actionOutput[Action.MESSAGE]!!
)
val client = monitorCtx.client
client!!.threadPool().threadContext.stashContext().use {
withClosableContext(
InjectorContextElement(
monitor.id,
monitorCtx.settings!!,
monitorCtx.threadPool!!.threadContext,
monitor.user?.roles,
monitor.user
)
) {
actionOutput[Action.MESSAGE_ID] = getConfigAndSendNotification(
action,
monitorCtx,
actionOutput[Action.SUBJECT],
actionOutput[Action.MESSAGE]!!
)
}
}
}
ActionRunResult(action.id, action.name, actionOutput, false, MonitorRunnerService.currentTime(), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.opensearch.client.OpenSearchClient
import org.opensearch.common.bytes.BytesReference
import org.opensearch.common.settings.Settings
import org.opensearch.common.util.concurrent.ThreadContext
import org.opensearch.common.util.concurrent.ThreadContext.StoredContext
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentHelper
Expand Down Expand Up @@ -207,28 +206,13 @@ suspend fun <T> NotificationsPluginInterface.suspendUntil(block: NotificationsPl
})
}

/**
* Store a [ThreadContext] and restore a [ThreadContext] when the coroutine resumes on a different thread.
*
* @param threadContext - a [ThreadContext] instance
*/
class ElasticThreadContextElement(private val threadContext: ThreadContext) : ThreadContextElement<Unit> {

companion object Key : CoroutineContext.Key<ElasticThreadContextElement>
private var context: StoredContext = threadContext.newStoredContext(true)

override val key: CoroutineContext.Key<*>
get() = Key

override fun restoreThreadContext(context: CoroutineContext, oldState: Unit) {
this.context = threadContext.stashContext()
}

override fun updateThreadContext(context: CoroutineContext) = this.context.close()
}

class InjectorContextElement(id: String, settings: Settings, threadContext: ThreadContext, private val roles: List<String>?) :
ThreadContextElement<Unit> {
class InjectorContextElement(
id: String,
settings: Settings,
threadContext: ThreadContext,
private val roles: List<String>?,
private val user: User? = null
) : ThreadContextElement<Unit> {

companion object Key : CoroutineContext.Key<InjectorContextElement>
override val key: CoroutineContext.Key<*>
Expand All @@ -238,6 +222,8 @@ class InjectorContextElement(id: String, settings: Settings, threadContext: Thre

override fun updateThreadContext(context: CoroutineContext) {
rolesInjectorHelper.injectRoles(roles)
// This is from where plugins extract backend roles. It should be passed when calling APIs of other plugins
rolesInjectorHelper.injectUserInfo(user)
}

override fun restoreThreadContext(context: CoroutineContext, oldState: Unit) {
Expand Down

0 comments on commit 821db45

Please sign in to comment.