Kusto NoAuth authentication (#1161)

* Added username and password to DataSourceConnectionDetails. Refactored KustoClient>GetKustoConnectionStringBuilder to accept no username or password for no credentials authentication.

* Removed invalid Unit Test and added 2 unit tests for testing authentication type.

* Added validation for dstsAuth and AzureMFA in DataSourceFactory. Added unit test for validation.
This commit is contained in:
Justin M
2021-02-23 17:29:09 -08:00
committed by GitHub
parent 294a6f4dc2
commit 13a7d4aa2b
4 changed files with 39 additions and 22 deletions

View File

@@ -7,5 +7,7 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Contracts
public string UserToken { get; set; } public string UserToken { get; set; }
public string ConnectionString { get; set; } public string ConnectionString { get; set; }
public string AuthenticationType { get; set; } public string AuthenticationType { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
} }
} }

View File

@@ -8,8 +8,8 @@ using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.Kusto.ServiceLayer.DataSource.Intellisense; using Microsoft.Kusto.ServiceLayer.DataSource.Intellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices; using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts; using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using Microsoft.Kusto.ServiceLayer.Utility; using Microsoft.Kusto.ServiceLayer.Utility;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
namespace Microsoft.Kusto.ServiceLayer.DataSource namespace Microsoft.Kusto.ServiceLayer.DataSource
{ {
@@ -18,8 +18,6 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource
{ {
public IDataSource Create(DataSourceType dataSourceType, ConnectionDetails connectionDetails, string ownerUri) public IDataSource Create(DataSourceType dataSourceType, ConnectionDetails connectionDetails, string ownerUri)
{ {
ValidationUtils.IsArgumentNotNullOrWhiteSpace(connectionDetails.AccountToken, nameof(connectionDetails.AccountToken));
switch (dataSourceType) switch (dataSourceType)
{ {
case DataSourceType.Kusto: case DataSourceType.Kusto:
@@ -39,13 +37,21 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource
private DataSourceConnectionDetails MapKustoConnectionDetails(ConnectionDetails connectionDetails) private DataSourceConnectionDetails MapKustoConnectionDetails(ConnectionDetails connectionDetails)
{ {
if (connectionDetails.AuthenticationType == "dstsAuth" || connectionDetails.AuthenticationType == "AzureMFA")
{
ValidationUtils.IsTrue<ArgumentException>(!string.IsNullOrWhiteSpace(connectionDetails.AccountToken),
$"The Kusto User Token is not specified - set {nameof(connectionDetails.AccountToken)}");
}
return new DataSourceConnectionDetails return new DataSourceConnectionDetails
{ {
ServerName = connectionDetails.ServerName, ServerName = connectionDetails.ServerName,
DatabaseName = connectionDetails.DatabaseName, DatabaseName = connectionDetails.DatabaseName,
ConnectionString = connectionDetails.ConnectionString, ConnectionString = connectionDetails.ConnectionString,
AuthenticationType = connectionDetails.AuthenticationType, AuthenticationType = connectionDetails.AuthenticationType,
UserToken = connectionDetails.AccountToken UserToken = connectionDetails.AccountToken,
UserName = connectionDetails.UserName,
Password = connectionDetails.Password
}; };
} }

View File

@@ -74,9 +74,6 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource
private KustoConnectionStringBuilder GetKustoConnectionStringBuilder(DataSourceConnectionDetails connectionDetails) private KustoConnectionStringBuilder GetKustoConnectionStringBuilder(DataSourceConnectionDetails connectionDetails)
{ {
ValidationUtils.IsTrue<ArgumentException>(!string.IsNullOrWhiteSpace(connectionDetails.UserToken),
$"The Kusto User Token is not specified - set {nameof(connectionDetails.UserToken)}");
var stringBuilder = string.IsNullOrWhiteSpace(connectionDetails.ConnectionString) var stringBuilder = string.IsNullOrWhiteSpace(connectionDetails.ConnectionString)
? new KustoConnectionStringBuilder(connectionDetails.ServerName, connectionDetails.DatabaseName) ? new KustoConnectionStringBuilder(connectionDetails.ServerName, connectionDetails.DatabaseName)
: new KustoConnectionStringBuilder(connectionDetails.ConnectionString); : new KustoConnectionStringBuilder(connectionDetails.ConnectionString);
@@ -87,10 +84,16 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource
stringBuilder.InitialCatalog = databaseName; stringBuilder.InitialCatalog = databaseName;
ValidationUtils.IsNotNull(ClusterName, nameof(ClusterName)); ValidationUtils.IsNotNull(ClusterName, nameof(ClusterName));
return connectionDetails.AuthenticationType == "dstsAuth" switch (connectionDetails.AuthenticationType)
? stringBuilder.WithDstsUserTokenAuthentication(connectionDetails.UserToken) {
: stringBuilder.WithAadUserTokenAuthentication(connectionDetails.UserToken); case "AzureMFA": return stringBuilder.WithAadUserTokenAuthentication(connectionDetails.UserToken);
case "dstsAuth": return stringBuilder.WithDstsUserTokenAuthentication(connectionDetails.UserToken);
default:
return string.IsNullOrWhiteSpace(connectionDetails.UserName) && string.IsNullOrWhiteSpace(connectionDetails.Password)
? stringBuilder
: stringBuilder.WithKustoBasicAuthentication(connectionDetails.UserName, connectionDetails.Password);
}
} }
private ClientRequestProperties GetClientRequestProperties(CancellationToken cancellationToken) private ClientRequestProperties GetClientRequestProperties(CancellationToken cancellationToken)

View File

@@ -7,17 +7,19 @@ namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
{ {
public class KustoClientTests public class KustoClientTests
{ {
[Test] [TestCase("dstsAuth")]
public void Constructor_Throws_ArgumentException_For_MissingToken() [TestCase("AzureMFA")]
{ public void Constructor_Throws_ArgumentException_For_MissingToken(string authType)
var connectionDetails = new DataSourceConnectionDetails {
{ var connectionDetails = new DataSourceConnectionDetails
UserToken = "" {
}; UserToken = "",
AuthenticationType = authType
Assert.Throws<ArgumentException>(() => new KustoClient(connectionDetails, "ownerUri")); };
}
Assert.Throws<ArgumentException>(() => new KustoClient(connectionDetails, "ownerUri"));
}
[Test] [Test]
public void Constructor_Sets_ClusterName_With_DefaultDatabaseName() public void Constructor_Sets_ClusterName_With_DefaultDatabaseName()
{ {
@@ -38,6 +40,8 @@ namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
[TestCase("dstsAuth")] [TestCase("dstsAuth")]
[TestCase("AzureMFA")] [TestCase("AzureMFA")]
[TestCase("NoAuth")]
[TestCase("SqlLogin")]
public void Constructor_Creates_Client_With_Valid_AuthenticationType(string authenticationType) public void Constructor_Creates_Client_With_Valid_AuthenticationType(string authenticationType)
{ {
string clusterName = "https://fake.url.com"; string clusterName = "https://fake.url.com";
@@ -46,7 +50,9 @@ namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
UserToken = "UserToken", UserToken = "UserToken",
ServerName = clusterName, ServerName = clusterName,
DatabaseName = "FakeDatabaseName", DatabaseName = "FakeDatabaseName",
AuthenticationType = authenticationType AuthenticationType = authenticationType,
UserName = authenticationType == "SqlLogin" ? "username": null,
Password = authenticationType == "SqlLogin" ? "password": null
}; };
var client = new KustoClient(connectionDetails, "ownerUri"); var client = new KustoClient(connectionDetails, "ownerUri");