make insecure channel and server explicit

pull/2658/head
Jan Tattermusch 10 years ago
parent 072e7ad1f3
commit a96ac058f4
  1. 4
      src/csharp/Grpc.Core.Tests/ClientServerTest.cs
  2. 2
      src/csharp/Grpc.Core.Tests/ServerTest.cs
  3. 4
      src/csharp/Grpc.Core.Tests/TimeoutsTest.cs
  4. 16
      src/csharp/Grpc.Core/Channel.cs
  5. 27
      src/csharp/Grpc.Core/Credentials.cs
  6. 6
      src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs
  7. 7
      src/csharp/Grpc.Core/Internal/CredentialsSafeHandle.cs
  8. 8
      src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
  9. 51
      src/csharp/Grpc.Core/Server.cs
  10. 22
      src/csharp/Grpc.Core/ServerCredentials.cs
  11. 2
      src/csharp/Grpc.Examples.MathClient/MathClient.cs
  12. 2
      src/csharp/Grpc.Examples.MathServer/MathServer.cs
  13. 4
      src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
  14. 4
      src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs
  15. 2
      src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
  16. 4
      src/csharp/Grpc.IntegrationTesting/InteropServer.cs
  17. 2
      src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs
  18. 4
      src/csharp/ext/grpc_csharp_ext.c

@ -79,9 +79,9 @@ namespace Grpc.Core.Tests
{ {
server = new Server(); server = new Server();
server.AddServiceDefinition(ServiceDefinition); server.AddServiceDefinition(ServiceDefinition);
int port = server.AddListeningPort(Host, Server.PickUnusedPort); int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
server.Start(); server.Start();
channel = new Channel(Host, port); channel = new Channel(Host, port, Credentials.Insecure);
} }
[TearDown] [TearDown]

@ -45,7 +45,7 @@ namespace Grpc.Core.Tests
public void StartAndShutdownServer() public void StartAndShutdownServer()
{ {
Server server = new Server(); Server server = new Server();
server.AddListeningPort("localhost", Server.PickUnusedPort); server.AddPort("localhost", Server.PickUnusedPort, ServerCredentials.Insecure);
server.Start(); server.Start();
server.ShutdownAsync().Wait(); server.ShutdownAsync().Wait();
GrpcEnvironment.Shutdown(); GrpcEnvironment.Shutdown();

@ -72,9 +72,9 @@ namespace Grpc.Core.Tests
{ {
server = new Server(); server = new Server();
server.AddServiceDefinition(ServiceDefinition); server.AddServiceDefinition(ServiceDefinition);
int port = server.AddListeningPort(Host, Server.PickUnusedPort); int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
server.Start(); server.Start();
channel = new Channel(Host, port); channel = new Channel(Host, port, Credentials.Insecure);
stringFromServerHandlerTcs = new TaskCompletionSource<string>(); stringFromServerHandlerTcs = new TaskCompletionSource<string>();
} }

@ -56,26 +56,24 @@ namespace Grpc.Core
/// Port will default to 80 for an unsecure channel and to 443 a secure channel. /// Port will default to 80 for an unsecure channel and to 443 a secure channel.
/// </summary> /// </summary>
/// <param name="host">The DNS name of IP address of the host.</param> /// <param name="host">The DNS name of IP address of the host.</param>
/// <param name="credentials">Optional credentials to create a secure channel.</param> /// <param name="credentials">Credentials to secure the channel.</param>
/// <param name="options">Channel options.</param> /// <param name="options">Channel options.</param>
public Channel(string host, Credentials credentials = null, IEnumerable<ChannelOption> options = null) public Channel(string host, Credentials credentials, IEnumerable<ChannelOption> options = null)
{ {
this.environment = GrpcEnvironment.GetInstance(); this.environment = GrpcEnvironment.GetInstance();
this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>();
EnsureUserAgentChannelOption(this.options); EnsureUserAgentChannelOption(this.options);
using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options)) using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options))
{ {
if (credentials != null) if (nativeCredentials != null)
{
using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
{ {
this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs); this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
} }
}
else else
{ {
this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs); this.handle = ChannelSafeHandle.CreateInsecure(host, nativeChannelArgs);
} }
} }
this.target = GetOverridenTarget(host, this.options); this.target = GetOverridenTarget(host, this.options);
@ -86,9 +84,9 @@ namespace Grpc.Core
/// </summary> /// </summary>
/// <param name="host">DNS name or IP address</param> /// <param name="host">DNS name or IP address</param>
/// <param name="port">the port</param> /// <param name="port">the port</param>
/// <param name="credentials">Optional credentials to create a secure channel.</param> /// <param name="credentials">Credentials to secure the channel.</param>
/// <param name="options">Channel options.</param> /// <param name="options">Channel options.</param>
public Channel(string host, int port, Credentials credentials = null, IEnumerable<ChannelOption> options = null) : public Channel(string host, int port, Credentials credentials, IEnumerable<ChannelOption> options = null) :
this(string.Format("{0}:{1}", host, port), credentials, options) this(string.Format("{0}:{1}", host, port), credentials, options)
{ {
} }

