1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

[EC-315] Record user IP and device type for OrgUser and ProviderUser events (#2119)

This commit is contained in:
Thomas Rittson 2022-07-18 10:24:46 +10:00 committed by GitHub
parent 6e19bfeb22
commit 6628dc3336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 2 deletions

View File

@ -210,7 +210,7 @@ namespace Bit.Core.Services
continue;
}
eventMessages.Add(new EventMessage
eventMessages.Add(new EventMessage(_currentContext)
{
OrganizationId = organizationUser.OrganizationId,
UserId = organizationUser.UserId,
@ -259,7 +259,7 @@ namespace Bit.Core.Services
{
continue;
}
eventMessages.Add(new EventMessage
eventMessages.Add(new EventMessage(_currentContext)
{
ProviderId = providerUser.ProviderId,
UserId = providerUser.UserId,

View File

@ -64,6 +64,28 @@ namespace Bit.Test.Common.Helpers
public static Expression<Predicate<T>> AssertPropertyEqual<T>(T expected, params string[] excludedPropertyStrings) =>
(T actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
private static Predicate<IEnumerable<T>> AssertPropertyEqualPredicate<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) => (actual) =>
{
// IEnumerable.Zip doesn't account for different lengths, we need to check this ourselves
if (actual.Count() != expected.Count())
{
throw new Exception(string.Concat($"Actual IEnumerable does not have the expected length.\n",
$"Expected: {expected.Count()}\n",
$"Actual: {actual.Count()}"));
}
var elements = expected.Zip(actual);
foreach (var (expectedEl, actualEl) in elements)
{
AssertPropertyEqual(expectedEl, actualEl, excludedPropertyStrings);
}
return true;
};
public static Expression<Predicate<IEnumerable<T>>> AssertPropertyEqual<T>(IEnumerable<T> expected, params string[] excludedPropertyStrings) =>
(actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual);
private static Predicate<T> AssertEqualExpectedPredicate<T>(T expected) => (actual) =>
{
Assert.Equal(expected, actual);

View File

@ -1,7 +1,9 @@
using Bit.Core.Context;
using Bit.Core.Entities;
using Bit.Core.Entities.Provider;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Models.Data.Organizations;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
@ -36,5 +38,72 @@ namespace Bit.Core.Test.Services
e.Type == eventType &&
e.InstallationId == installationId));
}
[Theory, BitAutoData]
public async Task LogOrganizationUserEvent_LogsRequiredInfo(OrganizationUser orgUser, EventType eventType, DateTime date,
Guid actingUserId, Guid providerId, string ipAddress, DeviceType deviceType, SutProvider<EventService> sutProvider)
{
var orgAbilities = new Dictionary<Guid, OrganizationAbility>()
{
{orgUser.OrganizationId, new OrganizationAbility() { UseEvents = true, Enabled = true } }
};
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilitiesAsync().Returns(orgAbilities);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(actingUserId);
sutProvider.GetDependency<ICurrentContext>().IpAddress.Returns(ipAddress);
sutProvider.GetDependency<ICurrentContext>().DeviceType.Returns(deviceType);
sutProvider.GetDependency<ICurrentContext>().ProviderIdForOrg(Arg.Any<Guid>()).Returns(providerId);
await sutProvider.Sut.LogOrganizationUserEventAsync(orgUser, eventType, date);
var expected = new List<IEvent>() {
new EventMessage()
{
IpAddress = ipAddress,
DeviceType = deviceType,
OrganizationId = orgUser.OrganizationId,
UserId = orgUser.UserId,
OrganizationUserId = orgUser.Id,
ProviderId = providerId,
Type = eventType,
ActingUserId = actingUserId,
Date = date
}
};
await sutProvider.GetDependency<IEventWriteService>().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual<IEvent>(expected, new[] { "IdempotencyId" })));
}
[Theory, BitAutoData]
public async Task LogProviderUserEvent_LogsRequiredInfo(ProviderUser providerUser, EventType eventType, DateTime date,
Guid actingUserId, Guid providerId, string ipAddress, DeviceType deviceType, SutProvider<EventService> sutProvider)
{
var providerAbilities = new Dictionary<Guid, ProviderAbility>()
{
{providerUser.ProviderId, new ProviderAbility() { UseEvents = true, Enabled = true } }
};
sutProvider.GetDependency<IApplicationCacheService>().GetProviderAbilitiesAsync().Returns(providerAbilities);
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(actingUserId);
sutProvider.GetDependency<ICurrentContext>().IpAddress.Returns(ipAddress);
sutProvider.GetDependency<ICurrentContext>().DeviceType.Returns(deviceType);
sutProvider.GetDependency<ICurrentContext>().ProviderIdForOrg(Arg.Any<Guid>()).Returns(providerId);
await sutProvider.Sut.LogProviderUserEventAsync(providerUser, eventType, date);
var expected = new List<IEvent>() {
new EventMessage()
{
IpAddress = ipAddress,
DeviceType = deviceType,
ProviderId = providerUser.ProviderId,
UserId = providerUser.UserId,
ProviderUserId = providerUser.Id,
Type = eventType,
ActingUserId = actingUserId,
Date = date
}
};
await sutProvider.GetDependency<IEventWriteService>().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual<IEvent>(expected, new[] { "IdempotencyId" })));
}
}
}