WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a3d9127

Browse files
authored
Refactor ProcessHandler arguments logic (#4711)
* Move PH classes to folder & update namespaces * wip: refactor PH args processing modes * Add wrapper to get cmd args * Correct knob descriptions * Add debug log for args mode * Fix file mode logic * Use switch/case * fix variables naming * Return old handler and move new to v2 * Get temp by hostcontext * Fix tests on visual studio * Added tests
1 parent db013b8 commit a3d9127

File tree

12 files changed

+803
-20
lines changed

12 files changed

+803
-20
lines changed

src/Agent.Sdk/Knob/AgentKnobs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,17 @@ public class AgentKnobs
534534

535535
public static readonly Knob ProcessHandlerEnableNewLogic = new Knob(
536536
nameof(ProcessHandlerEnableNewLogic),
537-
"Enables new sanitization logic for process handler",
537+
"Enables new args protect logic for process handler",
538538
new RuntimeKnobSource("AZP_75787_ENABLE_NEW_PH_LOGIC"),
539539
new EnvironmentKnobSource("AZP_75787_ENABLE_NEW_PH_LOGIC"),
540540
new BuiltInDefaultKnobSource("false"));
541541

542+
public static readonly Knob UseProcessHandlerV2 = new Knob(
543+
nameof(UseProcessHandlerV2),
544+
"Enables new Process handler (v2)",
545+
new RuntimeKnobSource("AGENT_USE_PROCESS_HANDLER_V2"),
546+
new BuiltInDefaultKnobSource("false"));
547+
542548
public static readonly Knob DisableDrainQueuesAfterTask = new Knob(
543549
nameof(DisableDrainQueuesAfterTask),
544550
"Forces the agent to disable draining queues after each task",

src/Agent.Worker/Handlers/HandlerFactory.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ public IHandler Create(
9393
else if (data is ProcessHandlerData)
9494
{
9595
// Process.
96-
handler = HostContext.CreateService<IProcessHandler>();
96+
if (AgentKnobs.UseProcessHandlerV2.GetValue(executionContext).AsBoolean())
97+
{
98+
Trace.Info("Using ProcessHandlerV2");
99+
handler = HostContext.CreateService<IProcessHandlerV2>();
100+
}
101+
else
102+
{
103+
handler = HostContext.CreateService<IProcessHandler>();
104+
}
105+
97106
(handler as IProcessHandler).Data = data as ProcessHandlerData;
98107
}
99108
else if (data is PowerShellHandlerData)

src/Agent.Worker/Handlers/Helpers/CmdArgsSanitizer.cs renamed to src/Agent.Worker/Handlers/ProcessHandler/CmdArgsSanitizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Text.RegularExpressions;
77
using Microsoft.VisualStudio.Services.Agent.Util;
88

9-
namespace Agent.Worker.Handlers.Helpers
9+
namespace Microsoft.VisualStudio.Services.Agent.Worker.Handlers
1010
{
1111
public static class CmdArgsSanitizer
1212
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Microsoft.VisualStudio.Services.Agent.Worker.Handlers
7+
{
8+
public class InvalidScriptArgsException : Exception
9+
{
10+
public InvalidScriptArgsException(string message) : base(message)
11+
{
12+
}
13+
}
14+
}

src/Agent.Worker/Handlers/ProcessHandler.cs renamed to src/Agent.Worker/Handlers/ProcessHandler/ProcessHandler.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using Agent.Sdk.Knob;
5-
using Agent.Worker.Handlers.Helpers;
65
using Microsoft.VisualStudio.Services.Agent.Util;
76
using Newtonsoft.Json;
87
using System;
@@ -365,11 +364,4 @@ private void OnOutputDataReceived(object sender, ProcessDataReceivedEventArgs e)
365364
}
366365
}
367366
}
368-
369-
public class InvalidScriptArgsException : Exception
370-
{
371-
public InvalidScriptArgsException(string message) : base(message)
372-
{
373-
}
374-
}
375367
}

src/Agent.Worker/Handlers/Helpers/ProcessHandlerHelper.cs renamed to src/Agent.Worker/Handlers/ProcessHandler/ProcessHandlerHelper.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
using System.Collections.Generic;
66
using Agent.Sdk.Knob;
77
using Microsoft.VisualStudio.Services.Agent.Util;
8-
using Microsoft.VisualStudio.Services.Agent.Worker;
98
using Microsoft.VisualStudio.Services.Common;
109

11-
namespace Agent.Worker.Handlers.Helpers
10+
namespace Microsoft.VisualStudio.Services.Agent.Worker.Handlers
1211
{
1312
public static class ProcessHandlerHelper
1413
{
@@ -152,6 +151,39 @@ public static (bool, Dictionary<string, object>) ValidateInputArguments(
152151
return (true, null);
153152
}
154153
}
154+
155+
public static (bool, Dictionary<string, object>) ValidateInputArgumentsV2(
156+
IExecutionContext context,
157+
string inputArgs,
158+
Dictionary<string, string> environment,
159+
bool canIncludeTelemetry)
160+
{
161+
context.Debug("Starting args env expansion");
162+
var (expandedArgs, envExpandTelemetry) = ExpandCmdEnv(inputArgs, environment);
163+
context.Debug($"Expanded args={expandedArgs}");
164+
165+
context.Debug("Starting args sanitization");
166+
var (sanitizedArgs, sanitizationTelemetry) = CmdArgsSanitizer.SanitizeArguments(expandedArgs);
167+
168+
if (sanitizedArgs != inputArgs)
169+
{
170+
Dictionary<string, object> telemetry = null;
171+
if (canIncludeTelemetry)
172+
{
173+
telemetry = envExpandTelemetry.ToDictionary();
174+
if (sanitizationTelemetry != null)
175+
{
176+
telemetry.AddRange(sanitizationTelemetry.ToDictionary());
177+
}
178+
}
179+
if (sanitizedArgs != expandedArgs)
180+
{
181+
return (false, telemetry);
182+
}
183+
}
184+
185+
return (true, null);
186+
}
155187
}
156188

157189
public class CmdTelemetry

0 commit comments

Comments
 (0)