@ -41,17 +41,40 @@ namespace Grpc.Core
/// </summary> /// </summary>
public abstract class Credentials public abstract class Credentials
{ {
static readonly Credentials InsecureInstance = new InsecureCredentialsImpl();
/// <summary> /// <summary>
/// Creates native object for the credentials. /// Returns instance of credential that provides no security and
/// will result in creating an unsecure channel with no encryption whatsoever.
/// </summary>
public static Credentials Insecure
{
get
{
return InsecureInstance;
}
}
/// <summary>
/// Creates native object for the credentials. May return null if insecure channel
/// should be created.
/// </summary> /// </summary>
/// <returns>The native credentials.</returns> /// <returns>The native credentials.</returns>
internal abstract CredentialsSafeHandle ToNativeCredentials(); internal abstract CredentialsSafeHandle ToNativeCredentials();
private sealed class InsecureCredentialsImpl : Credentials
{
internal override CredentialsSafeHandle ToNativeCredentials()
{
return null;
}
}
} }
/// <summary> /// <summary>
/// Client-side SSL credentials. /// Client-side SSL credentials.
/// </summary> /// </summary>
public class SslCredentials : Credentials public sealed class SslCredentials : Credentials
{ {
readonly string rootCertificates; readonly string rootCertificates;
readonly KeyCertificatePair keyCertificatePair; readonly KeyCertificatePair keyCertificatePair;

@ -41,7 +41,7 @@ namespace Grpc.Core.Internal
internal class ChannelSafeHandle : SafeHandleZeroIsInvalid internal class ChannelSafeHandle : SafeHandleZeroIsInvalid
{ {
[DllImport("grpc_csharp_ext.dll")] [DllImport("grpc_csharp_ext.dll")]
static extern ChannelSafeHandle grpcsharp_channel_create(string target, ChannelArgsSafeHandle channelArgs); static extern ChannelSafeHandle grpcsharp_insecure_channel_create(string target, ChannelArgsSafeHandle channelArgs);
[DllImport("grpc_csharp_ext.dll")] [DllImport("grpc_csharp_ext.dll")]
static extern ChannelSafeHandle grpcsharp_secure_channel_create(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs); static extern ChannelSafeHandle grpcsharp_secure_channel_create(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs);
@ -56,9 +56,9 @@ namespace Grpc.Core.Internal
{ {
} }
public static ChannelSafeHandle Create(string target, ChannelArgsSafeHandle channelArgs) public static ChannelSafeHandle CreateInsecure(string target, ChannelArgsSafeHandle channelArgs)
{ {
return grpcsharp_channel_create(target, channelArgs); return grpcsharp_insecure_channel_create(target, channelArgs);
} }
public static ChannelSafeHandle CreateSecure(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs) public static ChannelSafeHandle CreateSecure(CredentialsSafeHandle credentials, string target, ChannelArgsSafeHandle channelArgs)

@ -50,6 +50,13 @@ namespace Grpc.Core.Internal
{ {
} }
public static CredentialsSafeHandle CreateNullCredentials()
{
var creds = new CredentialsSafeHandle();
creds.SetHandle(IntPtr.Zero);
return creds;
}
public static CredentialsSafeHandle CreateSslCredentials(string pemRootCerts, KeyCertificatePair keyCertPair) public static CredentialsSafeHandle CreateSslCredentials(string pemRootCerts, KeyCertificatePair keyCertPair)
{ {
if (keyCertPair != null) if (keyCertPair != null)

@ -48,7 +48,7 @@ namespace Grpc.Core.Internal
static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args); static extern ServerSafeHandle grpcsharp_server_create(CompletionQueueSafeHandle cq, ChannelArgsSafeHandle args);
[DllImport("grpc_csharp_ext.dll")] [DllImport("grpc_csharp_ext.dll")]
static extern int grpcsharp_server_add_http2_port(ServerSafeHandle server, string addr); static extern int grpcsharp_server_add_insecure_http2_port(ServerSafeHandle server, string addr);
[DllImport("grpc_csharp_ext.dll")] [DllImport("grpc_csharp_ext.dll")]
static extern int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds); static extern int grpcsharp_server_add_secure_http2_port(ServerSafeHandle server, string addr, ServerCredentialsSafeHandle creds);
@ -77,12 +77,12 @@ namespace Grpc.Core.Internal
return grpcsharp_server_create(cq, args); return grpcsharp_server_create(cq, args);
} }
public int AddListeningPort(string addr) public int AddInsecurePort(string addr)
{ {
return grpcsharp_server_add_http2_port(this, addr); return grpcsharp_server_add_insecure_http2_port(this, addr);
} }
public int AddListeningPort(string addr, ServerCredentialsSafeHandle credentials) public int AddSecurePort(string addr, ServerCredentialsSafeHandle credentials)
{ {
return grpcsharp_server_add_secure_http2_port(this, addr, credentials); return grpcsharp_server_add_secure_http2_port(this, addr, credentials);
} }

@ -98,28 +98,31 @@ namespace Grpc.Core
} }
/// <summary> /// <summary>
/// Add a non-secure port on which server should listen. /// Add a port on which server should listen.
/// Only call this before Start(). /// Only call this before Start().
/// </summary> /// </summary>
/// <returns>The port on which server will be listening.</returns> /// <returns>The port on which server will be listening.</returns>
/// <param name="host">the host</param> /// <param name="host">the host</param>
/// <param name="port">the port. If zero, an unused port is chosen automatically.</param> /// <param name="port">the port. If zero, an unused port is chosen automatically.</param>
public int AddListeningPort(string host, int port) public int AddPort(string host, int port, ServerCredentials credentials)
{ {
return AddListeningPortInternal(host, port, null); lock (myLock)
}
/// <summary>
/// Add a non-secure port on which server should listen.
/// Only call this before Start().
/// </summary>
/// <returns>The port on which server will be listening.</returns>
/// <param name="host">the host</param>
/// <param name="port">the port. If zero, an unused port is chosen automatically.</param>
public int AddListeningPort(string host, int port, ServerCredentials credentials)
{ {
Preconditions.CheckNotNull(credentials); Preconditions.CheckNotNull(credentials);
return AddListeningPortInternal(host, port, credentials); Preconditions.CheckState(!startRequested);
var address = string.Format("{0}:{1}", host, port);
using (var nativeCredentials = credentials.ToNativeCredentials())
{
if (nativeCredentials != null)
{
return handle.AddSecurePort(address, nativeCredentials);
}
else
{
return handle.AddInsecurePort(address);
}
}
}
} }
/// <summary> /// <summary>
@ -186,26 +189,6 @@ namespace Grpc.Core
handle.Dispose(); handle.Dispose();
} }
private int AddListeningPortInternal(string host, int port, ServerCredentials credentials)
{
lock (myLock)
{
Preconditions.CheckState(!startRequested);
var address = string.Format("{0}:{1}", host, port);
if (credentials != null)
{
using (var nativeCredentials = credentials.ToNativeCredentials())
{
return handle.AddListeningPort(address, nativeCredentials);
}
}
else
{
return handle.AddListeningPort(address);
}
}
}
/// <summary> /// <summary>
/// Allows one new RPC call to be received by server. /// Allows one new RPC call to be received by server.
/// </summary> /// </summary>

