Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Move user instantiation to doExecute #343

Merged
merged 1 commit into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class TransportDeleteDestinationAction @Inject constructor(
) {

@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -72,7 +71,7 @@ class TransportDeleteDestinationAction @Inject constructor(
override fun doExecute(task: Task, request: DeleteDestinationRequest, actionListener: ActionListener<DeleteResponse>) {
val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)
val deleteRequest = DeleteRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, request.destinationId)
.setRefreshPolicy(request.refreshPolicy)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class TransportDeleteMonitorAction @Inject constructor(
) {

@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -70,7 +69,7 @@ class TransportDeleteMonitorAction @Inject constructor(
override fun doExecute(task: Task, request: DeleteMonitorRequest, actionListener: ActionListener<DeleteResponse>) {
val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)
val deleteRequest = DeleteRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, request.monitorId)
.setRefreshPolicy(request.refreshPolicy)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ class TransportExecuteMonitorAction @Inject constructor(
) : HandledTransportAction<ExecuteMonitorRequest, ExecuteMonitorResponse> (
ExecuteMonitorAction.NAME, transportService, actionFilters, ::ExecuteMonitorRequest) {

private var user: User? = null

override fun doExecute(task: Task, execMonitorRequest: ExecuteMonitorRequest, actionListener: ActionListener<ExecuteMonitorResponse>) {

val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

client.threadPool().threadContext.stashContext().use {
val executeMonitor = fun(monitor: Monitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class TransportGetAlertsAction @Inject constructor(
) {

@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -79,7 +78,7 @@ class TransportGetAlertsAction @Inject constructor(
ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT
)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

val tableProp = getAlertsRequest.table
val sortBuilder = SortBuilders
Expand Down Expand Up @@ -118,13 +117,14 @@ class TransportGetAlertsAction @Inject constructor(
.from(tableProp.startIndex)

client.threadPool().threadContext.stashContext().use {
resolve(searchSourceBuilder, actionListener)
resolve(searchSourceBuilder, actionListener, user)
}
}

fun resolve(
searchSourceBuilder: SearchSourceBuilder,
actionListener: ActionListener<GetAlertsResponse>
actionListener: ActionListener<GetAlertsResponse>,
user: User?
) {
// user is null when: 1/ security is disabled. 2/when user is super-admin.
if (user == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class TransportGetDestinationsAction @Inject constructor(
) {

@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -82,7 +81,7 @@ class TransportGetDestinationsAction @Inject constructor(
ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT
)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

val tableProp = getDestinationsRequest.table

Expand Down Expand Up @@ -120,13 +119,14 @@ class TransportGetDestinationsAction @Inject constructor(
searchSourceBuilder.query(queryBuilder)

client.threadPool().threadContext.stashContext().use {
resolve(searchSourceBuilder, actionListener)
resolve(searchSourceBuilder, actionListener, user)
}
}

fun resolve(
searchSourceBuilder: SearchSourceBuilder,
actionListener: ActionListener<GetDestinationsResponse>
actionListener: ActionListener<GetDestinationsResponse>,
user: User?
) {
if (user == null) {
// user is null when: 1/ security is disabled. 2/when user is super-admin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class TransportGetMonitorAction @Inject constructor(
) {

@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -68,7 +67,7 @@ class TransportGetMonitorAction @Inject constructor(
override fun doExecute(task: Task, getMonitorRequest: GetMonitorRequest, actionListener: ActionListener<GetMonitorResponse>) {
val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, getMonitorRequest.monitorId)
.version(getMonitorRequest.version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class TransportIndexDestinationAction @Inject constructor(
@Volatile private var indexTimeout = AlertingSettings.INDEX_TIMEOUT.get(settings)
@Volatile private var allowList = DestinationSettings.ALLOW_LIST.get(settings)
@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.INDEX_TIMEOUT) { indexTimeout = it }
Expand All @@ -71,7 +70,7 @@ class TransportIndexDestinationAction @Inject constructor(
override fun doExecute(task: Task, request: IndexDestinationRequest, actionListener: ActionListener<IndexDestinationResponse>) {
val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

if (!checkFilterByUserBackendRoles(filterByEnabled, user, actionListener)) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class TransportIndexMonitorAction @Inject constructor(
@Volatile private var maxActionThrottle = MAX_ACTION_THROTTLE_VALUE.get(settings)
@Volatile private var allowList = ALLOW_LIST.get(settings)
@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(ALERTING_MAX_MONITORS) { maxMonitors = it }
Expand All @@ -108,7 +107,7 @@ class TransportIndexMonitorAction @Inject constructor(

val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

if (!checkFilterByUserBackendRoles(filterByEnabled, user, actionListener)) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class TransportSearchMonitorAction @Inject constructor(
SearchMonitorAction.NAME, transportService, actionFilters, ::SearchMonitorRequest
) {
@Volatile private var filterByEnabled = AlertingSettings.FILTER_BY_BACKEND_ROLES.get(settings)
private var user: User? = null

init {
clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.FILTER_BY_BACKEND_ROLES) { filterByEnabled = it }
Expand All @@ -56,14 +55,14 @@ class TransportSearchMonitorAction @Inject constructor(
override fun doExecute(task: Task, searchMonitorRequest: SearchMonitorRequest, actionListener: ActionListener<SearchResponse>) {
val userStr = client.threadPool().threadContext.getTransient<String>(ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT)
log.debug("User and roles string from thread context: $userStr")
user = User.parse(userStr)
val user: User? = User.parse(userStr)

client.threadPool().threadContext.stashContext().use {
resolve(searchMonitorRequest, actionListener)
resolve(searchMonitorRequest, actionListener, user)
}
}

fun resolve(searchMonitorRequest: SearchMonitorRequest, actionListener: ActionListener<SearchResponse>) {
fun resolve(searchMonitorRequest: SearchMonitorRequest, actionListener: ActionListener<SearchResponse>, user: User?) {
if (user == null) {
// user header is null when: 1/ security is disabled. 2/when user is super-admin.
search(searchMonitorRequest.searchRequest, actionListener)
Expand Down