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

#4176 Add new option to disable SSL Validation in arguments #4617

Merged
merged 8 commits into from
May 20, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for multipart form data request body in PHP. [#3029](https://github.com/microsoft/kiota/issues/3029)
- Added uri-form encoded serialization for PHP. [#2074](https://github.com/microsoft/kiota/issues/2074)
- Added information message with base URL in the CLI experience. [#4635](https://github.com/microsoft/kiota/issues/4635)
- Added optional parameter --disable-ssl-validation for generate, show, and download commands. [#4176](https://github.com/microsoft/kiota/issues/4176)

### Changed

Expand Down
7 changes: 6 additions & 1 deletion src/Kiota.Builder/Configuration/DownloadConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ public bool CleanOutput
{
get; set;
}
public bool DisableSSLValidation
{
get; set;
}

public object Clone()
{
return new DownloadConfiguration
{
OutputPath = OutputPath,
CleanOutput = CleanOutput,
ClearCache = ClearCache
ClearCache = ClearCache,
DisableSSLValidation = DisableSSLValidation,
};
}
}
6 changes: 6 additions & 0 deletions src/Kiota.Builder/Configuration/GenerationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public object Clone()
Operation = Operation,
PatternsOverride = new(PatternsOverride ?? Enumerable.Empty<string>(), StringComparer.OrdinalIgnoreCase),
PluginTypes = new(PluginTypes ?? Enumerable.Empty<PluginType>()),
DisableSSLValidation = DisableSSLValidation,
};
}
private static readonly StringIEnumerableDeepComparer comparer = new();
Expand Down Expand Up @@ -200,6 +201,11 @@ private string NormalizeDescriptionLocation(string targetDirectory)
return OpenAPIFilePath;
}
public bool IsPluginConfiguration => PluginTypes.Count != 0;

public bool DisableSSLValidation
baywet marked this conversation as resolved.
Show resolved Hide resolved
{
get; set;
}
}
#pragma warning restore CA1056
#pragma warning restore CA2227
9 changes: 9 additions & 0 deletions src/Kiota.Builder/Lock/KiotaLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public bool IncludeAdditionalData
{
get; set;
}
/// <summary>
/// Whether SSL Validation was disabled for this client.
/// </summary>
public bool DisableSSLValidation
{
get; set;
}
#pragma warning disable CA2227
/// <summary>
/// The serializers used for this client.
Expand Down Expand Up @@ -108,6 +115,7 @@ public void UpdateGenerationConfigurationFromLock(GenerationConfiguration config
config.ExcludePatterns = ExcludePatterns;
config.OpenAPIFilePath = DescriptionLocation;
config.DisabledValidationRules = DisabledValidationRules;
config.DisableSSLValidation = DisableSSLValidation;
}
/// <summary>
/// Initializes a new instance of the <see cref="KiotaLock"/> class.
Expand Down Expand Up @@ -135,5 +143,6 @@ public KiotaLock(GenerationConfiguration config)
ExcludePatterns = config.ExcludePatterns;
DescriptionLocation = config.OpenAPIFilePath;
DisabledValidationRules = config.DisabledValidationRules;
DisableSSLValidation = config.DisableSSLValidation;
}
}
1 change: 1 addition & 0 deletions src/Kiota.Builder/Lock/KiotaLockComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public int GetHashCode([DisallowNull] KiotaLock obj)
{
if (obj == null) return 0;
return
obj.DisableSSLValidation.GetHashCode() * 59 +
_stringIEnumerableDeepComparer.GetHashCode(obj.DisabledValidationRules?.Order(StringComparer.OrdinalIgnoreCase) ?? Enumerable.Empty<string>()) * 53 +
obj.KiotaVersion.GetHashCode(StringComparison.OrdinalIgnoreCase) * 47 +
obj.LockFileVersion.GetHashCode(StringComparison.OrdinalIgnoreCase) * 43 +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"**/generate",
"**/repos/**/topics"
],
"disabledValidationRules": []
}
"disabledValidationRules": [],
"disableSSLValidation": false
}
26 changes: 25 additions & 1 deletion src/kiota/Handlers/BaseKiotaCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ protected static void DefaultSerializersAndDeserializers(GenerationConfiguration
Logger = logger,
FileName = "pat-api.github.com"
};
internal static readonly HttpClient httpClient = new();

private HttpClient? _httpClient;
protected HttpClient httpClient
{
get
{
_httpClient ??= GetHttpClient();
return _httpClient;
}
}
public required Option<LogLevel> LogLevelOption
{
get; init;
Expand All @@ -52,6 +61,21 @@ protected KiotaConfiguration Configuration
configObject.BindConfiguration(configuration);
return configObject;
});

