12
12
import org .apache .commons .text .StringSubstitutor ;
13
13
import org .opensearch .common .io .stream .BytesStreamOutput ;
14
14
import org .opensearch .commons .authuser .User ;
15
+ import org .opensearch .core .action .ActionListener ;
15
16
import org .opensearch .core .common .io .stream .StreamInput ;
16
17
import org .opensearch .core .common .io .stream .StreamOutput ;
17
18
import org .opensearch .core .xcontent .XContentBuilder ;
25
26
import java .util .List ;
26
27
import java .util .Map ;
27
28
import java .util .Optional ;
29
+ import java .util .concurrent .atomic .AtomicBoolean ;
30
+ import java .util .concurrent .atomic .AtomicInteger ;
31
+ import java .util .function .BiConsumer ;
32
+ import java .util .function .BiFunction ;
33
+ import java .util .function .Consumer ;
28
34
import java .util .function .Function ;
29
35
import java .util .regex .Matcher ;
30
36
import java .util .regex .Pattern ;
@@ -273,7 +279,7 @@ public void writeTo(StreamOutput out) throws IOException {
273
279
}
274
280
275
281
@ Override
276
- public void update (MLCreateConnectorInput updateContent , Function <String , String > function ) {
282
+ public void update (MLCreateConnectorInput updateContent , BiConsumer <String , ActionListener < String >> consumer , ActionListener < String > listener ) {
277
283
if (updateContent .getName () != null ) {
278
284
this .name = updateContent .getName ();
279
285
}
@@ -291,7 +297,7 @@ public void update(MLCreateConnectorInput updateContent, Function<String, String
291
297
}
292
298
if (updateContent .getCredential () != null && updateContent .getCredential ().size () > 0 ) {
293
299
this .credential = updateContent .getCredential ();
294
- encrypt (function );
300
+ encrypt (consumer , listener );
295
301
}
296
302
if (updateContent .getActions () != null ) {
297
303
this .actions = updateContent .getActions ();
@@ -349,15 +355,42 @@ private List<String> findStringParametersWithNullDefaultValue(String input) {
349
355
}
350
356
351
357
@ Override
352
- public void decrypt (String action , Function <String , String > function ) {
358
+ public void decrypt (String action , BiConsumer <String , ActionListener < String >> consumer , ActionListener < String > listener ) {
353
359
Map <String , String > decrypted = new HashMap <>();
360
+ AtomicBoolean completed = new AtomicBoolean (false );
361
+
354
362
for (String key : credential .keySet ()) {
355
- decrypted .put (key , function .apply (credential .get (key )));
356
- }
357
- this .decryptedCredential = decrypted ;
358
- Optional <ConnectorAction > connectorAction = findAction (action );
359
- Map <String , String > headers = connectorAction .isPresent () ? connectorAction .get ().getHeaders () : null ;
360
- this .decryptedHeaders = createDecryptedHeaders (headers );
363
+ consumer .accept (credential .get (key ), new ActionListener <>() {
364
+ @ Override
365
+ public void onResponse (String decryptedValue ) {
366
+ decrypted .put (key , decryptedValue );
367
+ if (decrypted .size () == credential .size () && !completed .get ()) {
368
+ completed .set (true );
369
+ decryptedCredential = decrypted ;
370
+ Optional <ConnectorAction > connectorAction = findAction (action );
371
+ Map <String , String > headers = connectorAction .isPresent () ? connectorAction .get ().getHeaders () : null ;
372
+ decryptedHeaders = createDecryptedHeaders (headers );
373
+ listener .onResponse ("All credentials encrypted successfully" ); // Notify that decryption is complete
374
+ }
375
+ }
376
+
377
+ @ Override
378
+ public void onFailure (Exception e ) {
379
+ log .error ("Failed to decrypt credential for key: " + key , e );
380
+ if (!completed .getAndSet (true )) {
381
+ listener .onFailure (e );
382
+ }
383
+ }
384
+ });
385
+ }
386
+ // Map<String, String> decrypted = new HashMap<>();
387
+ // for (String key : credential.keySet()) {
388
+ // decrypted.put(key, function.apply(credential.get(key)));
389
+ // }
390
+ // this.decryptedCredential = decrypted;
391
+ // Optional<ConnectorAction> connectorAction = findAction(action);
392
+ // Map<String, String> headers = connectorAction.isPresent() ? connectorAction.get().getHeaders() : null;
393
+ // this.decryptedHeaders = createDecryptedHeaders(headers);
361
394
}
362
395
363
396
@ Override
@@ -372,10 +405,35 @@ public Connector cloneConnector() {
372
405
}
373
406
374
407
@ Override
375
- public void encrypt (Function <String , String > function ) {
408
+ // public void encrypt(Function<String, String> function) {
409
+ // for (String key : credential.keySet()) {
410
+ // String encrypted = function.apply(credential.get(key));
411
+ // credential.put(key, encrypted);
412
+ // }
413
+ // }
414
+
415
+ public void encrypt (BiConsumer <String , ActionListener <String >> consumer , ActionListener <String > listener ) {
416
+ AtomicBoolean completed = new AtomicBoolean (false );
417
+
376
418
for (String key : credential .keySet ()) {
377
- String encrypted = function .apply (credential .get (key ));
378
- credential .put (key , encrypted );
419
+ consumer .accept (credential .get (key ), new ActionListener <>() {
420
+ @ Override
421
+ public void onResponse (String encrypted ) {
422
+ credential .put (key , encrypted );
423
+ if (credential .entrySet ().stream ().allMatch (entry -> entry .getValue () != null )) {
424
+ completed .set (true );
425
+ listener .onResponse ("All credentials encrypted successfully" );
426
+ }
427
+ }
428
+
429
+ @ Override
430
+ public void onFailure (Exception e ) {
431
+ log .error ("Failed to encrypt credential for key: " + key , e );
432
+ if (!completed .getAndSet (true )) {
433
+ listener .onFailure (e );
434
+ }
435
+ }
436
+ });
379
437
}
380
438
}
381
439
0 commit comments