-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
581 lines (517 loc) · 24.3 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
581 lines (517 loc) · 24.3 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
using System.Diagnostics;
using Altinn.App.Api.Controllers;
using Altinn.App.Api.Controllers.Attributes;
using Altinn.App.Api.Controllers.Conventions;
using Altinn.App.Api.Helpers;
using Altinn.App.Api.Helpers.Patch;
using Altinn.App.Api.Infrastructure.Filters;
using Altinn.App.Api.Infrastructure.Health;
using Altinn.App.Api.Infrastructure.Lifetime;
using Altinn.App.Api.Infrastructure.Middleware;
using Altinn.App.Api.Infrastructure.Telemetry;
using Altinn.App.Core.Constants;
using Altinn.App.Core.Extensions;
using Altinn.App.Core.Features;
using Altinn.App.Core.Features.Cache;
using Altinn.App.Core.Features.Correspondence.Extensions;
using Altinn.App.Core.Features.Maskinporten;
using Altinn.App.Core.Features.Maskinporten.Extensions;
using Altinn.App.Core.Features.Maskinporten.Models;
using Altinn.Common.PEP.Authorization;
using Altinn.Common.PEP.Clients;
using AltinnCore.Authentication.JwtCookie;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.FeatureManagement;
using Microsoft.IdentityModel.Tokens;
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Swashbuckle.AspNetCore.SwaggerUI;
namespace Altinn.App.Api.Extensions;
/// <summary>
/// Class for registering required services to run an Altinn application.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Add the controllers and views used by an Altinn application
/// </summary>
public static void AddAltinnAppControllersWithViews(this IServiceCollection services)
{
// We add this here because it uses a hosted service and we want it to run as early as possible
// so that consumers of the cache can rely on it being available.
services.AddAppConfigurationCache();
// Add API controllers from Altinn.App.Api
IMvcBuilder mvcBuilder = services.AddControllersWithViews(options =>
{
options.Filters.Add<TelemetryEnrichingResultFilter>();
options.Conventions.Add(new AltinnControllerConventions());
});
services.AddScopeAuthorization();
mvcBuilder
.AddApplicationPart(typeof(InstancesController).Assembly)
.AddXmlSerializerFormatters()
.AddJsonOptions(
JsonSettingNames.AltinnApi,
options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
}
)
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
});
}
/// <summary>
/// Adds all services to run an Altinn application.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> being built.</param>
/// <param name="config">A reference to the current <see cref="IConfiguration"/> object.</param>
/// <param name="env">A reference to the current <see cref="IWebHostEnvironment"/> object.</param>
public static void AddAltinnAppServices(
this IServiceCollection services,
IConfiguration config,
IWebHostEnvironment env
)
{
services.AddMemoryCache();
services.AddHealthChecks().AddCheck<HealthCheck>("default_health_check");
services.AddFeatureManagement();
services.AddPlatformServices(config, env);
services.AddAppServices(config, env);
services.ConfigureDataProtection();
var useOpenTelemetrySetting = config.GetValue<bool?>("AppSettings:UseOpenTelemetry");
// Use Application Insights as default, opt in to use Open Telemetry
if (useOpenTelemetrySetting is true)
{
AddOpenTelemetry(services, config, env);
}
else
{
AddApplicationInsights(services, config, env);
}
// AddMaskinportenClient adds a keyed service. This needs to happen after AddApplicationInsights,
// due to a bug in app insights: https://github.com/microsoft/ApplicationInsights-dotnet/issues/2828
services.AddMaskinportenClient();
services.AddCorrespondenceClient();
AddAuthenticationScheme(services, config, env);
AddAuthorizationPolicies(services);
AddAntiforgery(services);
services.AddSingleton<IAuthorizationHandler, AppAccessHandler>();
services.AddTransient<InternalPatchService>();
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
ConfigureGracefulShutdown(services, env);
// HttpClients for platform functionality. Registered as HttpClient so default HttpClientFactory is used
services.AddHttpClient<AuthorizationApiClient>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSwaggerFilter();
// Add swagger endpoint for end user system api documentation
var appId = StartupHelper.GetApplicationId();
services.Configure<SwaggerUIOptions>(c =>
{
c.SwaggerEndpoint($"/{appId}/v1/customOpenapi.json", $"End user app API for {appId}");
});
}
/// <summary>
/// <p>Configures the <see cref="MaskinportenClient"/> service with a configuration object which will be static for the lifetime of the service.</p>
/// <p>If you have already provided a <see cref="MaskinportenSettings"/> configuration, either manually or
/// implicitly via <see cref="WebHostBuilderExtensions.ConfigureAppWebHost"/>, this will be overridden.</p>
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configureOptions">
/// Action delegate that provides <see cref="MaskinportenSettings"/> configuration for the <see cref="MaskinportenClient"/> service
/// </param>
public static IServiceCollection ConfigureMaskinportenClient(
this IServiceCollection services,
Action<MaskinportenSettings> configureOptions
) =>
Altinn.App.Core.Features.Maskinporten.Extensions.ServiceCollectionExtensions.ConfigureMaskinportenClient(
services,
configureOptions
);
/// <summary>
/// <p>Binds a <see cref="MaskinportenClient"/> configuration to the supplied config section path.</p>
/// <p>If you have already provided a <see cref="MaskinportenSettings"/> configuration, either manually or
/// implicitly via <see cref="WebHostBuilderExtensions.ConfigureAppWebHost"/>, this will be overridden.</p>
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configSectionPath">The configuration section path (Eg. "MaskinportenSettings")</param>
public static IServiceCollection ConfigureMaskinportenClient(
this IServiceCollection services,
string configSectionPath
) =>
Altinn.App.Core.Features.Maskinporten.Extensions.ServiceCollectionExtensions.ConfigureMaskinportenClient(
services,
configSectionPath
);
/// <summary>
/// Adds Application Insights to the service collection.
/// </summary>
/// <param name="services">Services</param>
/// <param name="config">Config</param>
/// <param name="env">Environment</param>
internal static void AddApplicationInsights(
IServiceCollection services,
IConfiguration config,
IWebHostEnvironment env
)
{
var (applicationInsightsKey, applicationInsightsConnectionString) = GetAppInsightsConfig(config, env);
if (!string.IsNullOrEmpty(applicationInsightsKey) || !string.IsNullOrEmpty(applicationInsightsConnectionString))
{
services.AddApplicationInsightsTelemetry(
(options) =>
{
if (string.IsNullOrEmpty(applicationInsightsConnectionString))
{
#pragma warning disable CS0618 // Type or member is obsolete
// Set instrumentationKey for compatibility if connectionString does not exist.
options.InstrumentationKey = applicationInsightsKey;
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
options.ConnectionString = applicationInsightsConnectionString;
}
}
);
services.AddApplicationInsightsTelemetryProcessor<IdentityTelemetryFilter>();
services.AddApplicationInsightsTelemetryProcessor<HealthTelemetryFilter>();
services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
}
}
private static void AddOpenTelemetry(IServiceCollection services, IConfiguration config, IWebHostEnvironment env)
{
var appId = StartupHelper.GetApplicationId().Split("/")[1];
var appVersion = config.GetSection("AppSettings").GetValue<string>("AppVersion");
var isTest = config.GetSection("GeneralSettings").GetValue<bool>("IsTest");
if (string.IsNullOrWhiteSpace(appVersion))
{
appVersion = "Local";
}
services.AddHostedService<TelemetryInitialization>();
services.AddSingleton<Telemetry>();
// This bit of code makes ASP.NET Core spans always root.
// Depending on infrastructure used and how the application is exposed/called,
// it might be a good idea to be in control of the root span (and therefore the size, baggage etch)
// Taken from: https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1773
_ = Sdk.SuppressInstrumentation; // Just to trigger static constructor. The static constructor in Sdk initializes Propagators.DefaultTextMapPropagator which we depend on below
Sdk.SetDefaultTextMapPropagator(new OtelPropagator(Propagators.DefaultTextMapPropagator));
DistributedContextPropagator.Current = new AspNetCorePropagator();
var appInsightsConnectionString = GetAppInsightsConnectionStringForOtel(config, env);
var useOpenTelemetryCollector = config.GetValue<bool?>("AppSettings:UseOpenTelemetryCollector");
services
.AddOpenTelemetry()
.ConfigureResource(r =>
r.AddService(serviceName: appId, serviceVersion: appVersion, serviceInstanceId: Environment.MachineName)
)
.WithTracing(builder =>
{
builder = builder
.AddSource(appId)
.AddHttpClientInstrumentation(opts =>
{
opts.RecordException = true;
})
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
});
if (isTest)
return;
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
builder = builder.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
}
else
{
builder = builder.AddOtlpExporter();
}
})
.WithMetrics(builder =>
{
builder = builder
.AddMeter(appId)
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation();
if (isTest)
return;
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
builder = builder.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
}
else
{
builder = builder.AddOtlpExporter();
}
});
services.AddLogging(logging =>
{
logging.AddOpenTelemetry(options =>
{
options.IncludeFormattedMessage = true;
if (isTest)
return;
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
options.AddAzureMonitorLogExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
}
else
{
options.AddOtlpExporter();
}
});
});
}
private sealed class TelemetryInitialization(
ILogger<TelemetryInitialization> logger,
Telemetry telemetry,
MeterProvider meterProvider
) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
// This codepath for initialization is here only because it makes it a lot easier to
// query the metrics from Prometheus using 'increase' without the appearance of a "missed" sample.
// 'increase' in Prometheus will not interpret 'none' -> 1 as a delta/increase,
// so when querying the increase within a range, there may be 1 less sample than expected.
// So here we let the metrics be initialized to 0,
// and then run collection/flush on the OTel MeterProvider to make sure they are exported.
// The first time we then increment the metric, it will count as a change from 0 -> 1
telemetry.Init();
try
{
var task = Task.Factory.StartNew(
() =>
{
if (!meterProvider.ForceFlush(10_000))
{
logger.LogInformation("Failed to flush metrics after 10 seconds");
}
},
cancellationToken,
// Long running to avoid doing this blocking on a "normal" thread pool thread
TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default
);
if (await Task.WhenAny(task, Task.Delay(500, cancellationToken)) != task)
{
logger.LogInformation(
"Tried to flush metrics within 0.5 seconds but it was taking too long, proceeding with startup"
);
}
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
return;
logger.LogWarning(ex, "Failed to flush metrics");
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
/// <summary>
/// PDF generation works by using a headless browser to render the frontend of an app instance.
/// To make debugging PDF generation failures easier, we want requests originating from the PDF generator to be
/// contained in the root trace (process/next) as children. The frontend will set this header when making requests to the app backend in PDF mode.
/// </summary>
/// <param name="headers">Request headers attached to the span</param>
/// <returns></returns>
private static bool IsPdfGeneratorRequest(IHeaderDictionary headers) => headers.ContainsKey("X-Altinn-IsPdf");
internal sealed class OtelPropagator : TextMapPropagator
{
private readonly TextMapPropagator _inner;
public OtelPropagator(TextMapPropagator inner) => _inner = inner;
public override ISet<string>? Fields => _inner.Fields;
public override PropagationContext Extract<T>(
PropagationContext context,
T carrier,
Func<T, string, IEnumerable<string>?> getter
)
{
if (carrier is HttpRequest req && !IsPdfGeneratorRequest(req.Headers))
return default;
return _inner.Extract(context, carrier, getter);
}
public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter) =>
_inner.Inject(context, carrier, setter);
}
internal sealed class AspNetCorePropagator : DistributedContextPropagator
{
private readonly DistributedContextPropagator _inner;
public AspNetCorePropagator() => _inner = CreateDefaultPropagator();
public override IReadOnlyCollection<string> Fields => _inner.Fields;
public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(
object? carrier,
PropagatorGetterCallback? getter
)
{
if (carrier is IHeaderDictionary headers && !IsPdfGeneratorRequest(headers))
return null;
return _inner.ExtractBaggage(carrier, getter);
}
public override void ExtractTraceIdAndState(
object? carrier,
PropagatorGetterCallback? getter,
out string? traceId,
out string? traceState
)
{
if (carrier is IHeaderDictionary headers && !IsPdfGeneratorRequest(headers))
{
traceId = null;
traceState = null;
return;
}
_inner.ExtractTraceIdAndState(carrier, getter, out traceId, out traceState);
}
public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) =>
_inner.Inject(activity, carrier, setter);
}
private static void AddAuthorizationPolicies(IServiceCollection services)
{
services
.AddAuthorizationBuilder()
.AddPolicy(
AuthzConstants.POLICY_INSTANCE_READ,
policy => policy.Requirements.Add(new AppAccessRequirement("read"))
)
.AddPolicy(
AuthzConstants.POLICY_INSTANCE_WRITE,
policy => policy.Requirements.Add(new AppAccessRequirement("write"))
)
.AddPolicy(
AuthzConstants.POLICY_INSTANCE_DELETE,
policy => policy.Requirements.Add(new AppAccessRequirement("delete"))
)
.AddPolicy(
AuthzConstants.POLICY_INSTANCE_INSTANTIATE,
policy => policy.Requirements.Add(new AppAccessRequirement("instantiate"))
)
.AddPolicy(
AuthzConstants.POLICY_INSTANCE_COMPLETE,
policy => policy.Requirements.Add(new AppAccessRequirement("complete"))
);
}
private static void AddAuthenticationScheme(
IServiceCollection services,
IConfiguration config,
IWebHostEnvironment env
)
{
services
.AddAuthentication(JwtCookieDefaults.AuthenticationScheme)
.AddJwtCookie(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateIssuer = false,
ValidateAudience = false,
RequireExpirationTime = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
};
options.JwtCookieName = Altinn.App.Core.Constants.General.RuntimeCookieName;
options.MetadataAddress = config["AppSettings:OpenIdWellKnownEndpoint"];
if (env.IsDevelopment())
{
options.RequireHttpsMetadata = false;
}
});
}
private static void AddAntiforgery(IServiceCollection services)
{
services.AddAntiforgery(options =>
{
// asp .net core expects two types of tokens: One that is attached to the request as header, and the other one as cookie.
// The values of the tokens are not the same and both need to be present and valid in a "unsafe" request.
// Axios which we are using for client-side automatically extracts the value from the cookie named XSRF-TOKEN. We are setting this cookie in the UserController.
// We will therefore have two token cookies. One that contains the .net core cookie token; And one that is the request token and is added as a header in requests.
// The tokens are based on the logged-in user and must be updated if the user changes.
// https://docs.microsoft.com/en-us/aspnet/core/security/anti-request-forgery?view=aspnetcore-3.1
// https://github.com/axios/axios/blob/master/lib/defaults.js
options.Cookie.Name = "AS-XSRF-TOKEN";
options.HeaderName = "X-XSRF-TOKEN";
});
services.TryAddSingleton<ValidateAntiforgeryTokenIfAuthCookieAuthorizationFilter>();
}
private static (string? Key, string? ConnectionString) GetAppInsightsConfig(
IConfiguration config,
IHostEnvironment env
)
{
var isDevelopment = env.IsDevelopment();
string? key = isDevelopment
? config["ApplicationInsights:InstrumentationKey"]
: Environment.GetEnvironmentVariable("ApplicationInsights__InstrumentationKey");
string? connectionString = isDevelopment
? config["ApplicationInsights:ConnectionString"]
: Environment.GetEnvironmentVariable("ApplicationInsights__ConnectionString");
return (key, connectionString);
}
private static string? GetAppInsightsConnectionStringForOtel(IConfiguration config, IHostEnvironment env)
{
var (key, connString) = GetAppInsightsConfig(config, env);
if (string.IsNullOrWhiteSpace(connString))
{
connString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
}
if (!string.IsNullOrWhiteSpace(connString))
{
return connString;
}
if (!Guid.TryParse(key, out _))
{
return null;
}
return $"InstrumentationKey={key}";
}
private static void ConfigureGracefulShutdown(IServiceCollection services, IHostEnvironment env)
{
if (env.IsDevelopment())
return;
// Need to coordinate graceful shutdown (let's assume k8s as the scheduler/runtime):
// - deployment is configured with a terminationGracePeriod of 30s (default timeout before SIGKILL)
// - k8s flow of information is eventually consistent.
// it takes time for knowledge of SIGTERM on the worker node to propagate to e.g. networking layers
// (k8s Service -> Endspoints rotation. It takes time to be taken out of Endpoint rotation)
// - we want to gracefully drain ASP.NET core for requests, leaving some time for active requests to complete
// This leaves us with the following sequence of events
// - container receives SIGTERM
// - `AppHostLifetime` intercepts SIGTERM and delays for `shutdownDelay`
// - `AppHostLifetime` calls `IHostApplicationLifetime.StopApplication`, to start ASP.NET Core shutdown process
// - ASP.NET Core will spend a maximum of `shutdownTimeout` trying to drain active requests
// (cancelable requests can combine cancellation tokens with `IHostApplicationLifetime.ApplicationStopping`)
// - If ASP.NET Core completes shutdown within `shutdownTimeout`, everything is fine
// - If ASP.NET Core is stuck or in some way can't terminate, kubelet will eventually SIGKILL
var shutdownDelay = TimeSpan.FromSeconds(5);
var shutdownTimeout = TimeSpan.FromSeconds(20);
services.AddSingleton<IHostLifetime>(sp =>
ActivatorUtilities.CreateInstance<AppHostLifetime>(sp, shutdownDelay)
);
services.Configure<HostOptions>(options => options.ShutdownTimeout = shutdownTimeout);
}
}