Skip to content

Commit

Permalink
Merge pull request #643 from ITfoxtec/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
Revsgaard authored Nov 3, 2023
2 parents a4d81d4 + 76775d8 commit 4faf554
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/howto-saml-2.0-context-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Context Handler documentation and configuration:

Context Handler requires the Relying Party (RP) and Identity Provider (IdP) to use different OSES certificates. Therefore, consider connecting Context Handler in separate tracks where the OCES certificates can be configured without affecting any other configurations.

Two FoxIDs tracks can be connected with OpenID Connect. Please see the [connect FoxIDs with OpenID Connect](howto-oidc-foxids.md) guide. The track with a up-party connected to Context Handler is called the parallel FoxIDs track in the guide.
You can easy connect two tracks in the same tenant with a [track link](howto-tracklink-foxids.md).

## Certificate

Expand Down
2 changes: 1 addition & 1 deletion docs/up-party-howto-saml-2.0-nemlogin.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NemLog-in documentation and configuration:

NemLog-in requires the Relying Party (RP) to use a OSES certificate and a high level of logging. Therefore, consider connecting NemLog-in in a separate track where the OCES3 certificate and log level can be configured without affecting any other configuration.

Two FoxIDs tracks can be connected with OpenID Connect. Please see the [connect FoxIDs with OpenID Connect](howto-oidc-foxids.md) guide. The track with a up-party connected to NemLog-in is called the parallel FoxIDs track in the guide.
You can easy connect two tracks in the same tenant with a [track link](howto-tracklink-foxids.md).

## Certificate

Expand Down
38 changes: 19 additions & 19 deletions src/FoxIDs.Control/Controllers/Tracks/TTrackLogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,33 +323,33 @@ private async Task<bool> LoadMetricsAsync(LogsQueryClient client, List<InternalL
}

private string GetGeneralQueryExtend() =>
@"| extend f_DownPartyId = Properties.f_DownPartyId
| extend f_UpPartyId = Properties.f_UpPartyId
| extend f_SessionId = Properties.f_SessionId
| extend f_ExternalSessionId = Properties.f_ExternalSessionId
| extend f_UserId = Properties.f_UserId
| extend f_Email = Properties.f_Email
| extend f_UserAgent = Properties.f_UserAgent";
@$"| extend {Constants.Logs.DownPartyId} = Properties.{Constants.Logs.DownPartyId}
| extend {Constants.Logs.UpPartyId} = Properties.{Constants.Logs.UpPartyId}
| extend {Constants.Logs.SessionId} = Properties.{Constants.Logs.SessionId}
| extend {Constants.Logs.ExternalSequenceId} = Properties.{Constants.Logs.ExternalSequenceId}
| extend {Constants.Logs.UserId} = Properties.{Constants.Logs.UserId}
| extend {Constants.Logs.Email} = Properties.{Constants.Logs.Email}
| extend {Constants.Logs.UserAgent} = Properties.{Constants.Logs.UserAgent}";

private string GetGeneralQueryWhere(string filter) =>
@$"ClientIP contains '{filter}' or
f_DownPartyId contains '{filter}' or
f_UpPartyId contains '{filter}' or
f_SequenceId contains '{filter}' or
f_SessionId contains '{filter}' or
f_ExternalSessionId contains '{filter}' or
f_UserId contains '{filter}' or
f_Email contains '{filter}' or
f_UserAgent contains '{filter}'";
{Constants.Logs.DownPartyId} contains '{filter}' or
{Constants.Logs.UpPartyId} contains '{filter}' or
{Constants.Logs.SequenceId} contains '{filter}' or
{Constants.Logs.SessionId} contains '{filter}' or
{Constants.Logs.ExternalSequenceId} contains '{filter}' or
{Constants.Logs.UserId} contains '{filter}' or
{Constants.Logs.Email} contains '{filter}' or
{Constants.Logs.UserAgent} contains '{filter}'";

