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

SM-503: Add EmptySecretsManagerTrashJob (#2863)

* SM-503: Add EmptySecretsManagerJob

* SM-503: Fix date logic and refactor a few lines

* SM-503: Add logging

* SM-503: Move EmptySecretsManagerTrashJob to src/Api/SecretsManager/Jobs

* SM-503: Update trigger time for EmptySecretsManagerTrashJob

* SM-503: Switch to scope on one line

* SM-768: Update EFCore and related packages to >= 7.0

* SM-768: Update more packages for the EF 7 upgrade

* SM-768: Update the PostgreSQL package

* SM-768: Run dotnet restore --force-evaluate

* SM-768: Revert package upgrades for 3 projects

* SM-768: Update the dotnet-ef tool

* SM-503: Switch to using ExecuteDeleteAsync and fix param name

* SM-503: Rename trigger to smTrashCleanupTrigger

* SM-503: Fix OSS job issue

* SM-503: Only add trigger if not OSS for SM Trash Job
This commit is contained in:
Colton Hurst 2023-07-18 15:32:47 -04:00 committed by GitHub
parent a095e02e86
commit 1fe2f0fb57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 0 deletions

View File

@ -293,6 +293,16 @@ public class SecretRepository : Repository<Core.SecretsManager.Entities.Secret,
return policy == null ? (false, false) : (policy.Read, policy.Write);
}
public async Task EmptyTrash(DateTime currentDate, uint deleteAfterThisNumberOfDays)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
await dbContext.Secret.Where(s => s.DeletedDate != null && s.DeletedDate < currentDate.AddDays(-deleteAfterThisNumberOfDays)).ExecuteDeleteAsync();
await dbContext.SaveChangesAsync();
}
private IQueryable<SecretPermissionDetails> SecretToPermissionDetails(IQueryable<Secret> query, Guid userId, AccessClientType accessType)
{
var secrets = accessType switch

View File

@ -41,6 +41,11 @@ public class JobsHostedService : BaseJobsHostedService
.StartNow()
.WithCronSchedule("0 30 */12 * * ?")
.Build();
var smTrashCleanupTrigger = TriggerBuilder.Create()
.WithIdentity("SMTrashCleanupTrigger")
.StartNow()
.WithCronSchedule("0 0 22 * * ?")
.Build();
var randomDailySponsorshipSyncTrigger = TriggerBuilder.Create()
.WithIdentity("RandomDailySponsorshipSyncTrigger")
.StartAt(DateBuilder.FutureDate(new Random().Next(24), IntervalUnit.Hour))
@ -70,6 +75,10 @@ public class JobsHostedService : BaseJobsHostedService
jobs.Add(new Tuple<Type, ITrigger>(typeof(SelfHostedSponsorshipSyncJob), randomDailySponsorshipSyncTrigger));
}
#if !OSS
jobs.Add(new Tuple<Type, ITrigger>(typeof(EmptySecretsManagerTrashJob), smTrashCleanupTrigger));
#endif
Jobs = jobs;
await base.StartAsync(cancellationToken);
@ -88,4 +97,9 @@ public class JobsHostedService : BaseJobsHostedService
services.AddTransient<ValidateOrganizationsJob>();
services.AddTransient<ValidateOrganizationDomainJob>();
}
public static void AddCommercialSecretsManagerJobServices(IServiceCollection services)
{
services.AddTransient<EmptySecretsManagerTrashJob>();
}
}

View File

@ -0,0 +1,23 @@
using Bit.Core.Jobs;
using Bit.Core.SecretsManager.Repositories;
using Quartz;
namespace Bit.Api.Jobs;
public class EmptySecretsManagerTrashJob : BaseJob
{
private ISecretRepository _secretRepository;
private const uint DeleteAfterThisNumberOfDays = 30;
public EmptySecretsManagerTrashJob(ISecretRepository secretRepository, ILogger<EmptySecretsManagerTrashJob> logger) : base(logger)
{
_secretRepository = secretRepository;
}
protected override async Task ExecuteJobAsync(IJobExecutionContext context)
{
_logger.LogInformation("Execute job task: EmptySecretsManagerTrashJob: Start");
await _secretRepository.EmptyTrash(DateTime.UtcNow, DeleteAfterThisNumberOfDays);
_logger.LogInformation("Execute job task: EmptySecretsManagerTrashJob: End");
}
}

View File

@ -147,6 +147,7 @@ public class Startup
services.AddCommercialCoreServices();
services.AddCommercialSecretsManagerServices();
services.AddSecretsManagerEfRepositories();
Jobs.JobsHostedService.AddCommercialSecretsManagerJobServices(services);
#endif
// MVC

View File

@ -20,4 +20,5 @@ public interface ISecretRepository
Task<IEnumerable<Secret>> ImportAsync(IEnumerable<Secret> secrets);
Task UpdateRevisionDates(IEnumerable<Guid> ids);
Task<(bool Read, bool Write)> AccessToSecretAsync(Guid id, Guid userId, AccessClientType accessType);
Task EmptyTrash(DateTime nowTime, uint deleteAfterThisNumberOfDays);
}