protected HttpClient GetHttpClient()
{
var httpClientHandler = new HttpClientHandler();
baywet marked this conversation as resolved.
Show resolved Hide resolved
if (Configuration.Generation.DisableSSLValidation)
httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

var httpClient = new HttpClient(httpClientHandler);

disposables.Add(httpClientHandler);
disposables.Add(httpClient);

return httpClient;
}

private const string GitHubScope = "repo";
private Func<CancellationToken, Task<bool>> GetIsGitHubDeviceSignedInCallback(ILogger logger) => (cancellationToken) =>
{
Expand Down
6 changes: 6 additions & 0 deletions src/kiota/Handlers/KiotaDownloadCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ public required Option<bool> CleanOutputOption
{
get; init;
}
public required Option<bool> DisableSSLValidationOption
{
get; init;
}
public override async Task<int> InvokeAsync(InvocationContext context)
{
string searchTerm = context.ParseResult.GetValueForArgument(SearchTermArgument);
string version = context.ParseResult.GetValueForOption(VersionOption) ?? string.Empty;
string outputPath = context.ParseResult.GetValueForOption(OutputPathOption) ?? string.Empty;
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
bool clearCache = context.ParseResult.GetValueForOption(ClearCacheOption);
bool disableSSLValidation = context.ParseResult.GetValueForOption(DisableSSLValidationOption);
CancellationToken cancellationToken = context.BindingContext.GetService(typeof(CancellationToken)) is CancellationToken token ? token : CancellationToken.None;

Configuration.Download.ClearCache = clearCache;
Configuration.Download.DisableSSLValidation = disableSSLValidation;
Configuration.Download.CleanOutput = cleanOutput;
Configuration.Download.OutputPath = NormalizeSlashesInPath(outputPath);

Expand Down
6 changes: 6 additions & 0 deletions src/kiota/Handlers/KiotaGenerateCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
bool backingStore = context.ParseResult.GetValueForOption(BackingStoreOption);
bool excludeBackwardCompatible = context.ParseResult.GetValueForOption(ExcludeBackwardCompatibleOption);
bool clearCache = context.ParseResult.GetValueForOption(ClearCacheOption);
bool disableSSLValidation = context.ParseResult.GetValueForOption(DisableSSLValidationOption);
bool includeAdditionalData = context.ParseResult.GetValueForOption(AdditionalDataOption);
string className = context.ParseResult.GetValueForOption(ClassOption) ?? string.Empty;
string namespaceName = context.ParseResult.GetValueForOption(NamespaceOption) ?? string.Empty;
Expand Down Expand Up @@ -112,6 +113,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
Configuration.Generation.ApiManifestPath = NormalizeSlashesInPath(GetAbsolutePath(Configuration.Generation.ApiManifestPath));
Configuration.Generation.CleanOutput = cleanOutput;
Configuration.Generation.ClearCache = clearCache;
Configuration.Generation.DisableSSLValidation = disableSSLValidation;

var (loggerFactory, logger) = GetLoggerAndFactory<KiotaBuilder>(context, Configuration.Generation.OutputPath);
using (loggerFactory)
Expand Down Expand Up @@ -173,4 +175,8 @@ public required Option<bool> ExcludeBackwardCompatibleOption
get;
set;
}
public required Option<bool> DisableSSLValidationOption
baywet marked this conversation as resolved.
Show resolved Hide resolved
{
get; init;
}
}
6 changes: 6 additions & 0 deletions src/kiota/Handlers/KiotaShowCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public required Option<string> ManifestOption
{
get; init;
}
public required Option<bool> DisableSSLValidationOption
{
get; init;
}

