1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-08 06:28:14 -05:00

policy events

This commit is contained in:
Kyle Spearrin 2020-01-15 09:43:49 -05:00
parent 3f9b44f493
commit 58faf5266b
15 changed files with 135 additions and 5 deletions

View File

@ -44,5 +44,9 @@
Organization_Updated = 1600, Organization_Updated = 1600,
Organization_PurgedVault = 1601, Organization_PurgedVault = 1601,
// Organization_ClientExportedVault = 1602, // Organization_ClientExportedVault = 1602,
Policy_Created = 1700,
Policy_Updated = 1701,
Policy_Deleted = 1702,
} }
} }

View File

@ -21,6 +21,7 @@ namespace Bit.Core.Models.Api.Public
ItemId = ev.CipherId; ItemId = ev.CipherId;
CollectionId = ev.CollectionId; CollectionId = ev.CollectionId;
GroupId = ev.GroupId; GroupId = ev.GroupId;
PolicyId = ev.PolicyId;
MemberId = ev.OrganizationUserId; MemberId = ev.OrganizationUserId;
ActingUserId = ev.ActingUserId; ActingUserId = ev.ActingUserId;
Date = ev.Date; Date = ev.Date;
@ -55,6 +56,11 @@ namespace Bit.Core.Models.Api.Public
/// <example>f29a2515-91d2-4452-b49b-5e8040e6b0f4</example> /// <example>f29a2515-91d2-4452-b49b-5e8040e6b0f4</example>
public Guid? GroupId { get; set; } public Guid? GroupId { get; set; }
/// <summary> /// <summary>
/// The unique identifier of the related policy that the event describes.
/// </summary>
/// <example>f29a2515-91d2-4452-b49b-5e8040e6b0f4</example>
public Guid? PolicyId { get; set; }
/// <summary>
/// The unique identifier of the related member that the event describes. /// The unique identifier of the related member that the event describes.
/// </summary> /// </summary>
/// <example>e68b8629-85eb-4929-92c0-b84464976ba4</example> /// <example>e68b8629-85eb-4929-92c0-b84464976ba4</example>

View File

@ -20,6 +20,7 @@ namespace Bit.Core.Models.Api
CipherId = ev.CipherId; CipherId = ev.CipherId;
CollectionId = ev.CollectionId; CollectionId = ev.CollectionId;
GroupId = ev.GroupId; GroupId = ev.GroupId;
PolicyId = ev.PolicyId;
OrganizationUserId = ev.OrganizationUserId; OrganizationUserId = ev.OrganizationUserId;
ActingUserId = ev.ActingUserId; ActingUserId = ev.ActingUserId;
Date = ev.Date; Date = ev.Date;
@ -33,6 +34,7 @@ namespace Bit.Core.Models.Api
public Guid? CipherId { get; set; } public Guid? CipherId { get; set; }
public Guid? CollectionId { get; set; } public Guid? CollectionId { get; set; }
public Guid? GroupId { get; set; } public Guid? GroupId { get; set; }
public Guid? PolicyId { get; set; }
public Guid? OrganizationUserId { get; set; } public Guid? OrganizationUserId { get; set; }
public Guid? ActingUserId { get; set; } public Guid? ActingUserId { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }

View File

@ -21,6 +21,7 @@ namespace Bit.Core.Models.Data
public Guid? CipherId { get; set; } public Guid? CipherId { get; set; }
public Guid? CollectionId { get; set; } public Guid? CollectionId { get; set; }
public Guid? GroupId { get; set; } public Guid? GroupId { get; set; }
public Guid? PolicyId { get; set; }
public Guid? OrganizationUserId { get; set; } public Guid? OrganizationUserId { get; set; }
public Guid? ActingUserId { get; set; } public Guid? ActingUserId { get; set; }
public DeviceType? DeviceType { get; set; } public DeviceType? DeviceType { get; set; }

View File

