Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Throw OpensearchSecurityException incase of datasource authorization … #2634

Merged
merged 1 commit into from
Apr 22, 2024
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 @@ -9,9 +9,11 @@

import java.util.List;
import lombok.AllArgsConstructor;
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.client.Client;
import org.opensearch.commons.ConfigConstants;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

@AllArgsConstructor
Expand Down Expand Up @@ -49,11 +51,12 @@ public void authorizeDataSource(DataSourceMetadata dataSourceMetadata) {
}
}
if (!isAuthorized) {
throw new SecurityException(
throw new OpenSearchSecurityException(
String.format(
"User is not authorized to access datasource %s. "
+ "User should be mapped to any of the roles in %s for access.",
dataSourceMetadata.getName(), dataSourceMetadata.getAllowedRoles().toString()));
dataSourceMetadata.getName(), dataSourceMetadata.getAllowedRoles().toString()),
RestStatus.UNAUTHORIZED);
vamsimanohar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

import java.util.List;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.client.Client;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasource.model.DataSourceType;

Expand Down Expand Up @@ -90,14 +93,15 @@ public void testAuthorizeDataSourceWithException() {
.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT))
.thenReturn(userString);
DataSourceMetadata dataSourceMetadata = dataSourceMetadata();
SecurityException securityException =
OpenSearchSecurityException openSearchSecurityException =
Assert.assertThrows(
SecurityException.class,
OpenSearchSecurityException.class,
() -> this.dataSourceUserAuthorizationHelper.authorizeDataSource(dataSourceMetadata));
Assert.assertEquals(
Assertions.assertEquals(
"User is not authorized to access datasource test. "
+ "User should be mapped to any of the roles in [prometheus_access] for access.",
securityException.getMessage());
openSearchSecurityException.getMessage());
Assertions.assertEquals(RestStatus.UNAUTHORIZED, openSearchSecurityException.status());
}

private DataSourceMetadata dataSourceMetadata() {
Expand Down
Loading