Skip to content

Commit 1b5486a

Browse files
authored
Validate work dir during runner start up. (#4227)
1 parent 4214709 commit 1b5486a

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/Runner.Listener/Runner.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Runtime.CompilerServices;
8-
using System.Security.Cryptography;
98
using System.Security.Claims;
9+
using System.Security.Cryptography;
1010
using System.Text;
1111
using System.Threading;
1212
using System.Threading.Tasks;
@@ -406,12 +406,27 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
406406
try
407407
{
408408
Trace.Info(nameof(RunAsync));
409-
409+
410+
// Validate directory permissions.
411+
string workDirectory = HostContext.GetDirectory(WellKnownDirectory.Work);
412+
Trace.Info($"Validating directory permissions for: '{workDirectory}'");
413+
try
414+
{
415+
Directory.CreateDirectory(workDirectory);
416+
IOUtil.ValidateExecutePermission(workDirectory);
417+
}
418+
catch (Exception ex)
419+
{
420+
Trace.Error(ex);
421+
_term.WriteError($"Fail to create and validate runner's work directory '{workDirectory}'.");
422+
return Constants.Runner.ReturnCode.TerminatedError;
423+
}
424+
410425
// First try using migrated settings if available
411426
var configManager = HostContext.GetService<IConfigurationManager>();
412427
RunnerSettings migratedSettings = null;
413-
414-
try
428+
429+
try
415430
{
416431
migratedSettings = configManager.LoadMigratedSettings();
417432
Trace.Info("Loaded migrated settings from .runner_migrated file");
@@ -422,15 +437,15 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
422437
// If migrated settings file doesn't exist or can't be loaded, we'll use the provided settings
423438
Trace.Info($"Failed to load migrated settings: {ex.Message}");
424439
}
425-
440+
426441
bool usedMigratedSettings = false;
427-
442+
428443
if (migratedSettings != null)
429444
{
430445
// Try to create session with migrated settings first
431446
Trace.Info("Attempting to create session using migrated settings");
432447
_listener = GetMessageListener(migratedSettings, isMigratedSettings: true);
433-
448+
434449
try
435450
{
436451
CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken);
@@ -450,7 +465,7 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
450465
Trace.Error($"Exception when creating session with migrated settings: {ex}");
451466
}
452467
}
453-
468+
454469
// If migrated settings weren't used or session creation failed, use original settings
455470
if (!usedMigratedSettings)
456471
{
@@ -503,7 +518,7 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
503518
restartSession = true;
504519
break;
505520
}
506-
521+
507522
TaskAgentMessage message = null;
508523
bool skipMessageDeletion = false;
509524
try
@@ -859,7 +874,7 @@ private async Task<int> ExecuteRunnerAsync(RunnerSettings settings, bool runOnce
859874
{
860875
restart = false;
861876
returnCode = await RunAsync(settings, runOnce);
862-
877+
863878
if (returnCode == Constants.Runner.ReturnCode.RunnerConfigurationRefreshed)
864879
{
865880
Trace.Info("Runner configuration was refreshed, restarting session...");

0 commit comments

Comments
 (0)