@ -44,11 +44,33 @@ namespace Grpc.Core
/// </summary> /// </summary>
public abstract class ServerCredentials public abstract class ServerCredentials
{ {
static readonly ServerCredentials InsecureInstance = new InsecureServerCredentialsImpl();
/// <summary>
/// Returns instance of credential that provides no security and
/// will result in creating an unsecure server port with no encryption whatsoever.
/// </summary>
public static ServerCredentials Insecure
{
get
{
return InsecureInstance;
}
}
/// <summary> /// <summary>
/// Creates native object for the credentials. /// Creates native object for the credentials.
/// </summary> /// </summary>
/// <returns>The native credentials.</returns> /// <returns>The native credentials.</returns>
internal abstract ServerCredentialsSafeHandle ToNativeCredentials(); internal abstract ServerCredentialsSafeHandle ToNativeCredentials();
private sealed class InsecureServerCredentialsImpl : ServerCredentials
{
internal override ServerCredentialsSafeHandle ToNativeCredentials()
{
return null;
}
}
} }
/// <summary> /// <summary>

@ -39,7 +39,7 @@ namespace math
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
using (Channel channel = new Channel("127.0.0.1", 23456)) using (Channel channel = new Channel("127.0.0.1", 23456, Credentials.Insecure))
{ {
Math.IMathClient client = new Math.MathClient(channel); Math.IMathClient client = new Math.MathClient(channel);
MathExamples.DivExample(client); MathExamples.DivExample(client);

@ -44,7 +44,7 @@ namespace math
Server server = new Server(); Server server = new Server();
server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
int port = server.AddListeningPort(host, 23456); int port = server.AddPort(host, 23456, ServerCredentials.Insecure);
server.Start(); server.Start();
Console.WriteLine("MathServer listening on port " + port); Console.WriteLine("MathServer listening on port " + port);

@ -56,9 +56,9 @@ namespace math.Tests
{ {
server = new Server(); server = new Server();
server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
int port = server.AddListeningPort(host, Server.PickUnusedPort); int port = server.AddPort(host, Server.PickUnusedPort, ServerCredentials.Insecure);
server.Start(); server.Start();
channel = new Channel(host, port); channel = new Channel(host, port, Credentials.Insecure);
client = Math.NewClient(channel); client = Math.NewClient(channel);
// TODO(jtattermusch): get rid of the custom header here once we have dedicated tests // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests

@ -59,9 +59,9 @@ namespace Grpc.HealthCheck.Tests
server = new Server(); server = new Server();
server.AddServiceDefinition(Grpc.Health.V1Alpha.Health.BindService(serviceImpl)); server.AddServiceDefinition(Grpc.Health.V1Alpha.Health.BindService(serviceImpl));
int port = server.AddListeningPort(Host, Server.PickUnusedPort); int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
server.Start(); server.Start();
channel = new Channel(Host, port); channel = new Channel(Host, port, Credentials.Insecure);
client = Grpc.Health.V1Alpha.Health.NewClient(channel); client = Grpc.Health.V1Alpha.Health.NewClient(channel);
} }

@ -57,7 +57,7 @@ namespace Grpc.IntegrationTesting
{ {
server = new Server(); server = new Server();
server.AddServiceDefinition(TestService.BindService(new TestServiceImpl())); server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
int port = server.AddListeningPort(host, Server.PickUnusedPort, TestCredentials.CreateTestServerCredentials()); int port = server.AddPort(host, Server.PickUnusedPort, TestCredentials.CreateTestServerCredentials());
server.Start(); server.Start();
var options = new List<ChannelOption> var options = new List<ChannelOption>

@ -95,11 +95,11 @@ namespace Grpc.IntegrationTesting
int port = options.port.Value; int port = options.port.Value;
if (options.useTls) if (options.useTls)
{ {
server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials()); server.AddPort(host, port, TestCredentials.CreateTestServerCredentials());
} }
else else
{ {
server.AddListeningPort(host, options.port.Value); server.AddPort(host, options.port.Value, ServerCredentials.Insecure);
} }
Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port)); Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
server.Start(); server.Start();

@ -67,7 +67,7 @@ namespace Grpc.IntegrationTesting
server = new Server(); server = new Server();
server.AddServiceDefinition(TestService.BindService(new TestServiceImpl())); server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
int port = server.AddListeningPort(host, Server.PickUnusedPort, serverCredentials); int port = server.AddPort(host, Server.PickUnusedPort, serverCredentials);
server.Start(); server.Start();
var options = new List<ChannelOption> var options = new List<ChannelOption>

@ -367,7 +367,7 @@ grpcsharp_completion_queue_pluck(grpc_completion_queue *cq, void *tag) {
/* Channel */ /* Channel */
GPR_EXPORT grpc_channel *GPR_CALLTYPE GPR_EXPORT grpc_channel *GPR_CALLTYPE
grpcsharp_channel_create(const char *target, const grpc_channel_args *args) { grpcsharp_insecure_channel_create(const char *target, const grpc_channel_args *args) {
return grpc_channel_create(target, args); return grpc_channel_create(target, args);
} }
@ -717,7 +717,7 @@ grpcsharp_server_create(grpc_completion_queue *cq,
} }
GPR_EXPORT gpr_int32 GPR_CALLTYPE GPR_EXPORT gpr_int32 GPR_CALLTYPE
grpcsharp_server_add_http2_port(grpc_server *server, const char *addr) { grpcsharp_server_add_insecure_http2_port(grpc_server *server, const char *addr) {
return grpc_server_add_http2_port(server, addr); return grpc_server_add_http2_port(server, addr);
} }

Loading…
Cancel
Save