Skip to content

Commit

Permalink
Correction: Setting whole user object when calling notification plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Ravi Thaluru <[email protected]>
  • Loading branch information
thalurur committed Apr 19, 2022
1 parent 62858ce commit 6ba8c33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ object ManagedIndexRunner :
if (executedManagedIndexMetaData.isFailed) {
try {
// if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message
// publishErrorNotification(policy, executedManagedIndexMetaData)
publishErrorNotification(policy, executedManagedIndexMetaData)
} catch (e: Exception) {
logger.error("Failed to publish error notification", e)
val errorMessage = e.message ?: "Failed to publish error notification"
Expand Down Expand Up @@ -783,7 +783,7 @@ object ManagedIndexRunner :
errorNotificationRetryPolicy.retry(logger) {
val compiledMessage = compileTemplate(messageTemplate, managedIndexMetaData)
destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client)
channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, managedIndexMetaData, compiledMessage)
channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, managedIndexMetaData, compiledMessage, policy.user)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package org.opensearch.indexmanagement.indexstatemanagement.util
import org.opensearch.OpenSearchStatusException
import org.opensearch.client.Client
import org.opensearch.client.node.NodeClient
import org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT
import org.opensearch.commons.authuser.User
import org.opensearch.commons.destination.message.LegacyBaseMessage
import org.opensearch.commons.notifications.NotificationsPluginInterface
import org.opensearch.commons.notifications.action.LegacyPublishNotificationRequest
Expand Down Expand Up @@ -45,18 +47,28 @@ suspend fun LegacyBaseMessage.publishLegacyNotification(client: Client) {
/**
* Extension function for publishing a notification to a channel in the Notification plugin.
*/
suspend fun Channel.sendNotification(client: Client, title: String, managedIndexMetaData: ManagedIndexMetaData, compiledMessage: String) {
suspend fun Channel.sendNotification(
client: Client,
title: String,
managedIndexMetaData: ManagedIndexMetaData,
compiledMessage: String,
user: User?
) {
val channel = this
val res: SendNotificationResponse = NotificationsPluginInterface.suspendUntil {
this.sendNotification(
(client as NodeClient),
managedIndexMetaData.getEventSource(title),
ChannelMessage(compiledMessage, null, null),
listOf(channel.id),
it
)
client.threadPool().threadContext.stashContext().use {
// We need to set the user context information in the thread context for notification plugin to correctly resolve the user object
client.threadPool().threadContext.putTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT, user?.toString())
val res: SendNotificationResponse = NotificationsPluginInterface.suspendUntil {
this.sendNotification(
(client as NodeClient),
managedIndexMetaData.getEventSource(title),
ChannelMessage(compiledMessage, null, null),
listOf(channel.id),
it
)
}
validateResponseStatus(res.getStatus(), res.notificationEvent.eventSource.referenceId)
}
validateResponseStatus(res.getStatus(), res.notificationEvent.eventSource.referenceId)
}

fun ManagedIndexMetaData.getEventSource(title: String): EventSource {
Expand Down

0 comments on commit 6ba8c33

Please sign in to comment.