diff --git a/backend/src/Designer/Controllers/ResourceAdminController.cs b/backend/src/Designer/Controllers/ResourceAdminController.cs index 9002a8ff16f..39aa85d0e20 100644 --- a/backend/src/Designer/Controllers/ResourceAdminController.cs +++ b/backend/src/Designer/Controllers/ResourceAdminController.cs @@ -190,10 +190,10 @@ public async Task> AddResource(string org, [FromBo public async Task ImportResource(string org, string serviceCode, int serviceEdition, string environment, [FromBody] string resourceId) { string repository = string.Format("{0}-resources", org); - ServiceResource resource = await _altinn2MetadataClient.GetServiceResourceFromService(serviceCode, serviceEdition, environment); + ServiceResource resource = await _resourceRegistry.GetServiceResourceFromService(serviceCode, serviceEdition, environment); resource.Identifier = resourceId; _repository.AddServiceResource(org, resource); - XacmlPolicy policy = await _altinn2MetadataClient.GetXacmlPolicy(serviceCode, serviceEdition, resource.Identifier, environment); + XacmlPolicy policy = await _resourceRegistry.GetXacmlPolicy(serviceCode, serviceEdition, resource.Identifier, environment); await _repository.SavePolicy(org, repository, resource.Identifier, policy); return Ok(resource); } diff --git a/backend/src/Designer/Services/Implementation/ResourceRegistryService.cs b/backend/src/Designer/Services/Implementation/ResourceRegistryService.cs index 72a183f03e2..439d40ac56e 100644 --- a/backend/src/Designer/Services/Implementation/ResourceRegistryService.cs +++ b/backend/src/Designer/Services/Implementation/ResourceRegistryService.cs @@ -6,8 +6,11 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; +using System.Xml; using Altinn.ApiClients.Maskinporten.Interfaces; using Altinn.ApiClients.Maskinporten.Models; +using Altinn.Authorization.ABAC.Utils; +using Altinn.Authorization.ABAC.Xacml; using Altinn.Studio.Designer.Configuration; using Altinn.Studio.Designer.Exceptions; using Altinn.Studio.Designer.Helpers; @@ -252,6 +255,37 @@ public async Task> GetResourceList(string env) } } + public async Task GetServiceResourceFromService(string serviceCode, int serviceEditionCode, string environment) + { + string resourceRegisterUrl = GetResourceRegistryBaseUrl(environment); + string url = $"{resourceRegisterUrl}/resourceregistry/api/v1/altinn2export/resource/?serviceCode={serviceCode}&serviceEditionCode={serviceEditionCode}"; + + HttpResponseMessage response = await _httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + + string contentString = await response.Content.ReadAsStringAsync(); + ServiceResource serviceResource = JsonSerializer.Deserialize(contentString, _serializerOptions); + return serviceResource; + } + + public async Task GetXacmlPolicy(string serviceCode, int serviceEditionCode, string identifier, string environment) + { + string resourceRegisterUrl = GetResourceRegistryBaseUrl(environment); + string url = $"{resourceRegisterUrl}/resourceregistry/api/v1/altinn2export/policy/?serviceCode={serviceCode}&serviceEditionCode={serviceEditionCode}&resourceIdentifier={identifier}"; + + HttpResponseMessage response = await _httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + + string contentString = await response.Content.ReadAsStringAsync(); + XacmlPolicy policy; + using (XmlReader reader = XmlReader.Create(new StringReader(contentString))) + { + policy = XacmlParser.ParseXacmlPolicy(reader); + } + + return policy; + } + private async Task GetBearerTokenFromMaskinporten() { return await _maskinPortenService.GetToken(_maskinportenClientDefinition.ClientSettings.EncodedJwk, _maskinportenClientDefinition.ClientSettings.Environment, _maskinportenClientDefinition.ClientSettings.ClientId, _maskinportenClientDefinition.ClientSettings.Scope, _maskinportenClientDefinition.ClientSettings.Resource, _maskinportenClientDefinition.ClientSettings.ConsumerOrgNo); diff --git a/backend/src/Designer/Services/Interfaces/IResourceRegistry.cs b/backend/src/Designer/Services/Interfaces/IResourceRegistry.cs index 6b629bfb3c9..7efb24cd20e 100644 --- a/backend/src/Designer/Services/Interfaces/IResourceRegistry.cs +++ b/backend/src/Designer/Services/Interfaces/IResourceRegistry.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Altinn.Authorization.ABAC.Xacml; using Altinn.Studio.Designer.Models; using Microsoft.AspNetCore.Mvc; @@ -35,5 +36,15 @@ public interface IResourceRegistry /// /// The resource full list of all resources if exists Task> GetResourceList(string env); + + /// + /// Get Resource from Altinn 2 service + /// + Task GetServiceResourceFromService(string serviceCode, int serviceEditionCode, string environment); + + /// + /// Get Policy from Altinn 2 Service + /// + Task GetXacmlPolicy(string serviceCode, int serviceEditionCode, string identifier, string environment); } } diff --git a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/GetAltinn2LinkServicesTests.cs b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/GetAltinn2LinkServicesTests.cs index dcaed9c796d..5c3b77553c8 100644 --- a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/GetAltinn2LinkServicesTests.cs +++ b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/GetAltinn2LinkServicesTests.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Threading.Tasks; using Altinn.ResourceRegistry.Core.Models.Altinn2; +using Altinn.Studio.Designer.Models; using Microsoft.AspNetCore.Mvc.Testing; using Moq; using Xunit; @@ -36,7 +37,7 @@ public async Task GetFilteredLinkServices() ExternalServiceCode = "Test2", ExternalServiceEditionCode = 123 }); - + ResourceRegistryMock.Setup(r => r.GetResourceList(It.IsAny())).ReturnsAsync(new List()); Altinn2MetadataClientMock.Setup(r => r.AvailableServices(It.IsAny(), It.IsAny())).ReturnsAsync(services); // Act diff --git a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ImportResourceTests.cs b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ImportResourceTests.cs index 9ddbccb2bbc..ec22c2b1b55 100644 --- a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ImportResourceTests.cs +++ b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ImportResourceTests.cs @@ -34,8 +34,8 @@ public async Task ExportAltinn2Resource() XacmlPolicy policy = AuthorizationUtil.ParsePolicy("resource_registry_delegatableapi.xml"); - Altinn2MetadataClientMock.Setup(r => r.GetServiceResourceFromService(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(serviceResource); - Altinn2MetadataClientMock.Setup(r => r.GetXacmlPolicy(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(policy); + ResourceRegistryMock.Setup(r => r.GetServiceResourceFromService(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(serviceResource); + ResourceRegistryMock.Setup(r => r.GetXacmlPolicy(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(policy); // Act using HttpResponseMessage res = await HttpClient.SendAsync(httpRequestMessage); diff --git a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ResourceAdminControllerTestsBaseClass.cs b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ResourceAdminControllerTestsBaseClass.cs index 281cd3c21ba..7c406d27717 100644 --- a/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ResourceAdminControllerTestsBaseClass.cs +++ b/backend/tests/Designer.Tests/Controllers/ResourceAdminController/ResourceAdminControllerTestsBaseClass.cs @@ -16,6 +16,7 @@ public abstract class ResourceAdminControllerTestsBaseClass : Disag { protected readonly string VersionPrefix = "/designer/api"; protected readonly Mock RepositoryMock; + protected readonly Mock ResourceRegistryMock; protected readonly Mock Altinn2MetadataClientMock; protected override void ConfigureTestServices(IServiceCollection services) @@ -25,12 +26,14 @@ protected override void ConfigureTestServices(IServiceCollection services) c.RepositoryLocation = TestRepositoriesLocation); services.AddSingleton(); services.AddTransient(_ => RepositoryMock.Object); + services.AddTransient(_ => ResourceRegistryMock.Object); services.AddTransient(_ => Altinn2MetadataClientMock.Object); } protected ResourceAdminControllerTestsBaseClass(WebApplicationFactory factory) : base(factory) { RepositoryMock = new Mock(); + ResourceRegistryMock = new Mock(); Altinn2MetadataClientMock = new Mock(); }