Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/change file retention #340

Merged
merged 8 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Test/Altinn.Broker.Tests/FileTransferControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json;

using Altinn.Broker.Application;
using Altinn.Broker.Application.ExpireFileTransferCommand;
using Altinn.Broker.Core.Models;
using Altinn.Broker.Enums;
using Altinn.Broker.Models;
Expand Down Expand Up @@ -87,7 +88,7 @@ public async Task NormalFlow_WhenAllIsOK_Success()

// Confirm that it has been enqueued for deletion
_factory.HangfireBackgroundJobClient?.Verify(jobClient => jobClient.Create(
It.Is<Job>(job => (job.Method.DeclaringType != null) && job.Method.DeclaringType.Name == "ExpireFileTransferCommandHandler" && ((Guid)job.Args[0] == Guid.Parse(fileTransferId))),
It.Is<Job>(job => (job.Method.DeclaringType != null) && job.Method.DeclaringType.Name == "ExpireFileTransferCommandHandler" && (((ExpireFileTransferCommandRequest)job.Args[0]).FileTransferId == Guid.Parse(fileTransferId))),
It.IsAny<EnqueuedState>()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Altinn.Broker.Tests.Helpers;

using Hangfire;
using Hangfire.Common;
using Hangfire.MemoryStorage;
using Hangfire.States;

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -80,6 +82,7 @@ protected override void ConfigureWebHost(
config.UseMemoryStorage()
);
HangfireBackgroundJobClient = new Mock<IBackgroundJobClient>();
HangfireBackgroundJobClient.Setup(x => x.Create(It.IsAny<Job>(), It.IsAny<IState>())).Returns("1");
services.AddSingleton(HangfireBackgroundJobClient.Object);
HangfireRecurringJobClient = new Mock<IRecurringJobManager>();
services.AddSingleton(HangfireRecurringJobClient.Object);
Expand Down
1 change: 1 addition & 0 deletions Test/Altinn.Broker.Tests/Helpers/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ internal class TestConstants
internal const string DUMMY_LEGACY_TOKEN = "eyJraWQiOiJiZFhMRVduRGpMSGpwRThPZnl5TUp4UlJLbVo3MUxCOHUxeUREbVBpdVQwIiwiYWxnIjoiUlMyNTYifQ.eyJzY29wZSI6ImFsdGlubjpicm9rZXIubGVnYWN5IiwiaXNzIjoiaHR0cHM6Ly90ZXN0Lm1hc2tpbnBvcnRlbi5uby8iLCJjbGllbnRfYW1yIjoidmlya3NvbWhldHNzZXJ0aWZpa2F0IiwidG9rZW5fdHlwZSI6IkJlYXJlciIsImV4cCI6MTcwODU5Nzg3OCwiaWF0IjoxNzA4NTk0Mjc4LCJjbGllbnRfaWQiOiI2NzFjNTE2Zi03YWIwLTRhMTUtOTU1Yi1hODJkMTg2Y2VjYzAiLCJqdGkiOiJSTU90Mmk5NkxWZ3d4Z29NaWtXTnFNeDI0TndNZlBHc082Tl9YQjBQZzZ3IiwiY29uc3VtZXIiOnsiYXV0aG9yaXR5IjoiaXNvNjUyMy1hY3RvcmlkLXVwaXMiLCJJRCI6IjAxOTI6OTkxODI1ODI3In19.G268zp-aLUvmR1aTkkaMsZ9j6FT9FmvqKfTFOSP277F8X4BX5kLkm5v7G1MTgDybG0CUXxNGsyhMMlsGQscOZIsOe6QW05aoBFa1vWGOCsTLBaRbBm-LEU41dEPYqKzsDCh61p-zvINdNswuc5CG5vOwkKZi_PBbYUCEF6wIwe3eJ8ttNmunmEjBvOQcSIRllo-unIbzm4nsSQADnXRDAgeJ_jdl8k2s2N_Ose7qIE-usoVlKY53Ayax-V3ws8L22YxKHEbYnhx3oswfKg-ux2PrNFFFWfarUlpVnj1CFqY11ZlxXOS7sDRcwgc1gSnpTZWgysxAU0mGCoV03KwkYOMkVJp4UXkxL6WZ25RqTVb2YsIVq7g6m5BbAPJmZW-_OnpP6KZYQ8fCILYo6EIdn9TEot5Ffm8RzjlbXNseMS10oPCmQswe18TnzKaqFk2U6hOVhhakCvKxSsN0yDj9tsZitP_MOZPZ9ybVmK_jYrYNViJ02PLqnF5n3DMqcXDT";

internal const string RESOURCE_WITH_NO_ACCESS = "altinn-broker-test-resource-1-failed-access";
internal const string DUMMY_SERVICE_OWNER_TOKEN = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjM4QUE3QTc5MjUzNDNCQjE0NjFCRUUwMURCNUQwOTRBM0VCOTgwMjUiLCJ0eXAiOiJKV1QiLCJ4NWMiOiIzOEFBN0E3OTI1MzQzQkIxNDYxQkVFMDFEQjVEMDk0QTNFQjk4MDI1In0.eyJzY29wZSI6ImFsdGlubjpicm9rZXIuYWRtaW4gYWx0aW5uOnJlc291cmNlcmVnaXN0cnkvcmVzb3VyY2Uud3JpdGUgYWx0aW5uOmF1dGhvcml6YXRpb246cGRwIiwidG9rZW5fdHlwZSI6IkJlYXJlciIsImV4cCI6MTcwOTEzMDc2OCwiaWF0IjoxNzA5MTI4OTY4LCJjbGllbnRfaWQiOiI3MjJhOTYyZS1hNDVlLTQ3NGMtYjgwNi05M2NhOTQ1YWYyZDIiLCJqdGkiOiJqSGF4a0VJa3FmUjRaNDFEc05TZ2FjaXJvbW9oN0J0a0lHdWlHR2RKWGNxIiwiY29uc3VtZXIiOnsiYXV0aG9yaXR5IjoiaXNvNjUyMy1hY3RvcmlkLXVwaXMiLCJJRCI6IjAxOTI6OTkxODI1ODI3In0sInVybjphbHRpbm46b3JnTnVtYmVyIjoiOTkxODI1ODI3IiwidXJuOmFsdGlubjphdXRoZW50aWNhdGVtZXRob2QiOiJtYXNraW5wb3J0ZW4iLCJ1cm46YWx0aW5uOmF1dGhsZXZlbCI6MywiaXNzIjoiaHR0cHM6Ly9wbGF0Zm9ybS50dDAyLmFsdGlubi5uby9hdXRoZW50aWNhdGlvbi9hcGkvdjEvb3BlbmlkLyIsImFjdHVhbF9pc3MiOiJhbHRpbm4tdGVzdC10b29scyIsIm5iZiI6MTcwOTEyODk2OCwidXJuOmFsdGlubjpvcmciOiJkaWdkaXIifQ.sDV1h6Vfu154z2JBEenJFtoT - 6vJol3-4GHGlGnGYr2PHZnZNNyW-t9aE0yS-KOHpUXylfSV8l8JQE_3CPwYfIfHX4Sx_C6ku29VaQ4rLVr-2UFSKdk1qrUwUsMfeS9GKYMEtvtPzF3u9kYBnqKtkO2_cNpG1jJzdrGKdgwy36APqmeUZo1c0oUcj2fShvS_vQwxizJSGwGxP4GZ8VVGGIfX2CGkVyIT4o_dECImutfMKq0RTQWxL1PKTpnaIy8gG7sbnMMqE0w2WwgJzJpqmq9wA1Ccw48MfUQRoFURjMR2lxJnVxJaoMGxoPowQQMdKlL7OSqxbh8H2e1CjJSTMw";
}
67 changes: 67 additions & 0 deletions Test/Altinn.Broker.Tests/ServiceOwnerControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Xml;

using Altinn.Broker.Application;
using Altinn.Broker.Core.Models;
using Altinn.Broker.Enums;
using Altinn.Broker.Models;
using Altinn.Broker.Models.ServiceOwner;
using Altinn.Broker.Tests.Factories;
using Altinn.Broker.Tests.Helpers;

using Hangfire.Common;
using Hangfire.States;

using Microsoft.AspNetCore.Mvc;

using Moq;

using Xunit;

namespace Altinn.Broker.Tests;
public class ServiceOwnerControllerTests : IClassFixture<CustomWebApplicationFactory>
{
private readonly CustomWebApplicationFactory _factory;
private readonly HttpClient _serviceOwnerClient;
private readonly JsonSerializerOptions _responseSerializerOptions;

public ServiceOwnerControllerTests(CustomWebApplicationFactory factory)
{
_factory = factory;
_serviceOwnerClient = _factory.CreateClientWithAuthorization(TestConstants.DUMMY_SERVICE_OWNER_TOKEN);
_responseSerializerOptions = new JsonSerializerOptions(new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});
_responseSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
}

[Fact]
public async Task Get_ServiceOwner()
{
var response = await _serviceOwnerClient.GetFromJsonAsync<ServiceOwnerOverviewExt>($"broker/api/v1/serviceowner", _responseSerializerOptions);
Assert.Equal("Digitaliseringsdirektoratet Avd Oslo", response.Name);
}

[Fact]
public async Task Update_FileRetention_For_ServiceOwner()
{

var serviceOwnerUpdateFileRetentionExt = new ServiceOwnerUpdateFileRetentionExt
{
FileTransferTimeToLive = XmlConvert.ToString(TimeSpan.FromDays(90))
};

var retentionResponse = await _serviceOwnerClient.PutAsJsonAsync($"broker/api/v1/serviceowner/fileretention", serviceOwnerUpdateFileRetentionExt);
Assert.Equal(System.Net.HttpStatusCode.OK, retentionResponse.StatusCode);
var response = await _serviceOwnerClient.GetFromJsonAsync<ServiceOwnerOverviewExt>($"broker/api/v1/serviceowner", _responseSerializerOptions);
Assert.Equal(TimeSpan.FromDays(90), response.FileTransferTimeToLive);
}


}
166 changes: 165 additions & 1 deletion altinn3-broker-postman-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@
]
},
{
"name": "Register Service Owner",
"name": "Service Owner",
"item": [
{
"name": "Register Service Owner",
Expand Down Expand Up @@ -841,6 +841,170 @@
}
},
"response": []
},
{
"name": "Get Service Owner",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{serviceowner_token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": "",
"url": {
"raw": "{{baseUrl}}/broker/api/v1/serviceowner",
"host": [
"{{baseUrl}}"
],
"path": [
"broker",
"api",
"v1",
"serviceowner"
]
}
},
"response": [
{
"name": "Success",
"originalRequest": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "text/plain"
}
],
"body": "",
"url": {
"raw": "{{baseUrl}}/broker/api/v1/serviceowner",
"host": [
"{{baseUrl}}"
],
"path": [
"broker",
"api",
"v1",
"serviceowner"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [],
"body": "{\n \"Name\": \"string\",\n \"DeploymentStatus\": \"<int>\",\n \"FileTransferTimeToLive\": \"<string>\"\n}"
}
]
},
{
"name": "Update file retention",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{serviceowner_token}}",
"type": "string"
}
]
},
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"fileTransferTimeToLive\": \"P90D\"\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/broker/api/v1/serviceowner/fileretention",
"host": [
"{{baseUrl}}"
],
"path": [
"broker",
"api",
"v1",
"serviceowner",
"fileretention"
]
}
},
"response": [
{
"name": "Success",
"originalRequest": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "text/plain"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"fileTransferTimeToLive\": \"P90D\"\n}",
"options": {
"raw": {
"headerFamily": "json",
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/broker/api/v1/serviceowner/fileretention",
"host": [
"{{baseUrl}}"
],
"path": [
"broker",
"api",
"v1",
"serviceowner",
"fileretention"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [],
"body": ""
}
]
}
]
},
Expand Down
32 changes: 26 additions & 6 deletions src/Altinn.Broker.API/Controllers/ServiceOwnerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Xml;