public override async Task<int> InvokeAsync(InvocationContext context)
{
Expand All @@ -52,11 +56,13 @@ public override async Task<int> InvokeAsync(InvocationContext context)
List<string> includePatterns = context.ParseResult.GetValueForOption(IncludePatternsOption) ?? new List<string>();
List<string> excludePatterns = context.ParseResult.GetValueForOption(ExcludePatternsOption) ?? new List<string>();
bool clearCache = context.ParseResult.GetValueForOption(ClearCacheOption);
bool disableSSLValidation = context.ParseResult.GetValueForOption(DisableSSLValidationOption);
CancellationToken cancellationToken = context.BindingContext.GetService(typeof(CancellationToken)) is CancellationToken token ? token : CancellationToken.None;

var (loggerFactory, logger) = GetLoggerAndFactory<KiotaBuilder>(context);

Configuration.Search.ClearCache = clearCache;
Configuration.Generation.DisableSSLValidation = disableSSLValidation;
using (loggerFactory)
{
await CheckForNewVersionAsync(logger, cancellationToken).ConfigureAwait(false);
Expand Down
19 changes: 19 additions & 0 deletions src/kiota/KiotaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private static Command GetShowCommand()
var searchTermOption = GetSearchKeyOption();
var maxDepthOption = new Option<uint>("--max-depth", () => 5, "The maximum depth of the tree to display");
maxDepthOption.AddAlias("--m-d");
var disableSSLValidationOption = GetDisableSSLValidationOption(defaultGenerationConfiguration.DisableSSLValidation);
var displayCommand = new Command("show", "Displays the API tree in a given description."){
searchTermOption,
logLevelOption,
Expand All @@ -163,6 +164,7 @@ private static Command GetShowCommand()
includePatterns,
excludePatterns,
clearCacheOption,
disableSSLValidationOption,
};
displayCommand.Handler = new KiotaShowCommandHandler
{
Expand All @@ -175,6 +177,7 @@ private static Command GetShowCommand()
IncludePatternsOption = includePatterns,
ExcludePatternsOption = excludePatterns,
ClearCacheOption = clearCacheOption,
DisableSSLValidationOption = disableSSLValidationOption,
};
return displayCommand;
}
Expand Down Expand Up @@ -213,13 +216,16 @@ private static Command GetDownloadCommand()

var outputOption = GetOutputPathOption(defaultConfiguration.OutputPath);

var disableSSLValidationOption = GetDisableSSLValidationOption(defaultConfiguration.DisableSSLValidation);

var searchCommand = new Command("download", "Downloads an OpenAPI description from multiple registries."){
keyArgument,
logLevelOption,
clearCacheOption,
versionOption,
cleanOutputOption,
outputOption,
disableSSLValidationOption,
};
searchCommand.Handler = new KiotaDownloadCommandHandler
{
Expand All @@ -229,6 +235,7 @@ private static Command GetDownloadCommand()
VersionOption = versionOption,
CleanOutputOption = cleanOutputOption,
OutputPathOption = outputOption,
DisableSSLValidationOption = disableSSLValidationOption,
};
return searchCommand;
}
Expand Down Expand Up @@ -444,6 +451,8 @@ private static Command GetGenerateCommand()

var clearCacheOption = GetClearCacheOption(defaultConfiguration.ClearCache);

var disableSSLValidationOption = GetDisableSSLValidationOption(defaultConfiguration.DisableSSLValidation);

var command = new Command("generate", "Generates a REST HTTP API client from an OpenAPI description file.") {
descriptionOption,
manifestOption,
Expand All @@ -463,6 +472,7 @@ private static Command GetGenerateCommand()
excludePatterns,
dvrOption,
clearCacheOption,
disableSSLValidationOption,
};
command.Handler = new KiotaGenerateCommandHandler
{
Expand All @@ -484,6 +494,7 @@ private static Command GetGenerateCommand()
ExcludePatternsOption = excludePatterns,
DisabledValidationRulesOption = dvrOption,
ClearCacheOption = clearCacheOption,
DisableSSLValidationOption = disableSSLValidationOption,
};
return command;
}
Expand Down Expand Up @@ -541,6 +552,14 @@ private static Option<bool> GetClearCacheOption(bool defaultValue)
clearCacheOption.AddAlias("--cc");
return clearCacheOption;
}

private static Option<bool> GetDisableSSLValidationOption(bool defaultValue)
{
baywet marked this conversation as resolved.
Show resolved Hide resolved
var disableSSLValidationOption = new Option<bool>("--disable-ssl-validation", () => defaultValue, "Disables SSL certificate validation.");
disableSSLValidationOption.AddAlias("--dsv");
return disableSSLValidationOption;
}

private static void AddStringRegexValidator(Option<string> option, Regex validator, string parameterName, bool allowEmpty = false)
{
option.AddValidator(input =>
Expand Down
2 changes: 1 addition & 1 deletion src/kiota/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"kiota": {
"commandName": "Project",
"commandLineArgs": "--openapi C:\\src\\msgraph-sdk-powershell\\openApiDocs\\v1.0\\mail.yml -o C:\\Users\\darrmi\\source\\github\\darrelmiller\\OpenApiClient\\Generated -c GraphClient --loglevel Information"
"commandLineArgs": "generate --openapi https://localhost:3000/swagger.json -o E:\\OSS\\kiota\\Output\\local_3 -c GraphClient --log-level Information -l CSharp --dsv"
}
}
}
Loading