-
Couldn't load subscription status.
- Fork 337
[FEATURE] Endpoint to purge cache entries for specific users #5325
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
Closed
Rishav9852Kumar
wants to merge
14
commits into
opensearch-project:main
from
Rishav9852Kumar:rk-flush-user-cache
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad255cb
add flush cache for individual user
dlin2028 7cbd113
Add test that demonstrates flush cache for particular user
cwperks 5a158e4
Remove backendRegistry as api dependency
cwperks 8f5dc6f
Update test
cwperks 18cd883
test changes
prabhask5 eca4e83
add flush cache for individual user
dlin2028 972819a
Add test that demonstrates flush cache for particular user
cwperks f3ae0fb
Remove backendRegistry as api dependency
cwperks a30adaa
Update test
cwperks 6ca1e27
test changes
prabhask5 978a46d
merge
prabhask5 c68c802
Merge branch 'pk-flush-user-cache' into rk-flush-user-cache
88c922a
Merge branch 'opensearch-project:main' into rk-flush-user-cache
Rishav9852Kumar 82e44f6
Merge branch 'opensearch-project:main' into rk-flush-user-cache
Rishav9852Kumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/integrationTest/java/org/opensearch/security/api/FlushCacheApiIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.security.api; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| public class FlushCacheApiIntegrationTest extends AbstractApiIntegrationTest { | ||
| private final static String TEST_USER = "testuser"; | ||
|
|
||
| private String cachePath() { | ||
| return super.apiPath("cache"); | ||
| } | ||
|
|
||
| private String cachePath(String user) { | ||
| return super.apiPath("cache", "user", user); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlushCache() throws Exception { | ||
| withUser(NEW_USER, client -> { | ||
| forbidden(() -> client.get(cachePath())); | ||
| forbidden(() -> client.postJson(cachePath(), EMPTY_BODY)); | ||
| forbidden(() -> client.putJson(cachePath(), EMPTY_BODY)); | ||
| forbidden(() -> client.delete(cachePath())); | ||
| forbidden(() -> client.delete(cachePath(TEST_USER))); | ||
| }); | ||
| withUser(ADMIN_USER_NAME, localCluster.getAdminCertificate(), client -> { | ||
| notImplemented(() -> client.get(cachePath())); | ||
| notImplemented(() -> client.postJson(cachePath(), EMPTY_BODY)); | ||
| notImplemented(() -> client.putJson(cachePath(), EMPTY_BODY)); | ||
| final var deleteAllCacheResponse = ok(() -> client.delete(cachePath())); | ||
| assertThat( | ||
| deleteAllCacheResponse.getBody(), | ||
| deleteAllCacheResponse.getTextFromJsonBody("/message"), | ||
| is("Cache flushed successfully.") | ||
| ); | ||
| final var deleteUserCacheResponse = ok(() -> client.delete(cachePath(TEST_USER))); | ||
| assertThat( | ||
| deleteUserCacheResponse.getBody(), | ||
| deleteAllCacheResponse.getTextFromJsonBody("/message"), | ||
| is("Cache invalidated for user: " + TEST_USER) | ||
| ); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
src/integrationTest/java/org/opensearch/security/http/LdapAuthenticationCacheTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
Rishav9852Kumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
| package org.opensearch.security.http; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.RuleChain; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import org.opensearch.security.support.ConfigConstants; | ||
| import org.opensearch.test.framework.AuthorizationBackend; | ||
| import org.opensearch.test.framework.AuthzDomain; | ||
| import org.opensearch.test.framework.LdapAuthenticationConfigBuilder; | ||
| import org.opensearch.test.framework.LdapAuthorizationConfigBuilder; | ||
| import org.opensearch.test.framework.TestSecurityConfig; | ||
| import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain; | ||
| import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AuthenticationBackend; | ||
| import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.HttpAuthenticator; | ||
| import org.opensearch.test.framework.certificate.TestCertificates; | ||
| import org.opensearch.test.framework.cluster.ClusterManager; | ||
| import org.opensearch.test.framework.cluster.LocalCluster; | ||
| import org.opensearch.test.framework.cluster.TestRestClient; | ||
| import org.opensearch.test.framework.ldap.EmbeddedLDAPServer; | ||
| import org.opensearch.test.framework.log.LogsRule; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.not; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.contains; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.CN_GROUP_ADMIN; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.DN_GROUPS_TEST_ORG; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.DN_OPEN_SEARCH_PEOPLE_TEST_ORG; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.DN_PEOPLE_TEST_ORG; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.LDIF_DATA; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.LDIF_DATA_UPDATED_BACKEND_ROLES; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_KIRK; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_OPEN_SEARCH; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_SPOCK; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.USERNAME_ATTRIBUTE; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.USER_KIRK; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.USER_SEARCH; | ||
| import static org.opensearch.security.http.DirectoryInformationTrees.USER_SPOCK; | ||
| import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ADMIN_ENABLED; | ||
| import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; | ||
| import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; | ||
| import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.BASIC_AUTH_DOMAIN_ORDER; | ||
| import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; | ||
|
|
||
| /** | ||
| * Test uses plain (non TLS) connection between OpenSearch and LDAP server. | ||
| */ | ||
| @RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) | ||
| @ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
| public class LdapAuthenticationCacheTest { | ||
|
|
||
| private static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS); | ||
|
|
||
| private static final TestCertificates TEST_CERTIFICATES = new TestCertificates(); | ||
|
|
||
| public static final EmbeddedLDAPServer embeddedLDAPServer = new EmbeddedLDAPServer( | ||
| TEST_CERTIFICATES.getRootCertificateData(), | ||
| TEST_CERTIFICATES.getLdapCertificateData(), | ||
| LDIF_DATA | ||
| ); | ||
|
|
||
| public static LocalCluster cluster = new LocalCluster.Builder().testCertificates(TEST_CERTIFICATES) | ||
| .clusterManager(ClusterManager.SINGLENODE) | ||
| .anonymousAuth(false) | ||
| .nodeSettings( | ||
| Map.of( | ||
| ConfigConstants.SECURITY_AUTHCZ_REST_IMPERSONATION_USERS + "." + ADMIN_USER.getName(), | ||
| List.of(USER_KIRK), | ||
| SECURITY_RESTAPI_ROLES_ENABLED, | ||
| List.of("user_" + ADMIN_USER.getName() + "__" + ALL_ACCESS.getName()), | ||
| SECURITY_RESTAPI_ADMIN_ENABLED, | ||
| true | ||
| ) | ||
| ) | ||
| .authc( | ||
| new AuthcDomain("ldap", BASIC_AUTH_DOMAIN_ORDER + 1, true).httpAuthenticator(new HttpAuthenticator("basic").challenge(false)) | ||
| .backend( | ||
| new AuthenticationBackend("ldap").config( | ||
| () -> LdapAuthenticationConfigBuilder.config() | ||
| // this port is available when embeddedLDAPServer is already started, therefore Supplier interface is used to | ||
| // postpone | ||
| // execution of the code in this block. | ||
| .enableSsl(false) | ||
| .enableStartTls(false) | ||
| .hosts(List.of("localhost:" + embeddedLDAPServer.getLdapNonTlsPort())) | ||
| .bindDn(DN_OPEN_SEARCH_PEOPLE_TEST_ORG) | ||
| .password(PASSWORD_OPEN_SEARCH) | ||
| .userBase(DN_PEOPLE_TEST_ORG) | ||
| .userSearch(USER_SEARCH) | ||
| .usernameAttribute(USERNAME_ATTRIBUTE) | ||
| .build() | ||
| ) | ||
| ) | ||
| ) | ||
| .authc(AUTHC_HTTPBASIC_INTERNAL) | ||
| .users(ADMIN_USER) | ||
| .rolesMapping(new TestSecurityConfig.RoleMapping(ALL_ACCESS.getName()).backendRoles(CN_GROUP_ADMIN)) | ||
| .authz( | ||
| new AuthzDomain("ldap_roles").httpEnabled(true) | ||
| .authorizationBackend( | ||
| new AuthorizationBackend("ldap").config( | ||
| () -> new LdapAuthorizationConfigBuilder().hosts(List.of("localhost:" + embeddedLDAPServer.getLdapNonTlsPort())) | ||
| .enableSsl(false) | ||
| .bindDn(DN_OPEN_SEARCH_PEOPLE_TEST_ORG) | ||
| .password(PASSWORD_OPEN_SEARCH) | ||
| .userBase(DN_PEOPLE_TEST_ORG) | ||
| .userSearch(USER_SEARCH) | ||
| .usernameAttribute(USERNAME_ATTRIBUTE) | ||
| .roleBase(DN_GROUPS_TEST_ORG) | ||
| .roleSearch("(uniqueMember={0})") | ||
| .userRoleAttribute(null) | ||
| .userRoleName("disabled") | ||
| .roleName("cn") | ||
| .resolveNestedRoles(true) | ||
| .build() | ||
| ) | ||
| ) | ||
| ) | ||
| .build(); | ||
|
|
||
| @ClassRule | ||
| public static RuleChain ruleChain = RuleChain.outerRule(embeddedLDAPServer).around(cluster); | ||
|
|
||
| @Rule | ||
| public LogsRule logsRule = new LogsRule("com.amazon.dlic.auth.ldap.backend.LDAPAuthenticationBackend"); | ||
|
|
||
| @Test | ||
| public void shouldAuthenticateUserWithLdap_positive() { | ||
| try (TestRestClient client = cluster.getRestClient(USER_SPOCK, PASSWORD_SPOCK)) { | ||
| TestRestClient.HttpResponse response = client.getAuthInfo(); | ||
|
|
||
| response.assertStatusCode(200); | ||
|
|
||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("crew")); | ||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); | ||
| } | ||
|
|
||
| try (TestRestClient client = cluster.getRestClient(USER_KIRK, PASSWORD_KIRK)) { | ||
| TestRestClient.HttpResponse response = client.getAuthInfo(); | ||
|
|
||
| response.assertStatusCode(200); | ||
|
|
||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("admin")); | ||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); | ||
| } | ||
|
|
||
| embeddedLDAPServer.loadLdifData(LDIF_DATA_UPDATED_BACKEND_ROLES); | ||
|
|
||
| try (TestRestClient client = cluster.getRestClient(ADMIN_USER)) { | ||
Rishav9852Kumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TestRestClient.HttpResponse response = client.delete("_plugins/_security/api/cache/user/spock"); | ||
|
|
||
| response.assertStatusCode(200); | ||
| } | ||
|
|
||
| try (TestRestClient client = cluster.getRestClient(USER_SPOCK, PASSWORD_SPOCK)) { | ||
| TestRestClient.HttpResponse response = client.getAuthInfo(); | ||
|
|
||
| response.assertStatusCode(200); | ||
|
|
||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("enterprise", "crew")); | ||
| } | ||
|
|
||
| try (TestRestClient client = cluster.getRestClient(USER_KIRK, PASSWORD_KIRK)) { | ||
| TestRestClient.HttpResponse response = client.getAuthInfo(); | ||
|
|
||
| response.assertStatusCode(200); | ||
|
|
||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("admin")); | ||
| assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.