Skip to content

Commit 68bbaa9

Browse files
authored
Do not assign permissions when file does not exist (#9982) (#9988)
* Do not assign permissions when file does not exist * Fix unit tests * Fix unit tests
1 parent c7c042f commit 68bbaa9

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/WebJobs.Script/Workers/ProcessManagement/WorkerProcess.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.IO;
78
using System.Linq;
89
using System.Reactive.Linq;
910
using System.Threading.Tasks;
@@ -324,6 +325,12 @@ private void AssignUserExecutePermissionsIfNotExists()
324325
}
325326

326327
string filePath = p.StartInfo.FileName;
328+
if (!File.Exists(filePath))
329+
{
330+
_workerProcessLogger.LogDebug("File path does not exist, not assigning permissions: {filePath}", filePath);
331+
return;
332+
}
333+
327334
UnixFileInfo fileInfo = new UnixFileInfo(filePath);
328335
if (!fileInfo.FileAccessPermissions.HasFlag(FileAccessPermissions.UserExecute))
329336
{

test/WebJobs.Script.Tests/Workers/Http/HttpWorkerProcessTests.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.IO;
67
using System.Threading.Tasks;
78
using Microsoft.Azure.WebJobs.Script.Config;
89
using Microsoft.Azure.WebJobs.Script.Eventing;
@@ -19,6 +20,8 @@ public class HttpWorkerProcessTests
1920
private const string _testWorkerId = "testId";
2021
private const string _rootScriptPath = "c:\\testDir";
2122
private const int _workerPort = 8090;
23+
24+
private readonly string _executablePath = System.IO.Path.GetTempFileName();
2225
private readonly ScriptSettingsManager _settingsManager;
2326
private readonly Mock<IScriptEventManager> _mockEventManager = new Mock<IScriptEventManager>();
2427
private readonly IWorkerProcessFactory _defaultWorkerProcessFactory = new DefaultWorkerProcessFactory(new TestEnvironment(), new LoggerFactory());
@@ -33,7 +36,7 @@ public HttpWorkerProcessTests()
3336
_httpWorkerOptions = new HttpWorkerOptions()
3437
{
3538
Port = _workerPort,
36-
Arguments = new WorkerProcessArguments() { ExecutablePath = "test" },
39+
Arguments = new WorkerProcessArguments() { ExecutablePath = _executablePath },
3740
Description = new HttpWorkerDescription()
3841
{
3942
WorkingDirectory = @"c:\testDir"
@@ -67,8 +70,38 @@ public void CreateWorkerProcess_VerifyEnvVars(string processEnvValue)
6770
}
6871

6972
[Fact]
70-
public async Task StartProcess_LinuxConsumption_TriesToAssignExecutePermissions()
73+
public async Task StartProcess_LinuxConsumption_TriesToAssignExecutePermissions_Exists()
74+
{
75+
try
76+
{
77+
File.Create(_executablePath).Dispose();
78+
TestEnvironment testEnvironment = new TestEnvironment();
79+
testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, "TestContainer");
80+
var mockHttpWorkerProcess = new HttpWorkerProcess(_testWorkerId, _rootScriptPath, _httpWorkerOptions, _mockEventManager.Object, _defaultWorkerProcessFactory, _processRegistry, _testLogger, _languageWorkerConsoleLogSource.Object, testEnvironment, new TestMetricsLogger(), _serviceProviderMock.Object, new LoggerFactory());
81+
82+
try
83+
{
84+
await mockHttpWorkerProcess.StartProcessAsync();
85+
}
86+
catch
87+
{
88+
// expected to throw. Just verifying a log statement occurred before then.
89+
}
90+
91+
// Verify method invocation
92+
var testLogs = _testLogger.GetLogMessages();
93+
Assert.Contains("Error while assigning execute permission", testLogs[0].FormattedMessage);
94+
}
95+
finally
96+
{
97+
File.Delete(_executablePath);
98+
}
99+
}
100+
101+
[Fact]
102+
public async Task StartProcess_LinuxConsumption_TriesToAssignExecutePermissions_NotExists()
71103
{
104+
File.Delete(_executablePath);
72105
TestEnvironment testEnvironment = new TestEnvironment();
73106
testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, "TestContainer");
74107
var mockHttpWorkerProcess = new HttpWorkerProcess(_testWorkerId, _rootScriptPath, _httpWorkerOptions, _mockEventManager.Object, _defaultWorkerProcessFactory, _processRegistry, _testLogger, _languageWorkerConsoleLogSource.Object, testEnvironment, new TestMetricsLogger(), _serviceProviderMock.Object, new LoggerFactory());
@@ -79,12 +112,12 @@ public async Task StartProcess_LinuxConsumption_TriesToAssignExecutePermissions(
79112
}
80113
catch
81114
{
82-
// expected to throw. Just verifying a log statement occured before then.
115+
// expected to throw. Just verifying a log statement occurred before then.
83116
}
84117

85118
// Verify method invocation
86119
var testLogs = _testLogger.GetLogMessages();
87-
Assert.Contains("Error while assigning execute permission", testLogs[0].FormattedMessage);
120+
Assert.Contains("File path does not exist, not assigning permissions", testLogs[0].FormattedMessage);
88121
}
89122
}
90123
}

0 commit comments

Comments
 (0)