@ -18,6 +18,7 @@ namespace Bit.Core.Models.Data
OrganizationId = e.OrganizationId; OrganizationId = e.OrganizationId;
CipherId = e.CipherId; CipherId = e.CipherId;
CollectionId = e.CollectionId; CollectionId = e.CollectionId;
PolicyId = e.PolicyId;
GroupId = e.GroupId; GroupId = e.GroupId;
OrganizationUserId = e.OrganizationUserId; OrganizationUserId = e.OrganizationUserId;
DeviceType = e.DeviceType; DeviceType = e.DeviceType;
@ -31,6 +32,7 @@ namespace Bit.Core.Models.Data
public Guid? OrganizationId { get; set; } public Guid? OrganizationId { get; set; }
public Guid? CipherId { get; set; } public Guid? CipherId { get; set; }
public Guid? CollectionId { get; set; } public Guid? CollectionId { get; set; }
public Guid? PolicyId { get; set; }
public Guid? GroupId { get; set; } public Guid? GroupId { get; set; }
public Guid? OrganizationUserId { get; set; } public Guid? OrganizationUserId { get; set; }
public DeviceType? DeviceType { get; set; } public DeviceType? DeviceType { get; set; }

View File

@ -11,6 +11,7 @@ namespace Bit.Core.Models.Data
Guid? CipherId { get; set; } Guid? CipherId { get; set; }
Guid? CollectionId { get; set; } Guid? CollectionId { get; set; }
Guid? GroupId { get; set; } Guid? GroupId { get; set; }
Guid? PolicyId { get; set; }
Guid? OrganizationUserId { get; set; } Guid? OrganizationUserId { get; set; }
Guid? ActingUserId { get; set; } Guid? ActingUserId { get; set; }
DeviceType? DeviceType { get; set; } DeviceType? DeviceType { get; set; }

View File

@ -17,6 +17,7 @@ namespace Bit.Core.Models.Table
OrganizationId = e.OrganizationId; OrganizationId = e.OrganizationId;
CipherId = e.CipherId; CipherId = e.CipherId;
CollectionId = e.CollectionId; CollectionId = e.CollectionId;
PolicyId = e.PolicyId;
GroupId = e.GroupId; GroupId = e.GroupId;
OrganizationUserId = e.OrganizationUserId; OrganizationUserId = e.OrganizationUserId;
DeviceType = e.DeviceType; DeviceType = e.DeviceType;
@ -31,6 +32,7 @@ namespace Bit.Core.Models.Table
public Guid? OrganizationId { get; set; } public Guid? OrganizationId { get; set; }
public Guid? CipherId { get; set; } public Guid? CipherId { get; set; }
public Guid? CollectionId { get; set; } public Guid? CollectionId { get; set; }
public Guid? PolicyId { get; set; }
public Guid? GroupId { get; set; } public Guid? GroupId { get; set; }
public Guid? OrganizationUserId { get; set; } public Guid? OrganizationUserId { get; set; }
public DeviceType? DeviceType { get; set; } public DeviceType? DeviceType { get; set; }

View File

@ -153,6 +153,8 @@ namespace Bit.Core.Repositories.SqlServer
eventsTable.Columns.Add(cipherIdColumn); eventsTable.Columns.Add(cipherIdColumn);
var collectionIdColumn = new DataColumn(nameof(e.CollectionId), typeof(Guid)); var collectionIdColumn = new DataColumn(nameof(e.CollectionId), typeof(Guid));
eventsTable.Columns.Add(collectionIdColumn); eventsTable.Columns.Add(collectionIdColumn);
var policyIdColumn = new DataColumn(nameof(e.PolicyId), typeof(Guid));
eventsTable.Columns.Add(policyIdColumn);
var groupIdColumn = new DataColumn(nameof(e.GroupId), typeof(Guid)); var groupIdColumn = new DataColumn(nameof(e.GroupId), typeof(Guid));
eventsTable.Columns.Add(groupIdColumn); eventsTable.Columns.Add(groupIdColumn);
var organizationUserIdColumn = new DataColumn(nameof(e.OrganizationUserId), typeof(Guid)); var organizationUserIdColumn = new DataColumn(nameof(e.OrganizationUserId), typeof(Guid));
@ -182,6 +184,7 @@ namespace Bit.Core.Repositories.SqlServer
row[organizationIdColumn] = ev.OrganizationId.HasValue ? (object)ev.OrganizationId.Value : DBNull.Value; row[organizationIdColumn] = ev.OrganizationId.HasValue ? (object)ev.OrganizationId.Value : DBNull.Value;
row[cipherIdColumn] = ev.CipherId.HasValue ? (object)ev.CipherId.Value : DBNull.Value; row[cipherIdColumn] = ev.CipherId.HasValue ? (object)ev.CipherId.Value : DBNull.Value;
row[collectionIdColumn] = ev.CollectionId.HasValue ? (object)ev.CollectionId.Value : DBNull.Value; row[collectionIdColumn] = ev.CollectionId.HasValue ? (object)ev.CollectionId.Value : DBNull.Value;
row[policyIdColumn] = ev.PolicyId.HasValue ? (object)ev.PolicyId.Value : DBNull.Value;
row[groupIdColumn] = ev.GroupId.HasValue ? (object)ev.GroupId.Value : DBNull.Value; row[groupIdColumn] = ev.GroupId.HasValue ? (object)ev.GroupId.Value : DBNull.Value;
row[organizationUserIdColumn] = ev.OrganizationUserId.HasValue ? row[organizationUserIdColumn] = ev.OrganizationUserId.HasValue ?
(object)ev.OrganizationUserId.Value : DBNull.Value; (object)ev.OrganizationUserId.Value : DBNull.Value;