private string GetQuery(string fromType, string extend, string where)
{
return
@$"{fromType}
| extend f_TenantName = Properties.f_TenantName
| extend f_TrackName = Properties.f_TrackName
| extend f_SequenceId = Properties.f_SequenceId {(extend.IsNullOrEmpty() ? string.Empty : extend)}
| where f_TenantName == '{RouteBinding.TenantName}' and f_TrackName == '{RouteBinding.TrackName}' {(where.IsNullOrEmpty() ? string.Empty : where)}
| extend {Constants.Logs.TenantName} = Properties.{Constants.Logs.TenantName}
| extend {Constants.Logs.TrackName} = Properties.{Constants.Logs.TrackName}
| extend {Constants.Logs.SequenceId} = Properties.{Constants.Logs.SequenceId} {(extend.IsNullOrEmpty() ? string.Empty : extend)}
| where {Constants.Logs.TenantName} == '{RouteBinding.TenantName}' and {Constants.Logs.TrackName} == '{RouteBinding.TrackName}' {(where.IsNullOrEmpty() ? string.Empty : where)}
| limit {maxQueryLogItems}
| order by TimeGenerated";
}
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.Control/FoxIDs.Control.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>1.1.1.0</Version>
<Version>1.1.2.0</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
39 changes: 25 additions & 14 deletions src/FoxIDs.Control/Logic/UsageLogLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ public UsageLogLogic(FoxIDsControlSettings settings, TelemetryScopedLogger logge

public async Task<Api.UsageLogResponse> GetTrackUsageLog(Api.UsageLogRequest logRequest, string tenantName, string trackName, bool isMasterTenant = false, bool isMasterTrack = false)
{
var client = new LogsQueryClient(tokenCredential);
var rows = await LoadUsageEventsAsync(client, tenantName, trackName, GetQueryTimeRange(logRequest.TimeScope, logRequest.TimeOffset), logRequest, isMasterTenant);

var items = new List<Api.UsageLogItem>();

#region DB
if (logRequest.TimeScope == Api.UsageLogTimeScopes.ThisMonth && logRequest.SummarizeLevel == Api.UsageLogSummarizeLevels.Month)
{
if (isMasterTenant)
Expand Down Expand Up @@ -79,10 +78,15 @@ public UsageLogLogic(FoxIDsControlSettings settings, TelemetryScopedLogger logge
}
}
}
#endregion

#region logs
var dayPointer = 0;
var hourPointer = 0;
List<Api.UsageLogItem> dayItemsPointer = items;
List<Api.UsageLogItem> itemsPointer = items;
var client = new LogsQueryClient(tokenCredential);
var rows = await LoadUsageEventsAsync(client, tenantName, trackName, GetQueryTimeRange(logRequest.TimeScope, logRequest.TimeOffset), logRequest, isMasterTenant);
foreach (var row in rows)
{
if (logRequest.SummarizeLevel != Api.UsageLogSummarizeLevels.Month)
Expand Down Expand Up @@ -118,13 +122,15 @@ public UsageLogLogic(FoxIDsControlSettings settings, TelemetryScopedLogger logge
}
}

var logType = GetLogType(row);
var item = new Api.UsageLogItem
{
Type = GetLogType(row),
Value = GetCount(row),
Type = logType,
Value = GetCount(row, logType),
};
itemsPointer.Add(item);
}
#endregion

return new Api.UsageLogResponse { SummarizeLevel = logRequest.SummarizeLevel, Items = SortUsageTypes(items) };
}
Expand Down Expand Up @@ -279,9 +285,13 @@ private Api.UsageLogTypes GetLogType(LogsTableRow row)
return logType;
}

private long GetCount(LogsTableRow row)
private double GetCount(LogsTableRow row, Api.UsageLogTypes logType)
{
var count = row.GetInt64("UsageCount");
var count = row.GetDouble("UsageCount");
if (logType == Api.UsageLogTypes.Login || logType == Api.UsageLogTypes.TokenRequest)
{
count += row.GetDouble("UsageAddRating");
}
return count.HasValue ? count.Value : 0;
}

Expand Down Expand Up @@ -312,7 +322,7 @@ private QueryTimeRange GetQueryTimeRange(Api.UsageLogTimeScopes timeScope, int t
timePointer = timePointer.AddMonths(-1);
}
var startDate = new DateTimeOffset(timePointer.Year, timePointer.Month, 1, 0, 0, 0, TimeSpan.FromHours(timeOffset));
var endDate = startDate.AddMonths(1).AddDays(-1);
var endDate = startDate.AddMonths(1);
return new QueryTimeRange(startDate, endDate);
}

