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

Add Bruno test and fix max limit in revoke all #157

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Altinn.AccessManagement.Core.Configuration
{
/// <summary>
/// AppsInstanceDelegationSettings
/// </summary>
public class AppsInstanceDelegationSettings
{
/// <summary>
/// Sets the maximum policy files to handle under revoke all calls
/// </summary>
public int MaxPolicyFilesToRevoke { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ private static readonly ValidationErrorDescriptorFactory _factory
/// Gets a validation error descriptor for when a Resource not has any delegable rights for the app
/// </summary>
public static ValidationErrorDescriptor ToManyDelegationsToRevoke { get; }
= _factory.Create(5, $"There must be 10 or less policy files to update.");
= _factory.Create(5, $"There is to many policy files to update. Must delete individual delegations.");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.Formats.Asn1;
using Altinn.AccessManagement.Core.Clients.Interfaces;
using Altinn.AccessManagement.Core.Configuration;
using Altinn.AccessManagement.Core.Constants;
using Altinn.AccessManagement.Core.Enums;
using Altinn.AccessManagement.Core.Errors;
Expand All @@ -16,7 +17,7 @@
using Altinn.Platform.Register.Models;
using Altinn.Urn;
using Altinn.Urn.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Microsoft.Extensions.Options;

namespace Altinn.AccessManagement.Core.Services.Implementation;

Expand All @@ -29,17 +30,19 @@ public class AppsInstanceDelegationService : IAppsInstanceDelegationService
private readonly IPolicyInformationPoint _pip;
private readonly IPolicyAdministrationPoint _pap;
private readonly IResourceRegistryClient _resourceRegistryClient;
private readonly AppsInstanceDelegationSettings _appsInstanceDelegationSettings;
private readonly string appInstanceResourcePath = "appInstanceDelegationRequest.Resource";

/// <summary>
/// Initializes a new instance of the <see cref="AppsInstanceDelegationService"/> class.
/// </summary>
public AppsInstanceDelegationService(IPartiesClient partiesClient, IResourceRegistryClient resourceRegistryClient, IPolicyInformationPoint pip, IPolicyAdministrationPoint pap)
public AppsInstanceDelegationService(IPartiesClient partiesClient, IOptions<AppsInstanceDelegationSettings> appsInstanceDelegationSettings, IResourceRegistryClient resourceRegistryClient, IPolicyInformationPoint pip, IPolicyAdministrationPoint pap)
{
_partiesClient = partiesClient;
_pip = pip;
_resourceRegistryClient = resourceRegistryClient;
_pap = pap;
_appsInstanceDelegationSettings = appsInstanceDelegationSettings.Value;
}

private async Task<(UuidType DelegationType, Guid? Uuid)> TranslatePartyUuidToPersonOrganizationUuid(PartyUrn partyId)
Expand Down Expand Up @@ -306,6 +309,17 @@ public async Task<Result<List<AppsInstanceRevokeResponse>>> RevokeAll(AppsInstan
}
}

int limit = _appsInstanceDelegationSettings.MaxPolicyFilesToRevoke;
if (rightsToRevoke.Count > limit)
{
errors.Add(ValidationErrors.ToManyDelegationsToRevoke, "ResourceId");

if (errors.TryBuild(out errorResult))
{
return errorResult;
}
}

// Perform Revoke
List<InstanceRight> revokedResult = await _pap.TryWriteInstanceRevokeAllPolicyRules(rightsToRevoke, cancellationToken);
List<AppsInstanceRevokeResponse> result = TransformInstanceRightListToAppsInstanceDelegationResponseList(revokedResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private static void ConfigureAppsettings(this WebApplicationBuilder builder)
builder.Services.Configure<KeyVaultSettings>(config.GetSection("kvSetting"));
builder.Services.Configure<OidcProviderSettings>(config.GetSection("OidcProviders"));
builder.Services.Configure<UserProfileLookupSettings>(config.GetSection("UserProfileLookupSettings"));
builder.Services.Configure<AppsInstanceDelegationSettings>(config.GetSection("AppsInstanceDelegationSettings"));
}

private static void ConfigureAuthorization(this WebApplicationBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"RightsDelegationApi": false,
"OpenTelementry": false,
"UseNewQueryRepo": true
},
"AppsInstanceDelegationSettings": {
"MaxPolicyFilesToRevoke": 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace Altinn.AccessManagement.Tests.Controllers;

public class AppsInstanceDelegationControllerTest : IClassFixture<CustomWebApplicationFactory<AppsInstanceDelegationController>>

Check warning on line 25 in src/apps/Altinn.AccessManagement/test/Altinn.AccessManagement.Tests/Controllers/ResourceOwnerAPI/AppsInstanceDelegationControllerTest.cs

View workflow job for this annotation

GitHub Actions / CI (AccessManagement)

Missing XML comment for publicly visible type or member 'AppsInstanceDelegationControllerTest'
{
private readonly CustomWebApplicationFactory<AppsInstanceDelegationController> _factory;
private readonly JsonSerializerOptions options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
Expand Down Expand Up @@ -111,7 +111,7 @@

[Theory]
[MemberData(nameof(TestDataAppsInstanceDelegation.RevokeAll), MemberType = typeof(TestDataAppsInstanceDelegation))]
public async Task AppsInstanceDelegationController_ValidToken_RevokeAll_OK(string platformToken, string resourceId, string instanceId, Paginated<AppsInstanceRevokeResponseDto> expected)

Check warning on line 114 in src/apps/Altinn.AccessManagement/test/Altinn.AccessManagement.Tests/Controllers/ResourceOwnerAPI/AppsInstanceDelegationControllerTest.cs

View workflow job for this annotation

GitHub Actions / CI (AccessManagement)

Missing XML comment for publicly visible type or member 'AppsInstanceDelegationControllerTest.AppsInstanceDelegationController_ValidToken_RevokeAll_OK(string, string, string, Paginated<AppsInstanceRevokeResponseDto>)'
{
var client = GetTestClient(platformToken);

Expand All @@ -125,6 +125,22 @@
AssertionUtil.AssertPagination(expected, actual, AssertionUtil.AssertAppsInstanceRevokeResponseDto);
}

[Theory]
[MemberData(nameof(TestDataAppsInstanceDelegation.RevokeAllToManyPolicyFiles), MemberType = typeof(TestDataAppsInstanceDelegation))]
public async Task AppsInstanceDelegationController_ValidToken_RevokeAll_ToManyPolicyFilesToUpdate(string platformToken, string resourceId, string instanceId, AltinnProblemDetails expected)

Check warning on line 130 in src/apps/Altinn.AccessManagement/test/Altinn.AccessManagement.Tests/Controllers/ResourceOwnerAPI/AppsInstanceDelegationControllerTest.cs

View workflow job for this annotation

GitHub Actions / CI (AccessManagement)

Missing XML comment for publicly visible type or member 'AppsInstanceDelegationControllerTest.AppsInstanceDelegationController_ValidToken_RevokeAll_ToManyPolicyFilesToUpdate(string, string, string, AltinnProblemDetails)'
{
var client = GetTestClient(platformToken);

// Act
HttpResponseMessage response = await client.DeleteAsync($"accessmanagement/api/v1/app/delegationrevoke/resource/{resourceId}/instance/{instanceId}");

// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

AltinnProblemDetails actual = JsonSerializer.Deserialize<AltinnProblemDetails>(await response.Content.ReadAsStringAsync(), options);
TestDataAppsInstanceDelegation.AssertAltinnProblemDetailsEqual(expected, actual);
}

[Theory]
[MemberData(nameof(TestDataAppsInstanceDelegation.RevokeAllUnathorized), MemberType = typeof(TestDataAppsInstanceDelegation))]
public async Task AppsInstanceDelegationController_NoToken_RevokeAll_Unauthorized(string resourceId, string instanceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static class TestDataAppsInstanceDelegation

private static readonly string RevokeAllInstance = "00000000-0000-0000-0000-000000000010";

private static readonly string RevokeAllInstanceToManyPolicyFiles = "00000000-0000-0000-0000-000000000011";

/// <summary>
/// Test case: GET v1/apps/instancedelegation/{resourceId}/{instanceId}/delegationcheck
/// with:
Expand Down Expand Up @@ -129,6 +131,16 @@ public static class TestDataAppsInstanceDelegation
}
};

public static TheoryData<string, string, string, AltinnProblemDetails> RevokeAllToManyPolicyFiles() => new()
{
{
PrincipalUtil.GetAccessToken("ttd", "am-devtest-instancedelegation"),
AppId,
RevokeAllInstanceToManyPolicyFiles,
GetExpectedResponse<AltinnProblemDetails>("Revoke", AppId, RevokeAllInstanceToManyPolicyFiles)
}
};

public static TheoryData<string, string> RevokeAllUnathorized() => new()
{
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "Bad Request",
"status": 400,
"detail": "One or more validation errors occurred.",
"code": "STD-00000",
"validationErrors": [
{
"code": "AM.VLD-00005",
"detail": "There is to many policy files to update. Must delete individual delegations.",
"paths": [
"ResourceId"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<xacml:Policy PolicyId="urn:altinn:policyid:1" Version="1.0" RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:policy-combining-algorithm:deny-overrides" xmlns:xacml="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
<xacml:Description>Delegation policy containing all delegated rights/actions from urn:altinn:organization:uuid:0268b99a-5817-4bbf-9b62-d90b16d527ea to urn:altinn:person:uuid:ce4ba72b-d111-404f-95b5-313fb3847fa1, for the resource; app_ttd_am-devtest-instancedelegation</xacml:Description>
<xacml:Target />
<xacml:Rule RuleId="96b20b71-06fb-4d59-9538-38bfa72774fb" Effect="Permit">
<xacml:Description>Delegation of a right/action from urn:altinn:organization:uuid:0268b99a-5817-4bbf-9b62-d90b16d527ea to urn:altinn:person:uuid:ce4ba72b-d111-404f-95b5-313fb3847fa1, for the resource: app_ttd_am-devtest-instancedelegation, by: urn:altinn:resource:app_ttd_am-devtest-instancedelegation</xacml:Description>
<xacml:Target>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">CE4BA72B-D111-404F-95B5-313FB3847FA1</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:person:uuid" Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">app_ttd_am-devtest-instancedelegation</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">task_1</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:task" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">00000000-0000-0000-0000-000000000011</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource:instance-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">read</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
</xacml:Target>
</xacml:Rule>
</xacml:Policy>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<xacml:Policy PolicyId="urn:altinn:policyid:1" Version="1.0" RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:policy-combining-algorithm:deny-overrides" xmlns:xacml="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
<xacml:Description>Delegation policy containing all delegated rights/actions from urn:altinn:organization:uuid:0268b99a-5817-4bbf-9b62-d90b16d527ea to urn:altinn:person:uuid:ce4ba72b-d111-404f-95b5-313fb3847fa1, for the resource; app_ttd_am-devtest-instancedelegation</xacml:Description>
<xacml:Target />
<xacml:Rule RuleId="96b20b71-06fb-4d59-9538-38bfa72774fb" Effect="Permit">
<xacml:Description>Delegation of a right/action from urn:altinn:organization:uuid:0268b99a-5817-4bbf-9b62-d90b16d527ea to urn:altinn:person:uuid:ce4ba72b-d111-404f-95b5-313fb3847fa1, for the resource: app_ttd_am-devtest-instancedelegation, by: urn:altinn:resource:app_ttd_am-devtest-instancedelegation</xacml:Description>
<xacml:Target>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">0268B99A-5817-4BBF-9B62-D90B16D527EA</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:person:uuid" Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">app_ttd_am-devtest-instancedelegation</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">task_1</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:task" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">00000000-0000-0000-0000-000000000011</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource:instance-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">read</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
</xacml:Target>
</xacml:Rule>
<xacml:Rule RuleId="96b20b71-06fb-4d59-9538-38bfa72774fb" Effect="Permit">
<xacml:Description>Delegation of a right/action from urn:altinn:organization:uuid:0268b99a-5817-4bbf-9b62-d90b16d527ea to urn:altinn:person:uuid:ce4ba72b-d111-404f-95b5-313fb3847fa1, for the resource: app_ttd_am-devtest-instancedelegation, by: urn:altinn:resource:app_ttd_am-devtest-instancedelegation</xacml:Description>
<xacml:Target>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">0268B99A-5817-4BBF-9B62-D90B16D527EA</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:person:uuid" Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">app_ttd_am-devtest-instancedelegation</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">task_1</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:task" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">00000000-0000-0000-0000-000000000011</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:altinn:resource:instance-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
<xacml:AnyOf>
<xacml:AllOf>
<xacml:Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case">
<xacml:AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">sign</xacml:AttributeValue>
<xacml:AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false" />
</xacml:Match>
</xacml:AllOf>
</xacml:AnyOf>
</xacml:Target>
</xacml:Rule>
</xacml:Policy>
Loading
Loading