-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tool for running SQL AAD connection tests (#181)
- Loading branch information
1 parent
c6cb66f
commit 67f35d6
Showing
8 changed files
with
498 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" /> | ||
</startup> | ||
</configuration> |
70 changes: 70 additions & 0 deletions
70
tools/AzureSqlConnectionTest/AzureSqlConnectionTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{109E6F0E-CBBC-427A-89CF-8F8AACE479CF}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>AzureSqlConnectionTest</RootNamespace> | ||
<AssemblyName>AzureSqlConnectionTest</AssemblyName> | ||
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="TestApplication.cs" /> | ||
<Compile Include="TestRunner.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NuGet.Services.KeyVault\NuGet.Services.KeyVault.csproj"> | ||
<Project>{c87d0ef1-54aa-4b0b-89de-cff2dc941d11}</Project> | ||
<Name>NuGet.Services.KeyVault</Name> | ||
</ProjectReference> | ||
<ProjectReference Include="..\..\src\NuGet.Services.Sql\NuGet.Services.Sql.csproj"> | ||
<Project>{f5121b0a-669f-48bd-86dc-27c546d1a825}</Project> | ||
<Name>NuGet.Services.Sql</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.CommandLineUtils"> | ||
<Version>1.1.1</Version> | ||
</PackageReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace AzureSqlConnectionTest | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
new TestApplication().Execute(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("AzureSqlConnectionTest")] | ||
[assembly: AssemblyDescription("Tool for testing AAD authenticated SQL connections using NuGet.Services.Sql")] | ||
[assembly: AssemblyProduct("AzureSqlConnectionTest")] | ||
[assembly: AssemblyCopyright("Copyright © .NET Foundation 2017")] | ||
[assembly: ComVisible(false)] | ||
[assembly: Guid("109e6f0e-cbbc-427a-89cf-8f8aace479cf")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.CommandLineUtils; | ||
using NuGet.Services.KeyVault; | ||
|
||
namespace AzureSqlConnectionTest | ||
{ | ||
public class TestApplication : CommandLineApplication | ||
{ | ||
public CommandOption Help { get; } | ||
|
||
public CommandOption KeyVaultName { get; } | ||
|
||
public CommandOption KeyVaultClientId { get; } | ||
|
||
public CommandOption KeyVaultCertificateThumbprint { get; } | ||
|
||
public CommandOption ConnectionString { get; } | ||
|
||
public CommandOption Count { get; } | ||
|
||
public CommandOption PersistConnections { get; } | ||
|
||
public CommandOption DurationInSeconds { get; } | ||
|
||
public CommandOption IntervalInSeconds { get; } | ||
|
||
public CommandOption SpawnClients { get; } | ||
|
||
public CommandOption UseAdalOnly { get; } | ||
|
||
public TestApplication() | ||
{ | ||
Help = HelpOption("-? | -h | --help"); | ||
|
||
KeyVaultName = Option("-kv | --keyVaultName", "KeyVault name", CommandOptionType.SingleValue); | ||
KeyVaultClientId = Option("-kvcid | --keyVaultClientId", "KeyVault client id", CommandOptionType.SingleValue); | ||
KeyVaultCertificateThumbprint = Option("-kvct | --keyVaultCertThumbprint", "KeyVault certificate thumbprint", CommandOptionType.SingleValue); | ||
|
||
ConnectionString = Option("-cs | --connectionString", "SQL connection string", CommandOptionType.SingleValue); | ||
|
||
Count = Option("-c | --count", "Client count", CommandOptionType.SingleValue); | ||
PersistConnections = Option("-p | --persist", "Persist connections", CommandOptionType.NoValue); | ||
DurationInSeconds = Option("-d | --duration", "Duration in seconds", CommandOptionType.SingleValue); | ||
IntervalInSeconds = Option("-i | --interval", "Sleep interval in seconds", CommandOptionType.SingleValue); | ||
|
||
SpawnClients = Option("-spawn", "Spawn client processes", CommandOptionType.NoValue); | ||
UseAdalOnly = Option("-adal", "Use ADAL only (default token cache)", CommandOptionType.NoValue); | ||
|
||
OnExecute(() => ExecuteAsync().GetAwaiter().GetResult()); | ||
} | ||
|
||
public async Task<int> ExecuteAsync() | ||
{ | ||
if (!Help.HasValue()) | ||
{ | ||
var count = IntValue(Count, defaultValue: 1); | ||
var durationInSeconds = IntValue(DurationInSeconds, defaultValue: 60); | ||
var intervalInSeconds = IntValue(IntervalInSeconds, defaultValue: 15); | ||
|
||
if (count > 1 && SpawnClients.HasValue()) | ||
{ | ||
return await SpawnTestClients(count, durationInSeconds, intervalInSeconds); | ||
} | ||
|
||
var persist = PersistConnections.HasValue(); | ||
var useAdalOnly = UseAdalOnly.HasValue(); | ||
|
||
if (KeyVaultName.HasValue() | ||
&& KeyVaultClientId.HasValue() | ||
&& KeyVaultCertificateThumbprint.HasValue()) | ||
{ | ||
using (var kvCertificate = GetKeyVaultCertificate(KeyVaultCertificateThumbprint.Value())) | ||
{ | ||
var keyVaultConfig = new KeyVaultConfiguration( | ||
KeyVaultName.Value(), | ||
KeyVaultClientId.Value(), | ||
kvCertificate); | ||
|
||
var runner = new TestRunner( | ||
ConnectionString.Value(), | ||
keyVaultConfig); | ||
|
||
return await runner.TestConnectionsAsync( | ||
count, | ||
durationInSeconds, | ||
intervalInSeconds, | ||
persist, | ||
useAdalOnly); | ||
} | ||
} | ||
} | ||
|
||
ShowHelp(); | ||
|
||
return 1; | ||
} | ||
|
||
private async Task<int> SpawnTestClients(int count, int durationInSeconds, int intervalInSeconds) | ||
{ | ||
var clients = new Task<int>[count]; | ||
for (int i = 0; i < count; i++) | ||
{ | ||
clients[i] = Task.Run(() => SpawnTestClient(durationInSeconds, intervalInSeconds)); | ||
} | ||
|
||
await Task.WhenAll(clients); | ||
|
||
return clients.Count(c => c.Result != 0); | ||
} | ||
|
||
private int SpawnTestClient(int durationInSeconds, int intervalInSeconds) | ||
{ | ||
var commandLine = GetTestClientCommandLine(); | ||
|
||
var client = Process.Start(commandLine[0], commandLine[1]); | ||
if (!client.WaitForExit((durationInSeconds + (intervalInSeconds * 2)) * 1000)) | ||
{ | ||
client.Kill(); | ||
return 1; | ||
} | ||
|
||
return client.ExitCode; | ||
} | ||
|
||
private static string[] GetTestClientCommandLine() | ||
{ | ||
var commandLine = Environment.CommandLine; | ||
|
||
commandLine = Regex.Replace(commandLine, | ||
"-c(ount)? \\d+", | ||
"", | ||
RegexOptions.None, | ||
TimeSpan.FromSeconds(5)); | ||
|
||
commandLine = Regex.Replace(commandLine, | ||
"-spawn", | ||
"", | ||
RegexOptions.None, | ||
TimeSpan.FromSeconds(5)); | ||
|
||
var parts = commandLine.Split(' '); | ||
var arguments = parts | ||
.Skip(1) | ||
.Where(p => !String.IsNullOrEmpty(p)); | ||
|
||
return new[] { parts[0], String.Join(" ", arguments) }; | ||
} | ||
|
||
private static int IntValue(CommandOption option, int defaultValue = -1) | ||
{ | ||
return option.HasValue() && Int32.TryParse(option.Value(), out var result) | ||
? result | ||
: defaultValue; | ||
} | ||
|
||
private static X509Certificate2 GetKeyVaultCertificate(string kvCertThumbprint) | ||
{ | ||
return CertificateUtility.FindCertificateByThumbprint( | ||
StoreName.My, StoreLocation.LocalMachine, kvCertThumbprint, validationRequired: true); | ||
} | ||
} | ||
} |
Oops, something went wrong.