File tree Expand file tree Collapse file tree 6 files changed +102
-2
lines changed Expand file tree Collapse file tree 6 files changed +102
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 44//
55
66using System ;
7+ using System . Text . RegularExpressions ;
78using System . Threading . Tasks ;
89
910using CommandLine ;
10- using Microsoft . Azure . Functions . PowerShellWorker . PowerShell ;
1111using Microsoft . Azure . Functions . PowerShellWorker . Messaging ;
1212using Microsoft . Azure . Functions . PowerShellWorker . Utility ;
1313using 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments