Skip to content

adding tenantId to the connector executor when this is inline connector #3837

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

Merged
merged 3 commits into from
May 7, 2025
Merged
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 @@ -103,6 +103,10 @@ public void initModel(MLModel model, Map<String, Object> params, Encryptor encry
Connector connector = model.getConnector().cloneConnector();
connector
.decrypt(PREDICT.name(), (credential, tenantId) -> encryptor.decrypt(credential, model.getTenantId()), model.getTenantId());
// This situation can only happen for inline connector where we don't provide tenant id.
if (connector.getTenantId() == null && model.getTenantId() != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if model.getTenantId == null, this code won't do anything. But if that is an edge case to avoid in serverLess, you should check it and throw an exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If both are null, that means this is a single tenant environment, so we don't need this anyway.

connector.setTenantId(model.getTenantId());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am wondering why we don't set tenantId for the inline connector when registering the model which can make a consistent connector to use anywhere. Now the cloned connector and connectorExecutor has the tenantId but the original connector doesn't has it. If we want to use model.getConnector in code to reference, we have to check tenantId again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Model already has the tenantId information. So in the same entity to have the tenant id twice seems like redundant to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it's pros and cons. I am OK to merge it for now, we can have more discussion later.

}
this.connectorExecutor = MLEngineClassLoader.initInstance(connector.getProtocol(), connector, Connector.class);
this.connectorExecutor.setScriptService((ScriptService) params.get(SCRIPT_SERVICE));
this.connectorExecutor.setClusterService((ClusterService) params.get(CLUSTER_SERVICE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
package org.opensearch.ml.engine.algorithms.remote;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -39,7 +39,6 @@
import org.opensearch.ml.engine.MLEngineClassLoader;
import org.opensearch.ml.engine.MLStaticMockBase;
import org.opensearch.ml.engine.encryptor.Encryptor;
import org.opensearch.ml.engine.encryptor.EncryptorImpl;

import com.google.common.collect.ImmutableMap;

Expand All @@ -64,7 +63,9 @@ public class RemoteModelTest extends MLStaticMockBase {
public void setUp() {
MockitoAnnotations.openMocks(this);
remoteModel = new RemoteModel();
encryptor = spy(new EncryptorImpl(null, "m+dWmfmnNRiNlOdej/QelEkvMTyH//frS2TBeS2BP4w="));

encryptor = mock(Encryptor.class);
when(encryptor.decrypt(any(), any())).thenReturn("test_api_key");
}

@Test
Expand Down Expand Up @@ -189,7 +190,7 @@ public void initModel_NullHeader() {
when(mlModel.getConnector()).thenReturn(connector);
remoteModel.initModel(mlModel, ImmutableMap.of(), encryptor);
Map<String, String> decryptedHeaders = connector.getDecryptedHeaders();
Assert.assertNull(decryptedHeaders);
assertNull(decryptedHeaders);
}

@Test
Expand All @@ -200,12 +201,24 @@ public void initModel_WithHeader() {
Map<String, String> decryptedHeaders = connector.getDecryptedHeaders();
RemoteConnectorExecutor executor = remoteModel.getConnectorExecutor();
Assert.assertNotNull(executor);
Assert.assertNull(decryptedHeaders);
assertNull(decryptedHeaders);
Assert.assertNotNull(executor.getConnector().getDecryptedHeaders());
assertEquals(1, executor.getConnector().getDecryptedHeaders().size());
assertEquals("Bearer test_api_key", executor.getConnector().getDecryptedHeaders().get("Authorization"));
remoteModel.close();
Assert.assertNull(remoteModel.getConnectorExecutor());
assertNull(remoteModel.getConnectorExecutor());
}

@Test
public void initModel_setsTenantIdOnClonedConnector_whenMissing() {
Connector connector = createConnector(ImmutableMap.of("Authorization", "Bearer ${credential.key}"));
when(mlModel.getConnector()).thenReturn(connector);
when(mlModel.getTenantId()).thenReturn("tenantId");
remoteModel.initModel(mlModel, ImmutableMap.of(), encryptor);
RemoteConnectorExecutor executor = remoteModel.getConnectorExecutor();
remoteModel.close();
assertNull(connector.getTenantId());
assertEquals("tenantId", executor.getConnector().getTenantId());
}

private Connector createConnector(Map<String, String> headers) {
Expand All @@ -222,7 +235,7 @@ private Connector createConnector(Map<String, String> headers) {
.name("test connector")
.protocol(ConnectorProtocols.HTTP)
.version("1")
.credential(ImmutableMap.of("key", encryptor.encrypt("test_api_key", null)))
.credential(ImmutableMap.of("key", "dummy-encrypted-value"))
.actions(Arrays.asList(predictAction))
.build();
return connector;
Expand Down
Loading