mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 21:48:12 -05:00
Add changes for the phishing domain get
This commit is contained in:
parent
011d86417d
commit
e56b71abf2
7
src/Core/Repositories/IPhishingDomainRepository.cs
Normal file
7
src/Core/Repositories/IPhishingDomainRepository.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Bit.Core.Repositories;
|
||||||
|
|
||||||
|
public interface IPhishingDomainRepository
|
||||||
|
{
|
||||||
|
Task<ICollection<string>> GetActivePhishingDomainsAsync();
|
||||||
|
Task UpdatePhishingDomainsAsync(IEnumerable<string> domains);
|
||||||
|
}
|
@ -44,6 +44,7 @@ public static class DapperServiceCollectionExtensions
|
|||||||
services.AddSingleton<IOrganizationRepository, OrganizationRepository>();
|
services.AddSingleton<IOrganizationRepository, OrganizationRepository>();
|
||||||
services.AddSingleton<IOrganizationSponsorshipRepository, OrganizationSponsorshipRepository>();
|
services.AddSingleton<IOrganizationSponsorshipRepository, OrganizationSponsorshipRepository>();
|
||||||
services.AddSingleton<IOrganizationUserRepository, OrganizationUserRepository>();
|
services.AddSingleton<IOrganizationUserRepository, OrganizationUserRepository>();
|
||||||
|
services.AddSingleton<IPhishingDomainRepository, PhishingDomainRepository>();
|
||||||
services.AddSingleton<IPolicyRepository, PolicyRepository>();
|
services.AddSingleton<IPolicyRepository, PolicyRepository>();
|
||||||
services.AddSingleton<IProviderOrganizationRepository, ProviderOrganizationRepository>();
|
services.AddSingleton<IProviderOrganizationRepository, ProviderOrganizationRepository>();
|
||||||
services.AddSingleton<IProviderRepository, ProviderRepository>();
|
services.AddSingleton<IProviderRepository, ProviderRepository>();
|
||||||
@ -71,4 +72,10 @@ public static class DapperServiceCollectionExtensions
|
|||||||
services.AddSingleton<IEventRepository, EventRepository>();
|
services.AddSingleton<IEventRepository, EventRepository>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddDapper(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
// Register repositories
|
||||||
|
services.AddSingleton<IPhishingDomainRepository, PhishingDomainRepository>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
using System.Data;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Core.Settings;
|
||||||
|
using Dapper;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
|
||||||
|
namespace Bit.Infrastructure.Dapper.Repositories;
|
||||||
|
|
||||||
|
public class PhishingDomainRepository : IPhishingDomainRepository
|
||||||
|
{
|
||||||
|
private readonly string _connectionString;
|
||||||
|
|
||||||
|
public PhishingDomainRepository(GlobalSettings globalSettings)
|
||||||
|
: this(globalSettings.SqlServer.ConnectionString)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public PhishingDomainRepository(string connectionString)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ICollection<string>> GetActivePhishingDomainsAsync()
|
||||||
|
{
|
||||||
|
using (var connection = new SqlConnection(_connectionString))
|
||||||
|
{
|
||||||
|
var results = await connection.QueryAsync<string>(
|
||||||
|
"[dbo].[PhishingDomain_ReadAll]",
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
return results.AsList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdatePhishingDomainsAsync(IEnumerable<string> domains)
|
||||||
|
{
|
||||||
|
using (var connection = new SqlConnection(_connectionString))
|
||||||
|
{
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"[dbo].[PhishingDomain_DeleteAll]",
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
|
||||||
|
foreach (var domain in domains)
|
||||||
|
{
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"[dbo].[PhishingDomain_Create]",
|
||||||
|
new
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Domain = domain,
|
||||||
|
CreationDate = DateTime.UtcNow,
|
||||||
|
RevisionDate = DateTime.UtcNow
|
||||||
|
},
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -101,6 +101,7 @@ public static class EntityFrameworkServiceCollectionExtensions
|
|||||||
services.AddSingleton<IPasswordHealthReportApplicationRepository, PasswordHealthReportApplicationRepository>();
|
services.AddSingleton<IPasswordHealthReportApplicationRepository, PasswordHealthReportApplicationRepository>();
|
||||||
services.AddSingleton<ISecurityTaskRepository, SecurityTaskRepository>();
|
services.AddSingleton<ISecurityTaskRepository, SecurityTaskRepository>();
|
||||||
services.AddSingleton<IUserAsymmetricKeysRepository, UserAsymmetricKeysRepository>();
|
services.AddSingleton<IUserAsymmetricKeysRepository, UserAsymmetricKeysRepository>();
|
||||||
|
services.AddSingleton<IPhishingDomainRepository, PhishingDomainRepository>();
|
||||||
|
|
||||||
if (selfHosted)
|
if (selfHosted)
|
||||||
{
|
{
|
||||||
|
17
src/Infrastructure.EntityFramework/Models/PhishingDomain.cs
Normal file
17
src/Infrastructure.EntityFramework/Models/PhishingDomain.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Bit.Infrastructure.EntityFramework.Models;
|
||||||
|
|
||||||
|
public class PhishingDomain
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[MaxLength(255)]
|
||||||
|
public string Domain { get; set; }
|
||||||
|
|
||||||
|
public DateTime CreationDate { get; set; }
|
||||||
|
|
||||||
|
public DateTime RevisionDate { get; set; }
|
||||||
|
}
|
@ -80,6 +80,7 @@ public class DatabaseContext : DbContext
|
|||||||
public DbSet<PasswordHealthReportApplication> PasswordHealthReportApplications { get; set; }
|
public DbSet<PasswordHealthReportApplication> PasswordHealthReportApplications { get; set; }
|
||||||
public DbSet<SecurityTask> SecurityTasks { get; set; }
|
public DbSet<SecurityTask> SecurityTasks { get; set; }
|
||||||
public DbSet<OrganizationInstallation> OrganizationInstallations { get; set; }
|
public DbSet<OrganizationInstallation> OrganizationInstallations { get; set; }
|
||||||
|
public DbSet<PhishingDomain> PhishingDomains { get; set; }
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder builder)
|
protected override void OnModelCreating(ModelBuilder builder)
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Infrastructure.EntityFramework.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||||
|
|
||||||
|
public class PhishingDomainRepository : IPhishingDomainRepository
|
||||||
|
{
|
||||||
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||||
|
|
||||||
|
public PhishingDomainRepository(IServiceScopeFactory serviceScopeFactory)
|
||||||
|
{
|
||||||
|
_serviceScopeFactory = serviceScopeFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ICollection<string>> GetActivePhishingDomainsAsync()
|
||||||
|
{
|
||||||
|
using (var scope = _serviceScopeFactory.CreateScope())
|
||||||
|
{
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
var domains = await dbContext.PhishingDomains
|
||||||
|
.Select(d => d.Domain)
|
||||||
|
.ToListAsync();
|
||||||
|
return domains;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdatePhishingDomainsAsync(IEnumerable<string> domains)
|
||||||
|
{
|
||||||
|
using (var scope = _serviceScopeFactory.CreateScope())
|
||||||
|
{
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
|
||||||
|
// Clear existing domains
|
||||||
|
await dbContext.PhishingDomains.ExecuteDeleteAsync();
|
||||||
|
|
||||||
|
// Add new domains
|
||||||
|
var phishingDomains = domains.Select(d => new PhishingDomain
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Domain = d,
|
||||||
|
CreationDate = DateTime.UtcNow,
|
||||||
|
RevisionDate = DateTime.UtcNow
|
||||||
|
});
|
||||||
|
await dbContext.PhishingDomains.AddRangeAsync(phishingDomains);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
src/Sql/dbo/Stored Procedures/PhishingDomain_Create.sql
Normal file
24
src/Sql/dbo/Stored Procedures/PhishingDomain_Create.sql
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_Create]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@Domain NVARCHAR(255),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
INSERT INTO [dbo].[PhishingDomain]
|
||||||
|
(
|
||||||
|
[Id],
|
||||||
|
[Domain],
|
||||||
|
[CreationDate],
|
||||||
|
[RevisionDate]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
@Domain,
|
||||||
|
@CreationDate,
|
||||||
|
@RevisionDate
|
||||||
|
)
|
||||||
|
END
|
@ -0,0 +1,8 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_DeleteAll]
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
DELETE FROM
|
||||||
|
[dbo].[PhishingDomain]
|
||||||
|
END
|
12
src/Sql/dbo/Stored Procedures/PhishingDomain_ReadAll.sql
Normal file
12
src/Sql/dbo/Stored Procedures/PhishingDomain_ReadAll.sql
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_ReadAll]
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
[Domain]
|
||||||
|
FROM
|
||||||
|
[dbo].[PhishingDomain]
|
||||||
|
ORDER BY
|
||||||
|
[Domain] ASC
|
||||||
|
END
|
12
src/Sql/dbo/Tables/PhishingDomain.sql
Normal file
12
src/Sql/dbo/Tables/PhishingDomain.sql
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
CREATE TABLE [dbo].[PhishingDomain] (
|
||||||
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
|
[Domain] NVARCHAR(255) NOT NULL,
|
||||||
|
[CreationDate] DATETIME2(7) NOT NULL,
|
||||||
|
[RevisionDate] DATETIME2(7) NOT NULL,
|
||||||
|
CONSTRAINT [PK_PhishingDomain] PRIMARY KEY CLUSTERED ([Id] ASC)
|
||||||
|
);
|
||||||
|
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE NONCLUSTERED INDEX [IX_PhishingDomain_Domain]
|
||||||
|
ON [dbo].[PhishingDomain]([Domain] ASC);
|
86
util/Migrator/DbScripts/2024-03-19_00_PhishingDomain.sql
Normal file
86
util/Migrator/DbScripts/2024-03-19_00_PhishingDomain.sql
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
-- Create PhishingDomain table
|
||||||
|
IF OBJECT_ID('[dbo].[PhishingDomain]') IS NULL
|
||||||
|
BEGIN
|
||||||
|
CREATE TABLE [dbo].[PhishingDomain] (
|
||||||
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
|
[Domain] NVARCHAR(255) NOT NULL,
|
||||||
|
[CreationDate] DATETIME2(7) NOT NULL,
|
||||||
|
[RevisionDate] DATETIME2(7) NOT NULL,
|
||||||
|
CONSTRAINT [PK_PhishingDomain] PRIMARY KEY CLUSTERED ([Id] ASC)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE NONCLUSTERED INDEX [IX_PhishingDomain_Domain]
|
||||||
|
ON [dbo].[PhishingDomain]([Domain] ASC);
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Create PhishingDomain_ReadAll stored procedure
|
||||||
|
IF OBJECT_ID('[dbo].[PhishingDomain_ReadAll]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[PhishingDomain_ReadAll]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_ReadAll]
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
[Domain]
|
||||||
|
FROM
|
||||||
|
[dbo].[PhishingDomain]
|
||||||
|
ORDER BY
|
||||||
|
[Domain] ASC
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Create PhishingDomain_DeleteAll stored procedure
|
||||||
|
IF OBJECT_ID('[dbo].[PhishingDomain_DeleteAll]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[PhishingDomain_DeleteAll]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_DeleteAll]
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
DELETE FROM
|
||||||
|
[dbo].[PhishingDomain]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Create PhishingDomain_Create stored procedure
|
||||||
|
IF OBJECT_ID('[dbo].[PhishingDomain_Create]') IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
DROP PROCEDURE [dbo].[PhishingDomain_Create]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE PROCEDURE [dbo].[PhishingDomain_Create]
|
||||||
|
@Id UNIQUEIDENTIFIER,
|
||||||
|
@Domain NVARCHAR(255),
|
||||||
|
@CreationDate DATETIME2(7),
|
||||||
|
@RevisionDate DATETIME2(7)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
INSERT INTO [dbo].[PhishingDomain]
|
||||||
|
(
|
||||||
|
[Id],
|
||||||
|
[Domain],
|
||||||
|
[CreationDate],
|
||||||
|
[RevisionDate]
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
@Id,
|
||||||
|
@Domain,
|
||||||
|
@CreationDate,
|
||||||
|
@RevisionDate
|
||||||
|
)
|
||||||
|
END
|
||||||
|
GO
|
Loading…
x
Reference in New Issue
Block a user