-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Don't process ttl change if value has not changed * Optimize garbage collection on db connection * Transfer with speed declines doens't cancel * Add k6 load testing * Don't process ttl change if value has not changed * Optimize garbage collection on db connection * Transfer with speed declines doens't cancel * Add k6 load testing * keep connection alive for 1 hour * some changes to the test * update readme * format js file * Readme + some cleanup * Can change max filetransfer size on resource level * file transfer size should be nullable * Some readme changes * Fix spelling error and wrong error code * Remove inheritance from Repository --------- Co-authored-by: Hammerbeck <andreas.hammerbeck@digdir.no>
- Loading branch information
Showing
21 changed files
with
362 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
|
||
using Altinn.Broker.Models; | ||
using Altinn.Broker.Tests.Helpers; | ||
|
||
using Xunit; | ||
|
||
namespace Altinn.Broker.Tests; | ||
public class ResourceControllerTests : IClassFixture<CustomWebApplicationFactory> | ||
{ | ||
private readonly CustomWebApplicationFactory _factory; | ||
private readonly HttpClient _serviceOwnerClient; | ||
private readonly HttpClient _senderClient; | ||
private readonly HttpClient _recipientClient; | ||
private readonly JsonSerializerOptions _responseSerializerOptions; | ||
|
||
public ResourceControllerTests(CustomWebApplicationFactory factory) | ||
{ | ||
_factory = factory; | ||
_serviceOwnerClient = _factory.CreateClientWithAuthorization(TestConstants.DUMMY_SERVICE_OWNER_TOKEN); | ||
_senderClient = _factory.CreateClientWithAuthorization(TestConstants.DUMMY_SENDER_TOKEN); | ||
_recipientClient = _factory.CreateClientWithAuthorization(TestConstants.DUMMY_RECIPIENT_TOKEN); | ||
|
||
_responseSerializerOptions = new JsonSerializerOptions(new JsonSerializerOptions() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}); | ||
_responseSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter()); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task Update_Resource_Max_Upload_Size() | ||
{ | ||
var response = await _serviceOwnerClient.PutAsJsonAsync<ResourceExt>($"broker/api/v1/resource/MaxFileTransferSize", new ResourceExt | ||
{ | ||
ResourceId = "123", | ||
MaxFileTransferSize = 99999 | ||
}); | ||
Assert.True(response.IsSuccessStatusCode, await response.Content.ReadAsStringAsync()); | ||
} | ||
[Fact] | ||
public async Task Update_Resource_Max_Upload_Size_Over_Global_Should_Fail() | ||
{ | ||
var response = await _serviceOwnerClient.PutAsJsonAsync<ResourceExt>($"broker/api/v1/resource/MaxFileTransferSize", new ResourceExt | ||
{ | ||
ResourceId = "123", | ||
MaxFileTransferSize = 999999999999999 | ||
}); | ||
Assert.True(response.StatusCode == HttpStatusCode.BadRequest, await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[Fact] | ||
public async Task Update_Resource_Should_Fail_For_Sender() | ||
{ | ||
var response = await _senderClient.PutAsJsonAsync<ResourceExt>($"broker/api/v1/resource/MaxFileTransferSize", new ResourceExt | ||
{ | ||
ResourceId = "123", | ||
MaxFileTransferSize = 1000000 | ||
}); | ||
Assert.True(response.StatusCode == HttpStatusCode.Forbidden, await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
[Fact] | ||
public async Task Update_Resource_Should_Fail_For_Receiver() | ||
{ | ||
var response = await _recipientClient.PutAsJsonAsync<ResourceExt>($"broker/api/v1/resource/MaxFileTransferSize", new ResourceExt | ||
{ | ||
ResourceId = "123", | ||
MaxFileTransferSize = 1000000 | ||
}); | ||
Assert.True(response.StatusCode == HttpStatusCode.Forbidden, await response.Content.ReadAsStringAsync()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Altinn.Broker.API.Configuration; | ||
using Altinn.Broker.Core.Domain; | ||
using Altinn.Broker.Core.Domain.Enums; | ||
using Altinn.Broker.Core.Repositories; | ||
using Altinn.Broker.Core.Services; | ||
using Altinn.Broker.Middlewares; | ||
using Altinn.Broker.Models; | ||
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Altinn.Broker.Controllers; | ||
|
||
[ApiController] | ||
[Route("broker/api/v1/resource")] | ||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] | ||
[Authorize(Policy = AuthorizationConstants.ServiceOwner)] | ||
public class ResourceController : Controller | ||
{ | ||
private readonly IResourceRepository _resourceRepository; | ||
private readonly Core.Repositories.IAuthorizationService _resourceRightsRepository; | ||
private readonly IResourceManager _resourceManager; | ||
|
||
public ResourceController(IResourceRepository resourceRepository, IResourceManager resourceManager, Core.Repositories.IAuthorizationService resourceRightsRepository) | ||
{ | ||
_resourceRepository = resourceRepository; | ||
_resourceManager = resourceManager; | ||
_resourceRightsRepository = resourceRightsRepository; | ||
|
||
} | ||
|
||
[HttpPut] | ||
[Route("maxfiletransfersize")] | ||
public async Task<ActionResult> UpdateMaxFileTransferSize([FromBody] ResourceExt resourceExt, [ModelBinder(typeof(MaskinportenModelBinder))] CallerIdentity token, CancellationToken cancellationToken) | ||
{ | ||
var hasAccess = await _resourceRightsRepository.CheckUserAccess(resourceExt.ResourceId, token.ClientId, [ResourceAccessLevel.Write], false, cancellationToken); | ||
if (!hasAccess) | ||
{ | ||
return Unauthorized(); | ||
} | ||
var resource = await _resourceRepository.GetResource(resourceExt.ResourceId, cancellationToken); | ||
|
||
if (resource is null) | ||
{ | ||
return NotFound(); | ||
} | ||
if (resourceExt.MaxFileTransferSize < 0) | ||
{ | ||
return BadRequest("Max upload size cannot be negative"); | ||
} | ||
if (resourceExt.MaxFileTransferSize == resource.MaxFileTransferSize) | ||
{ | ||
return BadRequest("Max upload size is already set to the requested value"); | ||
} | ||
long maxFileTransferSize = long.Parse(Environment.GetEnvironmentVariable("MAX_FILE_UPLOAD_SIZE")); | ||
|
||
if (resourceExt.MaxFileTransferSize > maxFileTransferSize) | ||
{ | ||
return BadRequest("Max upload size cannot exceed the global maximum allowed size"); | ||
} | ||
await _resourceRepository.UpdateMaxFileTransferSize(resourceExt.ResourceId, resourceExt.MaxFileTransferSize, cancellationToken); | ||
return Ok(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Numerics; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Altinn.Broker.Models | ||
{ | ||
/// <summary> | ||
/// API input model for file initialization. | ||
/// </summary> | ||
public class ResourceExt | ||
{ | ||
/// <summary> | ||
/// The Altinn resource ID | ||
/// </summary> | ||
[JsonPropertyName("resourceId")] | ||
[StringLength(255, MinimumLength = 1)] | ||
[Required] | ||
public string ResourceId { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The max upload size for the resource in bytes | ||
/// </summary> | ||
[JsonPropertyName("maxFileTransferSize")] | ||
[Required] | ||
public long MaxFileTransferSize { get; set; } = 0; | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Altinn.Broker.Core/Repositories/IAltinnResourceRepoistory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
using System.Numerics; | ||
|
||
using Altinn.Broker.Core.Domain; | ||
|
||
namespace Altinn.Broker.Core.Repositories; | ||
public interface IAltinnResourceRepository | ||
{ | ||
Task<ResourceEntity?> GetResource(string resourceId, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
| ||
using System.Numerics; | ||
|
||
using Altinn.Broker.Core.Domain; | ||
|
||
namespace Altinn.Broker.Core.Repositories; | ||
public interface IResourceRepository | ||
{ | ||
Task<ResourceEntity?> GetResource(string resourceId, CancellationToken cancellationToken = default); | ||
Task UpdateMaxFileTransferSize(string resourceId, long maxSize, CancellationToken cancellationToken = default); | ||
Task CreateResource(ResourceEntity resource, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.