-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStartup.cs
819 lines (736 loc) · 43.4 KB
/
Startup.cs
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
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GovUk.Education.ExploreEducationStatistics.Admin.Controllers.Api;
using GovUk.Education.ExploreEducationStatistics.Admin.Database;
using GovUk.Education.ExploreEducationStatistics.Admin.Hubs;
using GovUk.Education.ExploreEducationStatistics.Admin.Hubs.Filters;
using GovUk.Education.ExploreEducationStatistics.Admin.Migrations.Custom;
using GovUk.Education.ExploreEducationStatistics.Admin.Models;
using GovUk.Education.ExploreEducationStatistics.Admin.Requests.Public.Data;
using GovUk.Education.ExploreEducationStatistics.Admin.Security;
using GovUk.Education.ExploreEducationStatistics.Admin.Security.AuthorizationHandlers;
using GovUk.Education.ExploreEducationStatistics.Admin.Services;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Cache;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Cache;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.ManageContent;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Methodologies;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Public.Data;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.ManageContent;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Methodologies;
using GovUk.Education.ExploreEducationStatistics.Admin.Services.Public.Data;
using GovUk.Education.ExploreEducationStatistics.Admin.Settings;
using GovUk.Education.ExploreEducationStatistics.Admin.ViewModels.Public.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Cache;
using GovUk.Education.ExploreEducationStatistics.Common.Cancellation;
using GovUk.Education.ExploreEducationStatistics.Common.Config;
using GovUk.Education.ExploreEducationStatistics.Common.Database;
using GovUk.Education.ExploreEducationStatistics.Common.Extensions;
using GovUk.Education.ExploreEducationStatistics.Common.Model;
using GovUk.Education.ExploreEducationStatistics.Common.Model.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Services;
using GovUk.Education.ExploreEducationStatistics.Common.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Common.Services.Interfaces.Security;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Database;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Repository;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Services;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Content.Services;
using GovUk.Education.ExploreEducationStatistics.Content.Services.Cache;
using GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.Cache;
using GovUk.Education.ExploreEducationStatistics.Data.Model;
using GovUk.Education.ExploreEducationStatistics.Data.Model.Database;
using GovUk.Education.ExploreEducationStatistics.Data.Model.Repository;
using GovUk.Education.ExploreEducationStatistics.Data.Model.Repository.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Data.Processor.Model;
using GovUk.Education.ExploreEducationStatistics.Data.Services;
using GovUk.Education.ExploreEducationStatistics.Data.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Public.Data.Model;
using GovUk.Education.ExploreEducationStatistics.Public.Data.Model.Database;
using GovUk.Education.ExploreEducationStatistics.Publisher.Model;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Notify.Client;
using Notify.Interfaces;
using Semver;
using Thinktecture;
using static GovUk.Education.ExploreEducationStatistics.Common.Utils.StartupUtils;
using ContentGlossaryService = GovUk.Education.ExploreEducationStatistics.Content.Services.GlossaryService;
using ContentMethodologyService = GovUk.Education.ExploreEducationStatistics.Content.Services.MethodologyService;
using ContentPublicationService = GovUk.Education.ExploreEducationStatistics.Content.Services.PublicationService;
using ContentReleaseService = GovUk.Education.ExploreEducationStatistics.Content.Services.ReleaseService;
using DataGuidanceService = GovUk.Education.ExploreEducationStatistics.Admin.Services.DataGuidanceService;
using DataSetService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Public.Data.DataSetService;
using GlossaryService = GovUk.Education.ExploreEducationStatistics.Admin.Services.GlossaryService;
using IContentGlossaryService = GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.IGlossaryService;
using IContentMethodologyService =
GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.IMethodologyService;
using IContentPublicationService =
GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.IPublicationService;
using IContentReleaseService = GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.IReleaseService;
using IDataGuidanceService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IDataGuidanceService;
using IDataSetService =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Public.Data.IDataSetService;
using IGlossaryService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IGlossaryService;
using IMethodologyImageService =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Methodologies.IMethodologyImageService;
using IMethodologyService =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.Methodologies.IMethodologyService;
using IPublicationRepository =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IPublicationRepository;
using IPublicationService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IPublicationService;
using IReleaseFileService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IReleaseFileService;
using IReleaseService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IReleaseService;
using IReleaseVersionRepository =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IReleaseVersionRepository;
using IThemeService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IThemeService;
using MethodologyImageService =
GovUk.Education.ExploreEducationStatistics.Admin.Services.Methodologies.MethodologyImageService;
using MethodologyService = GovUk.Education.ExploreEducationStatistics.Admin.Services.Methodologies.MethodologyService;
using PublicationRepository = GovUk.Education.ExploreEducationStatistics.Admin.Services.PublicationRepository;
using PublicationService = GovUk.Education.ExploreEducationStatistics.Admin.Services.PublicationService;
using ReleaseFileService = GovUk.Education.ExploreEducationStatistics.Admin.Services.ReleaseFileService;
using ReleaseService = GovUk.Education.ExploreEducationStatistics.Admin.Services.ReleaseService;
using ReleaseVersionRepository = GovUk.Education.ExploreEducationStatistics.Admin.Services.ReleaseVersionRepository;
using SameSiteMode = Microsoft.AspNetCore.Http.SameSiteMode;
using ThemeService = GovUk.Education.ExploreEducationStatistics.Admin.Services.ThemeService;
using HeaderNames = Microsoft.Net.Http.Headers.HeaderNames;
namespace GovUk.Education.ExploreEducationStatistics.Admin
{
public class Startup(
IConfiguration configuration,
IHostEnvironment hostEnvironment)
{
// This method gets called by the runtime. Use this method to add services to the container.
public virtual void ConfigureServices(IServiceCollection services)
{
// TODO EES-5073 Remove this when the Public Data db exists in ALL Azure environments.
var publicDataDbExists = configuration.GetValue<bool>("PublicDataDbExists");
services.AddHealthChecks();
/*
* Logging
*/
services.AddApplicationInsightsTelemetry()
.AddApplicationInsightsTelemetryProcessor<SensitiveDataTelemetryProcessor>();
/*
* Web configuration
*/
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = _ => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
services.AddControllers(options =>
{
options.AddCommaSeparatedQueryModelBinderProvider();
options.AddTrimStringBinderProvider();
})
.AddControllersAsServices();
services.AddHttpContextAccessor();
services.AddFluentValidation();
services.AddMvc(options =>
{
options.Filters.Add(new AuthorizeFilter(SecurityPolicies.RegisteredUser.ToString()));
options.Filters.Add(new OperationCancelledExceptionFilter());
options.Filters.Add(new ProblemDetailsResultFilter());
options.EnableEndpointRouting = false;
options.AllowEmptyInputInBodyModelBinding = true;
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
// Adds Brotli and Gzip compressing
services.AddResponseCompression(options => { options.EnableForHttps = true; });
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration => { configuration.RootPath = "wwwroot"; });
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
/*
* Database contexts
*/
// TODO EES-4869 - review if we need to retain these tables.
services.AddDbContext<UsersAndRolesDbContext>(options =>
options
.UseSqlServer(configuration.GetConnectionString("ContentDb"),
providerOptions =>
providerOptions
.MigrationsAssembly(typeof(Startup).Assembly.FullName)
.EnableCustomRetryOnFailure()
)
.EnableSensitiveDataLogging(hostEnvironment.IsDevelopment())
);
services.AddDbContext<ContentDbContext>(options =>
options
.UseSqlServer(configuration.GetConnectionString("ContentDb"),
providerOptions =>
providerOptions
.MigrationsAssembly(typeof(Startup).Assembly.FullName)
.EnableCustomRetryOnFailure()
)
.EnableSensitiveDataLogging(hostEnvironment.IsDevelopment())
);
services.AddDbContext<StatisticsDbContext>(options =>
options
.UseSqlServer(configuration.GetConnectionString("StatisticsDb"),
providerOptions =>
providerOptions
.MigrationsAssembly("GovUk.Education.ExploreEducationStatistics.Data.Model")
.AddBulkOperationSupport()
.EnableCustomRetryOnFailure()
)
.EnableSensitiveDataLogging(hostEnvironment.IsDevelopment())
);
// Only set up the `PublicDataDbContext` in non-integration test
// environments. Otherwise, the connection string will be null and
// cause the data source builder to throw a host exception.
if (!hostEnvironment.IsIntegrationTest())
{
var publicDataDbConnectionString = configuration.GetConnectionString("PublicDataDb")!;
// TODO EES-5073 Remove this check when the Public Data db is available in all Azure environments.
if (publicDataDbExists)
{
services.AddPsqlDbContext<PublicDataDbContext>(publicDataDbConnectionString, hostEnvironment);
}
}
/*
* Authentication and Authorization
*/
//
// This configuration sets up out-of-the-box Services to support the Identity Framework's Users, Roles and
// Claims. It sets up the UserManager and RoleManager in DI, and links them to the database via the
// UsersAndRolesDbContext.
//
// AddIdentityCore() sets up the bare minimum configuration to provide JWT validation and setting of the
// ClaimsPrincipal on the HttpContext based upon the content of incoming JWTs, as opposed to AddIdentity()
// which sets up additional configuration such as Forbidden and Login routes, which we don't need.
//
// In order to provide it with strategies for how to deal with invalid JWTs, unauthenticated users etc
// though, we need to register a handler for these.
//
// TODO EES-4869 - review if we want to keep these features of Identity Framework or not.
services
.AddIdentityCore<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddUserManager<UserManager<ApplicationUser>>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddUserStore<UserStore<ApplicationUser, IdentityRole, UsersAndRolesDbContext>>()
.AddRoleStore<RoleStore<IdentityRole, UsersAndRolesDbContext>>()
.AddEntityFrameworkStores<UsersAndRolesDbContext>();
services.Configure<IdentityOptions>(options =>
{
// Allow special characters such as apostrophes and @ symbols to be permitted in AspNetUsers'
// "Username" column. This allows us to store email addresses as Usernames when newly invited users
// sign in.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+'";
});
// This service helps to add additional information to the ClaimsPrincipal on the HttpContext after
// Identity Framework has verified that the incoming JWTs are valid (and has created the basic
// ClaimsPrincipal already from information in the JWT).
services.AddTransient<IClaimsTransformation, ClaimsPrincipalTransformationService>();
if (!hostEnvironment.IsIntegrationTest())
{
services
// This tells Identity Framework to look for Bearer tokens in incoming requests' Authorization
// headers as a way of identifying users.
.AddAuthentication(options =>
{
// This line tells Identity Framework to use the JWT mechanism for verifying users based upon
// JWTs found in Bearer tokens carried in the Authorization headers of requests made to the
// Admin API. It also tells Identity Framework to use its default AuthenticationHandler
// implementation for handling challenges, forbid errors etc with appropriate status code
// responses rather than redirect responses.
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// This adds verification of the incoming JWTs after they have been located in the
// Authorization headers above.
.AddMicrosoftIdentityWebApi(configuration.GetRequiredSection("OpenIdConnectIdentityFramework"));
// This helps Identity Framework with incoming websocket requests from SignalR, or any request where
// adding the Bearer token in the Authorization HTTP header is not possible, and is instead added as an
// "access_token" query parameter. This code grabs the token from the query parameter and makes it
// available for Identity Framework to find (in order for it to validate it and build its
// ClaimsPrincipal).
services.Configure<JwtBearerOptions>(
JwtBearerDefaults.AuthenticationScheme,
options =>
{
var originalOnMessageReceived = options.Events.OnMessageReceived;
options.Events.OnMessageReceived = async context =>
{
await originalOnMessageReceived(context);
if (!context.Token.IsNullOrEmpty())
{
return;
}
if (context.Request.Query.ContainsKey("access_token"))
{
context.Token = context.Request.Query["access_token"];
}
};
});
}
/*
* SignalR
*/
var signalRBuilder = services
.AddSignalR(
options =>
{
options.AddFilter<HttpContextHubFilter>();
}
)
.AddNewtonsoftJsonProtocol();
var azureSignalRConnectionString = configuration.GetValue<string>("Azure:SignalR:ConnectionString");
if (!azureSignalRConnectionString.IsNullOrEmpty())
{
signalRBuilder.AddAzureSignalR(azureSignalRConnectionString);
}
/*
* Configuration options
*/
services.Configure<PublicDataProcessorOptions>(
configuration.GetRequiredSection(PublicDataProcessorOptions.Section));
services.Configure<PreReleaseOptions>(configuration);
services.Configure<LocationsOptions>(configuration.GetRequiredSection(LocationsOptions.Locations));
services.Configure<ReleaseApprovalOptions>(
configuration.GetRequiredSection(ReleaseApprovalOptions.ReleaseApproval));
services.Configure<TableBuilderOptions>(configuration.GetRequiredSection(TableBuilderOptions.TableBuilder));
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
services.Configure<OpenIdConnectSpaClientOptions>(configuration.GetSection(
OpenIdConnectSpaClientOptions.OpenIdConnectSpaClient));
StartupSecurityConfiguration.ConfigureAuthorizationPolicies(services, configuration);
/*
* Services
*/
var coreStorageConnectionString = configuration.GetValue<string>("CoreStorage");
var publisherStorageConnectionString = configuration.GetValue<string>("PublisherStorage");
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// This service is responsible for handling calls immediately following successful login into the Admin SPA.
// It will determine if the user is an existing or a new user, and will register them locally if a new user.
services.AddTransient<ISignInService, SignInService>();
// TODO EES-3510 These services from the Content.Services namespace are used to update cached resources.
// EES-3528 plans to send a request to the Content API to update its cached resources instead of this
// being done from Admin directly, and so these DI dependencies should eventually be removed.
services.AddTransient<IContentGlossaryService, ContentGlossaryService>();
services.AddTransient<IContentMethodologyService, ContentMethodologyService>();
services.AddTransient<IContentPublicationService, ContentPublicationService>();
services.AddTransient<IContentReleaseService, ContentReleaseService>();
services.AddTransient<IGlossaryCacheService, GlossaryCacheService>();
services.AddTransient<IMethodologyCacheService, MethodologyCacheService>();
services.AddTransient<IPublicationCacheService, PublicationCacheService>();
services.AddTransient<IPublicationCacheService, PublicationCacheService>();
services.AddTransient<IReleaseCacheService, ReleaseCacheService>();
services.AddTransient<IFileRepository, FileRepository>();
services.AddTransient<IDataImportRepository, DataImportRepository>();
services.AddTransient<IReleaseFileRepository, ReleaseFileRepository>();
services.AddTransient<IReleaseDataFileRepository, ReleaseDataFileRepository>();
services.AddTransient<IReleaseDataFileService, ReleaseDataFileService>();
services.AddTransient<IDataGuidanceFileWriter, DataGuidanceFileWriter>();
services.AddTransient<IReleaseFileService, ReleaseFileService>();
services.AddTransient<IReleaseImageService, ReleaseImageService>();
services.AddTransient<IReleasePermissionService, ReleasePermissionService>();
services.AddTransient<IDataImportService, DataImportService>();
services.AddTransient<IImportStatusBauService, ImportStatusBauService>();
services.AddTransient<IPublishingService, PublishingService>();
services.AddTransient<IReleasePublishingStatusService, ReleasePublishingStatusService>();
services.AddTransient<IReleasePublishingStatusRepository, ReleasePublishingStatusRepository>();
services.AddTransient<IThemeService, ThemeService>();
services.AddTransient<ITopicService, TopicService>();
services.AddTransient<IPublicationService, PublicationService>();
services.AddTransient<IPublicationRepository, PublicationRepository>();
services.AddTransient<IMetaService, MetaService>();
services.AddTransient<IReleaseService, ReleaseService>();
services.AddTransient<IReleaseAmendmentService, ReleaseAmendmentService>();
services.AddTransient<IReleaseApprovalService, ReleaseApprovalService>();
services.AddTransient<ReleaseSubjectRepository.SubjectDeleter, ReleaseSubjectRepository.SubjectDeleter>();
services.AddTransient<IReleaseSubjectRepository, ReleaseSubjectRepository>();
services.AddTransient<IReleaseChecklistService, ReleaseChecklistService>();
services.AddTransient<IReleaseVersionRepository, ReleaseVersionRepository>();
services.AddTransient<IMethodologyService, MethodologyService>();
services.AddTransient<IMethodologyNoteService, MethodologyNoteService>();
services.AddTransient<IMethodologyNoteRepository, MethodologyNoteRepository>();
services.AddTransient<IMethodologyVersionRepository, MethodologyVersionRepository>();
services.AddTransient<IMethodologyRepository, MethodologyRepository>();
services.AddTransient<IMethodologyContentService, MethodologyContentService>();
services.AddTransient<IMethodologyFileRepository, MethodologyFileRepository>();
services.AddTransient<IMethodologyImageService, MethodologyImageService>();
services.AddTransient<IMethodologyAmendmentService, MethodologyAmendmentService>();
services.AddTransient<IMethodologyApprovalService, MethodologyApprovalService>();
services.AddTransient<IDataBlockService, DataBlockService>();
services.AddTransient<IPreReleaseUserService, PreReleaseUserService>();
services.AddTransient<IPreReleaseService, PreReleaseService>();
services.AddTransient<IPreReleaseSummaryService, PreReleaseSummaryService>();
services.AddTransient<IManageContentPageService, ManageContentPageService>();
services.AddTransient<IContentBlockService, ContentBlockService>();
services.AddTransient<IContentService, ContentService>();
services.AddTransient<IEmbedBlockService, EmbedBlockService>();
services.AddTransient<IContentBlockLockService, ContentBlockLockService>();
services.AddTransient<IKeyStatisticService, KeyStatisticService>();
services.AddTransient<IFeaturedTableService, FeaturedTableService>();
services.AddTransient<ICommentService, CommentService>();
services.AddTransient<IRelatedInformationService, RelatedInformationService>();
services.AddTransient<IReplacementService, ReplacementService>();
services.AddTransient<IUserRoleService, UserRoleService>();
services.AddTransient<IUserReleaseRoleService, UserReleaseRoleService>();
services.AddTransient<IUserPublicationRoleRepository, UserPublicationRoleRepository>();
services.AddTransient<IUserReleaseRoleRepository, UserReleaseRoleRepository>();
services.AddTransient<IUserReleaseInviteRepository, UserReleaseInviteRepository>();
services.AddTransient<IUserPublicationInviteRepository, UserPublicationInviteRepository>();
services.AddTransient<IRedirectsCacheService, RedirectsCacheService>();
services.AddTransient<IRedirectsService, RedirectsService>();
services.AddTransient<IDataSetCandidateService, DataSetCandidateService>();
services.AddTransient<IPostgreSqlRepository, PostgreSqlRepository>();
services.AddHttpClient<IProcessorClient, ProcessorClient>((provider, httpClient) =>
{
var options = provider.GetRequiredService<IOptions<PublicDataProcessorOptions>>();
httpClient.BaseAddress = new Uri(options.Value.Url);
httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, "EES Admin");
});
if (publicDataDbExists)
{
services.AddTransient<IDataSetService, DataSetService>();
services.AddTransient<IDataSetVersionService, DataSetVersionService>();
services.AddTransient<IDataSetVersionMappingService, DataSetVersionMappingService>();
}
else
{
// TODO EES-5073 Remove this once PublicDataDbContext is configured in ALL Azure environments.
// This is allowing for the PublicDataDbContext to be null.
services.AddTransient<IDataSetService, DataSetService>(provider =>
new DataSetService(provider.GetRequiredService<ContentDbContext>(),
provider.GetService<PublicDataDbContext>(),
provider.GetRequiredService<IProcessorClient>(),
provider.GetRequiredService<IUserService>()));
services.AddTransient<IDataSetVersionService, NoOpDataSetVersionService>();
services.AddTransient<IDataSetVersionMappingService, NoOpDataSetVersionMappingService>();
}
services.AddTransient<INotificationClient>(s =>
{
var notifyApiKey = configuration.GetValue<string>("NotifyApiKey");
if (!hostEnvironment.IsDevelopment() && !hostEnvironment.IsIntegrationTest())
{
return new NotificationClient(notifyApiKey);
}
if (notifyApiKey != null && notifyApiKey != "change-me")
{
return new NotificationClient(notifyApiKey);
}
var logger = s.GetRequiredService<ILogger<LoggingNotificationClient>>();
return new LoggingNotificationClient(logger);
});
services.AddTransient<IEmailService, EmailService>();
services.AddTransient<IBoundaryLevelRepository, BoundaryLevelRepository>();
services.AddTransient<IEmailTemplateService, EmailTemplateService>();
services.AddTransient<ITableBuilderService, TableBuilderService>();
services.AddTransient<IFilterRepository, FilterRepository>();
services.AddTransient<IFilterItemRepository, FilterItemRepository>();
services.AddTransient<IFootnoteService, FootnoteService>();
services.AddTransient<IFootnoteRepository, FootnoteRepository>();
services.AddTransient<IGeoJsonRepository, GeoJsonRepository>();
services.AddTransient<IGlossaryService, GlossaryService>();
services.AddTransient<IIndicatorGroupRepository, IndicatorGroupRepository>();
services.AddTransient<IIndicatorRepository, IndicatorRepository>();
services.AddTransient<ILocationRepository, LocationRepository>();
services.AddTransient<IDataGuidanceService, DataGuidanceService>();
services.AddTransient<IDataGuidanceDataSetService, DataGuidanceDataSetService>();
services.AddTransient<IObservationService, ObservationService>();
services.AddTransient<Data.Services.Interfaces.IReleaseService, Data.Services.ReleaseService>();
services.AddTransient<IContentSectionRepository, ContentSectionRepository>();
services.AddTransient<IReleaseNoteService, ReleaseNoteService>();
services.AddTransient<Content.Model.Repository.Interfaces.IReleaseVersionRepository,
Content.Model.Repository.ReleaseVersionRepository>();
services.AddTransient<Content.Model.Repository.Interfaces.IPublicationRepository,
Content.Model.Repository.PublicationRepository>();
services.AddTransient<ISubjectRepository, SubjectRepository>();
services.AddTransient<ITimePeriodService, TimePeriodService>();
services.AddTransient<IReleaseSubjectService, ReleaseSubjectService>();
services.AddTransient<ISubjectMetaService, SubjectMetaService>();
services.AddTransient<ISubjectResultMetaService, SubjectResultMetaService>();
services.AddTransient<ISubjectCsvMetaService, SubjectCsvMetaService>();
services.AddSingleton<DataServiceMemoryCache<BoundaryLevel>, DataServiceMemoryCache<BoundaryLevel>>();
services.AddSingleton<DataServiceMemoryCache<GeoJson>, DataServiceMemoryCache<GeoJson>>();
services.AddTransient<IUserManagementService, UserManagementService>();
services.AddTransient<IReleaseInviteService, ReleaseInviteService>();
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IUserInviteRepository, UserInviteRepository>();
services.AddTransient<IFileUploadsValidatorService, FileUploadsValidatorService>();
services.AddTransient<IReleaseFileBlobService, PrivateReleaseFileBlobService>();
services.AddSingleton<IPrivateBlobStorageService, PrivateBlobStorageService>();
services.AddSingleton<IPublicBlobStorageService, PublicBlobStorageService>();
services.AddTransient<ICoreTableStorageService, CoreTableStorageService>();
services.AddTransient<IPublisherTableStorageService, PublisherTableStorageService>();
services.AddSingleton<IGuidGenerator, SequentialGuidGenerator>();
AddPersistenceHelper<ContentDbContext>(services);
AddPersistenceHelper<StatisticsDbContext>(services);
AddPersistenceHelper<UsersAndRolesDbContext>(services);
services.AddTransient<AuthorizationHandlerService>();
services.AddScoped<DateTimeProvider>();
// This service allows a set of users to be pre-invited to the service on startup.
if (hostEnvironment.IsDevelopment())
{
services.AddTransient<BootstrapUsersService>();
}
// These services allow us to check our Policies within Controllers and Services
StartupSecurityConfiguration.ConfigureResourceBasedAuthorization(services);
services.AddSingleton<IFileTypeService, FileTypeService>();
services.AddTransient<IDataArchiveValidationService, DataArchiveValidationService>();
services.AddTransient<IBlobCacheService, BlobCacheService>(provider =>
new BlobCacheService(
provider.GetRequiredService<IPrivateBlobStorageService>(),
provider.GetRequiredService<ILogger<BlobCacheService>>()
));
services.AddTransient<ICacheKeyService, CacheKeyService>();
services.AddSingleton<IDataProcessorClient, DataProcessorClient>(_ =>
new DataProcessorClient(coreStorageConnectionString));
services.AddSingleton<IPublisherClient, PublisherClient>(_ =>
new PublisherClient(publisherStorageConnectionString));
/*
* Swagger
*/
if (configuration.GetValue<bool>("enableSwagger"))
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "Explore education statistics - Admin API",
Version = "v1"
});
c.CustomSchemaIds((type) => type.FullName);
c.AddSecurityDefinition("Bearer",
new OpenApiSecurityScheme
{
Description =
"Please enter into field the word 'Bearer' followed by a space and the JWT contents",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new[] { string.Empty }
}
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var provider = app.ApplicationServices;
// Enable caching and register any caching services.
CacheAspect.Enabled = true;
var privateCacheService = new BlobCacheService(
app.ApplicationServices.GetRequiredService<IPrivateBlobStorageService>(),
provider.GetRequiredService<ILogger<BlobCacheService>>()
);
var publicCacheService = new BlobCacheService(
app.ApplicationServices.GetRequiredService<IPublicBlobStorageService>(),
provider.GetRequiredService<ILogger<BlobCacheService>>()
);
BlobCacheAttribute.AddService("default", privateCacheService);
BlobCacheAttribute.AddService("public", publicCacheService);
if (!env.IsIntegrationTest())
{
UpdateDatabase(app, env);
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts(opts =>
{
opts.MaxAge(365);
opts.IncludeSubdomains();
opts.Preload();
});
}
app.UseResponseCompression();
if (configuration.GetValue<bool>("enableSwagger"))
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Admin API V1");
c.RoutePrefix = "docs";
});
}
// Security Headers
app.UseXContentTypeOptions();
app.UseXXssProtection(opts => opts.EnabledWithBlockMode());
app.UseXfo(opts => opts.SameOrigin());
app.UseReferrerPolicy(opts => opts.NoReferrerWhenDowngrade());
app.UseCsp(opts => opts
.BlockAllMixedContent()
.StyleSources(s => s.Self())
.StyleSources(s => s
.CustomSources(" https://cdnjs.cloudflare.com")
.UnsafeInline())
.FontSources(s => s.Self())
.FormActions(s =>
{
var loginAuthorityUrl = configuration.GetRequiredSection("OpenIdConnectIdentityFramework")
.GetValue<string>("Authority");
var loginAuthorityUri = new Uri(loginAuthorityUrl);
s
.CustomSources(loginAuthorityUri.GetLeftPart(UriPartial.Authority))
.Self();
})
.FrameAncestors(s => s.Self())
.ImageSources(s => s.Self())
.ImageSources(s => s.CustomSources("data:"))
.ScriptSources(s => s.Self())
.ScriptSources(s => s.UnsafeInline())
);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseHealthChecks("/api/health");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ReleaseContentHub>("/hubs/release-content");
}
);
app.UseMvc();
if (!env.IsIntegrationTest())
{
app.UseSpa(spa =>
{
if (env.IsDevelopment())
{
spa.Options.SourcePath = "../explore-education-statistics-admin";
spa.UseReactDevelopmentServer("start");
}
});
}
app.ServerFeatures.Get<IServerAddressesFeature>()
?.Addresses
.ForEach(address => Console.WriteLine($"Server listening on address: {address}"));
}
private void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env)
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetRequiredService<StatisticsDbContext>())
{
context.Database.SetCommandTimeout(int.MaxValue);
context.Database.Migrate();
}
using (var context = serviceScope.ServiceProvider.GetRequiredService<UsersAndRolesDbContext>())
{
context.Database.SetCommandTimeout(int.MaxValue);
context.Database.Migrate();
}
using (var context = serviceScope.ServiceProvider.GetRequiredService<ContentDbContext>())
{
context.Database.SetCommandTimeout(int.MaxValue);
context.Database.Migrate();
ApplyCustomMigrations();
}
}
if (env.IsDevelopment())
{
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope();
serviceScope.ServiceProvider
.GetRequiredService<BootstrapUsersService>()
.AddBootstrapUsers();
}
}
private static void ApplyCustomMigrations(params ICustomMigration[] migrations)
{
foreach (var migration in migrations)
{
migration.Apply();
}
}
}
internal class NoOpDataSetVersionService : IDataSetVersionService
{
public Task<List<DataSetVersionStatusSummary>> GetStatusesForReleaseVersion(
Guid releaseVersionId,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new List<DataSetVersionStatusSummary>());
}
public Task<Either<ActionResult, DataSetVersion>> GetDataSetVersion(
Guid dataSetId,
SemVersion version,
CancellationToken cancellationToken = default)
{
return Task.FromResult(new Either<ActionResult, DataSetVersion>(new NotFoundResult()));
}
public Task<Either<ActionResult, DataSetVersionSummaryViewModel>> CreateNextVersion(
Guid releaseFileId,
Guid dataSetId,
CancellationToken cancellationToken = default) => throw new NotImplementedException();
public Task<Either<ActionResult, Unit>> DeleteVersion(
Guid dataSetVersionId, CancellationToken cancellationToken = default)
{
return Task.FromResult(new Either<ActionResult, Unit>(Unit.Instance));
}
}
internal class NoOpDataSetVersionMappingService : IDataSetVersionMappingService
{
public Task<Either<ActionResult, LocationMappingPlan>> GetLocationMappings(
Guid nextDataSetVersionId,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();
public Task<Either<ActionResult, BatchLocationMappingUpdatesResponseViewModel>> ApplyBatchLocationMappingUpdates(
Guid nextDataSetVersionId,
BatchLocationMappingUpdatesRequest request,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();
public Task<Either<ActionResult, FilterMappingPlan>> GetFilterMappings(
Guid nextDataSetVersionId,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();
public Task<Either<ActionResult, BatchFilterOptionMappingUpdatesResponseViewModel>> ApplyBatchFilterOptionMappingUpdates(Guid nextDataSetVersionId,
BatchFilterOptionMappingUpdatesRequest request,
CancellationToken cancellationToken = default) =>
throw new NotImplementedException();
}
}