View File

@ -13,6 +13,7 @@ namespace Bit.Core.Services
Task LogCipherEventsAsync(IEnumerable<Tuple<Cipher, EventType, DateTime?>> events); Task LogCipherEventsAsync(IEnumerable<Tuple<Cipher, EventType, DateTime?>> events);
Task LogCollectionEventAsync(Collection collection, EventType type, DateTime? date = null); Task LogCollectionEventAsync(Collection collection, EventType type, DateTime? date = null);
Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null); Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null);
Task LogPolicyEventAsync(Policy policy, EventType type, DateTime? date = null);
Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type, DateTime? date = null); Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type, DateTime? date = null);
Task LogOrganizationEventAsync(Organization organization, EventType type, DateTime? date = null); Task LogOrganizationEventAsync(Organization organization, EventType type, DateTime? date = null);
} }

View File

@ -156,6 +156,25 @@ namespace Bit.Core.Services
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogPolicyEventAsync(Policy policy, EventType type, DateTime? date = null)
{
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
if(!CanUseEvents(orgAbilities, policy.OrganizationId))
{
return;
}
var e = new EventMessage(_currentContext)
{
OrganizationId = policy.OrganizationId,
PolicyId = policy.Id,
Type = type,
ActingUserId = _currentContext?.UserId,
Date = date.GetValueOrDefault(DateTime.UtcNow)
};
await _eventWriteService.CreateAsync(e);
}
public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type, public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type,
DateTime? date = null) DateTime? date = null)
{ {

View File

@ -3,8 +3,6 @@ using System.Threading.Tasks;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using Bit.Core.Repositories; using Bit.Core.Repositories;
using System.Collections.Generic;
using Bit.Core.Models.Data;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -46,20 +44,20 @@ namespace Bit.Core.Services
{ {
policy.CreationDate = policy.RevisionDate = DateTime.UtcNow; policy.CreationDate = policy.RevisionDate = DateTime.UtcNow;
await _policyRepository.CreateAsync(policy); await _policyRepository.CreateAsync(policy);
//await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Created); await _eventService.LogPolicyEventAsync(policy, Enums.EventType.Policy_Created);
} }
else else
{ {
policy.RevisionDate = DateTime.UtcNow; policy.RevisionDate = DateTime.UtcNow;
await _policyRepository.ReplaceAsync(policy); await _policyRepository.ReplaceAsync(policy);
//await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Updated); await _eventService.LogPolicyEventAsync(policy, Enums.EventType.Policy_Updated);
} }
} }
public async Task DeleteAsync(Policy policy) public async Task DeleteAsync(Policy policy)
{ {
await _policyRepository.DeleteAsync(policy); await _policyRepository.DeleteAsync(policy);
//await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Deleted); await _eventService.LogPolicyEventAsync(policy, Enums.EventType.Policy_Deleted);
} }
} }
} }

View File

@ -23,6 +23,11 @@ namespace Bit.Core.Services
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogPolicyEventAsync(Policy policy, EventType type, DateTime? date = null)
{
return Task.FromResult(0);
}
public Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null) public Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);

