Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -106,7 +106,7 @@ public void validateModelGroupAccess(User user, String modelGroupId, String acti
.onFailure(
new OpenSearchStatusException(
"User "
+ user.getName()
+ (user == null ? null : user.getName())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user is null and model group is public, does the user have access or not ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no.. null users are not granted access..

if you are talking from anonymous user login perspective then yes since anonymous users can access resource in two ways:

  1. Resource is shared with anonymous user or backend role (https://github.com/opensearch-project/security/blob/9e6047f99da4df3404e2d52f3afe4e49e508c3a5/src/main/java/org/opensearch/security/auth/BackendRegistry.java#L480)
  2. Resource is marked as public by following the new convention (https://github.com/opensearch-project/security/blob/main/RESOURCE_SHARING_AND_ACCESS_CONTROL.md#example-publicly-shared-resource)

+ " is not authorized to perform action "
+ action
+ " on ml-model-group id: "
Expand Down Expand Up @@ -173,6 +173,7 @@ public void validateModelGroupAccess(
listener.onResponse(true);
return;
}

if (shouldUseResourceAuthz(ML_MODEL_GROUP_RESOURCE_TYPE)) {
var resourceSharingClient = ResourceSharingClientAccessor.getInstance().getResourceSharingClient();
resourceSharingClient.verifyAccess(modelGroupId, ML_MODEL_GROUP_RESOURCE_TYPE, action, ActionListener.wrap(isAuthorized -> {
Expand All @@ -181,7 +182,7 @@ public void validateModelGroupAccess(
.onFailure(
new OpenSearchStatusException(
"User "
+ user.getName()
+ (user == null ? null : user.getName())
+ " is not authorized to perform action "
+ action
+ " on ml-model-group id: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,70 @@ public void test_ShouldUseResourceAuthz_FeatureDisabled_And_ClientNull() {
assertFalse(ModelAccessControlHelper.shouldUseResourceAuthz(CommonValue.ML_MODEL_GROUP_RESOURCE_TYPE));
}

public void test_ResourceAuthz_NotAuthorized_UserNull_UsesUnknownName() {
when(resourceSharingClient.isFeatureEnabledForType(CommonValue.ML_MODEL_GROUP_RESOURCE_TYPE)).thenReturn(true);
ResourceSharingClientAccessor.getInstance().setResourceSharingClient(resourceSharingClient);

doAnswer(invocation -> {
ActionListener<Boolean> listener = invocation.getArgument(3);
listener.onResponse(false);
return null;
}).when(resourceSharingClient).verifyAccess(any(), any(), any(), any());

modelAccessControlHelper
.validateModelGroupAccess(
null, // user
mlFeatureEnabledSetting,
"testTenant",
"testGroupID",
"testAction",
client,
sdkClient,
actionListener
);

ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
verify(actionListener).onFailure(argumentCaptor.capture());
Exception ex = argumentCaptor.getValue();
assertTrue(ex instanceof org.opensearch.OpenSearchStatusException);
assertTrue(ex.getMessage().contains("User null is not authorized"));

ResourceSharingClientAccessor.getInstance().setResourceSharingClient(null);
}

public void test_ResourceAuthz_NotAuthorized_UserPresent_UsesUserName() {
when(resourceSharingClient.isFeatureEnabledForType(CommonValue.ML_MODEL_GROUP_RESOURCE_TYPE)).thenReturn(true);
ResourceSharingClientAccessor.getInstance().setResourceSharingClient(resourceSharingClient);

doAnswer(invocation -> {
ActionListener<Boolean> listener = invocation.getArgument(3);
listener.onResponse(false);
return null;
}).when(resourceSharingClient).verifyAccess(any(), any(), any(), any());

User user = User.parse("owner|IT,HR|myTenant");

modelAccessControlHelper
.validateModelGroupAccess(
user,
mlFeatureEnabledSetting,
"testTenant",
"testGroupID",
"testAction",
client,
sdkClient,
actionListener
);

ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
verify(actionListener).onFailure(argumentCaptor.capture());
Exception ex = argumentCaptor.getValue();
assertTrue(ex instanceof org.opensearch.OpenSearchStatusException);
assertTrue(ex.getMessage().contains("User owner is not authorized"));

ResourceSharingClientAccessor.getInstance().setResourceSharingClient(null);
}

private GetResponse modelGroupBuilder(List<String> backendRoles, String access, String owner) throws IOException {
MLModelGroup mlModelGroup = MLModelGroup
.builder()
Expand Down
Loading