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/designer importfromregister #12239

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions backend/src/Designer/Controllers/ResourceAdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ public async Task<ActionResult<ServiceResource>> AddResource(string org, [FromBo
public async Task<ActionResult> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -252,6 +255,37 @@ public async Task<List<ServiceResource>> GetResourceList(string env)
}
}

public async Task<ServiceResource> 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<ServiceResource>(contentString, _serializerOptions);
return serviceResource;
}

public async Task<XacmlPolicy> 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<TokenResponse> GetBearerTokenFromMaskinporten()
{
return await _maskinPortenService.GetToken(_maskinportenClientDefinition.ClientSettings.EncodedJwk, _maskinportenClientDefinition.ClientSettings.Environment, _maskinportenClientDefinition.ClientSettings.ClientId, _maskinportenClientDefinition.ClientSettings.Scope, _maskinportenClientDefinition.ClientSettings.Resource, _maskinportenClientDefinition.ClientSettings.ConsumerOrgNo);
Expand Down
11 changes: 11 additions & 0 deletions backend/src/Designer/Services/Interfaces/IResourceRegistry.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -35,5 +36,15 @@ public interface IResourceRegistry
/// </summary>
/// <returns>The resource full list of all resources if exists</returns>
Task<List<ServiceResource>> GetResourceList(string env);

/// <summary>
/// Get Resource from Altinn 2 service
/// </summary>
Task<ServiceResource> GetServiceResourceFromService(string serviceCode, int serviceEditionCode, string environment);

/// <summary>
/// Get Policy from Altinn 2 Service
/// </summary>
Task<XacmlPolicy> GetXacmlPolicy(string serviceCode, int serviceEditionCode, string identifier, string environment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task GetFilteredLinkServices()
ExternalServiceCode = "Test2",
ExternalServiceEditionCode = 123
});

ResourceRegistryMock.Setup(r => r.GetResourceList(It.IsAny<string>())).ReturnsAsync(new List<ServiceResource>());
Altinn2MetadataClientMock.Setup(r => r.AvailableServices(It.IsAny<int>(), It.IsAny<string>())).ReturnsAsync(services);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public async Task ExportAltinn2Resource()

XacmlPolicy policy = AuthorizationUtil.ParsePolicy("resource_registry_delegatableapi.xml");

Altinn2MetadataClientMock.Setup(r => r.GetServiceResourceFromService(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>())).ReturnsAsync(serviceResource);
Altinn2MetadataClientMock.Setup(r => r.GetXacmlPolicy(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(policy);
ResourceRegistryMock.Setup(r => r.GetServiceResourceFromService(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>())).ReturnsAsync(serviceResource);
ResourceRegistryMock.Setup(r => r.GetXacmlPolicy(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(policy);

// Act
using HttpResponseMessage res = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class ResourceAdminControllerTestsBaseClass<TTesetClass> : Disag
{
protected readonly string VersionPrefix = "/designer/api";
protected readonly Mock<IRepository> RepositoryMock;
protected readonly Mock<IResourceRegistry> ResourceRegistryMock;
protected readonly Mock<IAltinn2MetadataClient> Altinn2MetadataClientMock;

protected override void ConfigureTestServices(IServiceCollection services)
Expand All @@ -25,12 +26,14 @@ protected override void ConfigureTestServices(IServiceCollection services)
c.RepositoryLocation = TestRepositoriesLocation);
services.AddSingleton<IGitea, IGiteaMock>();
services.AddTransient(_ => RepositoryMock.Object);
services.AddTransient(_ => ResourceRegistryMock.Object);
services.AddTransient(_ => Altinn2MetadataClientMock.Object);
}

protected ResourceAdminControllerTestsBaseClass(WebApplicationFactory<Program> factory) : base(factory)
{
RepositoryMock = new Mock<IRepository>();
ResourceRegistryMock = new Mock<IResourceRegistry>();
Altinn2MetadataClientMock = new Mock<IAltinn2MetadataClient>();
}

Expand Down
Loading