View File

@ -5,6 +5,7 @@
@OrganizationId UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER,
@CipherId UNIQUEIDENTIFIER, @CipherId UNIQUEIDENTIFIER,
@CollectionId UNIQUEIDENTIFIER, @CollectionId UNIQUEIDENTIFIER,
@PolicyId UNIQUEIDENTIFIER,
@GroupId UNIQUEIDENTIFIER, @GroupId UNIQUEIDENTIFIER,
@OrganizationUserId UNIQUEIDENTIFIER, @OrganizationUserId UNIQUEIDENTIFIER,
@ActingUserId UNIQUEIDENTIFIER, @ActingUserId UNIQUEIDENTIFIER,
@ -23,6 +24,7 @@ BEGIN
[OrganizationId], [OrganizationId],
[CipherId], [CipherId],
[CollectionId], [CollectionId],
[PolicyId],
[GroupId], [GroupId],
[OrganizationUserId], [OrganizationUserId],
[ActingUserId], [ActingUserId],
@ -38,6 +40,7 @@ BEGIN
@OrganizationId, @OrganizationId,
@CipherId, @CipherId,
@CollectionId, @CollectionId,
@PolicyId,
@GroupId, @GroupId,
@OrganizationUserId, @OrganizationUserId,
@ActingUserId, @ActingUserId,

View File

@ -5,6 +5,7 @@
[OrganizationId] UNIQUEIDENTIFIER NULL, [OrganizationId] UNIQUEIDENTIFIER NULL,
[CipherId] UNIQUEIDENTIFIER NULL, [CipherId] UNIQUEIDENTIFIER NULL,
[CollectionId] UNIQUEIDENTIFIER NULL, [CollectionId] UNIQUEIDENTIFIER NULL,
[PolicyId] UNIQUEIDENTIFIER NULL,
[GroupId] UNIQUEIDENTIFIER NULL, [GroupId] UNIQUEIDENTIFIER NULL,
[OrganizationUserId] UNIQUEIDENTIFIER NULL, [OrganizationUserId] UNIQUEIDENTIFIER NULL,
[ActingUserId] UNIQUEIDENTIFIER NULL, [ActingUserId] UNIQUEIDENTIFIER NULL,

View File

@ -175,3 +175,85 @@ SELECT
FROM FROM
[dbo].[Policy] [dbo].[Policy]
GO GO
IF COL_LENGTH('[dbo].[Event]', 'PolicyId') IS NULL
BEGIN
ALTER TABLE
[dbo].[Event]
ADD
[PolicyId] UNIQUEIDENTIFIER NULL
END
GO
IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'EventView')
BEGIN
DROP VIEW [dbo].[EventView]
END
GO
CREATE VIEW [dbo].[EventView]
AS
SELECT
*
FROM
[dbo].[Event]
GO
IF OBJECT_ID('[dbo].[Event_Create]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Event_Create]
END
GO
CREATE PROCEDURE [dbo].[Event_Create]
@Id UNIQUEIDENTIFIER,
@Type INT,
@UserId UNIQUEIDENTIFIER,
@OrganizationId UNIQUEIDENTIFIER,
@CipherId UNIQUEIDENTIFIER,
@CollectionId UNIQUEIDENTIFIER,
@PolicyId UNIQUEIDENTIFIER,
@GroupId UNIQUEIDENTIFIER,
@OrganizationUserId UNIQUEIDENTIFIER,
@ActingUserId UNIQUEIDENTIFIER,
@DeviceType SMALLINT,
@IpAddress VARCHAR(50),
@Date DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[Event]
(
[Id],
[Type],
[UserId],
[OrganizationId],
[CipherId],
[CollectionId],
[PolicyId],
[GroupId],
[OrganizationUserId],
[ActingUserId],
[DeviceType],
[IpAddress],
[Date]
)
VALUES
(
@Id,
@Type,
@UserId,
@OrganizationId,
@CipherId,
@CollectionId,
@PolicyId,
@GroupId,
@OrganizationUserId,
@ActingUserId,
@DeviceType,
@IpAddress,
@Date
)
END
GO