Skip to content

Commit

Permalink
Task: warn unsupported auth flows (#5768)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Nov 19, 2024
1 parent bc6f8f5 commit 065a960
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 197 deletions.
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public async Task<bool> GeneratePluginAsync(CancellationToken cancellationToken)
throw new InvalidOperationException("The OpenAPI document and the URL tree must be loaded before generating the plugins");
// generate plugin
sw.Start();
var pluginsService = new PluginsGenerationService(openApiDocument, openApiTree, config, Directory.GetCurrentDirectory());
var pluginsService = new PluginsGenerationService(openApiDocument, openApiTree, config, Directory.GetCurrentDirectory(), logger);
await pluginsService.GenerateManifestAsync(cancellationToken).ConfigureAwait(false);
StopLogAndReset(sw, $"step {++stepId} - generate plugin - took");
return stepId;
Expand Down
26 changes: 20 additions & 6 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.OpenApiExtensions;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.ApiManifest;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;
Expand All @@ -23,9 +24,10 @@ public partial class PluginsGenerationService
private readonly OpenApiUrlTreeNode TreeNode;
private readonly GenerationConfiguration Configuration;
private readonly string WorkingDirectory;
private readonly ILogger<KiotaBuilder> Logger;

public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode openApiUrlTreeNode,
GenerationConfiguration configuration, string workingDirectory)
GenerationConfiguration configuration, string workingDirectory, ILogger<KiotaBuilder> logger)
{
ArgumentNullException.ThrowIfNull(document);
ArgumentNullException.ThrowIfNull(openApiUrlTreeNode);
Expand All @@ -35,6 +37,7 @@ public PluginsGenerationService(OpenApiDocument document, OpenApiUrlTreeNode ope
TreeNode = openApiUrlTreeNode;
Configuration = configuration;
WorkingDirectory = workingDirectory;
Logger = logger;
}

private static readonly OpenAPIRuntimeComparer _openAPIRuntimeComparer = new();
Expand Down Expand Up @@ -258,7 +261,7 @@ private OpenApiDocument GetDocumentWithTrimmedComponentsAndResponses(OpenApiDocu

private PluginManifestDocument GetManifestDocument(string openApiDocumentPath)
{
var (runtimes, functions, conversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(OAIDocument, Configuration.PluginAuthInformation, TreeNode, openApiDocumentPath);
var (runtimes, functions, conversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(OAIDocument, Configuration.PluginAuthInformation, TreeNode, openApiDocumentPath, Logger);
var descriptionForHuman = OAIDocument.Info?.Description is string d && !string.IsNullOrEmpty(d) ? d : $"Description for {OAIDocument.Info?.Title}";
var manifestInfo = ExtractInfoFromDocument(OAIDocument.Info);
var pluginManifestDocument = new PluginManifestDocument
Expand Down Expand Up @@ -338,7 +341,7 @@ private sealed record OpenApiManifestInfo(
string ContactEmail = DefaultContactEmail);

private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimesFunctionsAndConversationStartersFromTree(OpenApiDocument document, PluginAuthConfiguration? authInformation, OpenApiUrlTreeNode currentNode,
string openApiDocumentPath)
string openApiDocumentPath, ILogger<KiotaBuilder> logger)
{
var runtimes = new List<OpenApiRuntime>();
var functions = new List<Function>();
Expand All @@ -348,10 +351,21 @@ private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimes
{
foreach (var operation in pathItem.Operations.Values.Where(static x => !string.IsNullOrEmpty(x.OperationId)))
{
var auth = configAuth;
try
{
auth = configAuth ?? GetAuth(operation.Security ?? document.SecurityRequirements);
}
catch (UnsupportedSecuritySchemeException e)
{
auth = new AnonymousAuth();
logger.LogWarning("Authentication warning: {OperationId} - {Message}", operation.OperationId, e.Message);
}

runtimes.Add(new OpenApiRuntime
{
// Configuration overrides document information
Auth = configAuth ?? GetAuth(operation.Security ?? document.SecurityRequirements),
Auth = auth,
Spec = new OpenApiRuntimeSpec { Url = openApiDocumentPath },
RunForFunctions = [operation.OperationId]
});
Expand All @@ -376,7 +390,7 @@ private static (OpenApiRuntime[], Function[], ConversationStarter[]) GetRuntimes

foreach (var node in currentNode.Children)
{
var (childRuntimes, childFunctions, childConversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(document, authInformation, node.Value, openApiDocumentPath);
var (childRuntimes, childFunctions, childConversationStarters) = GetRuntimesFunctionsAndConversationStartersFromTree(document, authInformation, node.Value, openApiDocumentPath, logger);
runtimes.AddRange(childRuntimes);
functions.AddRange(childFunctions);
conversationStarters.AddRange(childConversationStarters);
Expand All @@ -391,7 +405,7 @@ private static Auth GetAuth(IList<OpenApiSecurityRequirement> securityRequiremen
const string tooManySchemesError = "Multiple security requirements are not supported. Operations can only list one security requirement.";
if (securityRequirements.Count > 1 || securityRequirements.FirstOrDefault()?.Keys.Count > 1)
{
throw new InvalidOperationException(tooManySchemesError);
throw new UnsupportedSecuritySchemeException(tooManySchemesError);
}
var security = securityRequirements.FirstOrDefault();
var opSecurity = security?.Keys.FirstOrDefault();
Expand Down
Loading

0 comments on commit 065a960

Please sign in to comment.