mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 23:52:50 -05:00
[PM-6762] Move to Azure.Data.Tables (#3888)
* Move to Azure.Data.Tables * Reorder usings * Add new package to Renovate * Add manual serialization and deserialization due to enums * Properly retrieve just the next page
This commit is contained in:
@ -1,134 +0,0 @@
|
||||
using System.Collections;
|
||||
using Microsoft.Azure.Cosmos.Table;
|
||||
|
||||
namespace Bit.Core.Models.Data;
|
||||
|
||||
public class DictionaryEntity : TableEntity, IDictionary<string, EntityProperty>
|
||||
{
|
||||
private IDictionary<string, EntityProperty> _properties = new Dictionary<string, EntityProperty>();
|
||||
|
||||
public ICollection<EntityProperty> Values => _properties.Values;
|
||||
|
||||
public EntityProperty this[string key]
|
||||
{
|
||||
get => _properties[key];
|
||||
set => _properties[key] = value;
|
||||
}
|
||||
|
||||
public int Count => _properties.Count;
|
||||
|
||||
public bool IsReadOnly => _properties.IsReadOnly;
|
||||
|
||||
public ICollection<string> Keys => _properties.Keys;
|
||||
|
||||
public override void ReadEntity(IDictionary<string, EntityProperty> properties,
|
||||
OperationContext operationContext)
|
||||
{
|
||||
_properties = properties;
|
||||
}
|
||||
|
||||
public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
|
||||
{
|
||||
return _properties;
|
||||
}
|
||||
|
||||
public void Add(string key, EntityProperty value)
|
||||
{
|
||||
_properties.Add(key, value);
|
||||
}
|
||||
|
||||
public void Add(string key, bool value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, byte[] value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, DateTime? value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, DateTimeOffset? value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, double value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, Guid value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, int value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, long value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(string key, string value)
|
||||
{
|
||||
_properties.Add(key, new EntityProperty(value));
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<string, EntityProperty> item)
|
||||
{
|
||||
_properties.Add(item);
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
return _properties.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(string key)
|
||||
{
|
||||
return _properties.Remove(key);
|
||||
}
|
||||
|
||||
public bool TryGetValue(string key, out EntityProperty value)
|
||||
{
|
||||
return _properties.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_properties.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<string, EntityProperty> item)
|
||||
{
|
||||
return _properties.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<string, EntityProperty>[] array, int arrayIndex)
|
||||
{
|
||||
_properties.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<string, EntityProperty> item)
|
||||
{
|
||||
return _properties.Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, EntityProperty>> GetEnumerator()
|
||||
{
|
||||
return _properties.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return _properties.GetEnumerator();
|
||||
}
|
||||
}
|
@ -1,10 +1,73 @@
|
||||
using Bit.Core.Enums;
|
||||
using Azure;
|
||||
using Azure.Data.Tables;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Azure.Cosmos.Table;
|
||||
|
||||
namespace Bit.Core.Models.Data;
|
||||
|
||||
public class EventTableEntity : TableEntity, IEvent
|
||||
// used solely for interaction with Azure Table Storage
|
||||
public class AzureEvent : ITableEntity
|
||||
{
|
||||
public string PartitionKey { get; set; }
|
||||
public string RowKey { get; set; }
|
||||
public DateTimeOffset? Timestamp { get; set; }
|
||||
public ETag ETag { get; set; }
|
||||
|
||||
public DateTime Date { get; set; }
|
||||
public int Type { get; set; }
|
||||
public Guid? UserId { get; set; }
|
||||
public Guid? OrganizationId { get; set; }
|
||||
public Guid? InstallationId { get; set; }
|
||||
public Guid? ProviderId { get; set; }
|
||||
public Guid? CipherId { get; set; }
|
||||
public Guid? CollectionId { get; set; }
|
||||
public Guid? PolicyId { get; set; }
|
||||
public Guid? GroupId { get; set; }
|
||||
public Guid? OrganizationUserId { get; set; }
|
||||
public Guid? ProviderUserId { get; set; }
|
||||
public Guid? ProviderOrganizationId { get; set; }
|
||||
public int? DeviceType { get; set; }
|
||||
public string IpAddress { get; set; }
|
||||
public Guid? ActingUserId { get; set; }
|
||||
public int? SystemUser { get; set; }
|
||||
public string DomainName { get; set; }
|
||||
public Guid? SecretId { get; set; }
|
||||
public Guid? ServiceAccountId { get; set; }
|
||||
|
||||
public EventTableEntity ToEventTableEntity()
|
||||
{
|
||||
return new EventTableEntity
|
||||
{
|
||||
PartitionKey = PartitionKey,
|
||||
RowKey = RowKey,
|
||||
Timestamp = Timestamp,
|
||||
ETag = ETag,
|
||||
|
||||
Date = Date,
|
||||
Type = (EventType)Type,
|
||||
UserId = UserId,
|
||||
OrganizationId = OrganizationId,
|
||||
InstallationId = InstallationId,
|
||||
ProviderId = ProviderId,
|
||||
CipherId = CipherId,
|
||||
CollectionId = CollectionId,
|
||||
PolicyId = PolicyId,
|
||||
GroupId = GroupId,
|
||||
OrganizationUserId = OrganizationUserId,
|
||||
ProviderUserId = ProviderUserId,
|
||||
ProviderOrganizationId = ProviderOrganizationId,
|
||||
DeviceType = DeviceType.HasValue ? (DeviceType)DeviceType.Value : null,
|
||||
IpAddress = IpAddress,
|
||||
ActingUserId = ActingUserId,
|
||||
SystemUser = SystemUser.HasValue ? (EventSystemUser)SystemUser.Value : null,
|
||||
DomainName = DomainName,
|
||||
SecretId = SecretId,
|
||||
ServiceAccountId = ServiceAccountId
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class EventTableEntity : IEvent
|
||||
{
|
||||
public EventTableEntity() { }
|
||||
|
||||
@ -32,6 +95,11 @@ public class EventTableEntity : TableEntity, IEvent
|
||||
ServiceAccountId = e.ServiceAccountId;
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
public string RowKey { get; set; }
|
||||
public DateTimeOffset? Timestamp { get; set; }
|
||||
public ETag ETag { get; set; }
|
||||
|
||||
public DateTime Date { get; set; }
|
||||
public EventType Type { get; set; }
|
||||
public Guid? UserId { get; set; }
|
||||
@ -53,65 +121,36 @@ public class EventTableEntity : TableEntity, IEvent
|
||||
public Guid? SecretId { get; set; }
|
||||
public Guid? ServiceAccountId { get; set; }
|
||||
|
||||
public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
|
||||
public AzureEvent ToAzureEvent()
|
||||
{
|
||||
var result = base.WriteEntity(operationContext);
|
||||
return new AzureEvent
|
||||
{
|
||||
PartitionKey = PartitionKey,
|
||||
RowKey = RowKey,
|
||||
Timestamp = Timestamp,
|
||||
ETag = ETag,
|
||||
|
||||
var typeName = nameof(Type);
|
||||
if (result.ContainsKey(typeName))
|
||||
{
|
||||
result[typeName] = new EntityProperty((int)Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(typeName, new EntityProperty((int)Type));
|
||||
}
|
||||
|
||||
var deviceTypeName = nameof(DeviceType);
|
||||
if (result.ContainsKey(deviceTypeName))
|
||||
{
|
||||
result[deviceTypeName] = new EntityProperty((int?)DeviceType);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(deviceTypeName, new EntityProperty((int?)DeviceType));
|
||||
}
|
||||
|
||||
var systemUserTypeName = nameof(SystemUser);
|
||||
if (result.ContainsKey(systemUserTypeName))
|
||||
{
|
||||
result[systemUserTypeName] = new EntityProperty((int?)SystemUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(systemUserTypeName, new EntityProperty((int?)SystemUser));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override void ReadEntity(IDictionary<string, EntityProperty> properties,
|
||||
OperationContext operationContext)
|
||||
{
|
||||
base.ReadEntity(properties, operationContext);
|
||||
|
||||
var typeName = nameof(Type);
|
||||
if (properties.ContainsKey(typeName) && properties[typeName].Int32Value.HasValue)
|
||||
{
|
||||
Type = (EventType)properties[typeName].Int32Value.Value;
|
||||
}
|
||||
|
||||
var deviceTypeName = nameof(DeviceType);
|
||||
if (properties.ContainsKey(deviceTypeName) && properties[deviceTypeName].Int32Value.HasValue)
|
||||
{
|
||||
DeviceType = (DeviceType)properties[deviceTypeName].Int32Value.Value;
|
||||
}
|
||||
|
||||
var systemUserTypeName = nameof(SystemUser);
|
||||
if (properties.ContainsKey(systemUserTypeName) && properties[systemUserTypeName].Int32Value.HasValue)
|
||||
{
|
||||
SystemUser = (EventSystemUser)properties[systemUserTypeName].Int32Value.Value;
|
||||
}
|
||||
Date = Date,
|
||||
Type = (int)Type,
|
||||
UserId = UserId,
|
||||
OrganizationId = OrganizationId,
|
||||
InstallationId = InstallationId,
|
||||
ProviderId = ProviderId,
|
||||
CipherId = CipherId,
|
||||
CollectionId = CollectionId,
|
||||
PolicyId = PolicyId,
|
||||
GroupId = GroupId,
|
||||
OrganizationUserId = OrganizationUserId,
|
||||
ProviderUserId = ProviderUserId,
|
||||
ProviderOrganizationId = ProviderOrganizationId,
|
||||
DeviceType = DeviceType.HasValue ? (int)DeviceType.Value : null,
|
||||
IpAddress = IpAddress,
|
||||
ActingUserId = ActingUserId,
|
||||
SystemUser = SystemUser.HasValue ? (int)SystemUser.Value : null,
|
||||
DomainName = DomainName,
|
||||
SecretId = SecretId,
|
||||
ServiceAccountId = ServiceAccountId
|
||||
};
|
||||
}
|
||||
|
||||
public static List<EventTableEntity> IndexEvent(EventMessage e)
|
||||
|
@ -1,8 +1,9 @@
|
||||
using Microsoft.Azure.Cosmos.Table;
|
||||
using Azure;
|
||||
using Azure.Data.Tables;
|
||||
|
||||
namespace Bit.Core.Models.Data;
|
||||
|
||||
public class InstallationDeviceEntity : TableEntity
|
||||
public class InstallationDeviceEntity : ITableEntity
|
||||
{
|
||||
public InstallationDeviceEntity() { }
|
||||
|
||||
@ -27,6 +28,11 @@ public class InstallationDeviceEntity : TableEntity
|
||||
RowKey = parts[1];
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
public string RowKey { get; set; }
|
||||
public DateTimeOffset? Timestamp { get; set; }
|
||||
public ETag ETag { get; set; }
|
||||
|
||||
public static bool IsInstallationDeviceId(string deviceId)
|
||||
{
|
||||
return deviceId != null && deviceId.Length == 73 && deviceId[36] == '_';
|
||||
|
Reference in New Issue
Block a user