1
0
mirror of https://github.com/bitwarden/server synced 2026-02-24 16:42:52 +00:00

PM-31725 updated properties to match the pull in public/events (#6959)

This commit is contained in:
Vijay Oommen
2026-02-23 17:00:21 -06:00
committed by GitHub
parent 98d6217b9b
commit 5c77ae9810
2 changed files with 105 additions and 0 deletions

View File

@@ -13,10 +13,12 @@ public class IntegrationTemplateContext(EventMessage eventMessage)
public string DomainName => Event.DomainName;
public string IpAddress => Event.IpAddress;
public DeviceType? DeviceType => Event.DeviceType;
public int? DeviceTypeId => Event.DeviceType is not null ? (int)Event.DeviceType : null;
public Guid? ActingUserId => Event.ActingUserId;
public Guid? OrganizationUserId => Event.OrganizationUserId;
public DateTime Date => Event.Date;
public EventType Type => Event.Type;
public int TypeId => (int)Event.Type;
public Guid? UserId => Event.UserId;
public Guid? OrganizationId => Event.OrganizationId;
public Guid? CipherId => Event.CipherId;
@@ -51,4 +53,6 @@ public class IntegrationTemplateContext(EventMessage eventMessage)
public Organization? Organization { get; set; }
public string? OrganizationName => Organization?.DisplayName();
public int? SystemUser => Event.SystemUser is not null ? (int)Event.SystemUser : null;
}

View File

@@ -1,6 +1,7 @@
#nullable enable
using System.Text.Json;
using System.Text.Json.Nodes;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Dirt.Enums;
@@ -708,4 +709,104 @@ public class EventIntegrationHandlerTests
Assert.NotNull(capturedTags);
Assert.Contains(expectedTag, capturedTags);
}
[Theory, BitAutoData]
public async Task HandleEventAsync_SubstituteTemplateTags(EventMessage eventMessage)
{
eventMessage.OrganizationId = _organizationId;
var templateJson = @"{
""bw_serviceName"": ""bitwarden"",
""ddsource"": ""bitwarden"",
""service"": ""event-logs"",
""event"": {
""object"": ""event"",
""type"": ""#TypeId#"",
""typeName"": ""#Type#"",
""userId"": ""#UserId#"",
""organizationId"": ""#OrganizationId#"",
""providerId"": ""#ProviderId#"",
""cipherId"": ""#CipherId#"",
""collectionId"": ""#CollectionId#"",
""groupId"": ""#GroupId#"",
""policyId"": ""#PolicyId#"",
""organizationUserId"": ""#OrganizationUserId#"",
""providerUserId"": ""#ProviderUserId#"",
""providerOrganizationId"": ""#ProviderOrganizationId#"",
""actingUserId"": ""#ActingUserId#"",
""installationId"": ""#InstallationId#"",
""date"": ""#DateIso8601#"",
""deviceType"": ""#DeviceType#"",
""deviceTypeId"": ""#DeviceTypeId#"",
""ipAddress"": ""#IpAddress#"",
""systemUser"": ""#SystemUser#"",
""domainName"": ""#DomainName#"",
""secretId"": ""#SecretId#"",
""projectId"": ""#ProjectId#"",
""serviceAccountId"": ""#ServiceAccountId#""
}
}";
var sutProvider = GetSutProvider(OneConfiguration(templateJson));
await sutProvider.Sut.HandleEventAsync(eventMessage);
var deviceTypeId = eventMessage.DeviceType is not null ? (int)eventMessage.DeviceType : (int?)null;
var systemUser = eventMessage.SystemUser is not null ? (int)eventMessage.SystemUser : (int?)null;
var parsedJson = $@"{{
""bw_serviceName"": ""bitwarden"",
""ddsource"": ""bitwarden"",
""service"": ""event-logs"",
""event"": {{
""object"": ""event"",
""type"": ""{(int)eventMessage.Type}"",
""typeName"": ""{eventMessage.Type}"",
""userId"": ""{eventMessage.UserId}"",
""organizationId"": ""{eventMessage.OrganizationId}"",
""providerId"": ""{eventMessage.ProviderId}"",
""cipherId"": ""{eventMessage.CipherId}"",
""collectionId"": ""{eventMessage.CollectionId}"",
""groupId"": ""{eventMessage.GroupId}"",
""policyId"": ""{eventMessage.PolicyId}"",
""organizationUserId"": ""{eventMessage.OrganizationUserId}"",
""providerUserId"": ""{eventMessage.ProviderUserId}"",
""providerOrganizationId"": ""{eventMessage.ProviderOrganizationId}"",
""actingUserId"": ""{eventMessage.ActingUserId}"",
""installationId"": ""{eventMessage.InstallationId}"",
""date"": ""{eventMessage.Date.ToString("o")}"",
""deviceType"": ""{eventMessage.DeviceType}"",
""deviceTypeId"": ""{deviceTypeId}"",
""ipAddress"": ""{eventMessage.IpAddress}"",
""systemUser"": ""{systemUser}"",
""domainName"": ""{eventMessage.DomainName}"",
""secretId"": ""{eventMessage.SecretId}"",
""projectId"": ""{eventMessage.ProjectId}"",
""serviceAccountId"": ""{eventMessage.ServiceAccountId}""
}}
}}";
var expectedMessage = EventIntegrationHandlerTests.ExpectedMessage(
parsedJson
);
Assert.Single(_eventIntegrationPublisher.ReceivedCalls());
await _eventIntegrationPublisher.Received(1)
.PublishAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expectedMessage, new[] { "MessageId", "RenderedTemplate" })));
// compare renderedTemplate
var receivedCalls = _eventIntegrationPublisher.ReceivedCalls().ToList();
Assert.Single(receivedCalls);
var publishCall = receivedCalls.First();
var actualMessage = publishCall.GetArguments()[0] as IntegrationMessage<WebhookIntegrationConfigurationDetails>;
Assert.NotNull(actualMessage);
Assert.True(JsonStringsAreEqual(expectedMessage.RenderedTemplate!, actualMessage.RenderedTemplate!),
$"Expected: {expectedMessage.RenderedTemplate}\nActual: {actualMessage.RenderedTemplate}");
}
private bool JsonStringsAreEqual(string expectedJson, string actualJson)
{
var expectedDoc = JsonNode.Parse(expectedJson);
var actualDoc = JsonNode.Parse(actualJson);
return JsonNode.DeepEquals(expectedDoc, actualDoc);
}
}