Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when ConfigureHttpClient is used with gRPC #1257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Grpc.Net.ClientFactory/Internal/DefaultGrpcClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Options;

namespace Grpc.Net.ClientFactory.Internal
Expand All @@ -27,17 +28,20 @@ internal class DefaultGrpcClientFactory : GrpcClientFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly GrpcCallInvokerFactory _callInvokerFactory;
private readonly IOptionsMonitor<GrpcClientFactoryOptions> _clientFactoryOptionsMonitor;
private readonly IOptionsMonitor<GrpcClientFactoryOptions> _grpcClientFactoryOptionsMonitor;
private readonly IOptionsMonitor<HttpClientFactoryOptions> _httpClientFactoryOptionsMonitor;
private readonly IHttpMessageHandlerFactory _messageHandlerFactory;

public DefaultGrpcClientFactory(IServiceProvider serviceProvider,
GrpcCallInvokerFactory callInvokerFactory,
IOptionsMonitor<GrpcClientFactoryOptions> clientFactoryOptionsMonitor,
IOptionsMonitor<GrpcClientFactoryOptions> grpcClientFactoryOptionsMonitor,
IOptionsMonitor<HttpClientFactoryOptions> httpClientFactoryOptionsMonitor,
IHttpMessageHandlerFactory messageHandlerFactory)
{
_serviceProvider = serviceProvider;
_callInvokerFactory = callInvokerFactory;
_clientFactoryOptionsMonitor = clientFactoryOptionsMonitor;
_grpcClientFactoryOptionsMonitor = grpcClientFactoryOptionsMonitor;
_httpClientFactoryOptionsMonitor = httpClientFactoryOptionsMonitor;
_messageHandlerFactory = messageHandlerFactory;
}

Expand All @@ -49,7 +53,13 @@ public override TClient CreateClient<TClient>(string name) where TClient : class
throw new InvalidOperationException($"No gRPC client configured with name '{name}'.");
}

var clientFactoryOptions = _clientFactoryOptionsMonitor.Get(name);
var httpClientFactoryOptions = _httpClientFactoryOptionsMonitor.Get(name);
if (httpClientFactoryOptions.HttpClientActions.Count > 0)
{
throw new InvalidOperationException($"The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name '{name}'.");
}

var clientFactoryOptions = _grpcClientFactoryOptionsMonitor.Get(name);
var httpHandler = _messageHandlerFactory.CreateHandler(name);
var callInvoker = _callInvokerFactory.CreateCallInvoker(httpHandler, name, typeof(TClient), clientFactoryOptions);

Expand Down
139 changes: 139 additions & 0 deletions test/FunctionalTests/Client/AuthorizationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Greet;
using Grpc.AspNetCore.FunctionalTests.Infrastructure;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Grpc.Net.Client;
using Grpc.Tests.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework;

namespace Grpc.AspNetCore.FunctionalTests.Client
{
[TestFixture]
public class AuthorizationTests : FunctionalTestBase
{
[Test]
public async Task Client_CallCredentials_RoundtripToken()
{
// Arrange
string? authorization = null;
Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext context)
{
authorization = context.RequestHeaders.GetValue("authorization");

return Task.FromResult(new HelloReply());
}
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryTelemetryHeader);

var token = "token!";
var credentials = CallCredentials.FromInterceptor((context, metadata) =>
{
if (!string.IsNullOrEmpty(token))
{
metadata.Add("Authorization", $"Bearer {token}");
}
return Task.CompletedTask;
});

var options = new GrpcChannelOptions
{
LoggerFactory = LoggerFactory,
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials),
HttpHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
}
};

var channel = GrpcChannel.ForAddress(Fixture.GetUrl(TestServerEndpointName.Http2WithTls), options);
var client = TestClientFactory.Create<HelloRequest, HelloReply>(channel, method);

var call = client.UnaryCall(new HelloRequest { Name = "world" });

// Act
await call.ResponseAsync.DefaultTimeout();


Assert.AreEqual("Bearer token!", authorization);
}

[Test]
public async Task ClientFactory_CallCredentials_RoundtripToken()
{
string? authorization = null;
Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext context)
{
authorization = context.RequestHeaders.GetValue("authorization");

return Task.FromResult(new HelloReply());
}
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryTelemetryHeader);

var token = "token!";
var credentials = CallCredentials.FromInterceptor((context, metadata) =>
{
if (!string.IsNullOrEmpty(token))
{
metadata.Add("Authorization", $"Bearer {token}");
}
return Task.CompletedTask;
});

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ILoggerFactory>(LoggerFactory);
serviceCollection
.AddGrpcClient<TestClient<HelloRequest, HelloReply>>(options =>
{
options.Address = Fixture.GetUrl(TestServerEndpointName.Http2WithTls);
})
.ConfigureChannel(channel =>
{
channel.Credentials = ChannelCredentials.Create(new SslCredentials(), credentials);
})
.ConfigureGrpcClientCreator(invoker =>
{
return TestClientFactory.Create(invoker, method);
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
});
var services = serviceCollection.BuildServiceProvider();

var client = services.GetRequiredService<TestClient<HelloRequest, HelloReply>>();

var call = client.UnaryCall(new HelloRequest { Name = "world" });

await call.ResponseAsync.DefaultTimeout();

Assert.AreEqual("Bearer token!", authorization);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using Grpc.Tests.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -334,6 +335,7 @@ private static DefaultGrpcClientFactory CreateGrpcClientFactory(ServiceProvider
return new DefaultGrpcClientFactory(serviceProvider,
serviceProvider.GetRequiredService<GrpcCallInvokerFactory>(),
serviceProvider.GetRequiredService<IOptionsMonitor<GrpcClientFactoryOptions>>(),
serviceProvider.GetRequiredService<IOptionsMonitor<HttpClientFactoryOptions>>(),
serviceProvider.GetRequiredService<IHttpMessageHandlerFactory>());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Greet;
Expand Down Expand Up @@ -151,13 +152,12 @@ public void CreateClient_NoAddress_ThrowError()
}

[Test]
public void CreateClient_AddressSpecifiedOnHttpClientFactory_ThrowError()
public void CreateClient_ConfigureHttpClient_ThrowError()
{
// Arrange
var services = new ServiceCollection();
services
.AddGrpcClient<TestGreeterClient>()
// The underlying handler is used directly so no longer look for address on HttpClient
.ConfigureHttpClient(options => options.BaseAddress = new Uri("http://contoso"))
.ConfigurePrimaryHttpMessageHandler(() =>
{
Expand All @@ -172,7 +172,7 @@ public void CreateClient_AddressSpecifiedOnHttpClientFactory_ThrowError()
var ex = Assert.Throws<InvalidOperationException>(() => clientFactory.CreateClient<TestGreeterClient>(nameof(TestGreeterClient)))!;

// Assert
Assert.AreEqual(@"Could not resolve the address for gRPC client 'TestGreeterClient'. Set an address when registering the client: services.AddGrpcClient<TestGreeterClient>(o => o.Address = new Uri(""https://localhost:5001""))", ex.Message);
Assert.AreEqual("The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name 'TestGreeterClient'.", ex.Message);
}

#if NET472
Expand Down Expand Up @@ -346,6 +346,7 @@ private static DefaultGrpcClientFactory CreateGrpcClientFactory(ServiceProvider
return new DefaultGrpcClientFactory(serviceProvider,
serviceProvider.GetRequiredService<GrpcCallInvokerFactory>(),
serviceProvider.GetRequiredService<IOptionsMonitor<GrpcClientFactoryOptions>>(),
serviceProvider.GetRequiredService<IOptionsMonitor<HttpClientFactoryOptions>>(),
serviceProvider.GetRequiredService<IHttpMessageHandlerFactory>());
}
}
Expand Down