Skip to content

Commit 5d2ef26

Browse files
authored
Prevent attempts to select PS7 on Functions v2 (#407)
1 parent 35ea647 commit 5d2ef26

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

src/RequestProcessor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ internal class RequestProcessor
3939

4040
internal RequestProcessor(MessagingStream msgStream)
4141
{
42+
var invalidVersionMessage = FunctionsWorkerRuntimeVersionValidator.GetErrorMessage();
43+
if (invalidVersionMessage != null)
44+
{
45+
_initTerminatingError = new InvalidOperationException(invalidVersionMessage);
46+
}
47+
4248
_msgStream = msgStream;
4349
_powershellPool = new PowerShellManagerPool(msgStream);
4450

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
7+
{
8+
using System;
9+
using System.Text.RegularExpressions;
10+
11+
internal static class FunctionsWorkerRuntimeVersionValidator
12+
{
13+
private const string VersionVariableName = "FUNCTIONS_WORKER_RUNTIME_VERSION";
14+
15+
public static string GetErrorMessage()
16+
{
17+
var requestedVersion = Environment.GetEnvironmentVariable(VersionVariableName);
18+
return GetErrorMessage(requestedVersion);
19+
}
20+
21+
internal static string GetErrorMessage(string requestedVersion)
22+
{
23+
if (!string.IsNullOrWhiteSpace(requestedVersion)
24+
// Assuming this code is running on Functions runtime v2, allow
25+
// PowerShell version 6 only (ignoring leading and trailing spaces, and the optional ~ in front of 6)
26+
&& !Regex.IsMatch(requestedVersion, @"^\s*~?6\s*$"))
27+
{
28+
return string.Format(
29+
PowerShellWorkerStrings.InvalidFunctionsWorkerRuntimeVersion,
30+
VersionVariableName,
31+
requestedVersion);
32+
}
33+
34+
return null;
35+
}
36+
}
37+
}

src/Worker.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
//
55

66
using System;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89

910
using CommandLine;
10-
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell;
1111
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
1212
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
1313
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
@@ -30,6 +30,8 @@ public async static Task Main(string[] args)
3030
LogLevel.Information,
3131
string.Format(PowerShellWorkerStrings.PowerShellWorkerVersion, typeof(Worker).Assembly.GetName().Version));
3232

33+
ValidateFunctionsWorkerRuntimeVersion();
34+
3335
WorkerArguments arguments = null;
3436
Parser.Default.ParseArguments<WorkerArguments>(args)
3537
.WithParsed(ops => arguments = ops)
@@ -47,6 +49,15 @@ public async static Task Main(string[] args)
4749
msgStream.Write(startedMessage);
4850
await requestProcessor.ProcessRequestLoop();
4951
}
52+
53+
private static void ValidateFunctionsWorkerRuntimeVersion()
54+
{
55+
var message = FunctionsWorkerRuntimeVersionValidator.GetErrorMessage();
56+
if (message != null)
57+
{
58+
RpcLogger.WriteSystemLog(LogLevel.Critical, message);
59+
}
60+
}
5061
}
5162

5263
internal class WorkerArguments

src/resources/PowerShellWorkerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,7 @@
310310
<data name="CommandNotFoundException_Exception" xml:space="preserve">
311311
<value>CommandNotFoundException detected (exception).</value>
312312
</data>
313+
<data name="InvalidFunctionsWorkerRuntimeVersion" xml:space="preserve">
314+
<value>Invalid PowerShell version specified in the {0} environment variable: {1}. This version is not supported on Azure Functions Runtime v2.</value>
315+
</data>
313316
</root>

test/Unit/DependencyManagement/NewerDependencySnapshotDetectorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Test.DependencyManagement
99
using Xunit;
1010

1111
using PowerShellWorker.DependencyManagement;
12-
using Utility;
12+
using PowerShellWorker.Utility;
1313

1414
public class NewerDependencySnapshotDetectorTests
1515
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Utility
7+
{
8+
using Xunit;
9+
10+
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
11+
12+
public class FunctionsWorkerRuntimeVersionValidatorTests
13+
{
14+
[Theory]
15+
[InlineData(null)]
16+
[InlineData("")]
17+
[InlineData(" ")]
18+
[InlineData("6")]
19+
[InlineData("~6")]
20+
[InlineData(" 6 ")]
21+
[InlineData(" ~6 ")]
22+
public void NoErrorOnValidVersion(string versionToCheck)
23+
{
24+
Assert.Null(FunctionsWorkerRuntimeVersionValidator.GetErrorMessage(versionToCheck));
25+
}
26+
27+
[Theory]
28+
[InlineData("7")]
29+
[InlineData("~7")]
30+
[InlineData(" 7 ")]
31+
[InlineData(" ~7 ")]
32+
[InlineData("5")]
33+
[InlineData("8")]
34+
[InlineData("~")]
35+
[InlineData("anything else")]
36+
public void ErrorOnInvalidVersion(string versionToCheck)
37+
{
38+
var error = FunctionsWorkerRuntimeVersionValidator.GetErrorMessage(versionToCheck);
39+
Assert.NotNull(error);
40+
Assert.Contains(versionToCheck, error);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)