| | | 1 | | using System.Text.Encodings.Web; |
| | | 2 | | using System.Threading.RateLimiting; |
| | | 3 | | using Elsa.Caching.Options; |
| | | 4 | | using Elsa.Common.RecurringTasks; |
| | | 5 | | using Elsa.Expressions.Helpers; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Features.Services; |
| | | 8 | | using Elsa.Http.Options; |
| | | 9 | | using Elsa.Identity.Multitenancy; |
| | | 10 | | using Elsa.Persistence.EFCore.Extensions; |
| | | 11 | | using Elsa.Persistence.EFCore.Modules.Management; |
| | | 12 | | using Elsa.Persistence.EFCore.Modules.Runtime; |
| | | 13 | | using Elsa.Server.Web.Activities; |
| | | 14 | | using Elsa.Server.Web.ActivityHosts; |
| | | 15 | | using Elsa.Server.Web.Filters; |
| | | 16 | | using Elsa.Tenants; |
| | | 17 | | using Elsa.Tenants.AspNetCore; |
| | | 18 | | using Elsa.Tenants.Extensions; |
| | | 19 | | using Elsa.WorkflowProviders.BlobStorage.ElsaScript.Extensions; |
| | | 20 | | using Elsa.Workflows; |
| | | 21 | | using Elsa.Workflows.Activities.Flowchart.Extensions; |
| | | 22 | | using Elsa.Workflows.Api; |
| | | 23 | | using Elsa.Workflows.CommitStates.Strategies; |
| | | 24 | | using Elsa.Workflows.IncidentStrategies; |
| | | 25 | | using Elsa.Workflows.LogPersistence; |
| | | 26 | | using Elsa.Workflows.Options; |
| | | 27 | | using Elsa.Workflows.Runtime.Distributed.Extensions; |
| | | 28 | | using Elsa.Workflows.Runtime.Options; |
| | | 29 | | using Elsa.Workflows.Runtime.Tasks; |
| | | 30 | | using JetBrains.Annotations; |
| | | 31 | | using Microsoft.AspNetCore.RateLimiting; |
| | | 32 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | | 33 | | using Microsoft.Extensions.Options; |
| | | 34 | | |
| | | 35 | | // ReSharper disable RedundantAssignment |
| | | 36 | | const bool useReadOnlyMode = false; |
| | | 37 | | const bool useSignalR = false; // Disabled until Elsa Studio sends authenticated requests. |
| | | 38 | | const bool useStructuredLogs = false; // Enable to inspect backend logs from Elsa Studio. |
| | | 39 | | const bool useMultitenancy = true; |
| | | 40 | | const bool disableVariableWrappers = false; |
| | | 41 | | const string elsaApiRateLimitingPolicy = "elsa-api"; |
| | | 42 | | const string httpWorkflowRateLimitingPolicy = "elsa-http-workflows"; |
| | | 43 | | |
| | 3 | 44 | | ObjectConverter.StrictMode = true; |
| | | 45 | | |
| | 3 | 46 | | var builder = WebApplication.CreateBuilder(args); |
| | 3 | 47 | | var services = builder.Services; |
| | 3 | 48 | | var configuration = builder.Configuration; |
| | 3 | 49 | | var identitySection = configuration.GetSection("Identity"); |
| | 3 | 50 | | var identityTokenSection = identitySection.GetSection("Tokens"); |
| | 3 | 51 | | var ingressRateLimitingSection = configuration.GetSection("IngressRateLimiting"); |
| | 3 | 52 | | var useIngressRateLimiting = ingressRateLimitingSection.GetValue("Enabled", false); |
| | 3 | 53 | | var registerIngressRateLimitingPolicies = ingressRateLimitingSection.GetValue("RegisterReferencePolicies", useIngressRat |
| | 3 | 54 | | var configuredAllowLocalDistributedRuntimeLockProvider = configuration.GetValue<bool?>("DistributedRuntime:AllowLocalLoc |
| | 3 | 55 | | var allowLocalDistributedRuntimeLockProvider = |
| | 3 | 56 | | configuredAllowLocalDistributedRuntimeLockProvider ?? builder.Environment.IsDevelopment(); |
| | | 57 | | |
| | 3 | 58 | | services |
| | 3 | 59 | | .AddElsa(elsa => |
| | 3 | 60 | | { |
| | 3 | 61 | | elsa |
| | 3 | 62 | | .AddActivitiesFrom<Program>() |
| | 3 | 63 | | .AddActivityHost<Penguin>() |
| | 3 | 64 | | .AddWorkflowsFrom<Program>() |
| | 3 | 65 | | .UseIdentity(identity => |
| | 3 | 66 | | { |
| | 6 | 67 | | identity.TokenOptions += options => identityTokenSection.Bind(options); |
| | 3 | 68 | | identity.UseConfigurationBasedUserProvider(options => identitySection.Bind(options)); |
| | 3 | 69 | | identity.UseConfigurationBasedApplicationProvider(options => identitySection.Bind(options)); |
| | 3 | 70 | | identity.UseConfigurationBasedRoleProvider(options => identitySection.Bind(options)); |
| | 3 | 71 | | }) |
| | 3 | 72 | | .UseDefaultAuthentication() |
| | 3 | 73 | | .UseWorkflows(workflows => |
| | 3 | 74 | | { |
| | 3 | 75 | | workflows.UseCommitStrategies(strategies => |
| | 3 | 76 | | { |
| | 3 | 77 | | strategies.AddStandardStrategies(); |
| | 3 | 78 | | strategies.Add("Every 10 seconds", new PeriodicWorkflowStrategy(TimeSpan.FromSeconds(10))); |
| | 6 | 79 | | }); |
| | 3 | 80 | | }) |
| | 3 | 81 | | .UseFlowchart(flowchart => flowchart.UseTokenBasedExecution()) |
| | 3 | 82 | | .UseWorkflowManagement(management => |
| | 3 | 83 | | { |
| | 6 | 84 | | management.UseEntityFrameworkCore(ef => ef.UseSqlite()); |
| | 3 | 85 | | management.SetDefaultLogPersistenceMode(LogPersistenceMode.Inherit); |
| | 3 | 86 | | management.UseCache(); |
| | 3 | 87 | | management.UseReadOnlyMode(useReadOnlyMode); |
| | 3 | 88 | | }) |
| | 3 | 89 | | .UseWorkflowRuntime(runtime => |
| | 3 | 90 | | { |
| | 6 | 91 | | runtime.UseEntityFrameworkCore(ef => ef.UseSqlite()); |
| | 3 | 92 | | runtime.UseCache(); |
| | 3 | 93 | | runtime.UseDistributedRuntime(); |
| | 3 | 94 | | // This sample host acknowledges single-host file-system locks in development or explicit single-host op |
| | 3 | 95 | | runtime.DistributedLockingOptions = options => options.AllowLocalLockProviderInDistributedRuntime = allo |
| | 3 | 96 | | }) |
| | 3 | 97 | | .UseWorkflowsApi() |
| | 3 | 98 | | .UseFluentStorageProvider() |
| | 3 | 99 | | .UseElsaScriptBlobStorage() |
| | 3 | 100 | | .UseScheduling() |
| | 3 | 101 | | .UseCSharp(options => |
| | 3 | 102 | | { |
| | 3 | 103 | | configuration.GetSection("Scripting:CSharp").Bind(options); |
| | 3 | 104 | | options.DisableWrappers = disableVariableWrappers; |
| | 3 | 105 | | options.AppendScript("string Greet(string name) => $\"Hello {name}!\";"); |
| | 3 | 106 | | options.AppendScript("string SayHelloWorld() => Greet(\"World\");"); |
| | 3 | 107 | | }) |
| | 3 | 108 | | .UseJavaScript(options => |
| | 3 | 109 | | { |
| | 3 | 110 | | options.AllowClrAccess = true; |
| | 3 | 111 | | options.ConfigureEngine(engine => |
| | 3 | 112 | | { |
| | 19 | 113 | | engine.Execute("function greet(name) { return `Hello ${name}!`; }"); |
| | 19 | 114 | | engine.Execute("function sayHelloWorld() { return greet('World'); }"); |
| | 22 | 115 | | }); |
| | 3 | 116 | | }) |
| | 3 | 117 | | .UsePython(python => |
| | 3 | 118 | | { |
| | 3 | 119 | | python.PythonOptions += options => |
| | 3 | 120 | | { |
| | 3 | 121 | | // Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1 |
| | 3 | 122 | | // alternatively, you can set the PYTHONNET_PYDLL environment variable. |
| | 3 | 123 | | configuration.GetSection("Scripting:Python").Bind(options); |
| | 3 | 124 | | |
| | 3 | 125 | | options.AddScript(sb => |
| | 3 | 126 | | { |
| | 3 | 127 | | sb.AppendLine("def greet():"); |
| | 3 | 128 | | sb.AppendLine(" return \"Hello, welcome to Python!\""); |
| | 6 | 129 | | }); |
| | 6 | 130 | | }; |
| | 3 | 131 | | }) |
| | 6 | 132 | | .UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = HtmlEncoder.Default) |
| | 3 | 133 | | .UseHttp(http => |
| | 3 | 134 | | { |
| | 6 | 135 | | http.ConfigureHttpOptions = options => configuration.GetSection("Http").Bind(options); |
| | 3 | 136 | | http.UseCache(); |
| | 6 | 137 | | }); |
| | 3 | 138 | | |
| | 3 | 139 | | if(useMultitenancy) |
| | 3 | 140 | | { |
| | 3 | 141 | | elsa.UseTenants(tenants => |
| | 3 | 142 | | { |
| | 4 | 143 | | tenants.UseConfigurationBasedTenantsProvider(options => configuration.GetSection("Multitenancy").Bind(op |
| | 4 | 144 | | tenants.ConfigureMultitenancy(options => options.TenantResolverPipelineBuilder = new TenantResolverPipel |
| | 4 | 145 | | .Append<CurrentUserTenantResolver>()); |
| | 6 | 146 | | }); |
| | 3 | 147 | | } |
| | 3 | 148 | | |
| | 3 | 149 | | if(useStructuredLogs) |
| | 3 | 150 | | elsa.UseStructuredLogs(); |
| | 3 | 151 | | |
| | 3 | 152 | | ConfigureForTest?.Invoke(elsa); |
| | 6 | 153 | | }); |
| | | 154 | | |
| | | 155 | | // Obfuscate HTTP request headers. |
| | 3 | 156 | | services.AddActivityStateFilter<HttpRequestAuthenticationHeaderFilter>(); |
| | | 157 | | |
| | | 158 | | // Optionally configure recurring tasks using alternative schedules. |
| | 3 | 159 | | services.Configure<RecurringTaskOptions>(options => |
| | 3 | 160 | | { |
| | 3 | 161 | | options.Schedule.ConfigureTask<TriggerBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300)); |
| | 3 | 162 | | options.Schedule.ConfigureTask<PurgeBookmarkQueueRecurringTask>(TimeSpan.FromSeconds(300)); |
| | 3 | 163 | | options.Schedule.ConfigureTask<RestartInterruptedWorkflowsTask>(TimeSpan.FromSeconds(15)); |
| | 6 | 164 | | }); |
| | | 165 | | |
| | 9 | 166 | | services.Configure<RuntimeOptions>(options => { options.InactivityThreshold = TimeSpan.FromSeconds(15); }); |
| | 3 | 167 | | services.Configure<BookmarkQueuePurgeOptions>(options => options.Ttl = TimeSpan.FromSeconds(3600)); |
| | 6 | 168 | | services.Configure<CachingOptions>(options => options.CacheDuration = TimeSpan.FromDays(1)); |
| | 3 | 169 | | services.Configure<IncidentOptions>(options => options.DefaultIncidentStrategy = typeof(ContinueWithIncidentsStrategy)); |
| | 3 | 170 | | if (useIngressRateLimiting) |
| | | 171 | | { |
| | 0 | 172 | | services.PostConfigure<ApiEndpointOptions>(options => |
| | 0 | 173 | | { |
| | 0 | 174 | | if (options.RateLimitingPolicyName == null) |
| | 0 | 175 | | options.RateLimitingPolicyName = elsaApiRateLimitingPolicy; |
| | 0 | 176 | | }); |
| | 0 | 177 | | services.PostConfigure<HttpActivityOptions>(options => |
| | 0 | 178 | | { |
| | 0 | 179 | | if (options.RateLimitingPolicyName == null) |
| | 0 | 180 | | options.RateLimitingPolicyName = httpWorkflowRateLimitingPolicy; |
| | 0 | 181 | | }); |
| | | 182 | | } |
| | | 183 | | |
| | 3 | 184 | | services.AddRateLimiter(options => |
| | 3 | 185 | | { |
| | 0 | 186 | | options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| | 3 | 187 | | |
| | 0 | 188 | | if (registerIngressRateLimitingPolicies) |
| | 3 | 189 | | { |
| | 0 | 190 | | options.AddFixedWindowLimiter(elsaApiRateLimitingPolicy, limiterOptions => |
| | 0 | 191 | | { |
| | 0 | 192 | | limiterOptions.PermitLimit = ingressRateLimitingSection.GetValue("ApiPermitLimit", 120); |
| | 0 | 193 | | limiterOptions.Window = TimeSpan.FromSeconds(ingressRateLimitingSection.GetValue("ApiWindowSeconds", 60)); |
| | 0 | 194 | | limiterOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; |
| | 0 | 195 | | limiterOptions.QueueLimit = ingressRateLimitingSection.GetValue("ApiQueueLimit", 0); |
| | 0 | 196 | | }); |
| | 0 | 197 | | options.AddFixedWindowLimiter(httpWorkflowRateLimitingPolicy, limiterOptions => |
| | 0 | 198 | | { |
| | 0 | 199 | | limiterOptions.PermitLimit = ingressRateLimitingSection.GetValue("HttpWorkflowPermitLimit", 60); |
| | 0 | 200 | | limiterOptions.Window = TimeSpan.FromSeconds(ingressRateLimitingSection.GetValue("HttpWorkflowWindowSeconds" |
| | 0 | 201 | | limiterOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; |
| | 0 | 202 | | limiterOptions.QueueLimit = ingressRateLimitingSection.GetValue("HttpWorkflowQueueLimit", 0); |
| | 0 | 203 | | }); |
| | 3 | 204 | | } |
| | 3 | 205 | | }); |
| | 3 | 206 | | services |
| | 3 | 207 | | .AddHealthChecks() |
| | 3 | 208 | | .AddElsaReadinessChecks(includeDistributedLocks: true); |
| | 3 | 209 | | services.AddControllers(); |
| | 9 | 210 | | services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithE |
| | | 211 | | |
| | | 212 | | // Build the web application. |
| | 3 | 213 | | var app = builder.Build(); |
| | | 214 | | |
| | | 215 | | |
| | | 216 | | // Configure the pipeline. |
| | 3 | 217 | | if (app.Environment.IsDevelopment()) |
| | 3 | 218 | | app.UseDeveloperExceptionPage(); |
| | | 219 | | |
| | | 220 | | // CORS. |
| | 3 | 221 | | app.UseCors(); |
| | | 222 | | |
| | | 223 | | // Health checks. |
| | 3 | 224 | | app.MapHealthChecks("/health/live", new() |
| | 3 | 225 | | { |
| | 0 | 226 | | Predicate = _ => false |
| | 3 | 227 | | }); |
| | 3 | 228 | | app.MapHealthChecks("/health/ready", new() |
| | 3 | 229 | | { |
| | 0 | 230 | | Predicate = check => check.Tags.Contains(HealthCheckExtensions.ElsaTag) && check.Tags.Contains(HealthCheckExtensions |
| | 3 | 231 | | ResultStatusCodes = |
| | 3 | 232 | | { |
| | 3 | 233 | | [HealthStatus.Degraded] = StatusCodes.Status503ServiceUnavailable, |
| | 3 | 234 | | [HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable |
| | 3 | 235 | | } |
| | 3 | 236 | | }); |
| | 3 | 237 | | app.MapHealthChecks("/", new() |
| | 3 | 238 | | { |
| | 0 | 239 | | Predicate = _ => false |
| | 3 | 240 | | }); |
| | | 241 | | |
| | | 242 | | // Elsa API endpoints for designer. |
| | 3 | 243 | | var apiEndpointOptions = app.Services.GetRequiredService<IOptions<ApiEndpointOptions>>().Value; |
| | 3 | 244 | | var routePrefix = apiEndpointOptions.RoutePrefix; |
| | 3 | 245 | | app.MapWorkflowsApi(routePrefix); |
| | | 246 | | |
| | | 247 | | // Routing used for SignalR. |
| | 3 | 248 | | app.UseRouting(); |
| | | 249 | | |
| | 3 | 250 | | app.UseWorkflowsApiRateLimiting(routePrefix, apiEndpointOptions.RateLimitingPolicyName); |
| | | 251 | | |
| | | 252 | | // Elsa HTTP Endpoint activities. |
| | 3 | 253 | | var httpActivityOptions = app.Services.GetRequiredService<IOptions<HttpActivityOptions>>().Value; |
| | 3 | 254 | | app.UseWorkflowsRateLimiting(httpActivityOptions.BasePath, httpActivityOptions.RateLimitingPolicyName); |
| | 3 | 255 | | if (useIngressRateLimiting || |
| | 3 | 256 | | !string.IsNullOrWhiteSpace(apiEndpointOptions.RateLimitingPolicyName) || |
| | 3 | 257 | | !string.IsNullOrWhiteSpace(httpActivityOptions.RateLimitingPolicyName)) |
| | 0 | 258 | | app.UseRateLimiter(); |
| | | 259 | | |
| | | 260 | | // Security. |
| | 3 | 261 | | app.UseAuthentication(); |
| | 3 | 262 | | app.UseAuthorization(); |
| | | 263 | | |
| | | 264 | | // Multitenancy. |
| | | 265 | | if (useMultitenancy) |
| | 3 | 266 | | app.UseTenants(); |
| | | 267 | | |
| | | 268 | | // Captures unhandled exceptions and returns a JSON response. |
| | 3 | 269 | | app.UseJsonSerializationErrorHandler(); |
| | | 270 | | |
| | 3 | 271 | | app.UseWorkflows(); |
| | | 272 | | |
| | 3 | 273 | | app.MapControllers(); |
| | | 274 | | |
| | | 275 | | // Swagger API documentation. |
| | 3 | 276 | | if (app.Environment.IsDevelopment()) |
| | | 277 | | { |
| | 3 | 278 | | app.UseSwaggerUI(); |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | // SignalR. |
| | | 282 | | if (useSignalR) |
| | | 283 | | { |
| | | 284 | | app.UseWorkflowsSignalRHubs(); |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | // Structured log streaming for Studio diagnostics. |
| | | 288 | | if (useStructuredLogs) |
| | | 289 | | { |
| | | 290 | | app.UseStructuredLogs(); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | // Run. |
| | 3 | 294 | | await app.RunAsync(); |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// The main entry point for the application made public for end to end testing. |
| | | 298 | | /// </summary> |
| | | 299 | | [UsedImplicitly] |
| | | 300 | | public partial class Program |
| | | 301 | | { |
| | | 302 | | /// <summary> |
| | | 303 | | /// Set by the test runner to configure the module for testing. |
| | | 304 | | /// </summary> |
| | 7 | 305 | | public static Action<IModule>? ConfigureForTest { get; set; } |
| | | 306 | | } |