Expand Down Expand Up @@ -373,11 +383,11 @@ private string GetWhereDataSlice(string tenantName, string trackName)
var whereDataSlice = new List<string>();
if (!tenantName.IsNullOrWhiteSpace())
{
whereDataSlice.Add($"f_TenantName == '{tenantName}'");
whereDataSlice.Add($"{Constants.Logs.TenantName} == '{tenantName}'");
}
if (!trackName.IsNullOrWhiteSpace())
{
whereDataSlice.Add($"f_TrackName == '{trackName}'");
whereDataSlice.Add($"{Constants.Logs.TrackName} == '{trackName}'");
}
return string.Join(" and ", whereDataSlice);
}
Expand All @@ -386,11 +396,12 @@ private string GetQuery(string fromType, string whereDataSlice, string where, st
{
return
@$"{GetFromTypeAndUnion(fromType, isMasterTenant)}
| extend f_TenantName = Properties.f_TenantName
| extend f_TrackName = Properties.f_TrackName
| extend f_UsageType = Properties.f_UsageType
| extend {Constants.Logs.TenantName} = Properties.{Constants.Logs.TenantName}
| extend {Constants.Logs.TrackName} = Properties.{Constants.Logs.TrackName}
| extend {Constants.Logs.UsageType} = Properties.{Constants.Logs.UsageType}
| extend {Constants.Logs.UsageAddRating} = Properties.{Constants.Logs.UsageAddRating}
{(whereDataSlice.IsNullOrEmpty() ? string.Empty : $"| where {whereDataSlice} ")}| where {where}
| summarize UsageCount = count() by {preOrderSummarizeBy}tostring(f_UsageType)
| summarize UsageCount = count(), UsageAddRating = sum(todouble({Constants.Logs.UsageAddRating})) by {preOrderSummarizeBy}tostring({Constants.Logs.UsageType})
{(preSortBy.IsNullOrEmpty() ? string.Empty : $"| sort by {preSortBy}")}";
}

Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.ControlClient/FoxIDs.ControlClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>1.1.1.0</Version>
<Version>1.1.2.0</Version>
<RootNamespace>FoxIDs.Client</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.ControlShared/FoxIDs.ControlShared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>1.1.1.0</Version>
<Version>1.1.2.0</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ITfoxtec.Identity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace FoxIDs.Models.Api
{
Expand Down Expand Up @@ -84,6 +85,10 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
{
results.Add(new ValidationResult($"Require '{IdentityConstants.ResponseTypes.Code}' response type with PKCE.", new[] { nameof(RequirePkce), nameof(ResponseTypes) }));
}
if (RedirectUris?.Sum(i => i.Count()) > Constants.Models.OAuthDownParty.Client.RedirectUriSumLength)
{
results.Add(new ValidationResult($"The {RedirectUris} total summarised number of characters is more then {Constants.Models.OAuthDownParty.Client.RedirectUriSumLength}.", new[] { nameof(RedirectUris) }));
}
return results;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class UsageLogItem
{
public UsageLogTypes Type { get; set; }

public long Value { get; set; }
public double Value { get; set; }

public IEnumerable<UsageLogItem> SubItems { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/FoxIDs.Shared/FoxIDs.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>1.1.1.0</Version>
<Version>1.1.2.0</Version>
<RootNamespace>FoxIDs</RootNamespace>
<Authors>Anders Revsgaard</Authors>
<Company>ITfoxtec</Company>
Expand Down
62 changes: 60 additions & 2 deletions src/FoxIDs.Shared/Logic/PlanUsageLogic.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using FoxIDs.Infrastructure;
using FoxIDs.Models;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

namespace FoxIDs.Logic
{
Expand All @@ -16,12 +19,14 @@ public PlanUsageLogic(TelemetryScopedLogger logger, IHttpContextAccessor httpCon

public void LogLoginEvent(PartyTypes partyType)
{
logger.Event($"Usage {UsageLogTypes.Login}.{partyType} event.", properties: new Dictionary<string, string> { { Constants.Logs.UsageType, UsageLogTypes.Login.ToString() }, { Constants.Logs.UsageLoginType, partyType.ToString() } });
var addRating = GetLogAddRating();
logger.Event($"Usage {UsageLogTypes.Login}.{partyType} event.", properties: new Dictionary<string, string> { { Constants.Logs.UsageType, UsageLogTypes.Login.ToString() }, { Constants.Logs.UsageLoginType, partyType.ToString() }, { Constants.Logs.UsageAddRating, addRating.ToString(CultureInfo.InvariantCulture) } });
}

public void LogTokenRequestEvent(UsageLogTokenTypes tokenType)
{
logger.Event($"Usage {UsageLogTypes.TokenRequest}.{tokenType} event.", properties: new Dictionary<string, string> { { Constants.Logs.UsageType, UsageLogTypes.TokenRequest.ToString() }, { Constants.Logs.UsageTokenType, tokenType.ToString() } });
var addRating = GetLogAddRating();
logger.Event($"Usage {UsageLogTypes.TokenRequest}.{tokenType} event.", properties: new Dictionary<string, string> { { Constants.Logs.UsageType, UsageLogTypes.TokenRequest.ToString() }, { Constants.Logs.UsageTokenType, tokenType.ToString() }, { Constants.Logs.UsageAddRating, addRating.ToString(CultureInfo.InvariantCulture) } });
}

public void LogControlApiGetEvent()
Expand All @@ -38,5 +43,58 @@ private void LogEvent(UsageLogTypes planUsageType)
{
logger.Event($"Usage {planUsageType} event.", properties: new Dictionary<string, string> { { Constants.Logs.UsageType, planUsageType.ToString() } });
}

private double GetLogAddRating()
{
var scopedLogger = RouteBinding.Logging?.ScopedLogger;
var rating = 0.0;
if (scopedLogger != null)
{
if (scopedLogger.LogInfoTrace)
{
rating += 0.1;
}
if (scopedLogger.LogClaimTrace)
{
rating += 0.4;
}
if (scopedLogger.LogMessageTrace)
{
rating += 0.5;
}
if (scopedLogger.LogMetric)
{
rating += 0.01;
}
}

var scopedStreamLoggers = RouteBinding.Logging?.ScopedStreamLoggers;
if (scopedStreamLoggers?.Count() > 0)
{
foreach (var scopedStreamLogger in scopedStreamLoggers)
{
rating += 0.2;

if (scopedStreamLogger.LogInfoTrace)
{
rating += 0.005;
}
if (scopedStreamLogger.LogClaimTrace)
{
rating += 0.02;
}
if (scopedStreamLogger.LogMessageTrace)
{
rating += 0.02;
}
if (scopedStreamLogger.LogMetric)
{
rating += 0.001;
}
}
}

return Math.Round(rating, 1);
}
}
}
4 changes: 4 additions & 0 deletions src/FoxIDs.Shared/Models/Parties/OAuthDownClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
{
results.Add(new ValidationResult($"Require '{IdentityConstants.ResponseTypes.Code}' response type with PKCE.", new[] { nameof(RequirePkce), nameof(ResponseTypes) }));
}
if (RedirectUris?.Sum(i => i.Count()) > Constants.Models.OAuthDownParty.Client.RedirectUriSumLength)
{
results.Add(new ValidationResult($"The {RedirectUris} total summarised number of characters is more then {Constants.Models.OAuthDownParty.Client.RedirectUriSumLength}.", new[] { nameof(RedirectUris) }));
}
return results;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/FoxIDs.Shared/Models/Tracks/Logs/UsageLogTokenTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace FoxIDs.Models
{
public enum UsageLogTokenTypes
{
{
[EnumMember(Value = "authorization_code")]
AuthorizationCode = 5,
[EnumMember(Value = "refresh_token")]
RefreshToken = 10,
[EnumMember(Value = "client_credentials")]
Expand Down
Loading

0 comments on commit 4faf554

Please sign in to comment.