using Altinn.Broker.API.Configuration;
using Altinn.Broker.Application.UpdateFileRetention;
using Altinn.Broker.Core.Domain;
using Altinn.Broker.Core.Repositories;
using Altinn.Broker.Core.Services;
Expand Down Expand Up @@ -40,8 +41,8 @@ public async Task<ActionResult> InitializeServiceOwner([FromBody] ServiceOwnerIn
return Problem(detail: "Service owner already exists", statusCode: (int)HttpStatusCode.Conflict);
}

var fileTimeToLive = XmlConvert.ToTimeSpan(serviceOwnerInitializeExt.DeletionTime);
await _serviceOwnerRepository.InitializeServiceOwner(token.Consumer, serviceOwnerInitializeExt.Name, fileTimeToLive);
var fileTransferTimeToLive = XmlConvert.ToTimeSpan(serviceOwnerInitializeExt.DeletionTime);
await _serviceOwnerRepository.InitializeServiceOwner(token.Consumer, serviceOwnerInitializeExt.Name, fileTransferTimeToLive);
var serviceOwner = await _serviceOwnerRepository.GetServiceOwner(token.Consumer);
BackgroundJob.Enqueue(
() => _resourceManager.Deploy(serviceOwner!, cancellationToken)
Expand All @@ -51,10 +52,9 @@ public async Task<ActionResult> InitializeServiceOwner([FromBody] ServiceOwnerIn
}

[HttpGet]
[Route("{serviceOwnerId}")]
public async Task<ActionResult<ServiceOwnerOverviewExt>> GetServiceOwner(string serviceOwnerId, CancellationToken cancellationToken)
public async Task<ActionResult<ServiceOwnerOverviewExt>> GetServiceOwner([ModelBinder(typeof(MaskinportenModelBinder))] CallerIdentity token, CancellationToken cancellationToken)
{
var serviceOwner = await _serviceOwnerRepository.GetServiceOwner(serviceOwnerId);
var serviceOwner = await _serviceOwnerRepository.GetServiceOwner(token.Consumer);
if (serviceOwner is null)
{
return NotFound();
Expand All @@ -66,8 +66,28 @@ public async Task<ActionResult<ServiceOwnerOverviewExt>> GetServiceOwner(string
{
Name = serviceOwner.Name,
DeploymentStatus = (DeploymentStatusExt)deploymentStatus,
FileTimeToLive = serviceOwner.FileTransferTimeToLive
FileTransferTimeToLive = serviceOwner.FileTransferTimeToLive
};
}
[HttpPut]
[Route("fileretention")]
public async Task<ActionResult> UpdateFileRetention([FromBody] ServiceOwnerUpdateFileRetentionExt serviceOwnerUpdateFileRetentionExt, [ModelBinder(typeof(MaskinportenModelBinder))] CallerIdentity token, [FromServices] UpdateFileRetentionHandler updateFileRetentionHandler, CancellationToken cancellationToken)
{
var serviceOwner = await _serviceOwnerRepository.GetServiceOwner(token.Consumer);
if (serviceOwner is null)
{
return NotFound();
}

var fileTimeToLive = XmlConvert.ToTimeSpan(serviceOwnerUpdateFileRetentionExt.FileTransferTimeToLive);
await _serviceOwnerRepository.UpdateFileRetention(token.Consumer, fileTimeToLive);
await updateFileRetentionHandler.Process(new UpdateFileRetentionRequest
{
ServiceOwnerId = token.Consumer,
}, cancellationToken);

return Ok();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public ServiceOwnerOverviewExt() { }

public DeploymentStatusExt DeploymentStatus { get; set; }

public TimeSpan FileTimeToLive { get; set; }
public TimeSpan FileTransferTimeToLive { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Altinn.Broker.Models.ServiceOwner;

public class ServiceOwnerUpdateFileRetentionExt
{
public ServiceOwnerUpdateFileRetentionExt() { }

public string FileTransferTimeToLive { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public async Task<OneOf<Task, Error>> Process(ConfirmDownloadCommandRequest requ
if (shouldConfirmAll)
{
await _fileTransferStatusRepository.InsertFileTransferStatus(request.FileTransferId, FileTransferStatus.AllConfirmedDownloaded);
_backgroundJobClient.Enqueue<ExpireFileTransferCommandHandler>((expireFileTransferCommandHandler) => expireFileTransferCommandHandler.Process(request.FileTransferId, cancellationToken));
_backgroundJobClient.Enqueue<ExpireFileTransferCommandHandler>((expireFileTransferCommandHandler) => expireFileTransferCommandHandler.RescheduleExpireEvent(new ExpireFileTransferCommandRequest
{
FileTransferId = request.FileTransferId,
Force = true
}, cancellationToken));
await _eventBus.Publish(AltinnEventType.AllConfirmedDownloaded, fileTransfer.ResourceId, fileTransfer.FileTransferId.ToString(), fileTransfer.Sender.ActorExternalId, cancellationToken);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Altinn.Broker.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Altinn.Broker.Application.GetFileTransferOverviewQuery;
using Altinn.Broker.Application.GetFileTransfersQuery;
using Altinn.Broker.Application.InitializeFileTransferCommand;
using Altinn.Broker.Application.UpdateFileRetention;
using Altinn.Broker.Application.UploadFileCommand;

using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,5 +24,6 @@ public static void AddApplicationHandlers(this IServiceCollection services)
services.AddScoped<ExpireFileTransferCommandHandler>();
services.AddScoped<LegacyGetFilesQueryHandler>();
services.AddScoped<MalwareScanningResultHandler>();
services.AddScoped<UpdateFileRetentionHandler>();
}
}
Loading
Loading