dSTS Authentication (#1125)

* Refactored Kusto.ServiceLayer to pass ConnectionDetails to DataSourceFactory instead of connection string. Created KustoConnectionDetails to map needed details to KustoClient.

* Removed unused ScriptingScriptOperation from KustoServiceLayer.

* Created DstsAuthenticationManager and moved logic for getting DstsToken. Updated error message for failing to create KustoConnection.

* Removed DstsAuthenticationManager.cs. Refactored DataSourceFactory to retrieve UserToken from ConnectionDetails.

* Renamed AzureAccountToken in ConnectionDetails to AccountToken. Changed mapping to KustoConnectionDetails based on the AccountToken.

* Removed Kusto.Data reference from ConnectionService and ScriptingListObjectsOperation. Moved creation of KustoConnectionStringBuilder to DataSourceFactory

* Added accountToken validation to DataSourceFactory Create.

* Renamed KustoConnectionDetails to DataSourceConnectionDetails. Renamed AzureToken to AuthToken.
This commit is contained in:
Justin M
2021-01-14 13:49:09 -08:00
committed by GitHub
parent 822ffb2908
commit f0a5e11d51
22 changed files with 174 additions and 438 deletions

View File

@@ -24,9 +24,9 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting.Contracts
public string ScriptDestination { get; set; }
/// <summary>
/// Gets or sets connection string of the target database the scripting operation will run against.
/// Gets or sets the target database the scripting operation will run against.
/// </summary>
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
/// <summary>
/// Gets or sets a list of scripting objects to script.

View File

@@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Kusto.ServiceLayer.Scripting.Contracts;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.SqlTools.Utility;
@@ -14,7 +13,6 @@ using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.SqlScriptPublish;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using System.Diagnostics;
using Kusto.Data;
namespace Microsoft.Kusto.ServiceLayer.Scripting
{
@@ -158,7 +156,7 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
IEnumerable<ScriptingObject> selectedObjects = new List<ScriptingObject>(this.Parameters.ScriptingObjects);
_serverName = dataSource.ClusterName;
_databaseName = new KustoConnectionStringBuilder(this.Parameters.ConnectionString).InitialCatalog;
_databaseName = Parameters.DatabaseName;
UrnCollection urnCollection = new UrnCollection();
foreach (var scriptingObject in selectedObjects)
{

View File

@@ -9,7 +9,7 @@ using Microsoft.SqlServer.Management.SqlScriptPublish;
using Microsoft.Kusto.ServiceLayer.Scripting.Contracts;
using Microsoft.SqlTools.Utility;
using System.Diagnostics;
using Kusto.Data;
using Microsoft.Kusto.ServiceLayer.DataSource;
namespace Microsoft.Kusto.ServiceLayer.Scripting
{
@@ -103,7 +103,7 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
{
try
{
var builder = new KustoConnectionStringBuilder(this.Parameters.ConnectionString);
var builder = DataSourceFactory.CreateConnectionStringBuilder(DataSourceType.Kusto, Parameters.ConnectionString);
}
catch (Exception e)
{

View File

@@ -1,296 +0,0 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Kusto.Data;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.SqlServer.Management.SqlScriptPublish;
using Microsoft.Kusto.ServiceLayer.Scripting.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.Kusto.ServiceLayer.Scripting
{
/// <summary>
/// Class to represent an in-progress script operation.
/// </summary>
public sealed class ScriptingScriptOperation : SmoScriptingOperation
{
private int scriptedObjectCount = 0;
private int totalScriptedObjectCount = 0;
private int eventSequenceNumber = 1;
public ScriptingScriptOperation(ScriptingParams parameters, IDataSource dataSource) : base(parameters, dataSource)
{
}
public override void Execute()
{
SqlScriptPublishModel publishModel = null;
try
{
this.CancellationToken.ThrowIfCancellationRequested();
this.ValidateScriptDatabaseParams();
publishModel = BuildPublishModel();
publishModel.ScriptItemsCollected += this.OnPublishModelScriptItemsCollected;
publishModel.ScriptProgress += this.OnPublishModelScriptProgress;
publishModel.ScriptError += this.OnPublishModelScriptError;
publishModel.AllowSystemObjects = true;
ScriptDestination destination = !string.IsNullOrWhiteSpace(this.Parameters.ScriptDestination)
? (ScriptDestination)Enum.Parse(typeof(ScriptDestination), this.Parameters.ScriptDestination)
: ScriptDestination.ToSingleFile;
// SMO is currently hardcoded to produce UTF-8 encoding when running on dotnet core.
ScriptOutputOptions outputOptions = new ScriptOutputOptions
{
SaveFileMode = ScriptFileMode.Overwrite,
SaveFileName = this.Parameters.FilePath,
ScriptDestination = destination,
};
this.CancellationToken.ThrowIfCancellationRequested();
publishModel.GenerateScript(outputOptions);
this.CancellationToken.ThrowIfCancellationRequested();
Logger.Write(
TraceEventType.Verbose,
string.Format(
"Sending script complete notification event for operation {0}, sequence number {1} with total count {2} and scripted count {3}",
this.OperationId,
this.eventSequenceNumber,
this.totalScriptedObjectCount,
this.scriptedObjectCount));
ScriptText = publishModel.RawScript;
this.SendCompletionNotificationEvent(new ScriptingCompleteParams
{
Success = true,
});
}
catch (Exception e)
{
if (e.IsOperationCanceledException())
{
Logger.Write(TraceEventType.Information, string.Format("Scripting operation {0} was canceled", this.OperationId));
this.SendCompletionNotificationEvent(new ScriptingCompleteParams
{
Canceled = true,
});
}
else
{
Logger.Write(TraceEventType.Error, string.Format("Scripting operation {0} failed with exception {1}", this.OperationId, e));
this.SendCompletionNotificationEvent(new ScriptingCompleteParams
{
HasError = true,
ErrorMessage = e.Message,
ErrorDetails = e.ToString(),
});
}
}
finally
{
if (publishModel != null)
{
publishModel.ScriptItemsCollected -= this.OnPublishModelScriptItemsCollected;
publishModel.ScriptProgress -= this.OnPublishModelScriptProgress;
publishModel.ScriptError -= this.OnPublishModelScriptError;
}
}
}
protected override void SendCompletionNotificationEvent(ScriptingCompleteParams parameters)
{
base.SendCompletionNotificationEvent(parameters);
}
protected override void SendPlanNotificationEvent(ScriptingPlanNotificationParams parameters)
{
base.SendPlanNotificationEvent(parameters);
}
protected override void SendProgressNotificationEvent(ScriptingProgressNotificationParams parameters)
{
base.SendProgressNotificationEvent(parameters);
}
protected override void SetCommonEventProperties(ScriptingEventParams parameters)
{
base.SetCommonEventProperties(parameters);
parameters.SequenceNumber = this.eventSequenceNumber;
this.eventSequenceNumber += 1;
}
private SqlScriptPublishModel BuildPublishModel()
{
SqlScriptPublishModel publishModel = new SqlScriptPublishModel(this.Parameters.ConnectionString);
// See if any filtering criteria was specified. If not, we're scripting the entire database. Otherwise, the filtering
// criteria should include the target objects to script.
//
bool hasObjectsSpecified = this.Parameters.ScriptingObjects != null && this.Parameters.ScriptingObjects.Any();
bool hasCriteriaSpecified =
(this.Parameters.IncludeObjectCriteria != null && this.Parameters.IncludeObjectCriteria.Any()) ||
(this.Parameters.ExcludeObjectCriteria != null && this.Parameters.ExcludeObjectCriteria.Any()) ||
(this.Parameters.IncludeSchemas != null && this.Parameters.IncludeSchemas.Any()) ||
(this.Parameters.ExcludeSchemas != null && this.Parameters.ExcludeSchemas.Any()) ||
(this.Parameters.IncludeTypes != null && this.Parameters.IncludeTypes.Any()) ||
(this.Parameters.ExcludeTypes != null && this.Parameters.ExcludeTypes.Any());
bool scriptAllObjects = !hasObjectsSpecified && !hasCriteriaSpecified;
// In the getter for SqlScriptPublishModel.AdvancedOptions, there is some strange logic which will
// cause the SqlScriptPublishModel.AdvancedOptions to get reset and lose all values based the ordering
// of when SqlScriptPublishModel.ScriptAllObjects is set.
//
publishModel.ScriptAllObjects = scriptAllObjects;
if (scriptAllObjects)
{
// Due to the getter logic within publishModel.AdvancedOptions, we explicitly populate the options
// after we determine what objects we are scripting.
//
PopulateAdvancedScriptOptions(this.Parameters.ScriptOptions, publishModel.AdvancedOptions);
return publishModel;
}
IEnumerable<ScriptingObject> selectedObjects = new List<ScriptingObject>();
if (hasCriteriaSpecified)
{
// This is an expensive remote call to load all objects from the database.
//
List<ScriptingObject> allObjects = publishModel.GetDatabaseObjects();
selectedObjects = ScriptingObjectMatcher.Match(
this.Parameters.IncludeObjectCriteria,
this.Parameters.ExcludeObjectCriteria,
this.Parameters.IncludeSchemas,
this.Parameters.ExcludeSchemas,
this.Parameters.IncludeTypes,
this.Parameters.ExcludeTypes,
allObjects);
}
if (hasObjectsSpecified)
{
selectedObjects = selectedObjects.Union(this.Parameters.ScriptingObjects);
}
// Populating advanced options after we select our objects in question, otherwise we lose all
// advanced options. After this call to PopulateAdvancedScriptOptions, DO NOT reference the
// publishModel.AdvancedOptions getter as it will reset the options in the model.
//
PopulateAdvancedScriptOptions(this.Parameters.ScriptOptions, publishModel.AdvancedOptions);
Logger.Write(
TraceEventType.Information,
string.Format(
"Scripting object count {0}, objects: {1}",
selectedObjects.Count(),
string.Join(", ", selectedObjects)));
string server = GetServerNameFromLiveInstance();
string database = new KustoConnectionStringBuilder(this.Parameters.ConnectionString).InitialCatalog;
foreach (ScriptingObject scriptingObject in selectedObjects)
{
publishModel.SelectedObjects.Add(scriptingObject.ToUrn(server, database));
}
return publishModel;
}
private void OnPublishModelScriptError(object sender, ScriptEventArgs e)
{
this.CancellationToken.ThrowIfCancellationRequested();
Logger.Write(
TraceEventType.Verbose,
string.Format(
"Sending scripting error progress event, Urn={0}, OperationId={1}, Sequence={2}, Completed={3}, Error={4}",
e.Urn,
this.OperationId,
this.eventSequenceNumber,
e.Completed,
e?.Error?.ToString() ?? "null"));
// Keep scripting...it's a best effort operation.
e.ContinueScripting = true;
this.SendProgressNotificationEvent(new ScriptingProgressNotificationParams
{
ScriptingObject = e.Urn?.ToScriptingObject(),
Status = e.GetStatus(),
CompletedCount = this.scriptedObjectCount,
TotalCount = this.totalScriptedObjectCount,
ErrorMessage = e?.Error?.Message,
ErrorDetails = e?.Error?.ToString(),
});
}
private void OnPublishModelScriptItemsCollected(object sender, ScriptItemsArgs e)
{
this.CancellationToken.ThrowIfCancellationRequested();
List<ScriptingObject> scriptingObjects = e.Urns.Select(urn => urn.ToScriptingObject()).ToList();
this.totalScriptedObjectCount = scriptingObjects.Count;
Logger.Write(
TraceEventType.Verbose,
string.Format(
"Sending scripting plan notification event OperationId={0}, Sequence={1}, Count={2}, Objects: {3}",
this.OperationId,
this.eventSequenceNumber,
this.totalScriptedObjectCount,
string.Join(", ", e.Urns)));
this.SendPlanNotificationEvent(new ScriptingPlanNotificationParams
{
ScriptingObjects = scriptingObjects,
Count = scriptingObjects.Count,
});
}
private void OnPublishModelScriptProgress(object sender, ScriptEventArgs e)
{
this.CancellationToken.ThrowIfCancellationRequested();
if (e.Completed)
{
this.scriptedObjectCount += 1;
}
Logger.Write(
TraceEventType.Verbose,
string.Format(
"Sending progress event, Urn={0}, OperationId={1}, Sequence={2}, Status={3}, Error={4}",
e.Urn,
this.OperationId,
this.eventSequenceNumber,
e.GetStatus(),
e?.Error?.ToString() ?? "null"));
this.SendProgressNotificationEvent(new ScriptingProgressNotificationParams
{
ScriptingObject = e.Urn.ToScriptingObject(),
Status = e.GetStatus(),
CompletedCount = this.scriptedObjectCount,
TotalCount = this.totalScriptedObjectCount,
ErrorMessage = e?.Error?.Message,
ErrorDetails = e?.Error?.ToString(),
});
}
}
}

View File

@@ -92,13 +92,13 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
// if a connection string wasn't provided as a parameter then
// use the owner uri property to lookup its associated ConnectionInfo
// and then build a connection string out of that
if (parameters.ConnectionString == null)
if (parameters.DatabaseName == null)
{
ConnectionInfo connInfo;
_connectionService.TryFindConnection(parameters.OwnerUri, out connInfo);
if (connInfo != null)
{
parameters.ConnectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
parameters.DatabaseName = connInfo.ConnectionDetails.DatabaseName;
}
else
{
@@ -106,18 +106,15 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
}
}
SmoScriptingOperation operation;
var datasource = _connectionService.GetOrOpenConnection(parameters.OwnerUri, ConnectionType.Default)
.Result.GetUnderlyingConnection();
if (!ShouldCreateScriptAsOperation(parameters))
{
operation = new ScriptingScriptOperation(parameters, datasource);
}
else
{
operation = new ScriptAsScriptingOperation(parameters, _scripter, datasource);
throw new InvalidOperationException("Unable to create script.");
}
var datasource = _connectionService.GetOrOpenConnection(parameters.OwnerUri, ConnectionType.Default)
.Result.GetUnderlyingConnection();
SmoScriptingOperation operation = new ScriptAsScriptingOperation(parameters, _scripter, datasource);
operation.PlanNotification += (sender, e) => requestContext.SendEvent(ScriptingPlanNotificationEvent.Type, e).Wait();
operation.ProgressNotification += (sender, e) => requestContext.SendEvent(ScriptingProgressNotificationEvent.Type, e).Wait();
operation.CompleteNotification += (sender, e) => this.SendScriptingCompleteEvent(requestContext, ScriptingCompleteEvent.Type, e, operation, parameters.ScriptDestination);
@@ -136,17 +133,11 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
// Scripting as operation should be used to script one object.
// Scripting data and scripting to file is not supported by scripting as operation
// To script Select, alter and execute use scripting as operation. The other operation doesn't support those types
if( (parameters.ScriptingObjects != null && parameters.ScriptingObjects.Count == 1 && parameters.ScriptOptions != null
&& parameters.ScriptOptions.TypeOfDataToScript == "SchemaOnly" && parameters.ScriptDestination == "ToEditor") ||
parameters.Operation == ScriptingOperationType.Select || parameters.Operation == ScriptingOperationType.Execute ||
parameters.Operation == ScriptingOperationType.Alter)
{
return true;
}
else
{
return false;
}
return parameters.ScriptingObjects != null && parameters.ScriptingObjects.Count == 1 && parameters.ScriptOptions != null
&& parameters.ScriptOptions.TypeOfDataToScript == "SchemaOnly" && parameters.ScriptDestination == "ToEditor"
|| parameters.Operation == ScriptingOperationType.Select
|| parameters.Operation == ScriptingOperationType.Execute
|| parameters.Operation == ScriptingOperationType.Alter;
}
/// <summary>
@@ -227,7 +218,7 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
{
disposed = true;
foreach (ScriptingScriptOperation operation in this.ActiveOperations.Values)
foreach (var operation in this.ActiveOperations.Values)
{
operation.Dispose();
}

View File

@@ -10,7 +10,6 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Kusto.Data;
using static Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptOptions;
namespace Microsoft.Kusto.ServiceLayer.Scripting
@@ -80,19 +79,15 @@ namespace Microsoft.Kusto.ServiceLayer.Scripting
protected void ValidateScriptDatabaseParams()
{
try
if (string.IsNullOrWhiteSpace(Parameters.DatabaseName))
{
var builder = new KustoConnectionStringBuilder(this.Parameters.ConnectionString);
throw new ArgumentException(SR.ScriptingParams_ConnectionString_Property_Invalid);
}
catch (Exception e)
{
throw new ArgumentException(SR.ScriptingParams_ConnectionString_Property_Invalid, e);
}
if (this.Parameters.FilePath == null && this.Parameters.ScriptDestination != "ToEditor")
if (Parameters.FilePath == null && this.Parameters.ScriptDestination != "ToEditor")
{
throw new ArgumentException(SR.ScriptingParams_FilePath_Property_Invalid);
}
else if (this.Parameters.FilePath != null && this.Parameters.ScriptDestination != "ToEditor")
if (Parameters.FilePath != null && this.Parameters.ScriptDestination != "ToEditor")
{
if (!Directory.Exists(Path.GetDirectoryName(this.Parameters.FilePath)))
{