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

Task: warn unsupported auth flows #5768

Merged
merged 23 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a938048
initial validation
Nov 11, 2024
ebec1eb
change thrown error when multiple schemes exist
thewahome Nov 11, 2024
9c992b6
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 13, 2024
8ac37bf
pass anonymous auth when exception is thrown
thewahome Nov 13, 2024
3269e17
remove unnecessary validation functions
thewahome Nov 13, 2024
0d7ee41
Add tests to support previously failing schemes
thewahome Nov 13, 2024
aebfdc5
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 13, 2024
0d98e86
enable logging to warn users
Nov 14, 2024
afe8aeb
Set warning message
Nov 14, 2024
1904a89
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 15, 2024
eb4e57e
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 18, 2024
4ba1b4d
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 18, 2024
5cda494
change message to allow filtering
thewahome Nov 18, 2024
e2e97c1
Tag warning with categpry
thewahome Nov 18, 2024
0f1485d
log authentication error warnings in output channel
thewahome Nov 18, 2024
6c6b661
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 18, 2024
df40f4e
fix message
thewahome Nov 19, 2024
0a4270a
pass kiotaOutputChannel as dependency
thewahome Nov 19, 2024
cf674db
log warnings and open output tab
thewahome Nov 19, 2024
4cf39a5
show warning message and point to logs
thewahome Nov 19, 2024
ca04555
add output channel in tests
thewahome Nov 19, 2024
36ae532
Merge branch 'main' into task/warn-unsupported-auth-flows
thewahome Nov 19, 2024
0543133
chore: formatting
baywet Nov 19, 2024
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
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
Loading