Skip to content

Commit 2e05310

Browse files
Reads feature flag correctly
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
1 parent aedf1f3 commit 2e05310

9 files changed

+67
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.opensearch.ad.constant;
2+
3+
public class ConfigConstants {
4+
public static final String OPENSEARCH_RESOURCE_SHARING_ENABLED = "plugins.security.resource_sharing.enabled";
5+
public static final Boolean OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT = true;
6+
}

src/main/java/org/opensearch/ad/transport/IndexAnomalyDetectorTransportAction.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.opensearch.action.support.ActionFilters;
2828
import org.opensearch.action.support.HandledTransportAction;
2929
import org.opensearch.action.support.WriteRequest;
30+
import org.opensearch.ad.constant.ConfigConstants;
3031
import org.opensearch.ad.indices.ADIndexManagement;
3132
import org.opensearch.ad.model.AnomalyDetector;
3233
import org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler;
@@ -64,6 +65,7 @@ public class IndexAnomalyDetectorTransportAction extends HandledTransportAction<
6465
private volatile Boolean filterByEnabled;
6566
private final SearchFeatureDao searchFeatureDao;
6667
private final Settings settings;
68+
private final boolean resourceSharingEnabled;
6769

6870
@Inject
6971
public IndexAnomalyDetectorTransportAction(
@@ -90,6 +92,8 @@ public IndexAnomalyDetectorTransportAction(
9092
filterByEnabled = AnomalyDetectorSettings.AD_FILTER_BY_BACKEND_ROLES.get(settings);
9193
clusterService.getClusterSettings().addSettingsUpdateConsumer(AD_FILTER_BY_BACKEND_ROLES, it -> filterByEnabled = it);
9294
this.settings = settings;
95+
this.resourceSharingEnabled = settings
96+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
9397
}
9498

9599
@Override
@@ -116,7 +120,7 @@ private void resolveUserAndExecute(
116120
) {
117121
try {
118122
// If resource sharing flag is enabled then access evaluation will be performed at DLS level
119-
if (!featureEnabled && filterByEnabled) {
123+
if (!resourceSharingEnabled && filterByEnabled) {
120124
// Check if user has backend roles
121125
// When filter by is enabled, block users creating/updating detectors who do not have backend roles.
122126
String error = checkFilterByBackendRoles(requestedUser);
@@ -141,7 +145,8 @@ private void resolveUserAndExecute(
141145
clusterService,
142146
xContentRegistry,
143147
filterByBackendRole,
144-
AnomalyDetector.class
148+
AnomalyDetector.class,
149+
resourceSharingEnabled
145150
);
146151
} else {
147152
// Create Detector. No need to get current detector.

src/main/java/org/opensearch/ad/transport/PreviewAnomalyDetectorTransportAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.opensearch.action.support.HandledTransportAction;
3535
import org.opensearch.ad.AnomalyDetectorRunner;
3636
import org.opensearch.ad.constant.ADCommonMessages;
37+
import org.opensearch.ad.constant.ConfigConstants;
3738
import org.opensearch.ad.model.AnomalyDetector;
3839
import org.opensearch.ad.model.AnomalyResult;
3940
import org.opensearch.ad.settings.AnomalyDetectorSettings;
@@ -70,6 +71,7 @@ public class PreviewAnomalyDetectorTransportAction extends
7071
private volatile Boolean filterByEnabled;
7172
private final CircuitBreakerService adCircuitBreakerService;
7273
private Semaphore lock;
74+
private final boolean resourceSharingEnabled;
7375

7476
@Inject
7577
public PreviewAnomalyDetectorTransportAction(
@@ -93,6 +95,8 @@ public PreviewAnomalyDetectorTransportAction(
9395
clusterService.getClusterSettings().addSettingsUpdateConsumer(AD_FILTER_BY_BACKEND_ROLES, it -> filterByEnabled = it);
9496
this.adCircuitBreakerService = adCircuitBreakerService;
9597
this.lock = new Semaphore(MAX_CONCURRENT_PREVIEW.get(settings), true);
98+
this.resourceSharingEnabled = settings
99+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
96100
clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_CONCURRENT_PREVIEW, it -> { lock = new Semaphore(it); });
97101
}
98102

@@ -115,7 +119,8 @@ protected void doExecute(
115119
client,
116120
clusterService,
117121
xContentRegistry,
118-
AnomalyDetector.class
122+
AnomalyDetector.class,
123+
resourceSharingEnabled
119124
);
120125
} catch (Exception e) {
121126
logger.error(e);

src/main/java/org/opensearch/forecast/transport/ForecastRunOnceTransportAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.opensearch.action.search.SearchRequest;
2626
import org.opensearch.action.support.ActionFilters;
2727
import org.opensearch.action.support.HandledTransportAction;
28+
import org.opensearch.ad.constant.ConfigConstants;
2829
import org.opensearch.client.Client;
2930
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
3031
import org.opensearch.cluster.service.ClusterService;
@@ -97,6 +98,7 @@ public class ForecastRunOnceTransportAction extends HandledTransportAction<Forec
9798
private final FeatureManager featureManager;
9899
private final ForecastStats forecastStats;
99100
private volatile Boolean filterByEnabled;
101+
private final boolean resourceSharingEnabled;
100102

101103
protected volatile Integer maxSingleStreamForecasters;
102104
protected volatile Integer maxHCForecasters;
@@ -147,6 +149,8 @@ public ForecastRunOnceTransportAction(
147149
this.maxHCForecasters = MAX_HC_FORECASTERS.get(settings);
148150
this.maxForecastFeatures = MAX_FORECAST_FEATURES;
149151
this.maxCategoricalFields = ForecastNumericSetting.maxCategoricalFields();
152+
this.resourceSharingEnabled = settings
153+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
150154
clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_SINGLE_STREAM_FORECASTERS, it -> maxSingleStreamForecasters = it);
151155
clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_HC_FORECASTERS, it -> maxHCForecasters = it);
152156
}
@@ -166,7 +170,8 @@ protected void doExecute(Task task, ForecastResultRequest request, ActionListene
166170
client,
167171
clusterService,
168172
xContentRegistry,
169-
Forecaster.class
173+
Forecaster.class,
174+
resourceSharingEnabled
170175
);
171176
} catch (Exception e) {
172177
LOG.error(e);

src/main/java/org/opensearch/forecast/transport/IndexForecasterTransportAction.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.opensearch.action.support.ActionFilters;
2929
import org.opensearch.action.support.HandledTransportAction;
3030
import org.opensearch.action.support.WriteRequest;
31+
import org.opensearch.ad.constant.ConfigConstants;
3132
import org.opensearch.client.Client;
3233
import org.opensearch.cluster.service.ClusterService;
3334
import org.opensearch.common.inject.Inject;
@@ -63,6 +64,7 @@ public class IndexForecasterTransportAction extends HandledTransportAction<Index
6364
private final SearchFeatureDao searchFeatureDao;
6465
private final ForecastTaskManager taskManager;
6566
private final Settings settings;
67+
private final boolean resourceSharingEnabled;
6668

6769
@Inject
6870
public IndexForecasterTransportAction(
@@ -89,6 +91,8 @@ public IndexForecasterTransportAction(
8991
this.searchFeatureDao = searchFeatureDao;
9092
this.taskManager = taskManager;
9193
this.settings = settings;
94+
this.resourceSharingEnabled = settings
95+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
9296
}
9397

9498
@Override
@@ -125,7 +129,7 @@ private void resolveUserAndExecute(
125129
boolean filterByBackendRole = requestedUser == null ? false : filterByEnabled;
126130

127131
// If resource sharing flag is enabled then access evaluation will be performed at DLS level
128-
if (!featureEnabled && filterByEnabled) {
132+
if (!resourceSharingEnabled && filterByEnabled) {
129133
// Check if user has backend roles
130134
// When filter by is enabled, block users creating/updating detectors who do not have backend roles.
131135
String error = checkFilterByBackendRoles(requestedUser);
@@ -146,7 +150,8 @@ private void resolveUserAndExecute(
146150
clusterService,
147151
xContentRegistry,
148152
filterByBackendRole,
149-
Forecaster.class
153+
Forecaster.class,
154+
resourceSharingEnabled
150155
);
151156
} else {
152157
// Create Detector. No need to get current detector.

src/main/java/org/opensearch/timeseries/transport/BaseDeleteConfigTransportAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.opensearch.action.support.ActionFilters;
2525
import org.opensearch.action.support.HandledTransportAction;
2626
import org.opensearch.action.support.WriteRequest;
27+
import org.opensearch.ad.constant.ConfigConstants;
2728
import org.opensearch.ad.model.ADTask;
2829
import org.opensearch.client.Client;
2930
import org.opensearch.cluster.service.ClusterService;
@@ -69,6 +70,7 @@ public abstract class BaseDeleteConfigTransportAction<TaskCacheManagerType exten
6970
private final String stateIndex;
7071
private final Class<ConfigType> configTypeClass;
7172
private final List<TaskTypeEnum> batchTaskTypes;
73+
private final boolean resourceSharingEnabled;
7274

7375
public BaseDeleteConfigTransportAction(
7476
TransportService transportService,
@@ -100,6 +102,8 @@ public BaseDeleteConfigTransportAction(
100102
this.stateIndex = stateIndex;
101103
this.configTypeClass = configTypeClass;
102104
this.batchTaskTypes = historicalTaskTypes;
105+
this.resourceSharingEnabled = settings
106+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
103107
}
104108

105109
@Override
@@ -142,7 +146,8 @@ protected void doExecute(Task task, DeleteConfigRequest request, ActionListener<
142146
client,
143147
clusterService,
144148
xContentRegistry,
145-
configTypeClass
149+
configTypeClass,
150+
resourceSharingEnabled
146151
);
147152
} catch (Exception e) {
148153
LOG.error(e);

src/main/java/org/opensearch/timeseries/transport/BaseGetConfigTransportAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.opensearch.action.get.MultiGetResponse;
3232
import org.opensearch.action.support.ActionFilters;
3333
import org.opensearch.action.support.HandledTransportAction;
34+
import org.opensearch.ad.constant.ConfigConstants;
3435
import org.opensearch.client.Client;
3536
import org.opensearch.cluster.service.ClusterService;
3637
import org.opensearch.common.CheckedConsumer;
@@ -101,6 +102,7 @@ public abstract class BaseGetConfigTransportAction<GetConfigResponseType extends
101102
private final String singleStreamHistoricalTaskname;
102103
private final String hcHistoricalTaskName;
103104
private final TaskProfileRunnerType taskProfileRunner;
105+
private final boolean resourceSharingEnabled;
104106

105107
public BaseGetConfigTransportAction(
106108
TransportService transportService,
@@ -154,6 +156,8 @@ public BaseGetConfigTransportAction(
154156
this.hcHistoricalTaskName = hcHistoricalTaskName;
155157
this.singleStreamHistoricalTaskname = singleStreamHistoricalTaskname;
156158
this.taskProfileRunner = taskProfileRunner;
159+
this.resourceSharingEnabled = settings
160+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
157161
}
158162

159163
@Override
@@ -172,7 +176,8 @@ public void doExecute(Task task, ActionRequest request, ActionListener<GetConfig
172176
client,
173177
clusterService,
174178
xContentRegistry,
175-
configTypeClass
179+
configTypeClass,
180+
resourceSharingEnabled
176181
);
177182
} catch (Exception e) {
178183
LOG.error(e);

src/main/java/org/opensearch/timeseries/transport/BaseJobTransportAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.opensearch.action.ActionType;
1414
import org.opensearch.action.support.ActionFilters;
1515
import org.opensearch.action.support.HandledTransportAction;
16+
import org.opensearch.ad.constant.ConfigConstants;
1617
import org.opensearch.client.Client;
1718
import org.opensearch.cluster.service.ClusterService;
1819
import org.opensearch.common.settings.Setting;
@@ -53,6 +54,7 @@ public abstract class BaseJobTransportAction<IndexType extends Enum<IndexType> &
5354
private final String failtoStopMsg;
5455
private final Class<? extends Config> configClass;
5556
private final IndexJobActionHandlerType indexJobActionHandlerType;
57+
private final boolean resourceSharingEnabled;
5658

5759
public BaseJobTransportAction(
5860
TransportService transportService,
@@ -82,6 +84,8 @@ public BaseJobTransportAction(
8284
this.failtoStopMsg = failtoStopMsg;
8385
this.configClass = configClass;
8486
this.indexJobActionHandlerType = indexJobActionHandlerType;
87+
this.resourceSharingEnabled = settings
88+
.getAsBoolean(ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED, ConfigConstants.OPENSEARCH_RESOURCE_SHARING_ENABLED_DEFAULT);
8589
}
8690

8791
@Override
@@ -106,7 +110,8 @@ protected void doExecute(Task task, JobRequest request, ActionListener<JobRespon
106110
client,
107111
clusterService,
108112
xContentRegistry,
109-
configClass
113+
configClass,
114+
resourceSharingEnabled
110115
);
111116
} catch (Exception e) {
112117
logger.error(e);

src/main/java/org/opensearch/timeseries/util/ParseUtils.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
503503
Client client,
504504
ClusterService clusterService,
505505
NamedXContentRegistry xContentRegistry,
506-
Class<ConfigType> configTypeClass
506+
Class<ConfigType> configTypeClass,
507+
boolean resourceSharingEnabled
507508
) {
508509
try {
509510
if (requestedUser == null || configId == null) {
@@ -520,7 +521,8 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
520521
clusterService,
521522
xContentRegistry,
522523
filterByEnabled,
523-
configTypeClass
524+
configTypeClass,
525+
resourceSharingEnabled
524526
);
525527
}
526528
} catch (Exception e) {
@@ -550,7 +552,8 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
550552
ClusterService clusterService,
551553
NamedXContentRegistry xContentRegistry,
552554
boolean filterByBackendRole,
553-
Class<ConfigType> configTypeClass
555+
Class<ConfigType> configTypeClass,
556+
boolean resourceSharingEnabled
554557
) {
555558
if (clusterService.state().metadata().indices().containsKey(CommonName.CONFIG_INDEX)) {
556559
GetRequest request = new GetRequest(CommonName.CONFIG_INDEX).id(configId);
@@ -567,7 +570,8 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
567570
function,
568571
xContentRegistry,
569572
filterByBackendRole,
570-
configTypeClass
573+
configTypeClass,
574+
resourceSharingEnabled
571575
),
572576
exception -> {
573577
logger.error("Failed to get config: " + configId, exception);
@@ -610,7 +614,8 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
610614
Consumer<ConfigType> function,
611615
NamedXContentRegistry xContentRegistry,
612616
boolean filterByBackendRole,
613-
Class<ConfigType> configTypeClass
617+
Class<ConfigType> configTypeClass,
618+
boolean resourceSharingEnabled
614619
) {
615620
if (response.isExists()) {
616621
try (
@@ -619,7 +624,7 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
619624
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
620625
@SuppressWarnings("unchecked")
621626
ConfigType config = (ConfigType) Config.parseConfig(configTypeClass, parser);
622-
if(featureFlagEnabled) {
627+
if (resourceSharingEnabled) {
623628
// Permission evaluation will be done at DLS level in security plugin
624629
function.accept(config);
625630
} else {
@@ -630,9 +635,12 @@ public static <ConfigType extends Config, GetConfigResponseType extends ActionRe
630635
} else {
631636
logger.debug("User: " + requestUser.getName() + " does not have permissions to access config: " + configId);
632637
listener
633-
.onFailure(
634-
new OpenSearchStatusException(CommonMessages.NO_PERMISSION_TO_ACCESS_CONFIG + configId, RestStatus.FORBIDDEN)
635-
);
638+
.onFailure(
639+
new OpenSearchStatusException(
640+
CommonMessages.NO_PERMISSION_TO_ACCESS_CONFIG + configId,
641+
RestStatus.FORBIDDEN
642+
)
643+
);
636644
}
637645
}
638646

0 commit comments

Comments
 (0)