|
| 1 | +/* |
| 2 | +* Copyright OpenSearch Contributors |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* |
| 5 | +* The OpenSearch Contributors require contributions made to |
| 6 | +* this file be licensed under the Apache-2.0 license or a |
| 7 | +* compatible open source license. |
| 8 | +* |
| 9 | +*/ |
| 10 | +package org.opensearch.security.http; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 16 | +import org.junit.ClassRule; |
| 17 | +import org.junit.Rule; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.rules.RuleChain; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +import org.opensearch.security.support.ConfigConstants; |
| 23 | +import org.opensearch.test.framework.AuthorizationBackend; |
| 24 | +import org.opensearch.test.framework.AuthzDomain; |
| 25 | +import org.opensearch.test.framework.LdapAuthenticationConfigBuilder; |
| 26 | +import org.opensearch.test.framework.LdapAuthorizationConfigBuilder; |
| 27 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 28 | +import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain; |
| 29 | +import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AuthenticationBackend; |
| 30 | +import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.HttpAuthenticator; |
| 31 | +import org.opensearch.test.framework.certificate.TestCertificates; |
| 32 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 33 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 34 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 35 | +import org.opensearch.test.framework.ldap.EmbeddedLDAPServer; |
| 36 | +import org.opensearch.test.framework.log.LogsRule; |
| 37 | + |
| 38 | +import static org.hamcrest.CoreMatchers.not; |
| 39 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | +import static org.hamcrest.Matchers.contains; |
| 41 | +import static org.opensearch.security.http.DirectoryInformationTrees.CN_GROUP_ADMIN; |
| 42 | +import static org.opensearch.security.http.DirectoryInformationTrees.DN_GROUPS_TEST_ORG; |
| 43 | +import static org.opensearch.security.http.DirectoryInformationTrees.DN_OPEN_SEARCH_PEOPLE_TEST_ORG; |
| 44 | +import static org.opensearch.security.http.DirectoryInformationTrees.DN_PEOPLE_TEST_ORG; |
| 45 | +import static org.opensearch.security.http.DirectoryInformationTrees.LDIF_DATA; |
| 46 | +import static org.opensearch.security.http.DirectoryInformationTrees.LDIF_DATA_UPDATED_BACKEND_ROLES; |
| 47 | +import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_KIRK; |
| 48 | +import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_OPEN_SEARCH; |
| 49 | +import static org.opensearch.security.http.DirectoryInformationTrees.PASSWORD_SPOCK; |
| 50 | +import static org.opensearch.security.http.DirectoryInformationTrees.USERNAME_ATTRIBUTE; |
| 51 | +import static org.opensearch.security.http.DirectoryInformationTrees.USER_KIRK; |
| 52 | +import static org.opensearch.security.http.DirectoryInformationTrees.USER_SEARCH; |
| 53 | +import static org.opensearch.security.http.DirectoryInformationTrees.USER_SPOCK; |
| 54 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ADMIN_ENABLED; |
| 55 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; |
| 56 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; |
| 57 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.BASIC_AUTH_DOMAIN_ORDER; |
| 58 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 59 | + |
| 60 | +/** |
| 61 | +* Test uses plain (non TLS) connection between OpenSearch and LDAP server. |
| 62 | +*/ |
| 63 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 64 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 65 | +public class LdapAuthenticationCacheTest { |
| 66 | + |
| 67 | + private static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS); |
| 68 | + |
| 69 | + private static final TestCertificates TEST_CERTIFICATES = new TestCertificates(); |
| 70 | + |
| 71 | + public static final EmbeddedLDAPServer embeddedLDAPServer = new EmbeddedLDAPServer( |
| 72 | + TEST_CERTIFICATES.getRootCertificateData(), |
| 73 | + TEST_CERTIFICATES.getLdapCertificateData(), |
| 74 | + LDIF_DATA |
| 75 | + ); |
| 76 | + |
| 77 | + public static LocalCluster cluster = new LocalCluster.Builder().testCertificates(TEST_CERTIFICATES) |
| 78 | + .clusterManager(ClusterManager.SINGLENODE) |
| 79 | + .anonymousAuth(false) |
| 80 | + .nodeSettings( |
| 81 | + Map.of( |
| 82 | + ConfigConstants.SECURITY_AUTHCZ_REST_IMPERSONATION_USERS + "." + ADMIN_USER.getName(), |
| 83 | + List.of(USER_KIRK), |
| 84 | + SECURITY_RESTAPI_ROLES_ENABLED, |
| 85 | + List.of("user_" + ADMIN_USER.getName() + "__" + ALL_ACCESS.getName()), |
| 86 | + SECURITY_RESTAPI_ADMIN_ENABLED, |
| 87 | + true |
| 88 | + ) |
| 89 | + ) |
| 90 | + .authc( |
| 91 | + new AuthcDomain("ldap", BASIC_AUTH_DOMAIN_ORDER + 1, true).httpAuthenticator(new HttpAuthenticator("basic").challenge(false)) |
| 92 | + .backend( |
| 93 | + new AuthenticationBackend("ldap").config( |
| 94 | + () -> LdapAuthenticationConfigBuilder.config() |
| 95 | + // this port is available when embeddedLDAPServer is already started, therefore Supplier interface is used to |
| 96 | + // postpone |
| 97 | + // execution of the code in this block. |
| 98 | + .enableSsl(false) |
| 99 | + .enableStartTls(false) |
| 100 | + .hosts(List.of("localhost:" + embeddedLDAPServer.getLdapNonTlsPort())) |
| 101 | + .bindDn(DN_OPEN_SEARCH_PEOPLE_TEST_ORG) |
| 102 | + .password(PASSWORD_OPEN_SEARCH) |
| 103 | + .userBase(DN_PEOPLE_TEST_ORG) |
| 104 | + .userSearch(USER_SEARCH) |
| 105 | + .usernameAttribute(USERNAME_ATTRIBUTE) |
| 106 | + .build() |
| 107 | + ) |
| 108 | + ) |
| 109 | + ) |
| 110 | + .authc(AUTHC_HTTPBASIC_INTERNAL) |
| 111 | + .users(ADMIN_USER) |
| 112 | + .rolesMapping(new TestSecurityConfig.RoleMapping(ALL_ACCESS.getName()).backendRoles(CN_GROUP_ADMIN)) |
| 113 | + .authz( |
| 114 | + new AuthzDomain("ldap_roles").httpEnabled(true) |
| 115 | + .authorizationBackend( |
| 116 | + new AuthorizationBackend("ldap").config( |
| 117 | + () -> new LdapAuthorizationConfigBuilder().hosts(List.of("localhost:" + embeddedLDAPServer.getLdapNonTlsPort())) |
| 118 | + .enableSsl(false) |
| 119 | + .bindDn(DN_OPEN_SEARCH_PEOPLE_TEST_ORG) |
| 120 | + .password(PASSWORD_OPEN_SEARCH) |
| 121 | + .userBase(DN_PEOPLE_TEST_ORG) |
| 122 | + .userSearch(USER_SEARCH) |
| 123 | + .usernameAttribute(USERNAME_ATTRIBUTE) |
| 124 | + .roleBase(DN_GROUPS_TEST_ORG) |
| 125 | + .roleSearch("(uniqueMember={0})") |
| 126 | + .userRoleAttribute(null) |
| 127 | + .userRoleName("disabled") |
| 128 | + .roleName("cn") |
| 129 | + .resolveNestedRoles(true) |
| 130 | + .build() |
| 131 | + ) |
| 132 | + ) |
| 133 | + ) |
| 134 | + .build(); |
| 135 | + |
| 136 | + @ClassRule |
| 137 | + public static RuleChain ruleChain = RuleChain.outerRule(embeddedLDAPServer).around(cluster); |
| 138 | + |
| 139 | + @Rule |
| 140 | + public LogsRule logsRule = new LogsRule("com.amazon.dlic.auth.ldap.backend.LDAPAuthenticationBackend"); |
| 141 | + |
| 142 | + @Test |
| 143 | + public void shouldAuthenticateUserWithLdap_positive() { |
| 144 | + try (TestRestClient client = cluster.getRestClient(USER_SPOCK, PASSWORD_SPOCK)) { |
| 145 | + TestRestClient.HttpResponse response = client.getAuthInfo(); |
| 146 | + |
| 147 | + response.assertStatusCode(200); |
| 148 | + |
| 149 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("crew")); |
| 150 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); |
| 151 | + } |
| 152 | + |
| 153 | + try (TestRestClient client = cluster.getRestClient(USER_KIRK, PASSWORD_KIRK)) { |
| 154 | + TestRestClient.HttpResponse response = client.getAuthInfo(); |
| 155 | + |
| 156 | + response.assertStatusCode(200); |
| 157 | + |
| 158 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("admin")); |
| 159 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); |
| 160 | + } |
| 161 | + |
| 162 | + embeddedLDAPServer.loadLdifData(LDIF_DATA_UPDATED_BACKEND_ROLES); |
| 163 | + |
| 164 | + try (TestRestClient client = cluster.getRestClient(ADMIN_USER)) { |
| 165 | + TestRestClient.HttpResponse response = client.delete("_plugins/_security/api/cache/user/spock"); |
| 166 | + |
| 167 | + response.assertStatusCode(200); |
| 168 | + } |
| 169 | + |
| 170 | + try (TestRestClient client = cluster.getRestClient(USER_SPOCK, PASSWORD_SPOCK)) { |
| 171 | + TestRestClient.HttpResponse response = client.getAuthInfo(); |
| 172 | + |
| 173 | + response.assertStatusCode(200); |
| 174 | + |
| 175 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("enterprise", "crew")); |
| 176 | + } |
| 177 | + |
| 178 | + try (TestRestClient client = cluster.getRestClient(USER_KIRK, PASSWORD_KIRK)) { |
| 179 | + TestRestClient.HttpResponse response = client.getAuthInfo(); |
| 180 | + |
| 181 | + response.assertStatusCode(200); |
| 182 | + |
| 183 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), contains("admin")); |
| 184 | + assertThat(response.getTextArrayFromJsonBody("/backend_roles"), not(contains("enterprise"))); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments