From 480315272af618c9aaf75deb043ece7ba491f02d Mon Sep 17 00:00:00 2001 From: Michael Peng Date: Wed, 9 Feb 2022 09:32:14 -0800 Subject: [PATCH 1/5] Upgraded protobuf versions and removed Grpc.Core dependency --- src/Microsoft.Azure.Functions.PowerShellWorker.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Azure.Functions.PowerShellWorker.csproj b/src/Microsoft.Azure.Functions.PowerShellWorker.csproj index aed806b2..31099354 100644 --- a/src/Microsoft.Azure.Functions.PowerShellWorker.csproj +++ b/src/Microsoft.Azure.Functions.PowerShellWorker.csproj @@ -20,7 +20,7 @@ Licensed under the MIT license. See LICENSE file in the project root for full li - + From 4704d48c1ff3479d0f8737d94356f2e513982189 Mon Sep 17 00:00:00 2001 From: Michael Peng Date: Wed, 9 Feb 2022 09:32:29 -0800 Subject: [PATCH 2/5] Updated channel and option types used --- src/Messaging/MessagingStream.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Messaging/MessagingStream.cs b/src/Messaging/MessagingStream.cs index 82f1e108..714f0321 100644 --- a/src/Messaging/MessagingStream.cs +++ b/src/Messaging/MessagingStream.cs @@ -3,31 +3,33 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -using System; using System.Threading; using System.Threading.Tasks; -using Grpc.Core; +using Grpc.Net.Client; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging { internal class MessagingStream { - private readonly AsyncDuplexStreamingCall _call; + private readonly Grpc.Core.AsyncDuplexStreamingCall _call; private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(initialCount: 1, maxCount: 1); internal MessagingStream(string host, int port) { + // To call unsecured gRPC services, ensure the address starts with 'http' as opposed to 'https'. + // For more detail, see https://docs.microsoft.com/en-us/aspnet/core/grpc/client?view=aspnetcore-6.0 + string address = $"{host}:{port}"; const int maxMessageLength = int.MaxValue; - var channelOptions = new [] + var channelOptions = new GrpcChannelOptions { - new ChannelOption(ChannelOptions.MaxReceiveMessageLength, maxMessageLength), - new ChannelOption(ChannelOptions.MaxSendMessageLength, maxMessageLength) + MaxReceiveMessageSize = maxMessageLength, + MaxSendMessageSize = maxMessageLength, }; - Channel channel = new Channel(host, port, ChannelCredentials.Insecure, channelOptions); + GrpcChannel channel = GrpcChannel.ForAddress(address, channelOptions); _call = new FunctionRpc.FunctionRpcClient(channel).EventStream(); } From d16bcfffdfdfa179e9a9bbd05e23a3914ee60134 Mon Sep 17 00:00:00 2001 From: Michael Peng Date: Thu, 17 Feb 2022 12:52:28 -0800 Subject: [PATCH 3/5] Change channel credentials --- src/Messaging/MessagingStream.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Messaging/MessagingStream.cs b/src/Messaging/MessagingStream.cs index 714f0321..17dfd741 100644 --- a/src/Messaging/MessagingStream.cs +++ b/src/Messaging/MessagingStream.cs @@ -27,6 +27,7 @@ internal MessagingStream(string host, int port) { MaxReceiveMessageSize = maxMessageLength, MaxSendMessageSize = maxMessageLength, + Credentials = Grpc.Core.ChannelCredentials.Insecure }; GrpcChannel channel = GrpcChannel.ForAddress(address, channelOptions); From 271465061a255426f3ccff6b2b370a30ce60f767 Mon Sep 17 00:00:00 2001 From: Michael Peng Date: Fri, 18 Feb 2022 14:47:45 -0800 Subject: [PATCH 4/5] Added http prefix to url --- src/Messaging/MessagingStream.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Messaging/MessagingStream.cs b/src/Messaging/MessagingStream.cs index 17dfd741..c221d912 100644 --- a/src/Messaging/MessagingStream.cs +++ b/src/Messaging/MessagingStream.cs @@ -20,14 +20,13 @@ internal MessagingStream(string host, int port) { // To call unsecured gRPC services, ensure the address starts with 'http' as opposed to 'https'. // For more detail, see https://docs.microsoft.com/en-us/aspnet/core/grpc/client?view=aspnetcore-6.0 - string address = $"{host}:{port}"; + string address = $"http://{host}:{port}"; const int maxMessageLength = int.MaxValue; var channelOptions = new GrpcChannelOptions { MaxReceiveMessageSize = maxMessageLength, - MaxSendMessageSize = maxMessageLength, - Credentials = Grpc.Core.ChannelCredentials.Insecure + MaxSendMessageSize = maxMessageLength }; GrpcChannel channel = GrpcChannel.ForAddress(address, channelOptions); From c2ec3f72ab7f739030e9df7fe00ebdc1dd9b7c33 Mon Sep 17 00:00:00 2001 From: Michael Peng Date: Fri, 18 Feb 2022 15:01:06 -0800 Subject: [PATCH 5/5] Add valid URL check and explicitly include credentials --- src/Messaging/MessagingStream.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Messaging/MessagingStream.cs b/src/Messaging/MessagingStream.cs index c221d912..596c7737 100644 --- a/src/Messaging/MessagingStream.cs +++ b/src/Messaging/MessagingStream.cs @@ -3,9 +3,11 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // +using System; using System.Threading; using System.Threading.Tasks; +using Grpc.Core; using Grpc.Net.Client; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; @@ -13,23 +15,29 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging { internal class MessagingStream { - private readonly Grpc.Core.AsyncDuplexStreamingCall _call; + private readonly AsyncDuplexStreamingCall _call; private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(initialCount: 1, maxCount: 1); internal MessagingStream(string host, int port) { // To call unsecured gRPC services, ensure the address starts with 'http' as opposed to 'https'. // For more detail, see https://docs.microsoft.com/en-us/aspnet/core/grpc/client?view=aspnetcore-6.0 - string address = $"http://{host}:{port}"; + string uriString = $"http://{host}:{port}"; + if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri grpcUri)) + { + throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed."); + } + const int maxMessageLength = int.MaxValue; var channelOptions = new GrpcChannelOptions { MaxReceiveMessageSize = maxMessageLength, - MaxSendMessageSize = maxMessageLength + MaxSendMessageSize = maxMessageLength, + Credentials = ChannelCredentials.Insecure }; - GrpcChannel channel = GrpcChannel.ForAddress(address, channelOptions); + GrpcChannel channel = GrpcChannel.ForAddress(grpcUri, channelOptions); _call = new FunctionRpc.FunctionRpcClient(channel).EventStream(); }