-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathStartup.cs
More file actions
1367 lines (1206 loc) · 70.7 KB
/
Startup.cs
File metadata and controls
1367 lines (1206 loc) · 70.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO.Abstractions;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Auth;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.Converters;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Config.Utilities;
using Azure.DataApiBuilder.Core.AuthenticationHelpers;
using Azure.DataApiBuilder.Core.AuthenticationHelpers.AuthenticationSimulator;
using Azure.DataApiBuilder.Core.AuthenticationHelpers.UnauthenticatedAuthentication;
using Azure.DataApiBuilder.Core.Authorization;
using Azure.DataApiBuilder.Core.Configurations;
using Azure.DataApiBuilder.Core.Models;
using Azure.DataApiBuilder.Core.Parsers;
using Azure.DataApiBuilder.Core.Resolvers;
using Azure.DataApiBuilder.Core.Resolvers.Factories;
using Azure.DataApiBuilder.Core.Services;
using Azure.DataApiBuilder.Core.Services.Cache;
using Azure.DataApiBuilder.Core.Services.MetadataProviders;
using Azure.DataApiBuilder.Core.Services.OpenAPI;
using Azure.DataApiBuilder.Core.Telemetry;
using Azure.DataApiBuilder.Mcp.Core;
using Azure.DataApiBuilder.Service.Controllers;
using Azure.DataApiBuilder.Service.Exceptions;
using Azure.DataApiBuilder.Service.HealthCheck;
using Azure.DataApiBuilder.Service.Telemetry;
using Azure.DataApiBuilder.Service.Utilities;
using Azure.Identity;
using Azure.Monitor.Ingestion;
using HotChocolate;
using HotChocolate.AspNetCore;
using HotChocolate.Execution;
using HotChocolate.Execution.Configuration;
using HotChocolate.Types;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;
using NodaTime;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
using Serilog.Core;
using StackExchange.Redis;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.Backplane.StackExchangeRedis;
using ZiggyCreatures.Caching.Fusion.Serialization.SystemTextJson;
using CorsOptions = Azure.DataApiBuilder.Config.ObjectModel.CorsOptions;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace Azure.DataApiBuilder.Service
{
public class Startup(IConfiguration configuration, ILogger<Startup> logger)
{
public static LogLevel MinimumLogLevel = LogLevel.Error;
public static bool IsLogLevelOverriddenByCli;
public static AzureLogAnalyticsCustomLogCollector CustomLogCollector = new();
public static ApplicationInsightsOptions AppInsightsOptions = new();
public static OpenTelemetryOptions OpenTelemetryOptions = new();
public static AzureLogAnalyticsOptions AzureLogAnalyticsOptions = new();
public static FileSinkOptions FileSinkOptions = new();
public const string NO_HTTPS_REDIRECT_FLAG = "--no-https-redirect";
private StartupLogBuffer _logBuffer = new();
private readonly HotReloadEventHandler<HotReloadEventArgs> _hotReloadEventHandler = new();
private RuntimeConfigProvider? _configProvider;
private ILogger<Startup> _logger = logger;
public IConfiguration Configuration { get; } = configuration;
/// <summary>
/// Useful in cases where we need to:
/// Send telemetry data to a custom endpoint that is not supported by the default telemetry channel.
/// Modify the telemetry data before it is sent, such as adding custom properties or filtering out sensitive data.
/// Implement custom retry logic or error handling for telemetry data that fails to send.
/// For testing purposes.
/// </summary>
public static ITelemetryChannel? CustomTelemetryChannel { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Startup.AddValidFilters();
services.AddSingleton(_logBuffer);
services.AddSingleton(Program.LogLevelProvider);
services.AddSingleton(_hotReloadEventHandler);
string configFileName = Configuration.GetValue<string>("ConfigFileName") ?? FileSystemRuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
string? connectionString = Configuration.GetValue<string?>(
FileSystemRuntimeConfigLoader.RUNTIME_ENV_CONNECTION_STRING.Replace(FileSystemRuntimeConfigLoader.ENVIRONMENT_PREFIX, ""),
null);
IFileSystem fileSystem = new FileSystem();
FileSystemRuntimeConfigLoader configLoader = new(fileSystem, _hotReloadEventHandler, configFileName, connectionString, logBuffer: _logBuffer);
RuntimeConfigProvider configProvider = new(configLoader);
_configProvider = configProvider;
services.AddSingleton(fileSystem);
services.AddSingleton<FileSystemRuntimeConfigLoader>(sp => configLoader);
services.AddSingleton<RuntimeConfigProvider>(sp => configProvider);
bool runtimeConfigAvailable = configProvider.TryGetConfig(out RuntimeConfig? runtimeConfig);
if (runtimeConfigAvailable
&& runtimeConfig?.Runtime?.Telemetry?.ApplicationInsights is not null
&& runtimeConfig.Runtime.Telemetry.ApplicationInsights.Enabled)
{
// Add ApplicationTelemetry service and register
// custom ITelemetryInitializer implementation with the dependency injection
services.AddApplicationInsightsTelemetry();
services.AddSingleton<ITelemetryInitializer, AppInsightsTelemetryInitializer>();
}
if (runtimeConfigAvailable
&& runtimeConfig?.Runtime?.Telemetry?.OpenTelemetry is not null
&& runtimeConfig.Runtime.Telemetry.OpenTelemetry.Enabled
&& Uri.TryCreate(runtimeConfig.Runtime.Telemetry.OpenTelemetry.Endpoint, UriKind.Absolute, out Uri? otlpEndpoint))
{
services.Configure<OpenTelemetryLoggerOptions>(options =>
{
options.IncludeScopes = true;
options.ParseStateValues = true;
options.IncludeFormattedMessage = true;
});
services.AddOpenTelemetry()
.WithLogging(logging =>
{
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(runtimeConfig.Runtime.Telemetry.OpenTelemetry.ServiceName!))
.AddOtlpExporter(configure =>
{
configure.Endpoint = otlpEndpoint;
configure.Headers = runtimeConfig.Runtime.Telemetry.OpenTelemetry.Headers;
configure.Protocol = OtlpExportProtocol.Grpc;
});
})
.WithMetrics(metrics =>
{
metrics.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(runtimeConfig.Runtime.Telemetry.OpenTelemetry.ServiceName!))
// TODO: should we also add FusionCache metrics?
// To do so we just need to add the package ZiggyCreatures.FusionCache.OpenTelemetry and call
// .AddFusionCacheInstrumentation()
.AddOtlpExporter(configure =>
{
configure.Endpoint = otlpEndpoint;
configure.Headers = runtimeConfig.Runtime.Telemetry.OpenTelemetry.Headers;
configure.Protocol = OtlpExportProtocol.Grpc;
})
.AddMeter(TelemetryMetricsHelper.MeterName);
})
.WithTracing(tracing =>
{
tracing.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(runtimeConfig.Runtime.Telemetry.OpenTelemetry.ServiceName!))
.AddHttpClientInstrumentation()
// TODO: should we also add FusionCache traces?
// To do so we just need to add the package ZiggyCreatures.FusionCache.OpenTelemetry and call
// .AddFusionCacheInstrumentation()
.AddHotChocolateInstrumentation()
.AddOtlpExporter(configure =>
{
configure.Endpoint = otlpEndpoint;
configure.Headers = runtimeConfig.Runtime.Telemetry.OpenTelemetry.Headers;
configure.Protocol = OtlpExportProtocol.Grpc;
})
.AddSource(TelemetryTracesHelper.DABActivitySource.Name);
});
}
if (runtimeConfigAvailable
&& runtimeConfig?.Runtime?.Telemetry?.AzureLogAnalytics is not null
&& IsAzureLogAnalyticsAvailable(runtimeConfig.Runtime.Telemetry.AzureLogAnalytics))
{
services.AddSingleton<ICustomLogCollector, AzureLogAnalyticsCustomLogCollector>();
services.AddSingleton<ILoggerProvider, AzureLogAnalyticsLoggerProvider>();
services.AddSingleton(sp =>
{
AzureLogAnalyticsOptions options = runtimeConfig.Runtime.Telemetry.AzureLogAnalytics;
ManagedIdentityCredential credential = new();
LogsIngestionClient logsIngestionClient = new(new Uri(options.Auth!.DceEndpoint!), credential);
return new AzureLogAnalyticsFlusherService(options, CustomLogCollector, logsIngestionClient, _logger);
});
services.AddHostedService(sp => sp.GetRequiredService<AzureLogAnalyticsFlusherService>());
}
if (runtimeConfigAvailable
&& runtimeConfig?.Runtime?.Telemetry?.File is not null
&& runtimeConfig.Runtime.Telemetry.File.Enabled)
{
services.AddSingleton(sp =>
{
FileSinkOptions options = runtimeConfig.Runtime.Telemetry.File;
return new LoggerConfiguration().WriteTo.File(
path: options.Path,
rollingInterval: (RollingInterval)Enum.Parse(typeof(RollingInterval), options.RollingInterval),
retainedFileCountLimit: options.RetainedFileCountLimit,
fileSizeLimitBytes: options.FileSizeLimitBytes,
rollOnFileSizeLimit: true);
});
services.AddSingleton(sp => sp.GetRequiredService<LoggerConfiguration>().MinimumLevel.Verbose().CreateLogger());
}
services.AddSingleton(implementationFactory: serviceProvider =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(RuntimeConfigValidator).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<RuntimeConfigValidator>();
});
services.AddSingleton<RuntimeConfigValidator>();
services.AddSingleton<CosmosClientProvider>();
services.AddHealthChecks()
.AddCheck<BasicHealthCheck>(nameof(BasicHealthCheck));
services.AddSingleton<ILogger<FileSystemRuntimeConfigLoader>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(FileSystemRuntimeConfigLoader).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<FileSystemRuntimeConfigLoader>();
});
services.AddSingleton<ILogger<SqlQueryEngine>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(SqlQueryEngine).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<SqlQueryEngine>();
});
services.AddSingleton<ILogger<IQueryExecutor>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(IQueryExecutor).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<IQueryExecutor>();
});
services.AddSingleton<ILogger<ISqlMetadataProvider>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(ISqlMetadataProvider).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<ISqlMetadataProvider>();
});
// Below are the factory registrations that will enable multiple databases scenario.
// within these factories the various instances will be created based on the database type and datasourceName.
services.AddSingleton<IAbstractQueryManagerFactory, QueryManagerFactory>();
// Register IOboTokenProvider only when user-delegated auth is configured.
// This avoids registering a null singleton and supports hot-reload scenarios.
// Requires environment variables: DAB_OBO_CLIENT_ID, DAB_OBO_TENANT_ID, DAB_OBO_CLIENT_SECRET
//
// Design note: A single IOboTokenProvider is registered using one Azure AD app registration.
// Multiple databases with different database-audience values ARE supported - the audience
// is passed to GetAccessTokenOnBehalfOfAsync() at query execution time, allowing the same
// MSAL client to acquire tokens for different resource servers.
if (IsOboConfigured())
{
// Register IMsalClientWrapper for dependency injection
services.AddSingleton<IMsalClientWrapper>(serviceProvider =>
{
string? clientId = Environment.GetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR);
string? tenantId = Environment.GetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_TENANT_ID_ENV_VAR);
string? clientSecret = Environment.GetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_SECRET_ENV_VAR);
string authority = $"https://login.microsoftonline.com/{tenantId}";
IConfidentialClientApplication msalClient = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithAuthority(authority)
.WithClientSecret(clientSecret)
.Build();
return new MsalClientWrapper(msalClient);
});
// Register OboSqlTokenProvider with dependencies from DI
services.AddSingleton<IOboTokenProvider, OboSqlTokenProvider>();
}
services.AddSingleton<IQueryEngineFactory, QueryEngineFactory>();
services.AddSingleton<IMutationEngineFactory, MutationEngineFactory>();
services.AddSingleton<IMetadataProviderFactory, MetadataProviderFactory>();
services.AddSingleton<GraphQLSchemaCreator>();
services.AddSingleton<GQLFilterParser>();
services.AddSingleton<RequestValidator>();
services.AddSingleton<RestService>();
services.AddSingleton<HealthCheckHelper>();
services.AddSingleton<HttpUtilities>();
services.AddSingleton<BasicHealthReportResponseWriter>();
services.AddSingleton<ComprehensiveHealthReportResponseWriter>();
// ILogger explicit creation required for logger to use --LogLevel startup argument specified.
services.AddSingleton<ILogger<BasicHealthReportResponseWriter>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(BasicHealthReportResponseWriter).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<BasicHealthReportResponseWriter>();
});
// ILogger explicit creation required for logger to use --LogLevel startup argument specified.
services.AddSingleton<ILogger<ComprehensiveHealthReportResponseWriter>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(ComprehensiveHealthReportResponseWriter).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<ComprehensiveHealthReportResponseWriter>();
});
// ILogger explicit creation required for logger to use --LogLevel startup argument specified.
services.AddSingleton<ILogger<HealthCheckHelper>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(HealthCheckHelper).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<HealthCheckHelper>();
});
// ILogger explicit creation required for logger to use --LogLevel startup argument specified.
services.AddSingleton<ILogger<HttpUtilities>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(HttpUtilities).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<HttpUtilities>();
});
services.AddSingleton<ILogger<RestController>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(RestController).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<RestController>();
});
services.AddSingleton<ILogger<ClientRoleHeaderAuthenticationMiddleware>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelRuntime = new(MinimumLogLevel, typeof(ClientRoleHeaderAuthenticationMiddleware).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelRuntime);
return loggerFactory.CreateLogger<ClientRoleHeaderAuthenticationMiddleware>();
});
services.AddSingleton<ILogger<ConfigurationController>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(ConfigurationController).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<ConfigurationController>();
});
//Enable accessing HttpContext in RestService to get ClaimsPrincipal.
services.AddHttpContextAccessor();
services.AddHttpClient("ContextConfiguredHealthCheckClient")
.ConfigureHttpClient((serviceProvider, client) =>
{
int port = PortResolutionHelper.ResolveInternalPort();
string baseUri = $"http://localhost:{port}";
client.BaseAddress = new Uri(baseUri);
_logger.LogInformation($"Configured HealthCheck HttpClient BaseAddress as: {baseUri}");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = TimeSpan.FromSeconds(200);
})
.ConfigurePrimaryHttpMessageHandler(serviceProvider =>
{
// For debug purpose, USE_SELF_SIGNED_CERT can be set to true in Environment variables
bool allowSelfSigned = Environment.GetEnvironmentVariable("USE_SELF_SIGNED_CERT")?.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
HttpClientHandler handler = new();
if (allowSelfSigned)
{
handler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
}
return handler;
});
bool isMcpStdio = Configuration.GetValue<bool>("MCP:StdioMode");
if (isMcpStdio)
{
// Explicitly force Simulator when running in MCP stdio mode.
services.AddAuthentication(
defaultScheme: SimulatorAuthenticationDefaults.AUTHENTICATIONSCHEME)
.AddSimulatorAuthentication();
}
else if (runtimeConfig is not null && runtimeConfig.Runtime?.Host?.Mode is HostMode.Development)
{
// Development mode implies support for "Hot Reload". The V2 authentication function
// wires up all DAB supported authentication providers (schemes) so that at request time,
// the runtime config defined authentication provider is used to authenticate requests.
ConfigureAuthenticationV2(services, configProvider);
}
else
{
ConfigureAuthentication(services, configProvider);
}
services.AddAuthorization();
services.AddSingleton<ILogger<IAuthorizationHandler>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(IAuthorizationHandler).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<IAuthorizationHandler>();
});
services.AddSingleton<ILogger<IAuthorizationResolver>>(implementationFactory: (serviceProvider) =>
{
LogLevelInitializer logLevelInit = new(MinimumLogLevel, typeof(IAuthorizationResolver).FullName, _configProvider, _hotReloadEventHandler);
ILoggerFactory? loggerFactory = CreateLoggerFactoryForHostedAndNonHostedScenario(serviceProvider, logLevelInit);
return loggerFactory.CreateLogger<IAuthorizationResolver>();
});
services.AddSingleton<IAuthorizationHandler, RestAuthorizationHandler>();
services.AddSingleton<IAuthorizationResolver, AuthorizationResolver>();
services.AddSingleton<IOpenApiDocumentor, OpenApiDocumentor>();
AddGraphQLService(services, runtimeConfig?.Runtime?.GraphQL);
// Subscribe the GraphQL schema refresh method to the specific hot-reload event
_hotReloadEventHandler.Subscribe(
DabConfigEvents.GRAPHQL_SCHEMA_REFRESH_ON_CONFIG_CHANGED,
(_, _) => RefreshGraphQLSchema(services));
// Cache config
IFusionCacheBuilder fusionCacheBuilder = services.AddFusionCache()
.WithOptions(options =>
{
options.FactoryErrorsLogLevel = LogLevel.Debug;
options.EventHandlingErrorsLogLevel = LogLevel.Debug;
string? cachePartition = runtimeConfig?.Runtime?.Cache?.Level2?.Partition;
if (string.IsNullOrWhiteSpace(cachePartition) == false)
{
options.CacheKeyPrefix = cachePartition + "_";
options.BackplaneChannelPrefix = cachePartition + "_";
}
})
.WithDefaultEntryOptions(new FusionCacheEntryOptions
{
Duration = TimeSpan.FromSeconds(RuntimeCacheOptions.DEFAULT_TTL_SECONDS),
ReThrowBackplaneExceptions = false,
ReThrowDistributedCacheExceptions = false,
ReThrowSerializationExceptions = false,
});
// Level2 cache config
bool isLevel2Enabled = runtimeConfigAvailable
&& (runtimeConfig?.Runtime?.IsCachingEnabled ?? false)
&& (runtimeConfig?.Runtime?.Cache?.Level2?.Enabled ?? false);
if (isLevel2Enabled)
{
RuntimeCacheLevel2Options level2CacheOptions = runtimeConfig!.Runtime!.Cache!.Level2!;
string level2CacheProvider = level2CacheOptions.Provider ?? EntityCacheOptions.L2_CACHE_PROVIDER;
switch (level2CacheProvider.ToLowerInvariant())
{
case EntityCacheOptions.L2_CACHE_PROVIDER:
if (string.IsNullOrWhiteSpace(level2CacheOptions.ConnectionString))
{
throw new Exception($"Cache Provider: the \"{EntityCacheOptions.L2_CACHE_PROVIDER}\" level2 cache provider requires a valid connection-string. Please provide one.");
}
else
{
// NOTE: this is done to reuse the same connection multiplexer for both the cache and backplane
Task<IConnectionMultiplexer> connectionMultiplexerTask = CreateConnectionMultiplexerAsync(level2CacheOptions.ConnectionString);
fusionCacheBuilder
.WithSerializer(new FusionCacheSystemTextJsonSerializer())
.WithDistributedCache(new RedisCache(new RedisCacheOptions
{
ConnectionMultiplexerFactory = async () =>
{
return await connectionMultiplexerTask;
}
}))
.WithBackplane(new RedisBackplane(new RedisBackplaneOptions
{
ConnectionMultiplexerFactory = async () =>
{
return await connectionMultiplexerTask;
}
}));
}
break;
default:
throw new Exception($"Cache Provider: ${level2CacheOptions.Provider} not supported. Please provide a valid cache provider.");
}
}
services.AddSingleton<DabCacheService>();
services.AddDabMcpServer(configProvider);
services.AddSingleton<IMcpStdioServer, McpStdioServer>();
// Add Response Compression services based on config
ConfigureResponseCompression(services, runtimeConfig);
services.AddControllers();
}
/// <summary>
/// Creates a ConnectionMultiplexer for Redis with support for Azure Entra authentication.
/// </summary>
/// <param name="connectionString">The Redis connection string.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the connected IConnectionMultiplexer.</returns>
private static async Task<IConnectionMultiplexer> CreateConnectionMultiplexerAsync(string connectionString)
{
ConfigurationOptions options = ConfigurationOptions.Parse(connectionString);
if (ShouldUseEntraAuthForRedis(options))
{
options = await options.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); // CodeQL [SM05137] DefaultAzureCredential will use Managed Identity if available or fallback to default.
}
return await ConnectionMultiplexer.ConnectAsync(options);
}
/// <summary>
/// Determines whether Azure Entra authentication should be used.
/// Conditions:
/// - No password provided
/// - At least one endpoint is NOT localhost/loopback
/// </summary>
/// <param name="options">The Redis configuration options.</param>
/// <returns>True if Azure Entra authentication should be used; otherwise, false.</returns>
/// <remarks>Internal for testing.</remarks>
internal static bool ShouldUseEntraAuthForRedis(ConfigurationOptions options)
{
// Determine if an endpoint is localhost/loopback
static bool IsLocalhostEndpoint(EndPoint ep) => ep switch
{
DnsEndPoint dns => string.Equals(dns.Host, "localhost", StringComparison.OrdinalIgnoreCase),
IPEndPoint ip => IPAddress.IsLoopback(ip.Address),
_ => false,
};
return string.IsNullOrEmpty(options.Password)
&& options.EndPoints.Any(ep => !IsLocalhostEndpoint(ep));
}
/// <summary>
/// Configures HTTP response compression based on the runtime configuration.
/// Compression is applied at the middleware level and supports Gzip and Brotli.
/// Applies to REST, GraphQL, and MCP endpoints.
/// </summary>
private void ConfigureResponseCompression(IServiceCollection services, RuntimeConfig? runtimeConfig)
{
CompressionLevel compressionLevel = runtimeConfig?.Runtime?.Compression?.Level ?? CompressionOptions.DEFAULT_LEVEL;
// Only configure compression if level is not None
if (compressionLevel == CompressionLevel.None)
{
return;
}
System.IO.Compression.CompressionLevel systemCompressionLevel = compressionLevel switch
{
CompressionLevel.Fastest => System.IO.Compression.CompressionLevel.Fastest,
CompressionLevel.Optimal => System.IO.Compression.CompressionLevel.Optimal,
_ => System.IO.Compression.CompressionLevel.Optimal
};
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<Microsoft.AspNetCore.ResponseCompression.GzipCompressionProvider>();
options.Providers.Add<Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvider>();
});
services.Configure<Microsoft.AspNetCore.ResponseCompression.GzipCompressionProviderOptions>(options =>
{
options.Level = systemCompressionLevel;
});
services.Configure<Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProviderOptions>(options =>
{
options.Level = systemCompressionLevel;
});
_logger.LogDebug("Response compression enabled with level '{compressionLevel}' for REST, GraphQL, and MCP endpoints.", compressionLevel);
}
/// <summary>
/// Configure GraphQL services within the service collection of the
/// request pipeline.
/// - AllowIntrospection defaulted to false so HotChocolate configures a request validation rule
/// that checks for the presence of the GraphQL context key WellKnownContextData.IntrospectionAllowed
/// when determining whether to allow introspection requests to proceed.
/// </summary>
/// <param name="services">Service Collection</param>
/// <param name="graphQLRuntimeOptions">The GraphQL runtime options.</param>
private void AddGraphQLService(IServiceCollection services, GraphQLRuntimeOptions? graphQLRuntimeOptions)
{
IRequestExecutorBuilder server = services.AddGraphQLServer()
.AddInstrumentation()
.AddType(new DateTimeType(disableFormatCheck: graphQLRuntimeOptions?.EnableLegacyDateTimeScalar ?? true))
.AddHttpRequestInterceptor<DefaultHttpRequestInterceptor>()
.ConfigureSchema((serviceProvider, schemaBuilder) =>
{
// The GraphQLSchemaCreator is an application service that is not available on
// the schema specific service provider, this means we have to get it with
// the GetRootServiceProvider helper.
GraphQLSchemaCreator graphQLService = serviceProvider.GetRootServiceProvider().GetRequiredService<GraphQLSchemaCreator>();
graphQLService.InitializeSchemaAndResolvers(schemaBuilder);
})
.AddHttpRequestInterceptor<IntrospectionInterceptor>()
.AddAuthorizationHandler<GraphQLAuthorizationHandler>()
.BindRuntimeType<TimeOnly, HotChocolate.Types.NodaTime.LocalTimeType>()
.BindScalarType<HotChocolate.Types.NodaTime.LocalTimeType>("LocalTime")
.AddTypeConverter<LocalTime, TimeOnly>(
from => new TimeOnly(from.Hour, from.Minute, from.Second, from.Millisecond))
.AddTypeConverter<TimeOnly, LocalTime>(
from => new LocalTime(from.Hour, from.Minute, from.Second, from.Millisecond));
// Conditionally adds a maximum depth rule to the GraphQL queries/mutation selection set.
// This rule is only added if a positive depth limit is specified, ensuring that the server
// enforces a limit on the depth of incoming GraphQL queries/mutation to prevent extremely deep queries
// that could potentially lead to performance issues.
// Additionally, the skipIntrospectionFields parameter is set to true to skip depth limit enforcement on introspection queries.
if (graphQLRuntimeOptions is not null && graphQLRuntimeOptions.DepthLimit is > 0)
{
server = server.AddMaxExecutionDepthRule(maxAllowedExecutionDepth: graphQLRuntimeOptions.DepthLimit.Value, skipIntrospectionFields: true);
}
server.AddErrorFilter(error =>
{
if (error.Exception is not null)
{
_logger.LogError(exception: error.Exception, message: "A GraphQL request execution error occurred.");
return error.WithMessage(error.Exception.Message);
}
if (error.Code is not null)
{
_logger.LogError(message: "Error code: {errorCode}\nError message: {errorMessage}", error.Code, error.Message);
return error.WithMessage(error.Message);
}
return error;
})
.AddErrorFilter(error =>
{
if (error.Exception is DataApiBuilderException thrownException)
{
error = error
.WithException(null)
.WithMessage(thrownException.Message)
.WithCode($"{thrownException.SubStatusCode}");
// If user error i.e. validation error or conflict error with datasource, then retain location/path
if (!thrownException.StatusCode.IsClientError())
{
error = error.WithLocations(Array.Empty<Location>());
}
}
return error;
})
// Allows DAB to override the HTTP error code set by HotChocolate.
// This is used to ensure HTTP code 4XX is set when the datatbase
// returns a "bad request" error such as stored procedure params missing.
.UseRequest<DetermineStatusCodeMiddleware>()
.UseRequest<BuildRequestStateMiddleware>()
.UseDefaultPipeline();
}
/// <summary>
/// Refreshes the GraphQL schema when the runtime config updates during hot-reload scenario.
/// </summary>
private void RefreshGraphQLSchema(IServiceCollection services)
{
// Re-add GraphQL services with updated config.
RuntimeConfig runtimeConfig = _configProvider!.GetConfig();
Console.WriteLine("Updating GraphQL service.");
AddGraphQLService(services, runtimeConfig.Runtime?.GraphQL);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RuntimeConfigProvider runtimeConfigProvider, IHostApplicationLifetime hostLifetime)
{
bool isRuntimeReady = false;
if (runtimeConfigProvider.TryGetConfig(out RuntimeConfig? runtimeConfig))
{
// Set LogLevel based on RuntimeConfig
DynamicLogLevelProvider logLevelProvider = app.ApplicationServices.GetRequiredService<DynamicLogLevelProvider>();
logLevelProvider.UpdateFromRuntimeConfig(runtimeConfig);
FileSystemRuntimeConfigLoader configLoader = app.ApplicationServices.GetRequiredService<FileSystemRuntimeConfigLoader>();
//Flush all logs that were buffered before setting the LogLevel
configLoader.SetLogger(app.ApplicationServices.GetRequiredService<ILogger<FileSystemRuntimeConfigLoader>>());
configLoader.FlushLogBuffer();
// Configure Telemetry
ConfigureApplicationInsightsTelemetry(app, runtimeConfig);
ConfigureOpenTelemetry(runtimeConfig);
ConfigureAzureLogAnalytics(runtimeConfig);
ConfigureFileSink(app, runtimeConfig);
// Config provided before starting the engine.
isRuntimeReady = PerformOnConfigChangeAsync(app).Result;
if (!isRuntimeReady)
{
_logger.LogError(
message: "Could not initialize the engine with the runtime config file: {configFilePath}",
runtimeConfigProvider.ConfigFilePath);
hostLifetime.StopApplication();
}
}
else
{
// Config provided during runtime.
runtimeConfigProvider.IsLateConfigured = true;
runtimeConfigProvider.RuntimeConfigLoadedHandlers.Add(async (_, _) =>
{
isRuntimeReady = await PerformOnConfigChangeAsync(app);
return isRuntimeReady;
});
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (!Program.IsHttpsRedirectionDisabled)
{
// Use HTTPS redirection for all endpoints except /health and /graphql.
// This is necessary because ContextConfiguredHealthCheckClient base URI is http://localhost:{port} for internal API calls
app.UseWhen(
context => !(context.Request.Path.StartsWithSegments("/health") || context.Request.Path.StartsWithSegments("/graphql")),
appBuilder => appBuilder.UseHttpsRedirection()
);
}
// Response compression middleware should be placed early in the pipeline.
// Only use if compression is not set to None.
if (runtimeConfig?.Runtime?.Compression?.Level is not CompressionLevel.None)
{
app.UseResponseCompression();
}
// URL Rewrite middleware MUST be called prior to UseRouting().
// https://andrewlock.net/understanding-pathbase-in-aspnetcore/#placing-usepathbase-in-the-correct-location
app.UseCorrelationIdMiddleware();
app.UsePathRewriteMiddleware();
// SwaggerUI visualization of the OpenAPI description document is only available
// in developer mode in alignment with the restriction placed on ChilliCream's BananaCakePop IDE.
// Consequently, SwaggerUI is not presented in a StaticWebApps (late-bound config) environment.
if (IsUIEnabled(runtimeConfig, env))
{
app.UseSwaggerUI(c => // CodeQL [SM04686] SwaggerUI is only enabled for Development environment.
{
c.ConfigObject.Urls = new SwaggerEndpointMapper(app.ApplicationServices.GetService<RuntimeConfigProvider?>());
});
}
app.UseRouting();
// Adding CORS Middleware
if (runtimeConfig is not null && runtimeConfig.Runtime?.Host?.Cors is not null)
{
app.UseCors(corsPolicyBuilder =>
{
CorsOptions corsConfig = runtimeConfig.Runtime.Host.Cors;
ConfigureCors(corsPolicyBuilder, corsConfig);
});
}
app.Use(async (context, next) =>
{
bool isHealthCheckRequest = context.Request.Path == "/" && context.Request.Method == HttpMethod.Get.Method;
bool isSettingConfig = context.Request.Path.StartsWithSegments("/configuration")
&& context.Request.Method == HttpMethod.Post.Method;
if (isRuntimeReady || isHealthCheckRequest)
{
await next.Invoke();
}
else if (isSettingConfig)
{
if (isRuntimeReady)
{
context.Response.StatusCode = StatusCodes.Status409Conflict;
}
else
{
await next.Invoke();
}
}
else
{
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
}
});
app.UseAuthentication();
app.UseClientRoleHeaderAuthenticationMiddleware();
app.UseAuthorization();
// Authorization Engine middleware enforces that all requests (including introspection)
// include proper auth headers.
// - {Authorization header + Client role header for JWT}
// - {X-MS-CLIENT-PRINCIPAL + Client role header for EasyAuth}
// When enabled, the middleware will prevent Banana Cake Pop(GraphQL client) from loading
// without proper authorization headers.
app.UseClientRoleHeaderAuthorizationMiddleware();
IRequestExecutorManager requestExecutorManager = app.ApplicationServices.GetRequiredService<IRequestExecutorManager>();
_hotReloadEventHandler.Subscribe(
"GRAPHQL_SCHEMA_EVICTION_ON_CONFIG_CHANGED",
(_, _) => EvictGraphQLSchema(requestExecutorManager));
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// Special for MCP
endpoints.MapDabMcp(runtimeConfigProvider);
endpoints
.MapGraphQL()
.WithOptions(new GraphQLServerOptions
{
Tool = {
// Determines if accessing the endpoint from a browser
// will load the GraphQL Banana Cake Pop IDE.
Enable = IsUIEnabled(runtimeConfig, env)
}
});
// In development mode, Nitro is enabled at /graphql endpoint by default.
// Need to disable mapping Nitro explicitly as well to avoid ability to query
// at an additional endpoint: /graphql/ui.
endpoints
.MapNitroApp()
.WithOptions(new GraphQLToolOptions
{
Enable = false
});
endpoints.MapHealthChecks("/", new HealthCheckOptions
{
ResponseWriter = app.ApplicationServices.GetRequiredService<BasicHealthReportResponseWriter>().WriteResponse
});
});
}
/// <summary>
/// Evicts the GraphQL schema from the request executor resolver.
/// </summary>
private static void EvictGraphQLSchema(IRequestExecutorManager requestExecutorResolver)
{
Console.WriteLine("Evicting old GraphQL schema.");
requestExecutorResolver.EvictExecutor();
}
/// <summary>
/// If LogLevel is NOT overridden by CLI, attempts to find the
/// minimum log level based on host.mode in the runtime config if available.
/// Creates a logger factory with the minimum log level.
/// </summary>
public static ILoggerFactory CreateLoggerFactoryForHostedAndNonHostedScenario(IServiceProvider serviceProvider, LogLevelInitializer logLevelInitializer)
{
if (!IsLogLevelOverriddenByCli)
{
// If the log level is not overridden by command line arguments specified through CLI,
// attempt to get the runtime config to determine the loglevel based on host.mode.
// If runtime config is available, set the loglevel to Error if host.mode is Production,
// Debug if it is Development.
logLevelInitializer.SetLogLevel();
}
TelemetryClient? appTelemetryClient = serviceProvider.GetService<TelemetryClient>();
Logger? serilogLogger = serviceProvider.GetService<Logger>();
return Program.GetLoggerFactoryForLogLevel(logLevelInitializer.MinLogLevel, appTelemetryClient, logLevelInitializer, serilogLogger);
}
/// <summary>
/// Add services necessary for Authentication Middleware and based on the loaded
/// runtime configuration set the AuthenticationOptions to be either
/// EasyAuth based (by default) or JwtBearerOptions.
/// When no runtime configuration is set on engine startup, set the
/// default authentication scheme to EasyAuth.
/// </summary>
/// <param name="services">The service collection where authentication services are added.</param>
/// <param name="runtimeConfigurationProvider">The provider used to load runtime configuration.</param>
private void ConfigureAuthentication(IServiceCollection services, RuntimeConfigProvider runtimeConfigurationProvider)
{
if (runtimeConfigurationProvider.TryGetConfig(out RuntimeConfig? runtimeConfig) &&
runtimeConfig.Runtime?.Host?.Authentication is not null)
{
AuthenticationOptions authOptions = runtimeConfig.Runtime.Host.Authentication;
HostMode mode = runtimeConfig.Runtime.Host.Mode;
if (authOptions.IsJwtConfiguredIdentityProvider())
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.MapInboundClaims = false;
options.Audience = authOptions.Jwt!.Audience;
options.Authority = authOptions.Jwt!.Issuer;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
// Instructs the asp.net core middleware to use the data in the "roles" claim for User.IsInRole()
// See https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsprincipal.isinrole#remarks
RoleClaimType = AuthenticationOptions.ROLE_CLAIM_TYPE
};
});
}
else if (authOptions.IsEasyAuthAuthenticationProvider())
{
EasyAuthType easyAuthType = EnumExtensions.Deserialize<EasyAuthType>(runtimeConfig.Runtime.Host.Authentication.Provider);
bool isProductionMode = mode != HostMode.Development;
bool appServiceEnvironmentDetected = AppServiceAuthenticationInfo.AreExpectedAppServiceEnvVarsPresent();
if (easyAuthType == EasyAuthType.AppService && !appServiceEnvironmentDetected)
{
_logger.LogWarning(AppServiceAuthenticationInfo.APPSERVICE_DEV_MISSING_ENV_CONFIG);
}
string defaultScheme = easyAuthType == EasyAuthType.AppService
? EasyAuthAuthenticationDefaults.APPSERVICEAUTHSCHEME
: EasyAuthAuthenticationDefaults.SWAAUTHSCHEME;
services.AddAuthentication(defaultScheme)
.AddEnvDetectedEasyAuth();
_logger.LogInformation("Registered EasyAuth scheme: {Scheme}", defaultScheme);
}
else if (authOptions.IsUnauthenticatedAuthenticationProvider())
{
services.AddAuthentication(UnauthenticatedAuthenticationDefaults.AUTHENTICATIONSCHEME)
.AddUnauthenticatedAuthentication();
}
else if (mode == HostMode.Development && authOptions.IsAuthenticationSimulatorEnabled())
{
services.AddAuthentication(SimulatorAuthenticationDefaults.AUTHENTICATIONSCHEME)
.AddSimulatorAuthentication();
}
else
{
// Condition met when Jwt section (audience/authority), EasyAuth types, or Simulator (in development mode)
// values are not used in the authentication section.
throw new DataApiBuilderException(
message: "Authentication configuration not supported.",
statusCode: System.Net.HttpStatusCode.ServiceUnavailable,
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError);
}
}
else
{
// Sets EasyAuth as the default authentication scheme when runtime configuration
// is not present.
SetAppServiceAuthentication(services);
}
}
/// <summary>
/// Registers all DAB supported authentication providers (schemes) so that at request time,
/// DAB can use the runtime config's defined provider to authenticate requests.
/// The function includes JWT specific configuration handling:
/// - IOptionsChangeTokenSource{JwtBearerOptions} : Registers a change token source for dynamic config updates which
/// is used internally by JwtBearerHandler's OptionsMonitor to listen for changes in JwtBearerOptions.
/// - IConfigureOptions{JwtBearerOptions} : Registers named JwtBearerOptions whose "Configure(...)" function is
/// called by OptionsFactory internally by .NET to fetch the latest configuration from the RuntimeConfigProvider.
/// </summary>
/// <seealso cref="https://github.com/dotnet/aspnetcore/issues/49586#issuecomment-1671838595">Guidance for registering IOptionsChangeTokenSource</seealso>
/// <seealso cref="https://github.com/dotnet/aspnetcore/issues/21491#issuecomment-624240160">Guidance for registering named options.</seealso>
private static void ConfigureAuthenticationV2(IServiceCollection services, RuntimeConfigProvider runtimeConfigProvider)