Skip to content

Commit

Permalink
ReadyForPublish should be set after virus scan (#212)
Browse files Browse the repository at this point in the history
* We should do the check for related correspondences related to an attachment being ready for publish after the virus scan when running remotely.

* We can avoid having to inject so many dependencies in application handler by also injecting the helpers through DI and having their dependencies resolved automatically.

* Correspondences with malware attachments should fail

* Fix injection
  • Loading branch information
Ceredron authored Aug 19, 2024
1 parent 530c8b3 commit c1384a5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 64 deletions.
4 changes: 4 additions & 0 deletions src/Altinn.Correspondence.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Altinn.Correspondence.Application.GetCorrespondenceDetails;
using Altinn.Correspondence.Application.GetCorrespondenceOverview;
using Altinn.Correspondence.Application.GetCorrespondences;
using Altinn.Correspondence.Application.Helpers;
using Altinn.Correspondence.Application.InitializeAttachment;
using Altinn.Correspondence.Application.InitializeCorrespondence;
using Altinn.Correspondence.Application.InitializeMultipleCorrespondences;
Expand Down Expand Up @@ -35,5 +36,8 @@ public static void AddApplicationHandlers(this IServiceCollection services)
services.AddScoped<MalwareScanResultHandler>();
services.AddScoped<PurgeCorrespondenceHandler>();
services.AddScoped<UpdateMarkAsUnreadHandler>();

services.AddScoped<InitializeCorrespondenceHelper>();
services.AddScoped<UploadHelper>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ namespace Altinn.Correspondence.Application.Helpers
{
public class InitializeCorrespondenceHelper
{
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly ICorrespondenceStatusRepository _correspondenceStatusRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository;
private readonly IAttachmentRepository _attachmentRepository;
private readonly IStorageRepository _storageRepository;
private readonly IHostEnvironment _hostEnvironment;
private readonly UploadHelper _uploadHelper;

public InitializeCorrespondenceHelper(ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepositor, IAttachmentStatusRepository attachmentStatusRepository, IAttachmentRepository attachmentRepository, IStorageRepository storageRepository, IHostEnvironment hostEnvironment)
public InitializeCorrespondenceHelper(IAttachmentRepository attachmentRepository, IHostEnvironment hostEnvironment, UploadHelper uploadHelper)
{
_correspondenceRepository = correspondenceRepository;
_correspondenceStatusRepository = correspondenceStatusRepositor;
_attachmentStatusRepository = attachmentStatusRepository;
_attachmentRepository = attachmentRepository;
_hostEnvironment = hostEnvironment;
_storageRepository = storageRepository;
_uploadHelper = uploadHelper;

}
public Error? ValidateCorrespondenceContent(CorrespondenceContentEntity content)
Expand Down Expand Up @@ -93,7 +87,6 @@ public CorrespondenceStatus GetInitializeCorrespondenceStatus(CorrespondenceEnti

public async Task<Error?> UploadAttachments(List<AttachmentEntity> correspondenceAttachments, List<IFormFile> files, CancellationToken cancellationToken)
{
UploadHelper uploadHelper = new UploadHelper(_correspondenceRepository, _correspondenceStatusRepository, _attachmentStatusRepository, _attachmentRepository, _storageRepository, _hostEnvironment);
foreach (var file in files)
{
var attachment = correspondenceAttachments.FirstOrDefault(a => a.FileName.ToLower() == file.FileName.ToLower());
Expand All @@ -105,7 +98,7 @@ public CorrespondenceStatus GetInitializeCorrespondenceStatus(CorrespondenceEnti
OneOf<UploadAttachmentResponse, Error> uploadResponse;
await using (var f = file.OpenReadStream())
{
uploadResponse = await uploadHelper.UploadAttachment(f, attachment.Id, cancellationToken);
uploadResponse = await _uploadHelper.UploadAttachment(f, attachment.Id, cancellationToken);
}
var error = uploadResponse.Match(
_ => { return null; },
Expand All @@ -116,9 +109,9 @@ public CorrespondenceStatus GetInitializeCorrespondenceStatus(CorrespondenceEnti
return null;
}

public async Task<AttachmentEntity> ProcessAttachment(CorrespondenceAttachmentEntity correspondenceAttachment, bool shouldSave, CancellationToken cancellationToken)
public async Task<AttachmentEntity> ProcessAttachment(CorrespondenceAttachmentEntity correspondenceAttachment, CancellationToken cancellationToken)
{
if (!String.IsNullOrEmpty(correspondenceAttachment.Attachment?.DataLocationUrl))
if (!string.IsNullOrEmpty(correspondenceAttachment.Attachment?.DataLocationUrl))
{
var existingAttachment = await _attachmentRepository.GetAttachmentByUrl(correspondenceAttachment.Attachment.DataLocationUrl, cancellationToken);
if (existingAttachment != null)
Expand Down
35 changes: 25 additions & 10 deletions src/Altinn.Correspondence.Application/Helpers/UploadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public async Task<OneOf<UploadAttachmentResponse, Error>> UploadAttachment(Strea
var attachment = await _attachmentRepository.GetAttachmentById(attachmentId, true, cancellationToken);
if (attachment == null)
{
return Errors.AttachmentNotFound;
}

var currentStatus = await SetAttachmentStatus(attachmentId, AttachmentStatus.UploadProcessing, cancellationToken);
Expand Down Expand Up @@ -96,7 +97,7 @@ private async Task<AttachmentStatusEntity> SetAttachmentStatus(Guid attachmentId
await _attachmentStatusRepository.AddAttachmentStatus(currentStatus, cancellationToken);
return currentStatus;
}
public async Task CheckCorrespondenceStatusesAfterUploadAndPublish(Guid attachmentId, CancellationToken cancellationToken)
public async Task CheckCorrespondenceStatusesAfterUploadAndPublish(Guid attachmentId, bool uploadSuccessful, CancellationToken cancellationToken)
{
var attachment = await _attachmentRepository.GetAttachmentById(attachmentId, true, cancellationToken);
if (attachment == null)
Expand All @@ -113,15 +114,29 @@ public async Task CheckCorrespondenceStatusesAfterUploadAndPublish(Guid attachme
var list = new List<CorrespondenceStatusEntity>();
foreach (var correspondenceId in correspondences)
{
list.Add(
new CorrespondenceStatusEntity
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.ReadyForPublish,
StatusChanged = DateTime.UtcNow,
StatusText = CorrespondenceStatus.ReadyForPublish.ToString()
}
);
if (uploadSuccessful)
{
list.Add(
new CorrespondenceStatusEntity
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.ReadyForPublish,
StatusChanged = DateTime.UtcNow,
StatusText = CorrespondenceStatus.ReadyForPublish.ToString()
}
);
} else
{
list.Add(
new CorrespondenceStatusEntity
{
CorrespondenceId = correspondenceId,
Status = CorrespondenceStatus.Failed,
StatusChanged = DateTime.UtcNow,
StatusText = "Malware scan failed"
}
);
}
}
await _correspondenceStatusRepository.AddCorrespondenceStatuses(list, cancellationToken);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ public class InitializeCorrespondenceHandler : IHandler<InitializeCorrespondence
{
private readonly IAltinnAuthorizationService _altinnAuthorizationService;
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly ICorrespondenceStatusRepository _correspondenceStatusRepository;
private readonly IAttachmentRepository _attachmentRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository;
private readonly IEventBus _eventBus;
private readonly IStorageRepository _storageRepository;
private readonly IHostEnvironment _hostEnvironment;
IBackgroundJobClient _backgroundJobClient;
private readonly InitializeCorrespondenceHelper _initializeCorrespondenceHelper;
private readonly IBackgroundJobClient _backgroundJobClient;

public InitializeCorrespondenceHandler(IAltinnAuthorizationService altinnAuthorizationService, ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository, IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IStorageRepository storageRepository, IHostEnvironment hostEnvironment, IEventBus eventBus, IBackgroundJobClient backgroundJobClient)
public InitializeCorrespondenceHandler(InitializeCorrespondenceHelper initializeCorrespondenceHelper, IAltinnAuthorizationService altinnAuthorizationService, ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository, IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IStorageRepository storageRepository, IHostEnvironment hostEnvironment, IEventBus eventBus, IBackgroundJobClient backgroundJobClient)
{
_initializeCorrespondenceHelper = initializeCorrespondenceHelper;
_altinnAuthorizationService = altinnAuthorizationService;
_correspondenceRepository = correspondenceRepository;
_correspondenceStatusRepository = correspondenceStatusRepository;
_attachmentRepository = attachmentRepository;
_attachmentStatusRepository = attachmentStatusRepository;
_eventBus = eventBus;
_backgroundJobClient = backgroundJobClient;
_storageRepository = storageRepository;
_hostEnvironment = hostEnvironment;
}

public async Task<OneOf<InitializeCorrespondenceResponse, Error>> Process(InitializeCorrespondenceRequest request, CancellationToken cancellationToken)
Expand All @@ -48,32 +40,31 @@ public async Task<OneOf<InitializeCorrespondenceResponse, Error>> Process(Initia
{
return Errors.NoAttachments;
}
InitializeCorrespondenceHelper initializeCorrespondenceHelper = new InitializeCorrespondenceHelper(_correspondenceRepository, _correspondenceStatusRepository, _attachmentStatusRepository, _attachmentRepository, _storageRepository, _hostEnvironment);
var contentError = initializeCorrespondenceHelper.ValidateCorrespondenceContent(request.Correspondence.Content);
var contentError = _initializeCorrespondenceHelper.ValidateCorrespondenceContent(request.Correspondence.Content);
if (contentError != null)
{
return contentError;
}
var attachmentError = initializeCorrespondenceHelper.ValidateAttachmentFiles(request.Attachments, request.Correspondence.Content!.Attachments, false);
var attachmentError = _initializeCorrespondenceHelper.ValidateAttachmentFiles(request.Attachments, request.Correspondence.Content!.Attachments, false);
if (attachmentError != null) return attachmentError;
var attachments = new List<AttachmentEntity>();
if (request.Correspondence.Content!.Attachments.Count > 0)
{
foreach (var attachment in request.Correspondence.Content!.Attachments)
{
var a = await initializeCorrespondenceHelper.ProcessAttachment(attachment, true, cancellationToken);
var a = await _initializeCorrespondenceHelper.ProcessAttachment(attachment, cancellationToken);
attachments.Add(a);
}
}
if (request.Attachments.Count > 0)
{
var uploadError = await initializeCorrespondenceHelper.UploadAttachments(attachments, request.Attachments, cancellationToken);
var uploadError = await _initializeCorrespondenceHelper.UploadAttachments(attachments, request.Attachments, cancellationToken);
if (uploadError != null)
{
return uploadError;
}
}
var status = initializeCorrespondenceHelper.GetInitializeCorrespondenceStatus(request.Correspondence);
var status = _initializeCorrespondenceHelper.GetInitializeCorrespondenceStatus(request.Correspondence);
var statuses = new List<CorrespondenceStatusEntity>(){
new CorrespondenceStatusEntity
{
Expand All @@ -93,7 +84,7 @@ public async Task<OneOf<InitializeCorrespondenceResponse, Error>> Process(Initia
}

request.Correspondence.Statuses = statuses;
request.Correspondence.Notifications = initializeCorrespondenceHelper.ProcessNotifications(request.Correspondence.Notifications, cancellationToken);
request.Correspondence.Notifications = _initializeCorrespondenceHelper.ProcessNotifications(request.Correspondence.Notifications, cancellationToken);
var correspondence = await _correspondenceRepository.CreateCorrespondence(request.Correspondence, cancellationToken);
_backgroundJobClient.Schedule<PublishCorrespondenceService>((service) => service.Publish(correspondence.Id, cancellationToken), correspondence.VisibleFrom);
await _eventBus.Publish(AltinnEventType.CorrespondenceInitialized, correspondence.ResourceId, correspondence.Id.ToString(), "correspondence", correspondence.Sender, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ public class InitializeMultipleCorrespondencesHandler : IHandler<InitializeMulti
{
private readonly IAltinnAuthorizationService _altinnAuthorizationService;
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly ICorrespondenceStatusRepository _correspondenceStatusRepository;
private readonly IAttachmentRepository _attachmentRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository;
private readonly IEventBus _eventBus;
private readonly IStorageRepository _storageRepository;
private readonly IHostEnvironment _hostEnvironment;
IBackgroundJobClient _backgroundJobClient;
private readonly InitializeCorrespondenceHelper _initializeCorrespondenceHelper;
private readonly IBackgroundJobClient _backgroundJobClient;

public InitializeMultipleCorrespondencesHandler(IAltinnAuthorizationService altinnAuthorizationService, ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository, IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IStorageRepository storageRepository, IHostEnvironment hostEnvironment, IEventBus eventBus, IBackgroundJobClient backgroundJobClient)
public InitializeMultipleCorrespondencesHandler(InitializeCorrespondenceHelper initializeCorrespondenceHelper, IAltinnAuthorizationService altinnAuthorizationService, ICorrespondenceRepository correspondenceRepository, IEventBus eventBus, IBackgroundJobClient backgroundJobClient)
{
_initializeCorrespondenceHelper = initializeCorrespondenceHelper;
_altinnAuthorizationService = altinnAuthorizationService;
_correspondenceRepository = correspondenceRepository;
_correspondenceStatusRepository = correspondenceStatusRepository;
_attachmentRepository = attachmentRepository;
_attachmentStatusRepository = attachmentStatusRepository;
_eventBus = eventBus;
_backgroundJobClient = backgroundJobClient;
_storageRepository = storageRepository;
_hostEnvironment = hostEnvironment;
}

public async Task<OneOf<InitializeMultipleCorrespondencesResponse, Error>> Process(InitializeMultipleCorrespondencesRequest request, CancellationToken cancellationToken)
Expand All @@ -52,33 +44,32 @@ public async Task<OneOf<InitializeMultipleCorrespondencesResponse, Error>> Proce
{
return Errors.DuplicateRecipients;
}
InitializeCorrespondenceHelper initializeCorrespondenceHelper = new InitializeCorrespondenceHelper(_correspondenceRepository, _correspondenceStatusRepository, _attachmentStatusRepository, _attachmentRepository, _storageRepository, _hostEnvironment);
var contentError = initializeCorrespondenceHelper.ValidateCorrespondenceContent(request.Correspondence.Content);
var contentError = _initializeCorrespondenceHelper.ValidateCorrespondenceContent(request.Correspondence.Content);
if (contentError != null)
{
return contentError;
}

var attachmentError = initializeCorrespondenceHelper.ValidateAttachmentFiles(request.Attachments, request.Correspondence.Content!.Attachments, true);
var attachmentError = _initializeCorrespondenceHelper.ValidateAttachmentFiles(request.Attachments, request.Correspondence.Content!.Attachments, true);
if (attachmentError != null) return attachmentError;
var attachments = new List<AttachmentEntity>();
if (request.Correspondence.Content!.Attachments.Count() > 0)
{
foreach (var attachment in request.Correspondence.Content!.Attachments)
{
var a = await initializeCorrespondenceHelper.ProcessAttachment(attachment, true, cancellationToken);
var a = await _initializeCorrespondenceHelper.ProcessAttachment(attachment, cancellationToken);
attachments.Add(a);
}
}
if (request.Attachments.Count > 0)
{
var uploadError = await initializeCorrespondenceHelper.UploadAttachments(attachments, request.Attachments, cancellationToken);
var uploadError = await _initializeCorrespondenceHelper.UploadAttachments(attachments, request.Attachments, cancellationToken);
if (uploadError != null)
{
return uploadError;
}
}
var status = initializeCorrespondenceHelper.GetInitializeCorrespondenceStatus(request.Correspondence);
var status = _initializeCorrespondenceHelper.GetInitializeCorrespondenceStatus(request.Correspondence);
var correspondences = new List<CorrespondenceEntity>();
foreach (var recipient in request.Recipients)
{
Expand Down Expand Up @@ -108,7 +99,7 @@ public async Task<OneOf<InitializeMultipleCorrespondencesResponse, Error>> Proce
PropertyList = request.Correspondence.PropertyList.ToDictionary(x => x.Key, x => x.Value),
ReplyOptions = request.Correspondence.ReplyOptions,
IsReservable = request.Correspondence.IsReservable,
Notifications = initializeCorrespondenceHelper.ProcessNotifications(request.Correspondence.Notifications, cancellationToken),
Notifications = _initializeCorrespondenceHelper.ProcessNotifications(request.Correspondence.Notifications, cancellationToken),
Statuses = new List<CorrespondenceStatusEntity>(){
new CorrespondenceStatusEntity
{
Expand Down
Loading

0 comments on commit c1384a5

Please sign in to comment.