-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added TelemetryInitializer and Startup where its configured
- Loading branch information
1 parent
1f310fe
commit bdbd70e
Showing
4 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
11 changes: 4 additions & 7 deletions
11
Equinor.SubSurfAppManagementMonitoringNuGet/Authentication/AccessTokenService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
using Equinor.SubSurfAppManagementMonitoringNuGet.HealthServices.smda; | ||
|
||
namespace Equinor.SubSurfAppManagementMonitoringNuGet.Authentication; | ||
|
||
/// <summary> | ||
/// Defines methods for achieving accesstokens | ||
/// Defines the interface which must be implemented for achieving accesstokens for default HealthCheckServices such as smda: <see cref="SmdaHealthCheck"/> | ||
/// </summary> | ||
public interface IAccessTokenService | ||
{ | ||
/// <summary> | ||
/// Get access token on behalf of the app | ||
/// </summary> | ||
Task<string> GetAccessTokenAsync(string resourceId); | ||
|
||
/// <summary> | ||
/// Get access token on behalf of the user | ||
/// Get access token on behalf of the caller | ||
/// </summary> | ||
Task<string> GetAccessTokenOnBehalfOfAsync(string resourceId); | ||
} |
48 changes: 48 additions & 0 deletions
48
Equinor.SubSurfAppManagementMonitoringNuGet/Config/AuditTelemetryInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using System.Security.Claims; | ||
|
||
namespace Equinor.SubSurfAppManagementMonitoringNuGet.Config; | ||
|
||
public class AuditTelemetryInitializer : ITelemetryInitializer | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
private readonly IWebHostEnvironment _env; | ||
|
||
public AuditTelemetryInitializer(IHttpContextAccessor contextAccessor, IWebHostEnvironment env) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
_env = env; | ||
} | ||
|
||
public void Initialize(ITelemetry telemetry) | ||
{ | ||
var httpContext = _contextAccessor.HttpContext; | ||
try | ||
{ | ||
if (httpContext?.Connection.RemoteIpAddress != null) | ||
{ | ||
telemetry.Context.Location.Ip = httpContext.Connection.RemoteIpAddress.ToString(); | ||
} | ||
|
||
if (httpContext?.Request?.Headers["User-Agent"] is not null) | ||
{ | ||
telemetry.Context.GlobalProperties["user-agent"] = httpContext.Request.Headers["User-Agent"].ToString(); | ||
} | ||
|
||
if (httpContext?.User?.Identity?.IsAuthenticated == true && | ||
httpContext.User.HasClaim(c => c.Type == ClaimTypes.Name)) | ||
{ | ||
telemetry.Context.User.AuthenticatedUserId = | ||
httpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; | ||
; | ||
} | ||
} | ||
catch | ||
{ | ||
telemetry.Context.User.AuthenticatedUserId = "<unknown>"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Equinor.SubSurfAppManagementMonitoringNuGet.Config; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Equinor.SubSurfAppManagementMonitoringNuGet; | ||
|
||
public static class Startup | ||
{ | ||
/// <summary> | ||
/// Configures the default services and dependencies for the nuget package | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="Configuration"></param> | ||
public static void Configure(IServiceCollection services, IConfiguration Configuration) | ||
{ | ||
ConfigureDependencyInjection(services); | ||
} | ||
|
||
private static void ConfigureDependencyInjection(IServiceCollection services) | ||
{ | ||
services.AddSingleton<ITelemetryInitializer, AuditTelemetryInitializer>(); | ||
} | ||
|
||
} |