Skip to content

Commit

Permalink
Merge pull request #426 from microsoft/release/update/240108092744
Browse files Browse the repository at this point in the history
Resync Updates from Master Codebase.
  • Loading branch information
MattB-msft authored Jan 8, 2024
2 parents 284c3c2 + 920182b commit 33b6089
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 127 deletions.
6 changes: 2 additions & 4 deletions src/GeneralTools/DataverseClient/Client/Auth/AuthProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,13 @@ internal async static Task<AuthenticationResult> ObtainAccessTokenAsync(
case PromptBehavior.Auto:
break;
case PromptBehavior.Always:
userPrompt = Microsoft.Identity.Client.Prompt.ForceLogin;
case PromptBehavior.SelectAccount:
userPrompt = Microsoft.Identity.Client.Prompt.SelectAccount;
break;
case PromptBehavior.Never:
case PromptBehavior.RefreshSession:
userPrompt = Microsoft.Identity.Client.Prompt.NoPrompt;
break;
case PromptBehavior.SelectAccount:
userPrompt = Microsoft.Identity.Client.Prompt.SelectAccount;
break;
default:
break;
}
Expand Down
14 changes: 8 additions & 6 deletions src/GeneralTools/DataverseClient/Client/Auth/OnPremises_Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ internal static object CreateAndAuthenticateProxy<T>(IServiceManagement<T> servi
DataverseTraceLogger logSink = null)
{
bool createdLogSource = false;
Stopwatch dtProxyCreate = new Stopwatch();
dtProxyCreate.Start();
Stopwatch dtConnectTimeCheck = new Stopwatch();
Stopwatch dtProxyCreate = Stopwatch.StartNew();
Stopwatch dtConnectTimeCheck = Stopwatch.StartNew();
try
{
if (logSink == null)
Expand All @@ -54,7 +53,8 @@ internal static object CreateAndAuthenticateProxy<T>(IServiceManagement<T> servi
logSink.Log(string.Format(CultureInfo.InvariantCulture, "{0} - attempting to connect to On-Premises Dataverse server @ {1}", LogString, ServiceUri.ToString()), TraceEventType.Verbose);

// Create the Service configuration for that URL
dtConnectTimeCheck.Restart();
dtConnectTimeCheck.Stop();
dtConnectTimeCheck = Stopwatch.StartNew();
servicecfg = ServiceConfigurationFactoryAsync.CreateManagement<T>(ServiceUri);
dtConnectTimeCheck.Stop();
if (servicecfg == null)
Expand All @@ -73,7 +73,8 @@ internal static object CreateAndAuthenticateProxy<T>(IServiceManagement<T> servi
{
// Connect via anything other then AD.
// Setup for Auth Check Performance.
dtConnectTimeCheck.Restart();
dtConnectTimeCheck.Stop();
dtConnectTimeCheck = Stopwatch.StartNew();

// Deal with IFD QurikyNess in ADFS configuration, where ADFS can be configured to fall though to Kerb Auth.
AuthenticationCredentials authCred = ClaimsIFDFailOverAuth<T>(servicecfg, homeRealm, userCredentials);
Expand All @@ -89,7 +90,8 @@ internal static object CreateAndAuthenticateProxy<T>(IServiceManagement<T> servi
{
logSink.Log(string.Format(CultureInfo.InvariantCulture, "{0} - Initial Authenticated via {1} {3} . Auth Elapsed:{2}", LogString, servicecfg.AuthenticationType, dtConnectTimeCheck.Elapsed.ToString(), homeRealm.ToString()), TraceEventType.Verbose);
logSink.Log(string.Format(CultureInfo.InvariantCulture, "{0} - Relaying Auth to Resource Server: From {1} to {2}", LogString, homeRealm.ToString(), servicecfg.PolicyConfiguration.SecureTokenServiceIdentifier), TraceEventType.Verbose);
dtConnectTimeCheck.Restart();
dtConnectTimeCheck.Stop();
dtConnectTimeCheck = Stopwatch.StartNew();
// Auth token against the correct server.
AuthenticationCredentials authCred2 = servicecfg.Authenticate(new AuthenticationCredentials()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,18 @@ public FileBackedTokenCacheHints(string tokenPathAndFileName)
string hostName = "DvBaseClient";
if (AppDomain.CurrentDomain != null)
{
hostName = Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
if (hostName.IndexOfAny(Path.GetInvalidFileNameChars()) < 0)
{
foreach (var c in Path.GetInvalidFileNameChars())
{
hostName = hostName.Replace(c, '_');
}
}
hostName = Path.GetFileNameWithoutExtension(Utilities.CleanUpPotentialFileName(AppDomain.CurrentDomain.FriendlyName));
}
string hostVersion = Environs.XrmSdkFileVersion;
string companyName = typeof(OrganizationDetail).Assembly.GetCustomAttribute<AssemblyCompanyAttribute>().Company;
string companyName = Utilities.CleanUpPotentialFileName(typeof(OrganizationDetail).Assembly.GetCustomAttribute<AssemblyCompanyAttribute>().Company);


if (string.IsNullOrEmpty(tokenPathAndFileName))
{
tokenPathAndFileName = Path.Combine(MsalCacheHelper.UserRootDirectory, companyName?.Replace(" ", "_"), hostName, hostVersion, "dvtokens.dat");
}

System.Diagnostics.Trace.WriteLine($"TokenCacheFilePath: {tokenPathAndFileName}");
Trace.WriteLine($"TokenCacheFilePath: {tokenPathAndFileName}");

cacheFileDirectory = Path.GetDirectoryName(tokenPathAndFileName);
cacheFileName = Path.GetFileName(tokenPathAndFileName);
Expand Down
71 changes: 30 additions & 41 deletions src/GeneralTools/DataverseClient/Client/ConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ internal sealed class ConnectionService : IConnectionService, IDisposable
private PromptBehavior _promptBehavior; // prompt behavior
private string _tokenCachePath; // user specified token cache file path
private bool _isOnPremOAuth = false; // Identifies whether the connection is for OnPrem or Online Deployment for OAuth
private static string _userId = null; //cached userid reading from config file
private bool _isCalledbyExecuteRequest = false; //Flag indicating that the an request called by Execute_Command
private bool _isDefaultCredsLoginForOAuth = false; //Flag indicating that the user is trying to login with the current user id.

Expand Down Expand Up @@ -342,7 +341,7 @@ internal sealed class ConnectionService : IConnectionService, IDisposable
/// <summary>
/// Cached userid
/// </summary>
internal string UserId { get { return _userId; } }
internal string UserId { get { return _userAccount != null ? _userAccount.Username : string.Empty; } }

/// <summary>
/// Flag indicating that the an request called by Execute_Command used for OAuth
Expand Down Expand Up @@ -957,7 +956,6 @@ private void GenerateCacheKeys(bool useUniqueCacheName)
{
//unqueInstance = true; // this instance is unique.
_authority = string.Empty;
_userId = null;
Guid guID = Guid.NewGuid();
_ServiceCACHEName = _ServiceCACHEName + guID.ToString(); // Creating a unique instance name for the cache object.
}
Expand Down Expand Up @@ -1064,7 +1062,7 @@ private async Task<IOrganizationService> InitServiceAsync()
{
// Dataverse Service Endpoint to work with
IOrganizationService dvService = null;
Stopwatch dtQueryTimer = new Stopwatch();
Stopwatch dtQueryTimer = Stopwatch.StartNew();
try
{
if (!IsAClone)
Expand Down Expand Up @@ -1406,21 +1404,6 @@ private async Task<IOrganizationService> InitServiceAsync()
return null;
}

//// Do a WHO AM I request to make sure the connection is good.
//if (!UseExternalConnection)
//{
// Guid guIntialTrackingID = Guid.NewGuid();
// logEntry.Log(string.Format("Beginning Validation of Dataverse Connection. RequestID: {0}", guIntialTrackingID.ToString()));
// dtQueryTimer.Restart();
// user = await GetWhoAmIDetails(dvService, guIntialTrackingID).ConfigureAwait(false);
// dtQueryTimer.Stop();
// logEntry.Log(string.Format(CultureInfo.InvariantCulture, "Validation of Dataverse Connection Complete, total duration: {0}", dtQueryTimer.Elapsed.ToString()));
//}
//else
//{
// logEntry.Log("External Dataverse Connection Provided, Skipping Validation");
//}

return (IOrganizationService)dvService;

}
Expand Down Expand Up @@ -1483,8 +1466,7 @@ private async Task<IOrganizationService> InitServiceAsync()
private async Task<IOrganizationService> DoDirectLoginAsync(bool IsOnPrem = false)
{
logEntry.Log("Direct Login Process Started", TraceEventType.Verbose);
Stopwatch sw = new Stopwatch();
sw.Start();
Stopwatch sw = Stopwatch.StartNew();

IOrganizationService dvService = null;
Uri OrgWorkingURI = null;
Expand Down Expand Up @@ -1600,8 +1582,7 @@ private async Task GetServerVersion(IOrganizationService dvService, Uri uriOfIns
{
Guid trackingID = Guid.NewGuid();
logEntry.Log(string.Format("Querying Organization Version. Request ID: {0}", trackingID));
Stopwatch dtQueryTimer = new Stopwatch();
dtQueryTimer.Restart();
Stopwatch dtQueryTimer = Stopwatch.StartNew();

RetrieveVersionResponse getVersionResp = null;
var request = new RetrieveVersionRequest() { RequestId = trackingID };
Expand Down Expand Up @@ -1680,8 +1661,7 @@ private async Task RefreshInstanceDetails(IOrganizationService dvService, Uri ur
//TODO:// Add Logic here to improve perf by connecting to global disco.
Guid trackingID = Guid.NewGuid();
logEntry.Log(string.Format("Querying Organization Instance Details. Request ID: {0}", trackingID));
Stopwatch dtQueryTimer = new Stopwatch();
dtQueryTimer.Restart();
Stopwatch dtQueryTimer = Stopwatch.StartNew();

var request = new RetrieveCurrentOrganizationRequest() { AccessType = 0, RequestId = trackingID };
RetrieveCurrentOrganizationResponse resp;
Expand Down Expand Up @@ -1795,8 +1775,7 @@ internal async Task<WhoAmIResponse> GetWhoAmIDetails(IOrganizationService dvServ
{
if (dvService != null)
{
Stopwatch dtQueryTimer = new Stopwatch();
dtQueryTimer.Restart();
Stopwatch dtQueryTimer = Stopwatch.StartNew();
try
{
if (trackingID == Guid.Empty)
Expand Down Expand Up @@ -1894,6 +1873,8 @@ internal void SetClonedProperties(ServiceClient sourceClient)
debugingCloneStateFilter++;
EnableCookieRelay = sourceClient._connectionSvc.EnableCookieRelay;
debugingCloneStateFilter++;
RequestAdditionalHeadersAsync = sourceClient._connectionSvc.RequestAdditionalHeadersAsync;
debugingCloneStateFilter++;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -2098,8 +2079,15 @@ internal async Task<OrganizationResponse> Command_WebAPIProcess_ExecuteAsync(Org
{
postUri = $"{postUri}?{addedQueryParams}";
}

Guid userProvidedRequestId = Guid.Empty;
if (req.RequestId.HasValue && req.RequestId != Guid.Empty )
{
userProvidedRequestId = req.RequestId.Value;
}

// Execute request
var sResp = await Command_WebExecuteAsync(postUri, bodyOfRequest, methodToExecute, headers, "application/json", logMessageTag, callerId, disableConnectionLocking, maxRetryCount, retryPauseTime, uriOfInstance, cancellationToken: cancellationToken).ConfigureAwait(false);
var sResp = await Command_WebExecuteAsync(postUri, bodyOfRequest, methodToExecute, headers, "application/json", logMessageTag, callerId, disableConnectionLocking, maxRetryCount, retryPauseTime, uriOfInstance, cancellationToken: cancellationToken, requestTrackingId: userProvidedRequestId).ConfigureAwait(false);
if (sResp != null && sResp.IsSuccessStatusCode)
{
if (req is CreateRequest)
Expand Down Expand Up @@ -2182,7 +2170,7 @@ internal async Task<OrganizationResponse> Command_WebAPIProcess_ExecuteAsync(Org
internal async Task<HttpResponseMessage> Command_WebExecuteAsync(string queryString, string body, HttpMethod method, Dictionary<string, List<string>> customHeaders,
string contentType, string errorStringCheck, Guid callerId, bool disableConnectionLocking, int maxRetryCount, TimeSpan retryPauseTime, Uri uriOfInstance = null, Guid requestTrackingId = default, CancellationToken cancellationToken = default)
{
Stopwatch logDt = new Stopwatch();
Stopwatch logDt = Stopwatch.StartNew();
int retryCount = 0;
bool retry = false;

Expand Down Expand Up @@ -2350,8 +2338,8 @@ internal async Task<HttpResponseMessage> Command_WebExecuteAsync(string queryStr
// Add authorization header. - Here to catch the situation where a token expires during retry.
if (!customHeaders.ContainsKey(Utilities.RequestHeaders.AUTHORIZATION_HEADER))
customHeaders.Add(Utilities.RequestHeaders.AUTHORIZATION_HEADER, new List<string>() { string.Format("Bearer {0}", await RefreshClientTokenAsync().ConfigureAwait(false)) });

logDt.Restart(); // start clock.
logDt.Stop();
logDt = Stopwatch.StartNew();

logEntry.Log(string.Format(CultureInfo.InvariantCulture, "Execute Command - {0}{1}: {2}",
$"{method} {queryString}",
Expand Down Expand Up @@ -2534,7 +2522,7 @@ private bool ShouldRetryWebAPI(Exception ex, int retryCount, int maxRetryCount,
RequestId = requestTrackingId.Value;

HttpResponseMessage _httpResponse = null;
Stopwatch logDt = new Stopwatch();
Stopwatch logDt = Stopwatch.StartNew();
try
{
using (var _httpRequest = new HttpRequestMessage())
Expand Down Expand Up @@ -2596,7 +2584,8 @@ private bool ShouldRetryWebAPI(Exception ex, int retryCount, int maxRetryCount,

if (providedHttpClient != null)
{
logDt.Restart();
logDt.Stop();
logDt = Stopwatch.StartNew();
try
{
if (providedHttpClient.Timeout != MaxConnectionTimeout)
Expand All @@ -2613,7 +2602,8 @@ private bool ShouldRetryWebAPI(Exception ex, int retryCount, int maxRetryCount,
// Fall though logic to deal with an Http client not being passed in.
using (HttpClient httpCli = new HttpClient())
{
logDt.Restart();
logDt.Stop();
logDt = Stopwatch.StartNew();
try
{
if (httpCli.Timeout != MaxConnectionTimeout)
Expand Down Expand Up @@ -2805,7 +2795,7 @@ internal static async Task<OrganizationDetailCollection> DiscoverGlobalOrganizat
private static async Task<DiscoverOrganizationsResult> DiscoverOrganizations_InternalAsync(Uri discoveryServiceUri, ClientCredentials clientCredentials, X509Certificate2 loginCertificate, string clientId, Uri redirectUri, PromptBehavior promptBehavior, bool isOnPrem, string authority, bool useDefaultCreds = false, string tokenCacheStorePath = null, DataverseTraceLogger logSink = null, CancellationToken cancellationToken = default)
{
bool createdLogSource = false;
Stopwatch dtStartQuery = new Stopwatch();
Stopwatch dtStartQuery = Stopwatch.StartNew();
try
{
if (logSink == null)
Expand Down Expand Up @@ -2845,7 +2835,8 @@ private static async Task<DiscoverOrganizationsResult> DiscoverOrganizations_Int

try
{
dtStartQuery.Restart();
dtStartQuery.Stop();
dtStartQuery = Stopwatch.StartNew();
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)svcDiscoveryProxy.Execute(orgRequest);
dtStartQuery.Stop();

Expand Down Expand Up @@ -2973,8 +2964,7 @@ private static async Task<OrganizationDetailCollection> QueryGlobalDiscoveryAsyn
if (discoveryServiceUri == null)
throw new ArgumentNullException(nameof(discoveryServiceUri), "Discovery service uri cannot be null.");

Stopwatch dtStartQuery = new Stopwatch();
dtStartQuery.Start();
Stopwatch dtStartQuery = Stopwatch.StartNew();
// Initialize discovery service proxy.
logSink.Log("QueryGlobalDiscovery - Initializing Discovery Server Uri with " + discoveryServiceUri.ToString());

Expand Down Expand Up @@ -3143,10 +3133,9 @@ private async Task<IOrganizationService> ConnectAndInitServiceAsync(Organization

// Set the Org into system config
_organization = orgdata.UniqueName;
ConnectedOrganizationDetail = orgdata;
ConnectedOrganizationDetail = orgdata;

var logDt = new Stopwatch();
logDt.Start();
var logDt = Stopwatch.StartNew();
// Build User Credential
logEntry.Log("ConnectAndInitService - Initializing Organization Service Object", TraceEventType.Verbose);
// this to provide trouble shooting information when determining org connect failures.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public DataverseTelemetryBehaviors(ConnectionService cli)
logg.Log($"Failed to parse MaxReceivedMessageSizeOverride property. Value found: {maxRecvSz}. MaxReceivedMessageSizeOverride must be a valid integer.", System.Diagnostics.TraceEventType.Warning);
}

if (_maxBufferPoolSize == -1 && !string.IsNullOrEmpty(_configuration.Value.MaxBufferPoolSizeOveride))
if (_maxBufferPoolSize == -1 && !string.IsNullOrEmpty(_configuration.Value.MaxBufferPoolSizeOverride))
{
var maxBufferPoolSz = _configuration.Value.MaxBufferPoolSizeOveride;
var maxBufferPoolSz = _configuration.Value.MaxBufferPoolSizeOverride;
if (maxBufferPoolSz is string && !string.IsNullOrWhiteSpace(maxBufferPoolSz))
{
int.TryParse(maxBufferPoolSz, out _maxBufferPoolSize);
Expand Down
Loading

0 comments on commit 33b6089

Please sign in to comment.