From a6ff1f5506ed2316ba02bbb2b07c43d9c70cfe66 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 30 Oct 2024 09:19:14 +0100 Subject: [PATCH] Refactor JS expressions for accessing secrets (#6070) * Add support for secret accessor functions in JavaScript Refactored the rendering and evaluation of JavaScript to include method definitions alongside existing properties. Enhanced secret handling by generating asynchronous accessor functions, improving the clarity and functionality of secret management in scripting. * Refactor secret retrieval logic in JavaScript engine configuration Refactored the code to use a dedicated method `ResolveSecretAsync` for secret retrieval, improving clarity and maintainability. Added a check to ensure that only active secrets are decrypted, enhancing robustness and error handling. * Add SecretExpired notification and mediator to updater Introduces a new SecretExpired notification class and integrates it within DefaultExpiredSecretsUpdater. The updater now sends a SecretExpired notification via the mediator upon expiring a secret. --- src/apps/Elsa.Server.Web/Program.cs | 1 - .../Services/JintJavaScriptEvaluator.cs | 7 ++- .../TypeDefinitionDocumentRenderer.cs | 15 ++++-- .../Notifications/SecretExpired.cs | 5 ++ .../Services/DefaultExpiredSecretsUpdater.cs | 5 +- .../Services/DefaultSecretNameGenerator.cs | 2 +- .../JavaScript/ConfigureEngineWithSecrets.cs | 46 ++++--------------- .../SecretsTypeDefinitionProvider.cs | 7 +-- 8 files changed, 40 insertions(+), 48 deletions(-) create mode 100644 src/modules/Elsa.Secrets.Management/Notifications/SecretExpired.cs diff --git a/src/apps/Elsa.Server.Web/Program.cs b/src/apps/Elsa.Server.Web/Program.cs index ae7a2ffa9..8038fa15b 100644 --- a/src/apps/Elsa.Server.Web/Program.cs +++ b/src/apps/Elsa.Server.Web/Program.cs @@ -52,7 +52,6 @@ using Proto.Remote.GrpcNet; using StackExchange.Redis; // ReSharper disable RedundantAssignment - const PersistenceProvider persistenceProvider = PersistenceProvider.EntityFrameworkCore; const SqlDatabaseProvider sqlDatabaseProvider = SqlDatabaseProvider.Sqlite; const bool useHangfire = false; diff --git a/src/modules/Elsa.JavaScript/Services/JintJavaScriptEvaluator.cs b/src/modules/Elsa.JavaScript/Services/JintJavaScriptEvaluator.cs index 21f8147b4..c5ea7ad9a 100644 --- a/src/modules/Elsa.JavaScript/Services/JintJavaScriptEvaluator.cs +++ b/src/modules/Elsa.JavaScript/Services/JintJavaScriptEvaluator.cs @@ -48,7 +48,10 @@ public class JintJavaScriptEvaluator(IConfiguration configuration, INotification { options ??= new ExpressionEvaluatorOptions(); - var engineOptions = new Jint.Options(); + var engineOptions = new Jint.Options + { + ExperimentalFeatures = ExperimentalFeature.TaskInterop + }; if (_jintOptions.AllowClrAccess) engineOptions.AllowClr(); @@ -111,7 +114,7 @@ public class JintJavaScriptEvaluator(IConfiguration configuration, INotification { var preparedScript = GetOrCreatePrepareScript(expression); var result = engine.Evaluate(preparedScript); - return result.ToObject(); + return result.UnwrapIfPromise().ToObject(); } private Prepared