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

Added proxy for SBL Bridge for setting expired service #486

Merged
merged 1 commit into from
Sep 12, 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
6 changes: 6 additions & 0 deletions src/Altinn.ResourceRegistry.Core/Services/IAltinn2Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ public interface IAltinn2Services
/// Request delegations
/// </summary>
Task ExportDelegations(ExportDelegationsRequestBE exportDelegationsRequestBE, CancellationToken cancellationToken = default);

/// <summary>
/// Set service edition as expired
/// </summary>
/// <returns></returns>
Task SetServiceEditionExpired(string externalServiceCode, int externalServiceEditionCode, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Xml;
using Altinn.Authorization.ABAC.Utils;
using Altinn.Authorization.ABAC.Xacml;
Expand Down Expand Up @@ -50,7 +51,7 @@
}
catch (Exception ex)
{
throw new Exception($"Something went wrong when retrieving Action options", ex);

Check warning on line 54 in src/Altinn.ResourceRegistry.Integration/Clients/Altinn2ServicesClient.cs

View workflow job for this annotation

GitHub Actions / Analyze

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)

Check warning on line 54 in src/Altinn.ResourceRegistry.Integration/Clients/Altinn2ServicesClient.cs

View workflow job for this annotation

GitHub Actions / Analyze

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)
}
}

Expand Down Expand Up @@ -122,5 +123,14 @@

return policy;
}

/// <inheritdoc/>
public async Task SetServiceEditionExpired(string externalServiceCode, int externalServiceEditionCode, CancellationToken cancellationToken = default)
{
string bridgeBaseUrl = _settings.BridgeApiEndpoint;
string url = $"{bridgeBaseUrl}metadata/api/setserviceeditionexpired?externalServiceCode={externalServiceCode}&externalServiceEditionCode={externalServiceEditionCode}";
HttpResponseMessage response = await _client.GetAsync(url, cancellationToken);
response.EnsureSuccessStatusCode();
}
}
}
12 changes: 12 additions & 0 deletions src/Altinn.ResourceRegistry/Controllers/Altinn2ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@
return Created();
}

/// <summary>
/// Sets a given service expired to hide delegation functionality. Proxy for bridge functionality. Called by Altinn Studio and used as part of the migration of delegation process
/// </summary>
/// <returns></returns>
[Authorize(Policy = AuthzConstants.POLICY_ADMIN)]
[HttpGet("setserviceeditionexpired")]
public async Task<ActionResult> SetServiceEditionExpired([FromQueryAttribute] string externalServiceCode, [FromQueryAttribute] int externalServiceEditionCode, CancellationToken cancellationToken = default)
{
await _altinn2ServicesClient.SetServiceEditionExpired(externalServiceCode, externalServiceEditionCode, cancellationToken);
return Ok();
}

[NonAction]
private async Task<bool> ValidateMatchingOrgForDelegaton(ExportDelegationsRequestBE exportRequest, string org, CancellationToken cancellationToken = default)
{
Expand All @@ -122,7 +134,7 @@
return false;
}

foreach (ServiceResource resource in altinnService)

Check warning on line 137 in src/Altinn.ResourceRegistry/Controllers/Altinn2ExportController.cs

View workflow job for this annotation

GitHub Actions / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (resource.Identifier.Equals($"se_{exportRequest.ServiceCode}_{exportRequest.ServiceEditionCode}"))
{
Expand Down
39 changes: 39 additions & 0 deletions test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,45 @@
Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);
}

/// <summary>
/// Calls SetExpired endpoint without token
/// </summary>
[Fact]
public async Task Setserviceeditionexpired_WithoutToken()
{
HttpClient client = CreateClient();
string requestUri = "resourceregistry/api/v1/altinn2export/setserviceeditionexpired?externalServiceCode=4485&externalServiceEditionCode=2021";

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri)
{
};

HttpResponseMessage response = await client.SendAsync(httpRequestMessage);

Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);
}

/// <summary>
/// Calls SetExpired endpoint without token
/// </summary>
[Fact]
public async Task Setserviceeditionexpired_WithValidToken()
{
HttpClient client = CreateClient();
string token = PrincipalUtil.GetOrgToken("digdir", "991825827", "altinn:resourceregistry/resource.admin");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
string requestUri = "resourceregistry/api/v1/altinn2export/setserviceeditionexpired?externalServiceCode=4485&externalServiceEditionCode=2021";

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri)
{
};

HttpResponseMessage response = await client.SendAsync(httpRequestMessage);

Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}


/// <summary>
/// Tries to trigger batch without Altinn Studio token
/// </summary>
Expand Down Expand Up @@ -157,7 +196,7 @@
ResourceId = string.Empty
};

exportDelegationsRequestBE.ResourceId = null;

Check warning on line 199 in test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Cannot convert null literal to non-nullable reference type.

Check warning on line 199 in test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Cannot convert null literal to non-nullable reference type.

Check warning on line 199 in test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type.

Check warning on line 199 in test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Cannot convert null literal to non-nullable reference type.

Check warning on line 199 in test/Altinn.ResourceRegistry.Tests/Altinn2ExportControllerTest.cs

View workflow job for this annotation

GitHub Actions / Analyze

Cannot convert null literal to non-nullable reference type.

using HttpContent content = JsonContent.Create(exportDelegationsRequestBE);
HttpResponseMessage response = await client.PostAsync(requestUri, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,10 @@ public Task ExportDelegations(ExportDelegationsRequestBE exportDelegationsReques
{
return Task.CompletedTask;
}

public Task SetServiceEditionExpired(string externalServiceCode, int externalServiceEditionCode, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}
}
Loading