From 34f94ed24294ee1b5e241e3584f4b04936227a9d Mon Sep 17 00:00:00 2001 From: f-alizada <104755925+f-alizada@users.noreply.github.com> Date: Fri, 27 May 2022 14:14:12 +0200 Subject: [PATCH] Add possibility to parametrize API OAuth2 scope settings (#723) * Add parametrization possibility for api oauth2 scope * Fix tests concurrency issue with mock. Add oauth2 scope parameter to master template * Introduce ApiParameters variable for extractor, deprecate serviceUrlParameters * Update documentation according to new api parameter Co-authored-by: Farhad Alizada --- .../ExtractorConsoleAppConfiguration.cs | 12 ++- .../Common/Constants/GlobalConstants.cs | 2 + .../TemplateParameterProperties.cs | 2 +- .../Templates/Builders/TemplateBuilder.cs | 27 ++++-- .../EntityExtractors/APIExtractor.cs | 8 ++ .../EntityExtractors/BackendExtractor.cs | 2 +- .../MasterTemplateExtractor.cs | 14 ++++ .../EntityExtractors/ParametersExtractor.cs | 46 +++++++++- .../Extractor/Models/ApiParameterProperty.cs | 20 +++++ .../Extractor/Models/ExtractorParameters.cs | 15 ++-- src/README.md | 37 +++++---- .../Abstractions/ExtractorMockerTestsBase.cs | 16 ++-- .../ExtractorParametersCreationTests.cs | 2 +- .../Extractor/Scenarios/ApiExtractorTests.cs | 25 +++++- .../Scenarios/MasterTemplateExtractorTests.cs | 27 ++++-- .../Scenarios/ParametersExtractorTests.cs | 64 +++++++++++++- .../Moqs/ApiClients/MockApisClient.cs | 83 +++++++++++-------- 17 files changed, 312 insertions(+), 90 deletions(-) create mode 100644 src/ArmTemplates/Extractor/Models/ApiParameterProperty.cs diff --git a/src/ArmTemplates/Commands/Configurations/ExtractorConsoleAppConfiguration.cs b/src/ArmTemplates/Commands/Configurations/ExtractorConsoleAppConfiguration.cs index 88efe63a..e3b069e7 100644 --- a/src/ArmTemplates/Commands/Configurations/ExtractorConsoleAppConfiguration.cs +++ b/src/ArmTemplates/Commands/Configurations/ExtractorConsoleAppConfiguration.cs @@ -3,6 +3,7 @@ // Licensed under the MIT License. // -------------------------------------------------------------------------- +using System.Collections.Generic; using CommandLine; using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants; using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models; @@ -60,9 +61,6 @@ public class ExtractorConsoleAppConfiguration [Option(longName: "baseFileName", HelpText = "Specify base name of the template file")] public string BaseFileName { get; set; } - // this is used only from json-file and --extractorConfig option, so no option possibility here - public ServiceUrlProperty[] ServiceUrlParameters { get; set; } - [Option(longName: "paramServiceUrl", HelpText = "Parameterize serviceUrl")] public string ParamServiceUrl { get; set; } @@ -98,5 +96,13 @@ public class ExtractorConsoleAppConfiguration [Option(longName: "overrideProductGuids", HelpText = "Override product GUID identification to system generated")] public string OverrideProductGuids { get; set; } + + [Option(longName: "paramApiOauth2Scope", HelpText = "Parametrize API OAuth2 scope values")] + public string ParamApiOauth2Scope { get; set; } + + /// + /// Api parameter properties for overriding Api OAuth2 scope or/and Service urloverride. Available via extractor-config file only. + /// + public Dictionary ApiParameters { get; set; } } } \ No newline at end of file diff --git a/src/ArmTemplates/Common/Constants/GlobalConstants.cs b/src/ArmTemplates/Common/Constants/GlobalConstants.cs index 4a9e51f7..dc44ac83 100644 --- a/src/ArmTemplates/Common/Constants/GlobalConstants.cs +++ b/src/ArmTemplates/Common/Constants/GlobalConstants.cs @@ -39,6 +39,7 @@ public static class ParameterNames public const string LinkedTemplatesBaseUrl = "linkedTemplatesBaseUrl"; public const string NamedValueKeyVaultSecrets = "namedValueKeyVaultSecrets"; public const string BackendSettings = "backendSettings"; + public const string ApiOauth2ScopeSettings = "apiOauth2ScopeSettings"; } public static class ParameterPrefix @@ -48,5 +49,6 @@ public static class ParameterPrefix public const string Property = "Property"; public const string LogResourceId = "LogResourceId"; public const string Backend = "Backend"; + public const string ApiOauth2Scope = "ApiOauth2Scope"; } } \ No newline at end of file diff --git a/src/ArmTemplates/Common/Templates/Abstractions/TemplateParameterProperties.cs b/src/ArmTemplates/Common/Templates/Abstractions/TemplateParameterProperties.cs index ef1a4ec8..e8c03f30 100644 --- a/src/ArmTemplates/Common/Templates/Abstractions/TemplateParameterProperties.cs +++ b/src/ArmTemplates/Common/Templates/Abstractions/TemplateParameterProperties.cs @@ -26,6 +26,6 @@ public TemplateParameterProperties(string metadataDescription, string type) Description = metadataDescription, }; this.Type = type; - } + } } } diff --git a/src/ArmTemplates/Common/Templates/Builders/TemplateBuilder.cs b/src/ArmTemplates/Common/Templates/Builders/TemplateBuilder.cs index 4bbbc0d9..892bd5c8 100644 --- a/src/ArmTemplates/Common/Templates/Builders/TemplateBuilder.cs +++ b/src/ArmTemplates/Common/Templates/Builders/TemplateBuilder.cs @@ -60,7 +60,8 @@ public TemplateBuilder GenerateTemplateWithPresetProperties(ExtractorParameters => this.GenerateTemplateWithApimServiceNameProperty() .AddPolicyProperties(extractorParameters) .AddParameterizedServiceUrlProperty(extractorParameters) - .AddParameterizedApiLoggerIdProperty(extractorParameters); + .AddParameterizedApiLoggerIdProperty(extractorParameters) + .AddParameterizedApiScopeProperty(extractorParameters); public TemplateBuilder GenerateEmptyTemplate() { @@ -82,7 +83,7 @@ public TemplateBuilder AddPolicyProperties(ExtractorParameters extractorParamete { if (extractorParameters.PolicyXMLBaseUrl != null) { - TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties() + var policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties() { Type = "string" }; @@ -91,7 +92,7 @@ public TemplateBuilder AddPolicyProperties(ExtractorParameters extractorParamete if (extractorParameters.PolicyXMLSasToken != null) { - TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties() + var policyTemplateSasTokenParameterProperties = new TemplateParameterProperties() { Type = "string" }; @@ -104,9 +105,9 @@ public TemplateBuilder AddPolicyProperties(ExtractorParameters extractorParamete public TemplateBuilder AddParameterizedServiceUrlProperty(ExtractorParameters extractorParameters) { - if (extractorParameters.ParameterizeServiceUrl || extractorParameters.ServiceUrlParameters != null && extractorParameters.ServiceUrlParameters.Length > 0) + if (extractorParameters.ParameterizeServiceUrl) { - TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties() + var serviceUrlParamProperty = new TemplateParameterProperties() { Type = "object" }; @@ -116,11 +117,25 @@ public TemplateBuilder AddParameterizedServiceUrlProperty(ExtractorParameters ex return this; } + public TemplateBuilder AddParameterizedApiScopeProperty(ExtractorParameters extractorParameters) + { + if (extractorParameters.ParametrizeApiOauth2Scope) + { + var apiScopeParameterProperty = new TemplateParameterProperties() + { + Type = "object" + }; + this.template.Parameters.Add(ParameterNames.ApiOauth2ScopeSettings, apiScopeParameterProperty); + } + + return this; + } + public TemplateBuilder AddParameterizedApiLoggerIdProperty(ExtractorParameters extractorParameters) { if (extractorParameters.ParameterizeApiLoggerId) { - TemplateParameterProperties apiLoggerProperty = new TemplateParameterProperties() + var apiLoggerProperty = new TemplateParameterProperties() { Type = "object" }; diff --git a/src/ArmTemplates/Extractor/EntityExtractors/APIExtractor.cs b/src/ArmTemplates/Extractor/EntityExtractors/APIExtractor.cs index d46ebcdf..05206d20 100644 --- a/src/ArmTemplates/Extractor/EntityExtractors/APIExtractor.cs +++ b/src/ArmTemplates/Extractor/EntityExtractors/APIExtractor.cs @@ -145,6 +145,14 @@ void SetArmTemplateValuesToApiTemplateResource(ApiTemplateResource apiResource, apiResource.Properties.ServiceUrl = $"[parameters('{ParameterNames.ServiceUrl}').{ParameterNamingHelper.GenerateValidParameterName(originalServiceApiName, ParameterPrefix.Api)}]"; } + if (extractorParameters.ParametrizeApiOauth2Scope) + { + if (apiResource.Properties.AuthenticationSettings?.OAuth2?.Scope is not null) + { + apiResource.Properties.AuthenticationSettings.OAuth2.Scope = $"[parameters('{ParameterNames.ApiOauth2ScopeSettings}').{ParameterNamingHelper.GenerateValidParameterName(originalServiceApiName, ParameterPrefix.ApiOauth2Scope)}]"; + } + } + if (apiResource.Properties.ApiVersionSetId != null) { apiResource.DependsOn = Array.Empty(); diff --git a/src/ArmTemplates/Extractor/EntityExtractors/BackendExtractor.cs b/src/ArmTemplates/Extractor/EntityExtractors/BackendExtractor.cs index 1338c45c..a5e3fb77 100644 --- a/src/ArmTemplates/Extractor/EntityExtractors/BackendExtractor.cs +++ b/src/ArmTemplates/Extractor/EntityExtractors/BackendExtractor.cs @@ -110,7 +110,7 @@ void SaveBackendApiParametersToCache() } var backendApiParameters = new BackendApiParameters(); - var backendValidName = ParameterNamingHelper.GenerateValidParameterName(originalBackendName, ParameterPrefix.Diagnostic).ToLower(); + var backendValidName = ParameterNamingHelper.GenerateValidParameterName(originalBackendName, ParameterPrefix.Backend).ToLower(); if (!string.IsNullOrEmpty(backendResource.Properties.ResourceId)) { diff --git a/src/ArmTemplates/Extractor/EntityExtractors/MasterTemplateExtractor.cs b/src/ArmTemplates/Extractor/EntityExtractors/MasterTemplateExtractor.cs index f1d60bdc..605a5066 100644 --- a/src/ArmTemplates/Extractor/EntityExtractors/MasterTemplateExtractor.cs +++ b/src/ArmTemplates/Extractor/EntityExtractors/MasterTemplateExtractor.cs @@ -262,10 +262,17 @@ static MasterTemplateResource CreateLinkedMasterTemplateResourceForApiTemplate(s { masterResourceTemplate.Properties.Parameters.Add(ParameterNames.ServiceUrl, new TemplateParameterProperties() { Value = $"[parameters('{ParameterNames.ServiceUrl}')]" }); } + if (extractorParameters.ParameterizeApiLoggerId) { masterResourceTemplate.Properties.Parameters.Add(ParameterNames.ApiLoggerId, new TemplateParameterProperties() { Value = $"[parameters('{ParameterNames.ApiLoggerId}')]" }); } + + if (extractorParameters.ParametrizeApiOauth2Scope) + { + masterResourceTemplate.Properties.Parameters.Add(ParameterNames.ApiOauth2ScopeSettings, new TemplateParameterProperties() { Value = $"[parameters('{ParameterNames.ApiOauth2ScopeSettings}')]" }); + } + return masterResourceTemplate; } @@ -423,6 +430,13 @@ Dictionary CreateMasterTemplateParameters(E new TemplateParameterProperties(metadataDescription: "The settings for the Backends", type: "object")); } + if (extractorParameters.ParametrizeApiOauth2Scope) + { + parameters.Add( + ParameterNames.ApiOauth2ScopeSettings, + new TemplateParameterProperties(metadataDescription: "The settings for the APIs Oauth2 Scope values", type: "object")); + } + return parameters; } diff --git a/src/ArmTemplates/Extractor/EntityExtractors/ParametersExtractor.cs b/src/ArmTemplates/Extractor/EntityExtractors/ParametersExtractor.cs index 318ab497..66193c81 100644 --- a/src/ArmTemplates/Extractor/EntityExtractors/ParametersExtractor.cs +++ b/src/ArmTemplates/Extractor/EntityExtractors/ParametersExtractor.cs @@ -54,6 +54,7 @@ public async Task