From ccf39971efcc734227dbf0f60589d98e0d0bfc75 Mon Sep 17 00:00:00 2001 From: Jorge Canizales <jcanizales@google.com> Date: Mon, 16 Mar 2015 22:01:08 -0700 Subject: [PATCH] Makes GRPCChannel a cluster class of secure and unsecured. --- .../GRPCClient/private/GRPCChannel.h | 5 +- .../GRPCClient/private/GRPCChannel.m | 25 ++++++++-- .../GRPCClient/private/GRPCSecureChannel.h | 38 +++++++++++++++ .../GRPCClient/private/GRPCSecureChannel.m | 48 +++++++++++++++++++ .../GRPCClient/private/GRPCUnsecuredChannel.h | 38 +++++++++++++++ .../GRPCClient/private/GRPCUnsecuredChannel.m | 44 +++++++++++++++++ 6 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 src/objective-c/GRPCClient/private/GRPCSecureChannel.h create mode 100644 src/objective-c/GRPCClient/private/GRPCSecureChannel.m create mode 100644 src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.h create mode 100644 src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.m diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.h b/src/objective-c/GRPCClient/private/GRPCChannel.h index 2e07dcc3c7..bc6a47d469 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.h +++ b/src/objective-c/GRPCClient/private/GRPCChannel.h @@ -45,6 +45,7 @@ struct grpc_channel; // Convenience constructor to allow for reuse of connections. + (instancetype)channelToHost:(NSString *)host; -// Designated initializer -- (instancetype)initWithHost:(NSString *)host; +- (instancetype)initWithHost:(NSString *)host NS_DESIGNATED_INITIALIZER; + +- (instancetype)initWithChannel:(struct grpc_channel *)unmanagedChannel NS_DESIGNATED_INITIALIZER; @end diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index 7ddc01dc24..d998a1f32e 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCChannel.m @@ -33,7 +33,10 @@ #import "GRPCChannel.h" -#import <grpc/grpc.h> +#include <grpc/grpc.h> + +#import "GRPCSecureChannel.h" +#import "GRPCUnsecuredChannel.h" @implementation GRPCChannel @@ -46,13 +49,25 @@ return [self initWithHost:nil]; } -// Designated initializer - (instancetype)initWithHost:(NSString *)host { - if (!host) { - [NSException raise:NSInvalidArgumentException format:@"Host can't be nil."]; + NSURL *hostURL = [NSURL URLWithString:host]; + if (!hostURL) { + [NSException raise:NSInvalidArgumentException format:@"Invalid URL: %@", host]; + } + if (!hostURL.scheme || [hostURL.scheme isEqualToString:@"https"]) { + return [[GRPCSecureChannel alloc] initWithHost:host]; + } + if ([hostURL.scheme isEqualToString:@"http"]) { + return [[GRPCUnsecuredChannel alloc] initWithHost:host]; } + [NSException raise:NSInvalidArgumentException + format:@"URL scheme %@ isn't supported.", hostURL.scheme]; + return nil; // silence warning. +} + +- (instancetype)initWithChannel:(struct grpc_channel *)unmanagedChannel { if ((self = [super init])) { - _unmanagedChannel = grpc_channel_create(host.UTF8String, NULL); + _unmanagedChannel = unmanagedChannel; } return self; } diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannel.h b/src/objective-c/GRPCClient/private/GRPCSecureChannel.h new file mode 100644 index 0000000000..d34ceaea0c --- /dev/null +++ b/src/objective-c/GRPCClient/private/GRPCSecureChannel.h @@ -0,0 +1,38 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#import "GRPCChannel.h" + +@interface GRPCSecureChannel : GRPCChannel + +@end diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m new file mode 100644 index 0000000000..a12a1a8c17 --- /dev/null +++ b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m @@ -0,0 +1,48 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#import "GRPCSecureChannel.h" + +#import <grpc/grpc_security.h> + +@implementation GRPCSecureChannel + +- (instancetype)initWithHost:(NSString *)host { + // TODO(jcanizales): Get the certificates here. + grpc_credentials *credentials = grpc_ssl_credentials_create(NULL, NULL); + return (self = [super initWithChannel:grpc_secure_channel_create(credentials, + host.UTF8String, + NULL)]); +} + +@end diff --git a/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.h b/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.h new file mode 100644 index 0000000000..9d89cfb541 --- /dev/null +++ b/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.h @@ -0,0 +1,38 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#import "GRPCChannel.h" + +@interface GRPCUnsecuredChannel : GRPCChannel + +@end diff --git a/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.m b/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.m new file mode 100644 index 0000000000..d27f7ca565 --- /dev/null +++ b/src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.m @@ -0,0 +1,44 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#import "GRPCUnsecuredChannel.h" + +#include <grpc/grpc.h> + +@implementation GRPCUnsecuredChannel + +- (instancetype)initWithHost:(NSString *)host { + return (self = [super initWithChannel:grpc_channel_create(host.UTF8String, NULL)]); +} + +@end -- GitLab