Skip to content

Commit d252824

Browse files
authored
fixing validate access for multi-tenancy (#4196)
* fixing validate access for multi-tenancy Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> * adding unit test Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> --------- Signed-off-by: Dhrubo Saha <dhrubo@amazon.com>
1 parent 266bcfe commit d252824

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

plugin/src/main/java/org/opensearch/ml/helper/ModelAccessControlHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,20 @@ public void validateModelGroupAccess(
132132
SdkClient sdkClient,
133133
ActionListener<Boolean> listener
134134
) {
135-
if (modelGroupId == null
136-
|| (!mlFeatureEnabledSetting.isMultiTenancyEnabled()
137-
&& (isAdmin(user) || !isSecurityEnabledAndModelAccessControlEnabled(user)))) {
135+
if (modelGroupId == null) {
138136
listener.onResponse(true);
139137
return;
140138
}
139+
140+
if (mlFeatureEnabledSetting.isMultiTenancyEnabled()) {
141+
listener.onResponse(true); // Multi-tenancy handles access control
142+
return;
143+
}
144+
145+
if (isAdmin(user) || !isSecurityEnabledAndModelAccessControlEnabled(user)) {
146+
listener.onResponse(true); // Admin or security disabled
147+
return;
148+
}
141149
GetDataObjectRequest getModelGroupRequest = GetDataObjectRequest
142150
.builder()
143151
.index(ML_MODEL_GROUP_INDEX)

plugin/src/test/java/org/opensearch/ml/helper/ModelAccessControlHelperTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,49 @@ public void test_CreateSearchSourceBuilder() {
418418
assertNotNull(modelAccessControlHelper.createSearchSourceBuilder(user));
419419
}
420420

421+
public void test_MultiTenancyEnabled_BypassesValidation() {
422+
// Test that when multi-tenancy is enabled, validation is bypassed and returns true
423+
User user = User.parse("owner|IT,HR|myTenant");
424+
when(mlFeatureEnabledSetting.isMultiTenancyEnabled()).thenReturn(true);
425+
426+
modelAccessControlHelper
427+
.validateModelGroupAccess(user, mlFeatureEnabledSetting, null, "testGroupID", client, sdkClient, actionListener);
428+
429+
ArgumentCaptor<Boolean> argumentCaptor = ArgumentCaptor.forClass(Boolean.class);
430+
verify(actionListener).onResponse(argumentCaptor.capture());
431+
assertTrue(argumentCaptor.getValue());
432+
}
433+
434+
public void test_MultiTenancyDisabled_ProceedsWithValidation() throws IOException {
435+
// Test that when multi-tenancy is disabled, normal validation logic proceeds
436+
User user = User.parse("owner|IT,HR|myTenant");
437+
when(mlFeatureEnabledSetting.isMultiTenancyEnabled()).thenReturn(false);
438+
439+
// Setup a public model group to ensure validation passes
440+
String owner = "owner|IT,HR|myTenant";
441+
List<String> backendRoles = Arrays.asList("IT", "HR");
442+
setupModelGroup(owner, AccessMode.PUBLIC.getValue(), backendRoles);
443+
444+
PlainActionFuture<GetResponse> future = PlainActionFuture.newFuture();
445+
future.onResponse(getResponse);
446+
when(client.get(any())).thenReturn(future);
447+
448+
CountDownLatch latch = new CountDownLatch(1);
449+
LatchedActionListener<Boolean> latchedActionListener = new LatchedActionListener<>(actionListener, latch);
450+
modelAccessControlHelper
451+
.validateModelGroupAccess(user, mlFeatureEnabledSetting, null, "testGroupID", client, sdkClient, latchedActionListener);
452+
453+
try {
454+
latch.await(500, TimeUnit.MILLISECONDS);
455+
} catch (InterruptedException e) {
456+
Thread.currentThread().interrupt();
457+
}
458+
459+
ArgumentCaptor<Boolean> argumentCaptor = ArgumentCaptor.forClass(Boolean.class);
460+
verify(actionListener).onResponse(argumentCaptor.capture());
461+
assertTrue(argumentCaptor.getValue());
462+
}
463+
421464
private GetResponse modelGroupBuilder(List<String> backendRoles, String access, String owner) throws IOException {
422465
MLModelGroup mlModelGroup = MLModelGroup
423466
.builder()

0 commit comments

Comments
 (0)