Skip to content

Commit

Permalink
Correction on HybridConnection Uri (allows to activate Require Client…
Browse files Browse the repository at this point in the history
… Authorization)
  • Loading branch information
cgoconseils committed Dec 20, 2023
1 parent 22f3bfb commit 63d3fc3
Showing 1 changed file with 59 additions and 61 deletions.
120 changes: 59 additions & 61 deletions src/XrmFramework.Plugin/RemoteDebugger/Plugin.partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,90 +19,88 @@ private bool SendToRemoteDebugger(LocalPluginContext localContext)
localContext.Log("The context is genuine");
localContext.Log($"UnSecuredConfig : {UnSecuredConfigFull}");

//if (!string.IsNullOrEmpty(UnSecuredConfig) && UnSecuredConfig.Contains("debugSessions"))
//{
// var debuggerUnsecuredConfig = JsonConvert.DeserializeObject<DebuggerUnsecureConfig>(UnSecuredConfig);
var initiatingUserId = localContext.GetInitiatingUserId();
var rootUserId = localContext.GetRootUserId();

// localContext.Log($"Debug session ids : {string.Join(",", debuggerUnsecuredConfig.DebugSessionIds)}");
localContext.Log($"Initiating user Id : {initiatingUserId}, root user Id : {rootUserId}");

var initiatingUserId = localContext.GetInitiatingUserId();
var rootUserId = localContext.GetRootUserId();
var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery<DebugSession>();
queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.In,
initiatingUserId, rootUserId);
queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode,
ConditionOperator.Equal, DebugSessionState.Active.ToInt());

localContext.Log($"Initiating user Id : {initiatingUserId}, root user Id : {rootUserId}");
var debugSession = localContext.AdminOrganizationService.RetrieveAll<DebugSession>(queryDebugSessions)
.FirstOrDefault();

var queryDebugSessions = BindingModelHelper.GetRetrieveAllQuery<DebugSession>();
queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.DebugeeId, ConditionOperator.In, initiatingUserId, rootUserId);
queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.StateCode, ConditionOperator.Equal, DebugSessionState.Active.ToInt());
localContext.Log($"Debug session : {debugSession}");

//queryDebugSessions.Criteria.AddCondition(DebugSessionDefinition.Columns.Id, ConditionOperator.In, debuggerUnsecuredConfig.DebugSessionIds.Cast<object>().ToArray());
if (debugSession == null || debugSession.SessionEnd < DateTime.Today)
{
return false;
}

var debugSession = localContext.AdminOrganizationService.RetrieveAll<DebugSession>(queryDebugSessions).FirstOrDefault();
var remoteContext = localContext.RemoteContext;
remoteContext.Id = Guid.NewGuid();
remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName;
remoteContext.UnsecureConfig = UnSecuredConfigFull;
remoteContext.SecureConfig = SecuredConfig;

localContext.Log($"Debug session : {debugSession}");
var uri = new Uri($"{debugSession.RelayUrl.TrimEnd('/')}/{debugSession.HybridConnectionName}");

if (debugSession != null)
{
if (debugSession.SessionEnd >= DateTime.Today)
{

var remoteContext = localContext.RemoteContext;
remoteContext.Id = Guid.NewGuid();
remoteContext.TypeAssemblyQualifiedName = GetType().AssemblyQualifiedName;
remoteContext.UnsecureConfig = UnSecuredConfigFull;
remoteContext.SecureConfig = SecuredConfig;

var uri = new Uri($"{debugSession.RelayUrl}/{debugSession.HybridConnectionName}");
try
{
using var hybridConnection = new HybridConnection(debugSession.SasKeyName,
debugSession.SasConnectionKey, uri.AbsoluteUri);
var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext,
remoteContext.Id);

try
{
using var hybridConnection = new HybridConnection(debugSession.SasKeyName, debugSession.SasConnectionKey, uri.AbsoluteUri);
var message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Context, remoteContext, remoteContext.Id);

RemoteDebuggerMessage response;
while (true)
{
localContext.Log("Sending context to local machine : {0}", message);
RemoteDebuggerMessage response;
while (true)
{
localContext.Log("Sending context to local machine : {0}", message);

response = hybridConnection.SendMessage(message).GetAwaiter().GetResult();
response = hybridConnection.SendMessage(message).GetAwaiter().GetResult();

localContext.Log("Received response : {0}", response);
localContext.Log("Received response : {0}", response);

if (response.MessageType == RemoteDebuggerMessageType.Context || response.MessageType == RemoteDebuggerMessageType.Exception)
{
break;
}
if (response.MessageType == RemoteDebuggerMessageType.Context ||
response.MessageType == RemoteDebuggerMessageType.Exception)
{
break;
}

var request = response.GetOrganizationRequest();
var request = response.GetOrganizationRequest();

var service = response.UserId.HasValue ? localContext.GetService(response.UserId.Value) : localContext.AdminOrganizationService;
var service = response.UserId.HasValue
? localContext.GetService(response.UserId.Value)
: localContext.AdminOrganizationService;

var organizationResponse = service.Execute(request);
var organizationResponse = service.Execute(request);

message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response, organizationResponse, remoteContext.Id);
}
message = new RemoteDebuggerMessage(RemoteDebuggerMessageType.Response,
organizationResponse, remoteContext.Id);
}

if (response.MessageType == RemoteDebuggerMessageType.Exception)
{
throw response.GetException();
}
if (response.MessageType == RemoteDebuggerMessageType.Exception)
{
throw response.GetException();
}

var updatedContext = response.GetContext<RemoteDebugExecutionContext>();
var updatedContext = response.GetContext<RemoteDebugExecutionContext>();

localContext.UpdateContext(updatedContext);
localContext.UpdateContext(updatedContext);


return true;
}
catch (HttpRequestException)
{
// Run the plugin as deploy if the remote debugger is not connected
}
}
}
//}
return true;
}
catch (HttpRequestException)
{
// Run the plugin as deploy if the remote debugger is not connected
}
}

return false;
}
}
}
}

0 comments on commit 63d3fc3

Please sign in to comment.