-
Notifications
You must be signed in to change notification settings - Fork 163
[FEATURE] Improve EncryptorImpl with Asynchronous Handling for Scalability #3919
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
base: main
Are you sure you want to change the base?
Changes from all commits
80ad33a
5031849
d0ce2b3
5723dd7
189cee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.function.BiFunction; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
@@ -300,7 +302,7 @@ public void writeTo(StreamOutput out) throws IOException { | |
} | ||
|
||
@Override | ||
public void update(MLCreateConnectorInput updateContent, BiFunction<String, String, String> function) { | ||
public void update(MLCreateConnectorInput updateContent, BiFunction<String, String, Future<String>> function) { | ||
if (updateContent.getName() != null) { | ||
this.name = updateContent.getName(); | ||
} | ||
|
@@ -377,17 +379,32 @@ private List<String> findStringParametersWithNullDefaultValue(String input) { | |
} | ||
|
||
@Override | ||
public void decrypt(String action, BiFunction<String, String, String> function, String tenantId) { | ||
Map<String, String> decrypted = new HashMap<>(); | ||
public void decrypt(String action, BiFunction<String, String, Future<String>> function, String tenantId) { | ||
Map<String, Future<String>> decryptingTempCredential = new HashMap<>(); | ||
decryptedCredential = new HashMap<>(); | ||
for (String key : credential.keySet()) { | ||
decrypted.put(key, function.apply(credential.get(key), tenantId)); | ||
decryptingTempCredential.put(key, function.apply(credential.get(key), tenantId)); | ||
} | ||
this.decryptedCredential = decrypted; | ||
fillCredential(decryptingTempCredential, decryptedCredential); | ||
Comment on lines
+384
to
+388
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section of code is very confusing. It took me a good 10 minutes to figure out that we're updating a superclass field. Suggestions to improve readability:
Similar comments apply to both en/de and Http/Mcp connectors. |
||
Optional<ConnectorAction> connectorAction = findAction(action); | ||
Map<String, String> headers = connectorAction.map(ConnectorAction::getHeaders).orElse(null); | ||
this.decryptedHeaders = createDecryptedHeaders(headers); | ||
} | ||
|
||
private void fillCredential(Map<String, Future<String>> decrypted, Map<String, String> decryptedCredential) { | ||
for (String key : decrypted.keySet()) { | ||
try { | ||
if (decrypted.get(key) != null) { | ||
decryptedCredential.put(key, decrypted.get(key).get()); | ||
} else { | ||
decryptedCredential.put(key, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when the key is null, and you put value of null, what is the intention here? why don't you skip when the key is null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 why we are assigning null here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 why not use |
||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add meaningful exception message to indicate what goes wrong here. something like, failed to process fill Credentials. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you switch from |
||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Connector cloneConnector() { | ||
try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) { | ||
|
@@ -400,11 +417,12 @@ public Connector cloneConnector() { | |
} | ||
|
||
@Override | ||
public void encrypt(BiFunction<String, String, String> function, String tenantId) { | ||
public void encrypt(BiFunction<String, String, Future<String>> function, String tenantId) { | ||
Map<String, Future<String>> encryptingCredential = new HashMap<>(); | ||
for (String key : credential.keySet()) { | ||
String encrypted = function.apply(credential.get(key), tenantId); | ||
credential.put(key, encrypted); | ||
encryptingCredential.put(key, function.apply(credential.get(key), tenantId)); | ||
} | ||
fillCredential(encryptingCredential, credential); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.function.BiFunction; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
@@ -205,21 +207,37 @@ protected Map<String, String> createDecryptedHeaders(Map<String, String> headers | |
} | ||
|
||
@Override | ||
public void decrypt(String action, BiFunction<String, String, String> function, String tenantId) { | ||
Map<String, String> decrypted = new HashMap<>(); | ||
public void decrypt(String action, BiFunction<String, String, Future<String>> function, String tenantId) { | ||
Map<String, Future<String>> decryptingTempCredential = new HashMap<>(); | ||
decryptedCredential = new HashMap<>(); | ||
for (String key : credential.keySet()) { | ||
decrypted.put(key, function.apply(credential.get(key), tenantId)); | ||
decryptingTempCredential.put(key, function.apply(credential.get(key), tenantId)); | ||
} | ||
this.decryptedCredential = decrypted; | ||
fillCredential(decryptingTempCredential, decryptedCredential); | ||
this.decryptedHeaders = createDecryptedHeaders(headers); | ||
} | ||
|
||
private void fillCredential(Map<String, Future<String>> decrypted, Map<String, String> decryptedCredential) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Input validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better yet since we're creating a new map for |
||
for (String key : decrypted.keySet()) { | ||
try { | ||
if (decrypted.get(key) != null) { | ||
decryptedCredential.put(key, decrypted.get(key).get()); | ||
} else { | ||
decryptedCredential.put(key, null); | ||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void encrypt(BiFunction<String, String, String> function, String tenantId) { | ||
public void encrypt(BiFunction<String, String, Future<String>> function, String tenantId) { | ||
Map<String, Future<String>> encryptingCredential = new HashMap<>(); | ||
for (String key : credential.keySet()) { | ||
String encrypted = function.apply(credential.get(key), tenantId); | ||
credential.put(key, encrypted); | ||
encryptingCredential.put(key, function.apply(credential.get(key), tenantId)); | ||
} | ||
fillCredential(encryptingCredential, credential); | ||
} | ||
|
||
@Override | ||
|
@@ -332,7 +350,7 @@ public void writeTo(StreamOutput out) throws IOException { | |
} | ||
|
||
@Override | ||
public void update(MLCreateConnectorInput updateContent, BiFunction<String, String, String> function) { | ||
public void update(MLCreateConnectorInput updateContent, BiFunction<String, String, Future<String>> function) { | ||
if (updateContent.getName() != null) { | ||
this.name = updateContent.getName(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import java.util.function.BiFunction; | ||
|
||
import org.junit.Assert; | ||
|
@@ -39,13 +41,13 @@ public class AwsConnectorTest { | |
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
BiFunction<String, String, String> encryptFunction; | ||
BiFunction<String, String, String> decryptFunction; | ||
BiFunction<String, String, Future<String>> encryptFunction; | ||
BiFunction<String, String, Future<String>> decryptFunction;; | ||
|
||
@Before | ||
public void setUp() { | ||
encryptFunction = (s, v) -> "encrypted: " + s.toLowerCase(Locale.ROOT); | ||
decryptFunction = (s, v) -> "decrypted: " + s.toUpperCase(Locale.ROOT); | ||
encryptFunction = (s, v) -> CompletableFuture.supplyAsync(() -> "encrypted: " + s.toLowerCase(Locale.ROOT)); | ||
decryptFunction = (s, v) -> CompletableFuture.supplyAsync(() -> "decrypted: " + s.toUpperCase(Locale.ROOT)); | ||
Comment on lines
+49
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not including an You should be using a thread pool here. Example. Similar comment in HttpConnectorTest and AbstractConnectorTest. |
||
} | ||
|
||
@Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider an
ActionFuture
here. It has better handling of OpenSearch-specific thread pools, exceptions, and task APIs.