Skip to content

Commit de59efc

Browse files
authored
changes to hidden model code to use OPENDISTRO_SECURITY_USER instad of ssl principal (#1897)
* changes to hidden model code to use OPENDISTRO_SECURITY_USER instad of ssl principal Signed-off-by: Bhavana Ramaram <rbhavna@amazon.com>
1 parent 54c788a commit de59efc

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

plugin/src/main/java/org/opensearch/ml/utils/RestActionUtils.java

+27-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import static org.opensearch.ml.common.MLModel.MODEL_CONTENT_FIELD;
99
import static org.opensearch.ml.common.MLModel.OLD_MODEL_CONTENT_FIELD;
1010

11+
import java.security.AccessController;
12+
import java.security.PrivilegedActionException;
13+
import java.security.PrivilegedExceptionAction;
1114
import java.util.ArrayList;
1215
import java.util.Collections;
1316
import java.util.HashSet;
@@ -30,7 +33,6 @@
3033
import org.opensearch.cluster.node.DiscoveryNode;
3134
import org.opensearch.cluster.service.ClusterService;
3235
import org.opensearch.common.Nullable;
33-
import org.opensearch.common.util.concurrent.ThreadContext;
3436
import org.opensearch.commons.ConfigConstants;
3537
import org.opensearch.commons.authuser.User;
3638
import org.opensearch.core.action.ActionListener;
@@ -44,6 +46,8 @@
4446
import org.opensearch.search.fetch.subphase.FetchSourceContext;
4547
import org.opensearch.search.internal.InternalSearchResponse;
4648

49+
import com.fasterxml.jackson.databind.JsonNode;
50+
import com.fasterxml.jackson.databind.ObjectMapper;
4751
import com.google.common.annotations.VisibleForTesting;
4852

4953
import lombok.extern.log4j.Log4j2;
@@ -71,9 +75,12 @@ public class RestActionUtils {
7175
public static final String PARAMETER_TOOL_NAME = "tool_name";
7276

7377
public static final String OPENDISTRO_SECURITY_CONFIG_PREFIX = "_opendistro_security_";
74-
public static final String OPENDISTRO_SECURITY_SSL_PRINCIPAL = OPENDISTRO_SECURITY_CONFIG_PREFIX + "ssl_principal";
78+
79+
public static final String OPENDISTRO_SECURITY_USER = OPENDISTRO_SECURITY_CONFIG_PREFIX + "user";
7580

7681
static final Set<LdapName> adminDn = new HashSet<>();
82+
static final Set<String> adminUsernames = new HashSet<String>();
83+
static final ObjectMapper objectMapper = new ObjectMapper();
7784

7885
public static String getAlgorithm(RestRequest request) {
7986
String algorithm = request.param(PARAMETER_ALGORITHM);
@@ -212,7 +219,7 @@ public static Optional<String> getStringParam(RestRequest request, String paramN
212219
*/
213220
public static User getUserContext(Client client) {
214221
String userStr = client.threadPool().getThreadContext().getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
215-
logger.debug("Filtering result by " + userStr);
222+
logger.debug("Current user is " + userStr);
216223
return User.parse(userStr);
217224
}
218225

@@ -226,13 +233,25 @@ public static boolean isSuperAdminUser(ClusterService clusterService, Client cli
226233
logger.debug("{} is registered as an admin dn", dn);
227234
adminDn.add(new LdapName(dn));
228235
} catch (final InvalidNameException e) {
229-
logger.error("Unable to parse admin dn {}", dn, e);
236+
logger.debug("Unable to parse admin dn {}", dn, e);
237+
adminUsernames.add(dn);
230238
}
231239
}
232240

233-
ThreadContext threadContext = client.threadPool().getThreadContext();
234-
final String sslPrincipal = threadContext.getTransient(OPENDISTRO_SECURITY_SSL_PRINCIPAL);
235-
return isAdminDN(sslPrincipal);
241+
Object userObject = client.threadPool().getThreadContext().getTransient(OPENDISTRO_SECURITY_USER);
242+
if (userObject == null)
243+
return false;
244+
try {
245+
return AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> {
246+
String userContext = objectMapper.writeValueAsString(userObject);
247+
final JsonNode node = objectMapper.readTree(userContext);
248+
final String userName = node.get("name").asText();
249+
250+
return isAdminDN(userName);
251+
});
252+
} catch (PrivilegedActionException e) {
253+
throw new RuntimeException(e);
254+
}
236255
}
237256

238257
private static boolean isAdminDN(String dn) {
@@ -241,7 +260,7 @@ private static boolean isAdminDN(String dn) {
241260
try {
242261
return isAdminDN(new LdapName(dn));
243262
} catch (InvalidNameException e) {
244-
return false;
263+
return adminUsernames.contains(dn);
245264
}
246265
}
247266

plugin/src/test/java/org/opensearch/ml/utils/RestActionUtilsTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,20 @@ public void testIsSuperAdminUser() {
297297
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
298298

299299
when(clusterService.getSettings())
300-
.thenReturn(Settings.builder().putList(RestActionUtils.SECURITY_AUTHCZ_ADMIN_DN, "cn=admin").build());
300+
.thenReturn(
301+
Settings.builder().putList(RestActionUtils.SECURITY_AUTHCZ_ADMIN_DN, "CN=kirk,OU=client,O=client,L=test, C=de").build()
302+
);
301303
when(client.threadPool()).thenReturn(mock(ThreadPool.class));
302304
when(client.threadPool().getThreadContext()).thenReturn(threadContext);
303305

304-
threadContext.putTransient(RestActionUtils.OPENDISTRO_SECURITY_SSL_PRINCIPAL, "cn=admin");
306+
threadContext.putTransient(RestActionUtils.OPENDISTRO_SECURITY_USER, Map.of("name", "CN=kirk,OU=client,O=client,L=test,C=de"));
305307

306308
boolean isAdmin = RestActionUtils.isSuperAdminUser(clusterService, client);
307309
Assert.assertTrue(isAdmin);
308310
}
309311

312+
// Need to add a test case to cover non Ldap user
313+
310314
@Test
311315
public void testIsSuperAdminUser_NotAdmin() {
312316
ClusterService clusterService = mock(ClusterService.class);
@@ -317,7 +321,7 @@ public void testIsSuperAdminUser_NotAdmin() {
317321
.thenReturn(Settings.builder().putList(RestActionUtils.SECURITY_AUTHCZ_ADMIN_DN, "cn=admin").build());
318322
when(client.threadPool()).thenReturn(mock(ThreadPool.class));
319323
when(client.threadPool().getThreadContext()).thenReturn(threadContext);
320-
threadContext.putTransient(RestActionUtils.OPENDISTRO_SECURITY_SSL_PRINCIPAL, "cn=notadmin");
324+
threadContext.putTransient(RestActionUtils.OPENDISTRO_SECURITY_USER, Map.of("name", "nonAdmin"));
321325

322326
boolean isAdmin = RestActionUtils.isSuperAdminUser(clusterService, client);
323327
Assert.assertFalse(isAdmin);

0 commit comments

Comments
 (0)