From a96ba40f0d3d792d81676901d49577848cb3a8a5 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Fri, 15 May 2015 22:27:13 -0700 Subject: Base definition of the C++ auth context API. --- include/grpc++/auth_context.h | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 include/grpc++/auth_context.h (limited to 'include') diff --git a/include/grpc++/auth_context.h b/include/grpc++/auth_context.h new file mode 100644 index 0000000000..b0aebac210 --- /dev/null +++ b/include/grpc++/auth_context.h @@ -0,0 +1,68 @@ +/* + * + * 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. + * + */ + +#ifndef GRPCXX_AUTH_CONTEXT_H +#define GRPCXX_AUTH_CONTEXT_H + +#include + +#include + +#include + +namespace grpc { + +class AuthContext GRPC_FINAL : { + public: + typedef std::pair Property; + + // A peer identity, in general is one or more properties (in which case they + // have the same name). + std::vector GetPeerIdentity() const; + grpc::string GetPeerIdentityPropertyName() const; + + // Returns all the property values with the given name. + std::vector FindPropertyValues(const grpc::string& name) const; + + // Iteration over all the properties. + std::const_iterator begin() const; + std::const_iterator end() const; + + private: + grpc_auth_context *ctx_; +}; + +} // namespace grpc + +#endif // GRPCXX_AUTH_CONTEXT_H + -- cgit v1.2.3 From fc62dddd1ca3642bc6c1bb0cd408f1bac78d52e6 Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Mon, 29 Jun 2015 02:52:46 -0700 Subject: add client side census context --- include/grpc++/client_context.h | 6 ++++++ include/grpc/census.h | 4 ++++ src/core/census/grpc_context.c | 28 +++++++++++++++++++++++----- src/core/census/grpc_context.h | 18 ++++++++++++++++++ src/core/census/initialize.c | 2 ++ src/core/surface/call.c | 19 +++++++++---------- src/cpp/client/channel.cc | 2 ++ 7 files changed, 64 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 5e10875260..09aa10508d 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -107,6 +108,10 @@ class ClientContext { creds_ = creds; } + // Get and set census context + void set_census_context(census_context* ccp) { census_context_ = ccp; } + census_context* get_census_context() const { return census_context_; } + void TryCancel(); private: @@ -154,6 +159,7 @@ class ClientContext { gpr_timespec deadline_; grpc::string authority_; std::shared_ptr creds_; + census_context* census_context_; std::multimap send_initial_metadata_; std::multimap recv_initial_metadata_; std::multimap trailing_metadata_; diff --git a/include/grpc/census.h b/include/grpc/census.h index b2049b3289..3fc07affc8 100644 --- a/include/grpc/census.h +++ b/include/grpc/census.h @@ -61,6 +61,10 @@ enum census_functions { int census_initialize(int functions); void census_shutdown(); +/* If any census feature has been initialized, this funtion will return a + * non-zero value. */ +int census_available(); + /* Internally, Census relies on a context, which should be propagated across * RPC's. From the RPC subsystems viewpoint, this is an opaque data structure. * A context must be used as the first argument to all other census diff --git a/src/core/census/grpc_context.c b/src/core/census/grpc_context.c index cf2353199f..ffdab82570 100644 --- a/src/core/census/grpc_context.c +++ b/src/core/census/grpc_context.c @@ -34,12 +34,30 @@ #include #include "src/core/census/grpc_context.h" -void *grpc_census_context_create() { - census_context *context; - census_context_deserialize(NULL, &context); - return (void *)context; -} +void *grpc_census_context_create() { return NULL; } void grpc_census_context_destroy(void *context) { census_context_destroy((census_context *)context); } + +void grpc_census_call_set_context(grpc_call *call, census_context *context) { + if (!census_available()) { + return; + } + if (context == NULL) { + if (grpc_call_is_client(call)) { + census_context *context_ptr; + census_context_deserialize(NULL, &context_ptr); + grpc_call_context_set(call, GRPC_CONTEXT_TRACING, context_ptr, + grpc_census_context_destroy); + } else { + /* TODO(aveitch): server side context code to be implemented. */ + } + } else { + grpc_call_context_set(call, GRPC_CONTEXT_TRACING, context, NULL); + } +} + +census_context *grpc_census_call_get_context(grpc_call *call) { + return (census_context *)grpc_call_context_get(call, GRPC_CONTEXT_TRACING); +} diff --git a/src/core/census/grpc_context.h b/src/core/census/grpc_context.h index f610f6ce21..01f35c3213 100644 --- a/src/core/census/grpc_context.h +++ b/src/core/census/grpc_context.h @@ -36,7 +36,25 @@ #ifndef CENSUS_GRPC_CONTEXT_H #define CENSUS_GRPC_CONTEXT_H +#include +#include "src/core/surface/call.h" + +#ifdef __cplusplus +extern "C" { +#endif + void *grpc_census_context_create(); void grpc_census_context_destroy(void *context); +/* Set census context for the call; Must be called before first call to + grpc_call_start_batch(). */ +void grpc_census_call_set_context(grpc_call *call, census_context *context); + +/* Retrieve the calls current census context. */ +census_context *grpc_census_call_get_context(grpc_call *call); + +#ifdef __cplusplus +} +#endif + #endif /* CENSUS_GRPC_CONTEXT_H */ diff --git a/src/core/census/initialize.c b/src/core/census/initialize.c index 057ac78ee7..8016520641 100644 --- a/src/core/census/initialize.c +++ b/src/core/census/initialize.c @@ -48,3 +48,5 @@ int census_initialize(int functions) { } void census_shutdown() { census_fns_enabled = CENSUS_NONE; } + +int census_available() { return (census_fns_enabled != CENSUS_NONE); } diff --git a/src/core/surface/call.c b/src/core/surface/call.c index 02e0e59cad..ef430dd9df 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -296,8 +296,6 @@ grpc_call *grpc_call_create(grpc_channel *channel, grpc_completion_queue *cq, if (call->is_client) { call->request_set[GRPC_IOREQ_SEND_TRAILING_METADATA] = REQSET_DONE; call->request_set[GRPC_IOREQ_SEND_STATUS] = REQSET_DONE; - call->context[GRPC_CONTEXT_TRACING].value = grpc_census_context_create(); - call->context[GRPC_CONTEXT_TRACING].destroy = grpc_census_context_destroy; } GPR_ASSERT(add_initial_metadata_count < MAX_SEND_INITIAL_METADATA_COUNT); for (i = 0; i < add_initial_metadata_count; i++) { @@ -462,8 +460,7 @@ static int need_more_data(grpc_call *call) { (is_op_live(call, GRPC_IOREQ_RECV_CLOSE) && grpc_bbq_empty(&call->incoming_queue)) || (call->write_state == WRITE_STATE_INITIAL && !call->is_client) || - (call->cancel_with_status != GRPC_STATUS_OK) || - call->destroy_called; + (call->cancel_with_status != GRPC_STATUS_OK) || call->destroy_called; } static void unlock(grpc_call *call) { @@ -1151,7 +1148,8 @@ static void execute_op(grpc_call *call, grpc_transport_op *op) { } else { finished_loose_op_allocated_args *args = gpr_malloc(sizeof(*args)); args->call = call; - grpc_iomgr_closure_init(&args->closure, finished_loose_op_allocated, args); + grpc_iomgr_closure_init(&args->closure, finished_loose_op_allocated, + args); op->on_consumed = &args->closure; } } @@ -1223,13 +1221,13 @@ static gpr_uint32 decode_compression(grpc_mdelem *md) { } else { gpr_uint32 parsed_clevel_bytes; if (gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value), - GPR_SLICE_LENGTH(md->value->slice), - &parsed_clevel_bytes)) { + GPR_SLICE_LENGTH(md->value->slice), + &parsed_clevel_bytes)) { /* the following cast is safe, as a gpr_uint32 should be able to hold all * possible values of the grpc_compression_level enum */ - clevel = (grpc_compression_level) parsed_clevel_bytes; + clevel = (grpc_compression_level)parsed_clevel_bytes; } else { - clevel = GRPC_COMPRESS_LEVEL_NONE; /* could not parse, no compression */ + clevel = GRPC_COMPRESS_LEVEL_NONE; /* could not parse, no compression */ } grpc_mdelem_set_user_data(md, destroy_compression, (void *)(gpr_intptr)(clevel + COMPRESS_OFFSET)); @@ -1252,7 +1250,8 @@ static void recv_metadata(grpc_call *call, grpc_metadata_batch *md) { set_status_code(call, STATUS_FROM_WIRE, decode_status(md)); } else if (key == grpc_channel_get_message_string(call->channel)) { set_status_details(call, STATUS_FROM_WIRE, grpc_mdstr_ref(md->value)); - } else if (key == grpc_channel_get_compresssion_level_string(call->channel)) { + } else if (key == + grpc_channel_get_compresssion_level_string(call->channel)) { set_decode_compression_level(call, decode_compression(md)); } else { dest = &call->buffered_metadata[is_trailing]; diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 72593f877e..5bc6f6fd91 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -39,6 +39,7 @@ #include #include +#include "src/core/census/grpc_context.h" #include "src/core/profiling/timers.h" #include #include @@ -68,6 +69,7 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, ? target_.c_str() : context->authority().c_str(), context->raw_deadline()); + grpc_census_call_set_context(c_call, context->get_census_context()); GRPC_TIMER_MARK(GRPC_PTAG_CPP_CALL_CREATED, c_call); context->set_call(c_call, shared_from_this()); return Call(c_call, this, cq); -- cgit v1.2.3 From e2aa487936de5d1734137be8a6b46766aa337c0b Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Wed, 1 Jul 2015 02:00:32 -0700 Subject: review changes --- include/grpc++/client_context.h | 2 +- src/core/census/grpc_context.c | 4 +--- src/core/census/grpc_context.h | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 09aa10508d..4d96d862e7 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -38,7 +38,6 @@ #include #include -#include #include #include #include @@ -47,6 +46,7 @@ struct grpc_call; struct grpc_completion_queue; +struct census_context; namespace grpc { diff --git a/src/core/census/grpc_context.c b/src/core/census/grpc_context.c index ffdab82570..0ed63469b6 100644 --- a/src/core/census/grpc_context.c +++ b/src/core/census/grpc_context.c @@ -34,9 +34,7 @@ #include #include "src/core/census/grpc_context.h" -void *grpc_census_context_create() { return NULL; } - -void grpc_census_context_destroy(void *context) { +static void grpc_census_context_destroy(void *context) { census_context_destroy((census_context *)context); } diff --git a/src/core/census/grpc_context.h b/src/core/census/grpc_context.h index 01f35c3213..4637e7218e 100644 --- a/src/core/census/grpc_context.h +++ b/src/core/census/grpc_context.h @@ -43,9 +43,6 @@ extern "C" { #endif -void *grpc_census_context_create(); -void grpc_census_context_destroy(void *context); - /* Set census context for the call; Must be called before first call to grpc_call_start_batch(). */ void grpc_census_call_set_context(grpc_call *call, census_context *context); -- cgit v1.2.3 From 2805be1aa5a3cfe0f425ab89de9bad0d5c48a1d1 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Wed, 1 Jul 2015 02:47:18 -0700 Subject: Adding support for raw access token credentials. --- include/grpc++/credentials.h | 6 ++++ include/grpc/grpc_security.h | 9 ++++-- src/core/security/credentials.c | 53 +++++++++++++++++++++++++++++++++++ src/cpp/client/secure_credentials.cc | 7 +++++ test/core/security/credentials_test.c | 22 +++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/grpc++/credentials.h b/include/grpc++/credentials.h index 7a40cd199d..0eaaefcbca 100644 --- a/include/grpc++/credentials.h +++ b/include/grpc++/credentials.h @@ -120,6 +120,12 @@ std::shared_ptr JWTCredentials(const grpc::string& json_key, std::shared_ptr RefreshTokenCredentials( const grpc::string& json_refresh_token); +// Builds access token credentials. +// access_token is an oauth2 access token that was fetched using an out of band +// mechanism. +std::shared_ptr AccessTokenCredentials( + const grpc::string& access_token); + // Builds IAM credentials. std::shared_ptr IAMCredentials( const grpc::string& authorization_token, diff --git a/include/grpc/grpc_security.h b/include/grpc/grpc_security.h index 7a6aa66670..1f91e65278 100644 --- a/include/grpc/grpc_security.h +++ b/include/grpc/grpc_security.h @@ -126,13 +126,18 @@ grpc_credentials *grpc_jwt_credentials_create(const char *json_key, grpc_credentials *grpc_refresh_token_credentials_create( const char *json_refresh_token); -/* Creates a fake transport security credentials object for testing. */ -grpc_credentials *grpc_fake_transport_security_credentials_create(void); +/* Creates an Oauth2 Access Token credentials with an access token that was + aquired by an out of band mechanism. */ +grpc_credentials *grpc_access_token_credentials_create( + const char *access_token); /* Creates an IAM credentials object. */ grpc_credentials *grpc_iam_credentials_create(const char *authorization_token, const char *authority_selector); +/* Creates a fake transport security credentials object for testing. */ +grpc_credentials *grpc_fake_transport_security_credentials_create(void); + /* --- Secure channel creation. --- */ /* The caller of the secure_channel_create functions may override the target diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c index cf663faf2d..2f2f17c315 100644 --- a/src/core/security/credentials.c +++ b/src/core/security/credentials.c @@ -876,6 +876,59 @@ grpc_credentials *grpc_fake_oauth2_credentials_create( return &c->base; } +/* -- Oauth2 Access Token credentials. -- */ + +typedef struct { + grpc_credentials base; + grpc_credentials_md_store *access_token_md; +} grpc_access_token_credentials; + +static void access_token_destroy(grpc_credentials *creds) { + grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds; + grpc_credentials_md_store_unref(c->access_token_md); + gpr_free(c); +} + +static int access_token_has_request_metadata(const grpc_credentials *creds) { + return 1; +} + +static int access_token_has_request_metadata_only( + const grpc_credentials *creds) { + return 1; +} + +static void access_token_get_request_metadata(grpc_credentials *creds, + grpc_pollset *pollset, + const char *service_url, + grpc_credentials_metadata_cb cb, + void *user_data) { + grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds; + cb(user_data, c->access_token_md->entries, 1, GRPC_CREDENTIALS_OK); +} + +static grpc_credentials_vtable access_token_vtable = { + access_token_destroy, access_token_has_request_metadata, + access_token_has_request_metadata_only, access_token_get_request_metadata, + NULL}; + +grpc_credentials *grpc_access_token_credentials_create( + const char *access_token) { + grpc_access_token_credentials *c = + gpr_malloc(sizeof(grpc_access_token_credentials)); + char *token_md_value; + memset(c, 0, sizeof(grpc_access_token_credentials)); + c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2; + c->base.vtable = &access_token_vtable; + gpr_ref_init(&c->base.refcount, 1); + c->access_token_md = grpc_credentials_md_store_create(1); + gpr_asprintf(&token_md_value, "Bearer %s", access_token); + grpc_credentials_md_store_add_cstrings( + c->access_token_md, GRPC_AUTHORIZATION_METADATA_KEY, token_md_value); + gpr_free(token_md_value); + return &c->base; +} + /* -- Fake transport security credentials. -- */ static void fake_transport_security_credentials_destroy( diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index b5134b3140..4d200908fb 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -117,6 +117,13 @@ std::shared_ptr RefreshTokenCredentials( grpc_refresh_token_credentials_create(json_refresh_token.c_str())); } +// Builds access token credentials. +std::shared_ptr AccessTokenCredentials( + const grpc::string& access_token) { + return WrapCredentials( + grpc_access_token_credentials_create(access_token.c_str())); +} + // Builds IAM credentials. std::shared_ptr IAMCredentials( const grpc::string& authorization_token, diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index 4253be6b07..e8bb730849 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -331,6 +331,27 @@ static void test_iam_creds(void) { check_iam_metadata, creds); } +static void check_access_token_metadata(void *user_data, + grpc_credentials_md *md_elems, + size_t num_md, + grpc_credentials_status status) { + grpc_credentials *c = (grpc_credentials *)user_data; + expected_md emd[] = {{GRPC_AUTHORIZATION_METADATA_KEY, "Bearer blah"}}; + GPR_ASSERT(status == GRPC_CREDENTIALS_OK); + GPR_ASSERT(num_md == 1); + check_metadata(emd, md_elems, num_md); + grpc_credentials_unref(c); +} + +static void test_access_token_creds(void) { + grpc_credentials *creds = grpc_access_token_credentials_create("blah"); + GPR_ASSERT(grpc_credentials_has_request_metadata(creds)); + GPR_ASSERT(grpc_credentials_has_request_metadata_only(creds)); + GPR_ASSERT(strcmp(creds->type, GRPC_CREDENTIALS_TYPE_OAUTH2) == 0); + grpc_credentials_get_request_metadata(creds, NULL, test_service_url, + check_access_token_metadata, creds); +} + static void check_ssl_oauth2_composite_metadata( void *user_data, grpc_credentials_md *md_elems, size_t num_md, grpc_credentials_status status) { @@ -863,6 +884,7 @@ int main(int argc, char **argv) { test_oauth2_token_fetcher_creds_parsing_missing_token_type(); test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime(); test_iam_creds(); + test_access_token_creds(); test_ssl_oauth2_composite_creds(); test_ssl_oauth2_iam_composite_creds(); test_compute_engine_creds_success(); -- cgit v1.2.3 From 3abe60b9d08ff5a784a39f7c4a10c631547c3526 Mon Sep 17 00:00:00 2001 From: yang-g Date: Mon, 6 Jul 2015 14:00:36 -0700 Subject: compiles --- BUILD | 9 +++ Makefile | 5 ++ build.json | 11 +++ include/grpc++/auth_context.h | 24 +++---- include/grpc++/client_context.h | 3 + include/grpc++/server_context.h | 4 ++ src/core/security/security_context.h | 8 +++ src/cpp/client/client_context.cc | 5 ++ src/cpp/common/create_auth_context.h | 42 +++++++++++ src/cpp/common/insecure_auth_context.h | 59 ++++++++++++++++ src/cpp/common/insecure_create_auth_context.cc | 46 ++++++++++++ src/cpp/common/secure_auth_context.cc | 81 ++++++++++++++++++++++ src/cpp/common/secure_auth_context.h | 61 ++++++++++++++++ src/cpp/common/secure_create_auth_context.cc | 50 +++++++++++++ src/cpp/server/server_context.cc | 6 ++ tools/doxygen/Doxyfile.c++ | 2 +- tools/doxygen/Doxyfile.c++.internal | 2 +- vsprojects/grpc++/grpc++.vcxproj | 7 ++ vsprojects/grpc++/grpc++.vcxproj.filters | 15 ++++ vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj | 5 ++ .../grpc++_unsecure.vcxproj.filters | 12 ++++ 21 files changed, 440 insertions(+), 17 deletions(-) create mode 100644 src/cpp/common/create_auth_context.h create mode 100644 src/cpp/common/insecure_auth_context.h create mode 100644 src/cpp/common/insecure_create_auth_context.cc create mode 100644 src/cpp/common/secure_auth_context.cc create mode 100644 src/cpp/common/secure_auth_context.h create mode 100644 src/cpp/common/secure_create_auth_context.cc (limited to 'include') diff --git a/BUILD b/BUILD index 6371a020f4..48879036bc 100644 --- a/BUILD +++ b/BUILD @@ -563,11 +563,15 @@ cc_library( name = "grpc++", srcs = [ "src/cpp/client/secure_credentials.h", + "src/cpp/common/secure_auth_context.h", "src/cpp/server/secure_server_credentials.h", "src/cpp/client/channel.h", + "src/cpp/common/create_auth_context.h", "src/cpp/proto/proto_utils.h", "src/cpp/server/thread_pool.h", "src/cpp/client/secure_credentials.cc", + "src/cpp/common/secure_auth_context.cc", + "src/cpp/common/secure_create_auth_context.cc", "src/cpp/server/secure_server_credentials.cc", "src/cpp/client/channel.cc", "src/cpp/client/channel_arguments.cc", @@ -598,6 +602,7 @@ cc_library( hdrs = [ "include/grpc++/async_generic_service.h", "include/grpc++/async_unary_call.h", + "include/grpc++/auth_context.h", "include/grpc++/byte_buffer.h", "include/grpc++/channel_arguments.h", "include/grpc++/channel_interface.h", @@ -646,9 +651,12 @@ cc_library( cc_library( name = "grpc++_unsecure", srcs = [ + "src/cpp/common/insecure_auth_context.h", "src/cpp/client/channel.h", + "src/cpp/common/create_auth_context.h", "src/cpp/proto/proto_utils.h", "src/cpp/server/thread_pool.h", + "src/cpp/common/insecure_create_auth_context.cc", "src/cpp/client/channel.cc", "src/cpp/client/channel_arguments.cc", "src/cpp/client/client_context.cc", @@ -678,6 +686,7 @@ cc_library( hdrs = [ "include/grpc++/async_generic_service.h", "include/grpc++/async_unary_call.h", + "include/grpc++/auth_context.h", "include/grpc++/byte_buffer.h", "include/grpc++/channel_arguments.h", "include/grpc++/channel_interface.h", diff --git a/Makefile b/Makefile index 5529af7868..2be4c38d79 100644 --- a/Makefile +++ b/Makefile @@ -3378,6 +3378,8 @@ endif LIBGRPC++_SRC = \ src/cpp/client/secure_credentials.cc \ + src/cpp/common/secure_auth_context.cc \ + src/cpp/common/secure_create_auth_context.cc \ src/cpp/server/secure_server_credentials.cc \ src/cpp/client/channel.cc \ src/cpp/client/channel_arguments.cc \ @@ -3408,6 +3410,7 @@ LIBGRPC++_SRC = \ PUBLIC_HEADERS_CXX += \ include/grpc++/async_generic_service.h \ include/grpc++/async_unary_call.h \ + include/grpc++/auth_context.h \ include/grpc++/byte_buffer.h \ include/grpc++/channel_arguments.h \ include/grpc++/channel_interface.h \ @@ -3667,6 +3670,7 @@ $(OBJDIR)/$(CONFIG)/test/cpp/util/subprocess.o: $(GENDIR)/test/cpp/util/messages LIBGRPC++_UNSECURE_SRC = \ + src/cpp/common/insecure_create_auth_context.cc \ src/cpp/client/channel.cc \ src/cpp/client/channel_arguments.cc \ src/cpp/client/client_context.cc \ @@ -3696,6 +3700,7 @@ LIBGRPC++_UNSECURE_SRC = \ PUBLIC_HEADERS_CXX += \ include/grpc++/async_generic_service.h \ include/grpc++/async_unary_call.h \ + include/grpc++/auth_context.h \ include/grpc++/byte_buffer.h \ include/grpc++/channel_arguments.h \ include/grpc++/channel_interface.h \ diff --git a/build.json b/build.json index e8c23c6aeb..89607e1a6e 100644 --- a/build.json +++ b/build.json @@ -30,6 +30,7 @@ "public_headers": [ "include/grpc++/async_generic_service.h", "include/grpc++/async_unary_call.h", + "include/grpc++/auth_context.h", "include/grpc++/byte_buffer.h", "include/grpc++/channel_arguments.h", "include/grpc++/channel_interface.h", @@ -65,6 +66,7 @@ ], "headers": [ "src/cpp/client/channel.h", + "src/cpp/common/create_auth_context.h", "src/cpp/proto/proto_utils.h", "src/cpp/server/thread_pool.h" ], @@ -514,10 +516,13 @@ "language": "c++", "headers": [ "src/cpp/client/secure_credentials.h", + "src/cpp/common/secure_auth_context.h", "src/cpp/server/secure_server_credentials.h" ], "src": [ "src/cpp/client/secure_credentials.cc", + "src/cpp/common/secure_auth_context.cc", + "src/cpp/common/secure_create_auth_context.cc", "src/cpp/server/secure_server_credentials.cc" ], "deps": [ @@ -567,6 +572,12 @@ "name": "grpc++_unsecure", "build": "all", "language": "c++", + "headers": [ + "src/cpp/common/insecure_auth_context.h" + ], + "src": [ + "src/cpp/common/insecure_create_auth_context.cc" + ], "deps": [ "gpr", "grpc_unsecure" diff --git a/include/grpc++/auth_context.h b/include/grpc++/auth_context.h index b0aebac210..158f8e3f07 100644 --- a/include/grpc++/auth_context.h +++ b/include/grpc++/auth_context.h @@ -34,32 +34,26 @@ #ifndef GRPCXX_AUTH_CONTEXT_H #define GRPCXX_AUTH_CONTEXT_H -#include +#include -#include - -#include +#include namespace grpc { -class AuthContext GRPC_FINAL : { +class AuthContext { public: typedef std::pair Property; + virtual ~AuthContext() {} + // A peer identity, in general is one or more properties (in which case they // have the same name). - std::vector GetPeerIdentity() const; - grpc::string GetPeerIdentityPropertyName() const; + virtual std::vector GetPeerIdentity() const = 0; + virtual grpc::string GetPeerIdentityPropertyName() const = 0; // Returns all the property values with the given name. - std::vector FindPropertyValues(const grpc::string& name) const; - - // Iteration over all the properties. - std::const_iterator begin() const; - std::const_iterator end() const; - - private: - grpc_auth_context *ctx_; + virtual std::vector FindPropertyValues( + const grpc::string& name) const = 0; }; } // namespace grpc diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index ecf4cc7f7b..66d3c249a1 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -108,6 +109,8 @@ class ClientContext { creds_ = creds; } + std::unique_ptr auth_context() const; + void TryCancel(); private: diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index d88a3ae262..5a6af299e3 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -35,8 +35,10 @@ #define GRPCXX_SERVER_CONTEXT_H #include +#include #include +#include #include #include @@ -89,6 +91,8 @@ class ServerContext { return client_metadata_; } + std::unique_ptr auth_context() const; + private: friend class ::grpc::Server; template diff --git a/src/core/security/security_context.h b/src/core/security/security_context.h index d8909cd6f1..e6ca919130 100644 --- a/src/core/security/security_context.h +++ b/src/core/security/security_context.h @@ -36,6 +36,10 @@ #include "src/core/security/credentials.h" +#ifdef __cplusplus +extern "C" { +#endif + /* --- grpc_auth_context --- High level authentication context object. Can optionally be chained. */ @@ -90,5 +94,9 @@ typedef struct { grpc_server_security_context *grpc_server_security_context_create(void); void grpc_server_security_context_destroy(void *ctx); +#ifdef __cplusplus +} +#endif + #endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONTEXT_H */ diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index 72cdd49d19..ac154d557a 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -36,6 +36,7 @@ #include #include #include +#include "src/cpp/common/create_auth_context.h" namespace grpc { @@ -75,6 +76,10 @@ void ClientContext::set_call(grpc_call* call, } } +std::unique_ptr ClientContext::auth_context() const { + return CreateAuthContext(call_); +} + void ClientContext::TryCancel() { if (call_) { grpc_call_cancel(call_); diff --git a/src/cpp/common/create_auth_context.h b/src/cpp/common/create_auth_context.h new file mode 100644 index 0000000000..24c00c43a8 --- /dev/null +++ b/src/cpp/common/create_auth_context.h @@ -0,0 +1,42 @@ +/* + * + * 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. + * + */ +#include + +#include +#include + +namespace grpc { + +std::unique_ptr CreateAuthContext(grpc_call* call); + +} // namespace grpc diff --git a/src/cpp/common/insecure_auth_context.h b/src/cpp/common/insecure_auth_context.h new file mode 100644 index 0000000000..8d1653a477 --- /dev/null +++ b/src/cpp/common/insecure_auth_context.h @@ -0,0 +1,59 @@ +/* + * + * 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. + * + */ + +#ifndef GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H +#define GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H + +#include + +namespace grpc { + +class InsecureAuthContext : public AuthContext { + public: + ~InsecureAuthContext() GRPC_OVERRIDE {} + + std::vector GetPeerIdentity() const GRPC_OVERRIDE { + return std::vector(); + } + + grpc::string GetPeerIdentityPropertyName() const GRPC_OVERRIDE { return ""; } + + std::vector FindPropertyValues(const grpc::string& name) const + GRPC_OVERRIDE { + return std::vector(); + } +}; + +} // namespace grpc + +#endif // GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H diff --git a/src/cpp/common/insecure_create_auth_context.cc b/src/cpp/common/insecure_create_auth_context.cc new file mode 100644 index 0000000000..79d868254d --- /dev/null +++ b/src/cpp/common/insecure_create_auth_context.cc @@ -0,0 +1,46 @@ +/* + * + * 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. + * + */ +#include + +#include +#include +#include "src/cpp/common/insecure_auth_context.h" + +namespace grpc { + +std::unique_ptr CreateAuthContext(grpc_call* call) { + (void)call; + return std::unique_ptr(new InsecureAuthContext); +} + +} // namespace grpc diff --git a/src/cpp/common/secure_auth_context.cc b/src/cpp/common/secure_auth_context.cc new file mode 100644 index 0000000000..4e616b913e --- /dev/null +++ b/src/cpp/common/secure_auth_context.cc @@ -0,0 +1,81 @@ +/* + * + * 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. + * + */ + +#include "src/cpp/common/secure_auth_context.h" + +#include "src/core/security/security_context.h" + +namespace grpc { + +SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx) + : ctx_(grpc_auth_context_ref(ctx)) {} + +SecureAuthContext::~SecureAuthContext() { grpc_auth_context_unref(ctx_); } + +std::vector SecureAuthContext::GetPeerIdentity() const { + if (!ctx_) { + return std::vector(); + } + grpc_auth_property_iterator iter = grpc_auth_context_peer_identity(ctx_); + std::vector identity; + const grpc_auth_property* property = nullptr; + while ((property = grpc_auth_property_iterator_next(&iter))) { + identity.push_back(grpc::string(property->value, property->value_length)); + } + return identity; +} + +grpc::string SecureAuthContext::GetPeerIdentityPropertyName() const { + if (!ctx_) { + return ""; + } + const char* name = grpc_auth_context_peer_identity_property_name(ctx_); + return name == nullptr ? "" : name; +} + +std::vector SecureAuthContext::FindPropertyValues( + const grpc::string& name) const { + if (!ctx_) { + return std::vector(); + } + grpc_auth_property_iterator iter = + grpc_auth_context_find_properties_by_name(ctx_, name.c_str()); + const grpc_auth_property* property = nullptr; + std::vector values; + while ((property = grpc_auth_property_iterator_next(&iter))) { + values.push_back(grpc::string(property->value, property->value_length)); + } + return values; +} + +} // namespace grpc diff --git a/src/cpp/common/secure_auth_context.h b/src/cpp/common/secure_auth_context.h new file mode 100644 index 0000000000..892f8d522c --- /dev/null +++ b/src/cpp/common/secure_auth_context.h @@ -0,0 +1,61 @@ +/* + * + * 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. + * + */ + +#ifndef GRPC_INTERNAL_CPP_COMMON_SECURE_AUTH_CONTEXT_H +#define GRPC_INTERNAL_CPP_COMMON_SECURE_AUTH_CONTEXT_H + +#include +#include "src/core/security/security_context.h" + +namespace grpc { + +class SecureAuthContext : public AuthContext { + public: + SecureAuthContext(grpc_auth_context* ctx); + + ~SecureAuthContext() GRPC_OVERRIDE; + + std::vector GetPeerIdentity() const GRPC_OVERRIDE; + + grpc::string GetPeerIdentityPropertyName() const GRPC_OVERRIDE; + + std::vector FindPropertyValues(const grpc::string& name) const + GRPC_OVERRIDE; + + private: + grpc_auth_context* ctx_; +}; + +} // namespace grpc + +#endif // GRPC_INTERNAL_CPP_COMMON_SECURE_AUTH_CONTEXT_H diff --git a/src/cpp/common/secure_create_auth_context.cc b/src/cpp/common/secure_create_auth_context.cc new file mode 100644 index 0000000000..d9fba4f4d4 --- /dev/null +++ b/src/cpp/common/secure_create_auth_context.cc @@ -0,0 +1,50 @@ +/* + * + * 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. + * + */ +#include + +#include +#include +#include +#include "src/cpp/common/secure_auth_context.h" + +namespace grpc { + +std::unique_ptr CreateAuthContext(grpc_call* call) { + grpc_auth_context* context = nullptr; + if (call) { + context = const_cast(grpc_call_auth_context(call)); + } + return std::unique_ptr(new SecureAuthContext(context)); +} + +} // namespace grpc diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 6b5e41d0a8..d5a582ccd9 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -39,6 +39,8 @@ #include #include +#include "src/cpp/common/create_auth_context.h" + namespace grpc { // CompletionOp @@ -140,4 +142,8 @@ bool ServerContext::IsCancelled() { return completion_op_ && completion_op_->CheckCancelled(cq_); } +std::unique_ptr ServerContext::auth_context() const { + return CreateAuthContext(call_); +} + } // namespace grpc diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 5616f2c466..e3337235e5 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -760,7 +760,7 @@ WARN_LOGFILE = # spaces. # Note: If this tag is empty the current directory is searched. -INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h +INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/auth_context.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 6d323274c9..2e682edb67 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -760,7 +760,7 @@ WARN_LOGFILE = # spaces. # Note: If this tag is empty the current directory is searched. -INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h src/cpp/client/secure_credentials.h src/cpp/server/secure_server_credentials.h src/cpp/client/channel.h src/cpp/proto/proto_utils.h src/cpp/server/thread_pool.h src/cpp/client/secure_credentials.cc src/cpp/server/secure_server_credentials.cc src/cpp/client/channel.cc src/cpp/client/channel_arguments.cc src/cpp/client/client_context.cc src/cpp/client/client_unary_call.cc src/cpp/client/create_channel.cc src/cpp/client/credentials.cc src/cpp/client/generic_stub.cc src/cpp/client/insecure_credentials.cc src/cpp/client/internal_stub.cc src/cpp/common/call.cc src/cpp/common/completion_queue.cc src/cpp/common/rpc_method.cc src/cpp/proto/proto_utils.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc src/cpp/server/insecure_server_credentials.cc src/cpp/server/server.cc src/cpp/server/server_builder.cc src/cpp/server/server_context.cc src/cpp/server/server_credentials.cc src/cpp/server/thread_pool.cc src/cpp/util/byte_buffer.cc src/cpp/util/slice.cc src/cpp/util/status.cc src/cpp/util/time.cc +INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/auth_context.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h src/cpp/client/secure_credentials.h src/cpp/common/secure_auth_context.h src/cpp/server/secure_server_credentials.h src/cpp/client/channel.h src/cpp/common/create_auth_context.h src/cpp/proto/proto_utils.h src/cpp/server/thread_pool.h src/cpp/client/secure_credentials.cc src/cpp/common/secure_auth_context.cc src/cpp/common/secure_create_auth_context.cc src/cpp/server/secure_server_credentials.cc src/cpp/client/channel.cc src/cpp/client/channel_arguments.cc src/cpp/client/client_context.cc src/cpp/client/client_unary_call.cc src/cpp/client/create_channel.cc src/cpp/client/credentials.cc src/cpp/client/generic_stub.cc src/cpp/client/insecure_credentials.cc src/cpp/client/internal_stub.cc src/cpp/common/call.cc src/cpp/common/completion_queue.cc src/cpp/common/rpc_method.cc src/cpp/proto/proto_utils.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc src/cpp/server/insecure_server_credentials.cc src/cpp/server/server.cc src/cpp/server/server_builder.cc src/cpp/server/server_context.cc src/cpp/server/server_credentials.cc src/cpp/server/thread_pool.cc src/cpp/util/byte_buffer.cc src/cpp/util/slice.cc src/cpp/util/status.cc src/cpp/util/time.cc # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses diff --git a/vsprojects/grpc++/grpc++.vcxproj b/vsprojects/grpc++/grpc++.vcxproj index d233f9e3d3..7e24ab3171 100644 --- a/vsprojects/grpc++/grpc++.vcxproj +++ b/vsprojects/grpc++/grpc++.vcxproj @@ -148,6 +148,7 @@ + @@ -183,14 +184,20 @@ + + + + + + diff --git a/vsprojects/grpc++/grpc++.vcxproj.filters b/vsprojects/grpc++/grpc++.vcxproj.filters index dd375c7238..74a7177fa9 100644 --- a/vsprojects/grpc++/grpc++.vcxproj.filters +++ b/vsprojects/grpc++/grpc++.vcxproj.filters @@ -4,6 +4,12 @@ src\cpp\client + + src\cpp\common + + + src\cpp\common + src\cpp\server @@ -90,6 +96,9 @@ include\grpc++ + + include\grpc++ + include\grpc++ @@ -191,12 +200,18 @@ src\cpp\client + + src\cpp\common + src\cpp\server src\cpp\client + + src\cpp\common + src\cpp\proto diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj index 9b2ef9137d..52431b67a2 100644 --- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -148,6 +148,7 @@ + @@ -182,11 +183,15 @@ + + + + diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index d616e336e4..37fc3d0eec 100644 --- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -1,6 +1,9 @@ + + src\cpp\common + src\cpp\client @@ -84,6 +87,9 @@ include\grpc++ + + include\grpc++ + include\grpc++ @@ -182,9 +188,15 @@ + + src\cpp\common + src\cpp\client + + src\cpp\common + src\cpp\proto -- cgit v1.2.3 From 822d2c7bebf9810903bdc63e24dc26eb2640f756 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 7 Jul 2015 16:08:00 -0700 Subject: Support registering services against specific hosts --- include/grpc++/server.h | 4 ++-- include/grpc++/server_builder.h | 26 ++++++++++++++++++++++++-- src/cpp/client/channel.cc | 2 +- src/cpp/server/server.cc | 9 +++++---- src/cpp/server/server_builder.cc | 18 ++++++++++++++---- test/cpp/end2end/end2end_test.cc | 27 +++++++++++++++++++++++++-- test/cpp/util/messages.proto | 1 + 7 files changed, 72 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 6a9e757e77..94ee0b6a4a 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -84,8 +84,8 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { int max_message_size); // Register a service. This call does not take ownership of the service. // The service must exist for the lifetime of the Server instance. - bool RegisterService(RpcService* service); - bool RegisterAsyncService(AsynchronousService* service); + bool RegisterService(const grpc::string *host, RpcService* service); + bool RegisterAsyncService(const grpc::string *host, AsynchronousService* service); void RegisterAsyncGenericService(AsyncGenericService* service); // Add a listening port. Can be called multiple times. int AddListeningPort(const grpc::string& addr, ServerCredentials* creds); diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index ecee475e3e..2003d18ef5 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -69,6 +69,19 @@ class ServerBuilder { // Register a generic service. void RegisterAsyncGenericService(AsyncGenericService* service); + // Register a service. This call does not take ownership of the service. + // The service must exist for the lifetime of the Server instance returned by + // BuildAndStart(). + void RegisterService(const grpc::string& host, + SynchronousService* service); + + // Register an asynchronous service. New calls will be delevered to cq. + // This call does not take ownership of the service or completion queue. + // The service and completion queuemust exist for the lifetime of the Server + // instance returned by BuildAndStart(). + void RegisterAsyncService(const grpc::string& host, + AsynchronousService* service); + // Set max message size in bytes. void SetMaxMessageSize(int max_message_size) { max_message_size_ = max_message_size; @@ -98,9 +111,18 @@ class ServerBuilder { int* selected_port; }; + typedef std::unique_ptr HostString; + template struct NamedService { + explicit NamedService(T* s) : service(s) {} + explicit NamedService(const grpc::string& h, T *s) + : host(new grpc::string(h)), service(s) {} + HostString host; + T* service; + }; + int max_message_size_; - std::vector services_; - std::vector async_services_; + std::vector> services_; + std::vector> async_services_; std::vector ports_; std::vector cqs_; std::shared_ptr creds_; diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 72593f877e..406811d47f 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -59,7 +59,7 @@ Channel::~Channel() { grpc_channel_destroy(c_channel_); } Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) { auto c_call = - method.channel_tag() + method.channel_tag() && context->authority().empty() ? grpc_channel_create_registered_call(c_channel_, cq->cq(), method.channel_tag(), context->raw_deadline()) diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 1437b2dea7..f9d20ff579 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -207,10 +207,11 @@ Server::~Server() { delete sync_methods_; } -bool Server::RegisterService(RpcService* service) { +bool Server::RegisterService(const grpc::string *host, RpcService* service) { for (int i = 0; i < service->GetMethodCount(); ++i) { RpcServiceMethod* method = service->GetMethod(i); - void* tag = grpc_server_register_method(server_, method->name(), nullptr); + void* tag = grpc_server_register_method( + server_, method->name(), host ? host->c_str() : nullptr); if (!tag) { gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", method->name()); @@ -222,14 +223,14 @@ bool Server::RegisterService(RpcService* service) { return true; } -bool Server::RegisterAsyncService(AsynchronousService* service) { +bool Server::RegisterAsyncService(const grpc::string *host, AsynchronousService* service) { GPR_ASSERT(service->server_ == nullptr && "Can only register an asynchronous service against one server."); service->server_ = this; service->request_args_ = new void*[service->method_count_]; for (size_t i = 0; i < service->method_count_; ++i) { void* tag = grpc_server_register_method(server_, service->method_names_[i], - nullptr); + host ? host->c_str() : nullptr); if (!tag) { gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", service->method_names_[i]); diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 3ee1d54e76..fa158c919e 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -51,11 +51,21 @@ std::unique_ptr ServerBuilder::AddCompletionQueue() { } void ServerBuilder::RegisterService(SynchronousService* service) { - services_.push_back(service->service()); + services_.emplace_back(service->service()); } void ServerBuilder::RegisterAsyncService(AsynchronousService* service) { - async_services_.push_back(service); + async_services_.emplace_back(service); +} + +void ServerBuilder::RegisterService( + const grpc::string& addr, SynchronousService* service) { + services_.emplace_back(addr, service->service()); +} + +void ServerBuilder::RegisterAsyncService( + const grpc::string& addr, AsynchronousService* service) { + async_services_.emplace_back(addr, service); } void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { @@ -97,13 +107,13 @@ std::unique_ptr ServerBuilder::BuildAndStart() { } for (auto service = services_.begin(); service != services_.end(); service++) { - if (!server->RegisterService(*service)) { + if (!server->RegisterService(service->host.get(), service->service)) { return nullptr; } } for (auto service = async_services_.begin(); service != async_services_.end(); service++) { - if (!server->RegisterAsyncService(*service)) { + if (!server->RegisterAsyncService(service->host.get(), service->service)) { return nullptr; } } diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 5e850ea30a..ce12cc8488 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -87,12 +87,16 @@ void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { public: - TestServiceImpl() : signal_client_(false) {} + TestServiceImpl() : signal_client_(false), host_(nullptr) {} + explicit TestServiceImpl(const grpc::string& host) : signal_client_(false), host_(new grpc::string(host)) {} Status Echo(ServerContext* context, const EchoRequest* request, EchoResponse* response) GRPC_OVERRIDE { response->set_message(request->message()); MaybeEchoDeadline(context, request, response); + if (host_) { + response->mutable_param()->set_host(*host_); + } if (request->has_param() && request->param().client_cancel_after_us()) { { std::unique_lock lock(mu_); @@ -191,6 +195,7 @@ class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { private: bool signal_client_; std::mutex mu_; + std::unique_ptr host_; }; class TestServiceImplDupPkg @@ -205,7 +210,7 @@ class TestServiceImplDupPkg class End2endTest : public ::testing::Test { protected: - End2endTest() : kMaxMessageSize_(8192), thread_pool_(2) {} + End2endTest() : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {} void SetUp() GRPC_OVERRIDE { int port = grpc_pick_unused_port_or_die(); @@ -215,6 +220,7 @@ class End2endTest : public ::testing::Test { builder.AddListeningPort(server_address_.str(), FakeTransportSecurityServerCredentials()); builder.RegisterService(&service_); + builder.RegisterService("special", &special_service_); builder.SetMaxMessageSize( kMaxMessageSize_); // For testing max message size. builder.RegisterService(&dup_pkg_service_); @@ -236,6 +242,7 @@ class End2endTest : public ::testing::Test { std::ostringstream server_address_; const int kMaxMessageSize_; TestServiceImpl service_; + TestServiceImpl special_service_; TestServiceImplDupPkg dup_pkg_service_; ThreadPool thread_pool_; }; @@ -254,6 +261,22 @@ static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, } } +TEST_F(End2endTest, SimpleRpcWithHost) { + ResetStub(); + + EchoRequest request; + EchoResponse response; + request.set_message("Hello"); + + ClientContext context; + context.set_authority("special"); + Status s = stub_->Echo(&context, request, &response); + EXPECT_EQ(response.message(), request.message()); + EXPECT_TRUE(response.has_param()); + EXPECT_EQ(response.param().host(), "special"); + EXPECT_TRUE(s.ok()); +} + TEST_F(End2endTest, SimpleRpc) { ResetStub(); SendRpc(stub_.get(), 1); diff --git a/test/cpp/util/messages.proto b/test/cpp/util/messages.proto index 062f66c091..dc8572cc9c 100644 --- a/test/cpp/util/messages.proto +++ b/test/cpp/util/messages.proto @@ -46,6 +46,7 @@ message EchoRequest { message ResponseParams { optional int64 request_deadline = 1; + optional string host = 2; } message EchoResponse { -- cgit v1.2.3 From d9b6fcfee4cb986f148762a4a7d0794de9b3ba62 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 7 Jul 2015 16:28:20 -0700 Subject: Support older compilers --- include/grpc++/server_builder.h | 4 ++-- src/cpp/server/server_builder.cc | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index 2003d18ef5..d6bb3bd090 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -121,8 +121,8 @@ class ServerBuilder { }; int max_message_size_; - std::vector> services_; - std::vector> async_services_; + std::vector>> services_; + std::vector>> async_services_; std::vector ports_; std::vector cqs_; std::shared_ptr creds_; diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index fa158c919e..86c78f05ff 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -51,21 +51,21 @@ std::unique_ptr ServerBuilder::AddCompletionQueue() { } void ServerBuilder::RegisterService(SynchronousService* service) { - services_.emplace_back(service->service()); + services_.emplace_back(new NamedService(service->service())); } void ServerBuilder::RegisterAsyncService(AsynchronousService* service) { - async_services_.emplace_back(service); + async_services_.emplace_back(new NamedService(service)); } void ServerBuilder::RegisterService( const grpc::string& addr, SynchronousService* service) { - services_.emplace_back(addr, service->service()); + services_.emplace_back(new NamedService(addr, service->service())); } void ServerBuilder::RegisterAsyncService( const grpc::string& addr, AsynchronousService* service) { - async_services_.emplace_back(addr, service); + async_services_.emplace_back(new NamedService(addr, service)); } void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) { @@ -107,13 +107,13 @@ std::unique_ptr ServerBuilder::BuildAndStart() { } for (auto service = services_.begin(); service != services_.end(); service++) { - if (!server->RegisterService(service->host.get(), service->service)) { + if (!server->RegisterService((*service)->host.get(), (*service)->service)) { return nullptr; } } for (auto service = async_services_.begin(); service != async_services_.end(); service++) { - if (!server->RegisterAsyncService(service->host.get(), service->service)) { + if (!server->RegisterAsyncService((*service)->host.get(), (*service)->service)) { return nullptr; } } -- cgit v1.2.3 From 85c04f938f242031cf819f626da21ac597ccd6a6 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 7 Jul 2015 17:47:31 -0700 Subject: resolve comments --- BUILD | 1 - build.json | 3 -- include/grpc++/client_context.h | 3 +- include/grpc++/server_context.h | 7 ++- src/cpp/client/client_context.cc | 7 ++- src/cpp/common/insecure_auth_context.h | 59 ---------------------- src/cpp/common/insecure_create_auth_context.cc | 5 +- src/cpp/common/secure_auth_context.h | 2 +- src/cpp/common/secure_create_auth_context.cc | 11 ++-- src/cpp/server/server.cc | 4 +- src/cpp/server/server_context.cc | 5 +- test/cpp/end2end/end2end_test.cc | 2 +- tools/run_tests/sources_and_headers.json | 2 - vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj | 1 - .../grpc++_unsecure.vcxproj.filters | 3 -- 15 files changed, 28 insertions(+), 87 deletions(-) delete mode 100644 src/cpp/common/insecure_auth_context.h (limited to 'include') diff --git a/BUILD b/BUILD index b1b2ce3134..884b168660 100644 --- a/BUILD +++ b/BUILD @@ -717,7 +717,6 @@ cc_library( cc_library( name = "grpc++_unsecure", srcs = [ - "src/cpp/common/insecure_auth_context.h", "src/cpp/client/channel.h", "src/cpp/common/create_auth_context.h", "src/cpp/server/thread_pool.h", diff --git a/build.json b/build.json index 4daf6128e5..e3268620d6 100644 --- a/build.json +++ b/build.json @@ -619,9 +619,6 @@ "name": "grpc++_unsecure", "build": "all", "language": "c++", - "headers": [ - "src/cpp/common/insecure_auth_context.h" - ], "src": [ "src/cpp/common/insecure_create_auth_context.cc" ], diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 3bf5edc6c0..26bd7c830f 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -108,7 +108,7 @@ class ClientContext { creds_ = creds; } - std::unique_ptr auth_context() const; + std::shared_ptr auth_context() const; void TryCancel(); @@ -157,6 +157,7 @@ class ClientContext { gpr_timespec deadline_; grpc::string authority_; std::shared_ptr creds_; + mutable std::shared_ptr auth_context_; std::multimap send_initial_metadata_; std::multimap recv_initial_metadata_; std::multimap trailing_metadata_; diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index 3bf21e02bf..a4ee986df1 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -99,7 +99,9 @@ class ServerContext { return client_metadata_; } - std::unique_ptr auth_context() const; + std::shared_ptr auth_context() const { + return auth_context_; + } private: friend class ::grpc::Server; @@ -137,12 +139,15 @@ class ServerContext { ServerContext(gpr_timespec deadline, grpc_metadata* metadata, size_t metadata_count); + void set_call(grpc_call* call); + CompletionOp* completion_op_; gpr_timespec deadline_; grpc_call* call_; CompletionQueue* cq_; bool sent_initial_metadata_; + std::shared_ptr auth_context_; std::multimap client_metadata_; std::multimap initial_metadata_; std::multimap trailing_metadata_; diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index ac154d557a..c68f6dd9f8 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -76,8 +76,11 @@ void ClientContext::set_call(grpc_call* call, } } -std::unique_ptr ClientContext::auth_context() const { - return CreateAuthContext(call_); +std::shared_ptr ClientContext::auth_context() const { + if (auth_context_.get() == nullptr) { + auth_context_ = CreateAuthContext(call_); + } + return auth_context_; } void ClientContext::TryCancel() { diff --git a/src/cpp/common/insecure_auth_context.h b/src/cpp/common/insecure_auth_context.h deleted file mode 100644 index 8d1653a477..0000000000 --- a/src/cpp/common/insecure_auth_context.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * - * 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. - * - */ - -#ifndef GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H -#define GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H - -#include - -namespace grpc { - -class InsecureAuthContext : public AuthContext { - public: - ~InsecureAuthContext() GRPC_OVERRIDE {} - - std::vector GetPeerIdentity() const GRPC_OVERRIDE { - return std::vector(); - } - - grpc::string GetPeerIdentityPropertyName() const GRPC_OVERRIDE { return ""; } - - std::vector FindPropertyValues(const grpc::string& name) const - GRPC_OVERRIDE { - return std::vector(); - } -}; - -} // namespace grpc - -#endif // GRPC_INTERNAL_CPP_COMMON_INSECURE_AUTH_CONTEXT_H diff --git a/src/cpp/common/insecure_create_auth_context.cc b/src/cpp/common/insecure_create_auth_context.cc index 79d868254d..07fc0bd549 100644 --- a/src/cpp/common/insecure_create_auth_context.cc +++ b/src/cpp/common/insecure_create_auth_context.cc @@ -34,13 +34,12 @@ #include #include -#include "src/cpp/common/insecure_auth_context.h" namespace grpc { -std::unique_ptr CreateAuthContext(grpc_call* call) { +std::shared_ptr CreateAuthContext(grpc_call* call) { (void)call; - return std::unique_ptr(new InsecureAuthContext); + return std::shared_ptr(); } } // namespace grpc diff --git a/src/cpp/common/secure_auth_context.h b/src/cpp/common/secure_auth_context.h index 892f8d522c..b406f675ae 100644 --- a/src/cpp/common/secure_auth_context.h +++ b/src/cpp/common/secure_auth_context.h @@ -39,7 +39,7 @@ namespace grpc { -class SecureAuthContext : public AuthContext { +class SecureAuthContext GRPC_FINAL : public AuthContext { public: SecureAuthContext(grpc_auth_context* ctx); diff --git a/src/cpp/common/secure_create_auth_context.cc b/src/cpp/common/secure_create_auth_context.cc index d9fba4f4d4..21994945f8 100644 --- a/src/cpp/common/secure_create_auth_context.cc +++ b/src/cpp/common/secure_create_auth_context.cc @@ -39,12 +39,13 @@ namespace grpc { -std::unique_ptr CreateAuthContext(grpc_call* call) { - grpc_auth_context* context = nullptr; - if (call) { - context = const_cast(grpc_call_auth_context(call)); +std::shared_ptr CreateAuthContext(grpc_call* call) { + if (call == nullptr) { + return std::shared_ptr(); } - return std::unique_ptr(new SecureAuthContext(context)); + grpc_auth_context* context = + const_cast(grpc_call_auth_context(call)); + return std::shared_ptr(new SecureAuthContext(context)); } } // namespace grpc diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 1437b2dea7..033c18490b 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -118,7 +118,7 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { has_request_payload_(mrd->has_request_payload_), request_payload_(mrd->request_payload_), method_(mrd->method_) { - ctx_.call_ = mrd->call_; + ctx_.set_call(mrd->call_); ctx_.cq_ = &cq_; GPR_ASSERT(mrd->in_flight_); mrd->in_flight_ = false; @@ -325,7 +325,7 @@ bool Server::BaseAsyncRequest::FinalizeResult(void** tag, bool* status) { } } grpc_metadata_array_destroy(&initial_metadata_array_); - context_->call_ = call_; + context_->set_call(call_); context_->cq_ = call_cq_; Call call(call_, server_, call_cq_, server_->max_message_size_); if (*status && call_) { diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 357d1a5a02..1bb3a8bcc4 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -148,8 +148,9 @@ bool ServerContext::IsCancelled() { return completion_op_ && completion_op_->CheckCancelled(cq_); } -std::unique_ptr ServerContext::auth_context() const { - return CreateAuthContext(call_); +void ServerContext::set_call(grpc_call* call) { + call_ = call; + auth_context_ = CreateAuthContext(call); } } // namespace grpc diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index d626fc0c94..60af84100a 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -85,7 +85,7 @@ void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, template void CheckAuthContext(T* context) { - std::unique_ptr auth_ctx = context->auth_context(); + std::shared_ptr auth_ctx = context->auth_context(); std::vector fake = auth_ctx->FindPropertyValues("transport_security_type"); EXPECT_EQ(1, fake.size()); diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 795f1f46a8..344933a67c 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -9666,7 +9666,6 @@ "include/grpc++/time.h", "src/cpp/client/channel.h", "src/cpp/common/create_auth_context.h", - "src/cpp/common/insecure_auth_context.h", "src/cpp/server/thread_pool.h" ], "language": "c++", @@ -9722,7 +9721,6 @@ "src/cpp/common/call.cc", "src/cpp/common/completion_queue.cc", "src/cpp/common/create_auth_context.h", - "src/cpp/common/insecure_auth_context.h", "src/cpp/common/insecure_create_auth_context.cc", "src/cpp/common/rpc_method.cc", "src/cpp/proto/proto_utils.cc", diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj index 984fb6884e..944e7e0001 100644 --- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -186,7 +186,6 @@ - diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index ae348d3fb5..73b0a5dccd 100644 --- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -194,9 +194,6 @@ - - src\cpp\common - src\cpp\client -- cgit v1.2.3 From 2ee8f0b978901b6b2bee5a2afd1650847f324edf Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 8 Jul 2015 07:53:20 -0700 Subject: Update comments --- include/grpc++/server_builder.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index d6bb3bd090..c397fddf5a 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -58,27 +58,32 @@ class ServerBuilder { // Register a service. This call does not take ownership of the service. // The service must exist for the lifetime of the Server instance returned by // BuildAndStart(). + // Matches requests with any :authority void RegisterService(SynchronousService* service); - // Register an asynchronous service. New calls will be delevered to cq. + // Register an asynchronous service. // This call does not take ownership of the service or completion queue. // The service and completion queuemust exist for the lifetime of the Server // instance returned by BuildAndStart(). + // Matches requests with any :authority void RegisterAsyncService(AsynchronousService* service); // Register a generic service. + // Matches requests with any :authority void RegisterAsyncGenericService(AsyncGenericService* service); // Register a service. This call does not take ownership of the service. // The service must exist for the lifetime of the Server instance returned by // BuildAndStart(). + // Only matches requests with :authority \a host void RegisterService(const grpc::string& host, SynchronousService* service); - // Register an asynchronous service. New calls will be delevered to cq. + // Register an asynchronous service. // This call does not take ownership of the service or completion queue. // The service and completion queuemust exist for the lifetime of the Server // instance returned by BuildAndStart(). + // Only matches requests with :authority \a host void RegisterAsyncService(const grpc::string& host, AsynchronousService* service); -- cgit v1.2.3 From b09caa967d82c34d90ecb1109c816f7399b34df3 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 8 Jul 2015 07:53:47 -0700 Subject: Remove extraneous explicit --- include/grpc++/server_builder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index c397fddf5a..44ee00eec9 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -119,7 +119,7 @@ class ServerBuilder { typedef std::unique_ptr HostString; template struct NamedService { explicit NamedService(T* s) : service(s) {} - explicit NamedService(const grpc::string& h, T *s) + NamedService(const grpc::string& h, T *s) : host(new grpc::string(h)), service(s) {} HostString host; T* service; -- cgit v1.2.3 From f9e8e59b1c113b614736b89cb2cb4e543ba82d9f Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 9 Jul 2015 12:32:15 -0700 Subject: Resolve comments --- include/grpc/grpc_security.h | 8 ++++++-- src/core/security/client_auth_filter.c | 12 +++--------- src/core/security/security_context.c | 14 +++++++++++--- src/cpp/common/secure_auth_context.cc | 9 +++------ test/cpp/common/secure_auth_context_test.cc | 2 -- 5 files changed, 23 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/include/grpc/grpc_security.h b/include/grpc/grpc_security.h index 7a6aa66670..0154d8ac2a 100644 --- a/include/grpc/grpc_security.h +++ b/include/grpc/grpc_security.h @@ -243,8 +243,12 @@ const char *grpc_auth_context_peer_identity_property_name( /* Returns 1 if the peer is authenticated, 0 otherwise. */ int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx); -/* Gets the auth context from the call. */ -const grpc_auth_context *grpc_call_auth_context(grpc_call *call); +/* Gets the auth context from the call. Caller needs to call + grpc_auth_context_release on the returned context. */ +grpc_auth_context *grpc_call_auth_context(grpc_call *call); + +/* Releases the auth context returned from grpc_call_auth_context. */ +void grpc_auth_context_release(grpc_auth_context *context); #ifdef __cplusplus } diff --git a/src/core/security/client_auth_filter.c b/src/core/security/client_auth_filter.c index 16611ffae2..632a742250 100644 --- a/src/core/security/client_auth_filter.c +++ b/src/core/security/client_auth_filter.c @@ -212,15 +212,9 @@ static void auth_start_transport_op(grpc_call_element *elem, grpc_client_security_context_destroy; } sec_ctx = op->context[GRPC_CONTEXT_SECURITY].value; - if (sec_ctx->auth_context == NULL) { - sec_ctx->auth_context = - GRPC_AUTH_CONTEXT_REF(chand->security_connector->base.auth_context, - "client_auth_filter"); - } else { - sec_ctx->auth_context->chained = - GRPC_AUTH_CONTEXT_REF(chand->security_connector->base.auth_context, - "client_auth_filter chained"); - } + GRPC_AUTH_CONTEXT_UNREF(sec_ctx->auth_context, "client auth filter"); + sec_ctx->auth_context = GRPC_AUTH_CONTEXT_REF( + chand->security_connector->base.auth_context, "client_auth_filter"); } if (op->bind_pollset) { diff --git a/src/core/security/security_context.c b/src/core/security/security_context.c index 4d56549f9b..8ce7876bd8 100644 --- a/src/core/security/security_context.c +++ b/src/core/security/security_context.c @@ -69,12 +69,20 @@ grpc_call_error grpc_call_set_credentials(grpc_call *call, return GRPC_CALL_OK; } -const grpc_auth_context *grpc_call_auth_context(grpc_call *call) { +grpc_auth_context *grpc_call_auth_context(grpc_call *call) { void *sec_ctx = grpc_call_context_get(call, GRPC_CONTEXT_SECURITY); if (sec_ctx == NULL) return NULL; return grpc_call_is_client(call) - ? ((grpc_client_security_context *)sec_ctx)->auth_context - : ((grpc_server_security_context *)sec_ctx)->auth_context; + ? GRPC_AUTH_CONTEXT_REF( + ((grpc_client_security_context *)sec_ctx)->auth_context, + "grpc_call_auth_context client") + : GRPC_AUTH_CONTEXT_REF( + ((grpc_server_security_context *)sec_ctx)->auth_context, + "grpc_call_auth_context server"); +} + +void grpc_auth_context_release(grpc_auth_context *context) { + GRPC_AUTH_CONTEXT_UNREF(context, "grpc_auth_context_unref"); } /* --- grpc_client_security_context --- */ diff --git a/src/cpp/common/secure_auth_context.cc b/src/cpp/common/secure_auth_context.cc index d3606af0f6..4513723653 100644 --- a/src/cpp/common/secure_auth_context.cc +++ b/src/cpp/common/secure_auth_context.cc @@ -33,16 +33,13 @@ #include "src/cpp/common/secure_auth_context.h" -#include "src/core/security/security_context.h" +#include namespace grpc { -SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx) - : ctx_(GRPC_AUTH_CONTEXT_REF(ctx, "SecureAuthContext")) {} +SecureAuthContext::SecureAuthContext(grpc_auth_context* ctx) : ctx_(ctx) {} -SecureAuthContext::~SecureAuthContext() { - GRPC_AUTH_CONTEXT_UNREF(ctx_, "SecureAuthContext"); -} +SecureAuthContext::~SecureAuthContext() { grpc_auth_context_release(ctx_); } std::vector SecureAuthContext::GetPeerIdentity() const { if (!ctx_) { diff --git a/test/cpp/common/secure_auth_context_test.cc b/test/cpp/common/secure_auth_context_test.cc index e65a257803..6f8fb8f2cb 100644 --- a/test/cpp/common/secure_auth_context_test.cc +++ b/test/cpp/common/secure_auth_context_test.cc @@ -66,8 +66,6 @@ TEST_F(SecureAuthContextTest, Properties) { std::vector bar = context.FindPropertyValues("foo"); EXPECT_EQ(1, bar.size()); EXPECT_EQ("bar", bar[0]); - - GRPC_AUTH_CONTEXT_UNREF(ctx, "SecureAuthContextTest"); } } // namespace -- cgit v1.2.3