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

Bug fix for Authorization HTTP Client and APIEndpointConfiguration serialization #1283

Merged
merged 4 commits into from
Jul 26, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override Dictionary<string, ResourceTypeDescriptor> GetResourceTypes()

/// <inheritdoc/>
protected override string _name => ResourceProviderNames.FoundationaLLM_AIModel;
private const string AIMODEL_REFERENCES_FILE_NAME = "_aiModel-references.json";
private const string AIMODEL_REFERENCES_FILE_NAME = "_ai-model-references.json";
private const string AIMODEL_REFERENCES_FILE_PATH =
$"/{ResourceProviderNames.FoundationaLLM_AIModel}/{AIMODEL_REFERENCES_FILE_NAME}";

Expand Down
39 changes: 31 additions & 8 deletions src/dotnet/Authorization/Services/AuthorizationService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using FoundationaLLM.Authorization.Models.Configuration;
using FoundationaLLM.Common.Authentication;
using FoundationaLLM.Common.Constants;
using FoundationaLLM.Common.Interfaces;
using FoundationaLLM.Common.Models.Authentication;
using FoundationaLLM.Common.Models.Authorization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;

Expand All @@ -16,16 +19,16 @@ namespace FoundationaLLM.Authorization.Services
public class AuthorizationService : IAuthorizationService
{
private readonly AuthorizationServiceSettings _settings;
private readonly IHttpClientFactoryService _httpClientFactoryService;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<AuthorizationService> _logger;

public AuthorizationService(
IHttpClientFactoryService httpClientFactoryService,
IHttpClientFactory httpClientFactory,
IOptions<AuthorizationServiceSettings> options,
ILogger<AuthorizationService> logger)
{
_settings = options.Value;
_httpClientFactoryService = httpClientFactoryService;
_httpClientFactory = httpClientFactory;
_logger = logger;
}

Expand All @@ -48,7 +51,7 @@ public async Task<ActionAuthorizationResult> ProcessAuthorizationRequest(
SecurityGroupIds = userIdentity.GroupIds
};

var httpClient = await _httpClientFactoryService.CreateClient(HttpClientNames.AuthorizationAPI, userIdentity);
var httpClient = await CreateHttpClient();
var response = await httpClient.PostAsync(
$"/instances/{instanceId}/authorize",
JsonContent.Create(authorizationRequest));
Expand Down Expand Up @@ -77,7 +80,7 @@ public async Task<RoleAssignmentResult> ProcessRoleAssignmentRequest(
{
try
{
var httpClient = await _httpClientFactoryService.CreateClient(HttpClientNames.AuthorizationAPI, userIdentity);
var httpClient = await CreateHttpClient();
var response = await httpClient.PostAsync(
$"/instances/{instanceId}/roleassignments",
JsonContent.Create(roleAssignmentRequest));
Expand Down Expand Up @@ -113,7 +116,7 @@ public async Task<Dictionary<string, RoleAssignmentsWithActionsResult>> ProcessR

try
{
var httpClient = await _httpClientFactoryService.CreateClient(HttpClientNames.AuthorizationAPI, userIdentity);
var httpClient = await CreateHttpClient();
var response = await httpClient.PostAsync(
$"/instances/{instanceId}/roleassignments/querywithactions",
JsonContent.Create(request));
Expand Down Expand Up @@ -143,7 +146,7 @@ public async Task<List<object>> GetRoleAssignments(
{
try
{
var httpClient = await _httpClientFactoryService.CreateClient(HttpClientNames.AuthorizationAPI, userIdentity);
var httpClient = await CreateHttpClient();
var response = await httpClient.PostAsync(
$"/instances/{instanceId}/roleassignments/query",
JsonContent.Create(queryParameters));
Expand Down Expand Up @@ -172,7 +175,7 @@ public async Task<RoleAssignmentResult> RevokeRoleAssignment(
{
try
{
var httpClient = await _httpClientFactoryService.CreateClient(HttpClientNames.AuthorizationAPI, userIdentity);
var httpClient = await CreateHttpClient();
var response = await httpClient.DeleteAsync(
$"/instances/{instanceId}/roleassignments/{roleAssignment}");

Expand All @@ -196,5 +199,25 @@ public async Task<RoleAssignmentResult> RevokeRoleAssignment(
return new RoleAssignmentResult() { Success = false };
}
}

/// <summary>
/// Exception to the unified HTTP client factory when consuming the Authorization API.
/// </summary>
/// <returns></returns>
private async Task<HttpClient> CreateHttpClient()
{
var httpClient = _httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(_settings.APIUrl);

var credentials = DefaultAuthentication.AzureCredential;
var tokenResult = await credentials.GetTokenAsync(
new([_settings.APIScope]),
default);

httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", tokenResult.Token);

return httpClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public APIEndpointConfiguration() =>
/// The type of authentication required for accessing the API.
/// </summary>
[JsonPropertyName("authentication_type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public required AuthenticationTypes AuthenticationType { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FoundationaLLM.Common.Interfaces;
using FoundationaLLM.Common.Models.Authentication;
using FoundationaLLM.Common.Models.Authentication;
using FoundationaLLM.Common.Services;
using Microsoft.Extensions.Configuration;
using NSubstitute;
Expand Down
Loading