From 9aadeb815b3839a323cab3096e1d16ce7ad27e6f Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 15 Apr 2015 11:01:08 -0700 Subject: [PATCH 01/26] Get GRPC_TRACE tcp working again --- src/core/surface/init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/surface/init.c b/src/core/surface/init.c index 4de51a666f1..5a119a47cc7 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -61,8 +61,8 @@ void grpc_init(void) { grpc_register_tracer("http", &grpc_http_trace); grpc_register_tracer("batch", &grpc_trace_batch); grpc_security_pre_init(); - grpc_tracer_init("GRPC_TRACE"); grpc_iomgr_init(); + grpc_tracer_init("GRPC_TRACE"); census_init(); grpc_timers_log_global_init(); } From eaccf90aef0253d59130a8f581bd6a70dae0474f Mon Sep 17 00:00:00 2001 From: Yang Gao Date: Wed, 15 Apr 2015 14:05:07 -0700 Subject: [PATCH 02/26] Return 1 when test_case is not supported in interop test --- test/cpp/interop/client.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index 189cdeb0eed..960166555b6 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -86,6 +86,7 @@ int main(int argc, char** argv) { ParseCommandLineFlags(&argc, &argv, true); + int ret = 0; grpc::testing::InteropClient client( CreateChannelForTestCase(FLAGS_test_case)); if (FLAGS_test_case == "empty_unary") { @@ -132,9 +133,10 @@ int main(int argc, char** argv) { "large_unary|client_streaming|server_streaming|half_duplex|ping_pong|" "service_account_creds|compute_engine_creds|jwt_token_creds", FLAGS_test_case.c_str()); + ret = 1; } client.Reset(nullptr); grpc_shutdown(); - return 0; + return ret; } From 103837ee6bcfd05f0cd17b8f742d582a6082216d Mon Sep 17 00:00:00 2001 From: Yang Gao Date: Wed, 15 Apr 2015 15:23:54 -0700 Subject: [PATCH 03/26] Wrap ParseCommandLineFlags in InitTest --- Makefile | 90 +++++++++++++++++++++++++------ build.json | 32 ++++++++--- examples/pubsub/main.cc | 10 +--- test/cpp/interop/client.cc | 10 +--- test/cpp/interop/client_helper.cc | 7 --- test/cpp/interop/server.cc | 10 +--- test/cpp/interop/server_helper.cc | 7 --- test/cpp/qps/qps_driver.cc | 10 +--- test/cpp/qps/worker.cc | 10 +--- test/cpp/util/grpc_cli.cc | 10 +--- test/cpp/util/test_config.cc | 52 ++++++++++++++++++ test/cpp/util/test_config.h | 45 ++++++++++++++++ 12 files changed, 206 insertions(+), 87 deletions(-) create mode 100644 test/cpp/util/test_config.cc create mode 100644 test/cpp/util/test_config.h diff --git a/Makefile b/Makefile index 0511c77fec0..5a17c9765e5 100644 --- a/Makefile +++ b/Makefile @@ -1087,7 +1087,7 @@ privatelibs: privatelibs_c privatelibs_cxx privatelibs_c: $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_fake_security.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_fullstack.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_fullstack_uds.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_simple_ssl_fullstack.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_simple_ssl_with_oauth2_fullstack.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_socket_pair.a $(LIBDIR)/$(CONFIG)/libend2end_fixture_chttp2_socket_pair_one_byte_at_a_time.a $(LIBDIR)/$(CONFIG)/libend2end_test_bad_hostname.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_accept.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_accept_and_writes_closed.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_invoke.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_before_invoke.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_in_a_vacuum.a $(LIBDIR)/$(CONFIG)/libend2end_test_census_simple_request.a $(LIBDIR)/$(CONFIG)/libend2end_test_disappearing_server.a $(LIBDIR)/$(CONFIG)/libend2end_test_early_server_shutdown_finishes_inflight_calls.a $(LIBDIR)/$(CONFIG)/libend2end_test_early_server_shutdown_finishes_tags.a $(LIBDIR)/$(CONFIG)/libend2end_test_empty_batch.a $(LIBDIR)/$(CONFIG)/libend2end_test_graceful_server_shutdown.a $(LIBDIR)/$(CONFIG)/libend2end_test_invoke_large_request.a $(LIBDIR)/$(CONFIG)/libend2end_test_max_concurrent_streams.a $(LIBDIR)/$(CONFIG)/libend2end_test_no_op.a $(LIBDIR)/$(CONFIG)/libend2end_test_ping_pong_streaming.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_binary_metadata_and_payload.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_metadata_and_payload.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_payload.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_with_large_metadata.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_with_payload.a $(LIBDIR)/$(CONFIG)/libend2end_test_simple_delayed_request.a $(LIBDIR)/$(CONFIG)/libend2end_test_simple_request.a $(LIBDIR)/$(CONFIG)/libend2end_test_thread_stress.a $(LIBDIR)/$(CONFIG)/libend2end_test_writes_done_hangs_with_pending_read.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_accept_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_accept_and_writes_closed_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_after_invoke_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_before_invoke_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_cancel_in_a_vacuum_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_census_simple_request_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_disappearing_server_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_early_server_shutdown_finishes_inflight_calls_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_early_server_shutdown_finishes_tags_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_graceful_server_shutdown_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_invoke_large_request_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_max_concurrent_streams_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_no_op_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_ping_pong_streaming_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_binary_metadata_and_payload_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_metadata_and_payload_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_payload_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_response_with_trailing_metadata_and_payload_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_with_large_metadata_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_request_with_payload_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_simple_delayed_request_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_simple_request_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_thread_stress_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_test_writes_done_hangs_with_pending_read_legacy.a $(LIBDIR)/$(CONFIG)/libend2end_certs.a -privatelibs_cxx: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libqps.a +privatelibs_cxx: $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libqps.a buildtests: buildtests_c buildtests_cxx @@ -3517,6 +3517,62 @@ $(OBJDIR)/$(CONFIG)/src/cpp/util/status.o: $(OBJDIR)/$(CONFIG)/src/cpp/util/time.o: +LIBGRPC++_TEST_CONFIG_SRC = \ + test/cpp/util/test_config.cc \ + + +LIBGRPC++_TEST_CONFIG_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_TEST_CONFIG_SRC)))) + +ifeq ($(NO_SECURE),true) + +# You can't build secure libraries if you don't have OpenSSL with ALPN. + +$(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a: openssl_dep_error + + +else + +ifeq ($(NO_PROTOBUF),true) + +# You can't build a C++ library if you don't have protobuf - a bit overreached, but still okay. + +$(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a: protobuf_dep_error + + +else + +ifneq ($(OPENSSL_DEP),) +# This is to ensure the embedded OpenSSL is built beforehand, properly +# installing headers to their final destination on the drive. We need this +# otherwise parallel compilation will fail if a source is compiled first. +test/cpp/util/test_config.cc: $(OPENSSL_DEP) +endif + +$(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a: $(ZLIB_DEP) $(OPENSSL_DEP) $(PROTOBUF_DEP) $(LIBGRPC++_TEST_CONFIG_OBJS) + $(E) "[AR] Creating $@" + $(Q) mkdir -p `dirname $@` + $(Q) rm -f $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a + $(Q) $(AR) rcs $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LIBGRPC++_TEST_CONFIG_OBJS) +ifeq ($(SYSTEM),Darwin) + $(Q) ranlib $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a +endif + + + + +endif + +endif + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(LIBGRPC++_TEST_CONFIG_OBJS:.o=.dep) +endif +endif + +$(OBJDIR)/$(CONFIG)/test/cpp/util/test_config.o: + + LIBGRPC++_TEST_UTIL_SRC = \ $(GENDIR)/test/cpp/util/messages.pb.cc $(GENDIR)/test/cpp/util/messages.grpc.pb.cc \ $(GENDIR)/test/cpp/util/echo.pb.cc $(GENDIR)/test/cpp/util/echo.grpc.pb.cc \ @@ -8717,16 +8773,16 @@ $(BINDIR)/$(CONFIG)/grpc_cli: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/grpc_cli: $(PROTOBUF_DEP) $(GRPC_CLI_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/grpc_cli: $(PROTOBUF_DEP) $(GRPC_CLI_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(GRPC_CLI_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/grpc_cli + $(Q) $(LDXX) $(LDFLAGS) $(GRPC_CLI_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/grpc_cli endif endif -$(OBJDIR)/$(CONFIG)/test/cpp/util/grpc_cli.o: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/cpp/util/grpc_cli.o: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a deps_grpc_cli: $(GRPC_CLI_OBJS:.o=.dep) @@ -8878,10 +8934,10 @@ $(BINDIR)/$(CONFIG)/interop_client: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/interop_client: $(PROTOBUF_DEP) $(INTEROP_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/interop_client: $(PROTOBUF_DEP) $(INTEROP_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(INTEROP_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/interop_client + $(Q) $(LDXX) $(LDFLAGS) $(INTEROP_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_client_main.a $(LIBDIR)/$(CONFIG)/libinterop_client_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/interop_client endif @@ -8918,10 +8974,10 @@ $(BINDIR)/$(CONFIG)/interop_server: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/interop_server: $(PROTOBUF_DEP) $(INTEROP_SERVER_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/interop_server: $(PROTOBUF_DEP) $(INTEROP_SERVER_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(INTEROP_SERVER_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/interop_server + $(Q) $(LDXX) $(LDFLAGS) $(INTEROP_SERVER_OBJS) $(LIBDIR)/$(CONFIG)/libinterop_server_main.a $(LIBDIR)/$(CONFIG)/libinterop_server_helper.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/interop_server endif @@ -9001,16 +9057,16 @@ $(BINDIR)/$(CONFIG)/pubsub_client: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/pubsub_client: $(PROTOBUF_DEP) $(PUBSUB_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/pubsub_client: $(PROTOBUF_DEP) $(PUBSUB_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(PUBSUB_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/pubsub_client + $(Q) $(LDXX) $(LDFLAGS) $(PUBSUB_CLIENT_OBJS) $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) -o $(BINDIR)/$(CONFIG)/pubsub_client endif endif -$(OBJDIR)/$(CONFIG)/examples/pubsub/main.o: $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/examples/pubsub/main.o: $(LIBDIR)/$(CONFIG)/libpubsub_client_lib.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a deps_pubsub_client: $(PUBSUB_CLIENT_OBJS:.o=.dep) @@ -9127,16 +9183,16 @@ $(BINDIR)/$(CONFIG)/qps_driver: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/qps_driver: $(PROTOBUF_DEP) $(QPS_DRIVER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/qps_driver: $(PROTOBUF_DEP) $(QPS_DRIVER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(QPS_DRIVER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/qps_driver + $(Q) $(LDXX) $(LDFLAGS) $(QPS_DRIVER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/qps_driver endif endif -$(OBJDIR)/$(CONFIG)/test/cpp/qps/qps_driver.o: $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/cpp/qps/qps_driver.o: $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a deps_qps_driver: $(QPS_DRIVER_OBJS:.o=.dep) @@ -9211,16 +9267,16 @@ $(BINDIR)/$(CONFIG)/qps_worker: protobuf_dep_error else -$(BINDIR)/$(CONFIG)/qps_worker: $(PROTOBUF_DEP) $(QPS_WORKER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(BINDIR)/$(CONFIG)/qps_worker: $(PROTOBUF_DEP) $(QPS_WORKER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(E) "[LD] Linking $@" $(Q) mkdir -p `dirname $@` - $(Q) $(LDXX) $(LDFLAGS) $(QPS_WORKER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/qps_worker + $(Q) $(LDXX) $(LDFLAGS) $(QPS_WORKER_OBJS) $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/qps_worker endif endif -$(OBJDIR)/$(CONFIG)/test/cpp/qps/worker.o: $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/cpp/qps/worker.o: $(LIBDIR)/$(CONFIG)/libqps.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc++_test_config.a deps_qps_worker: $(QPS_WORKER_OBJS:.o=.dep) diff --git a/build.json b/build.json index ce0726ba269..b885d2eb17e 100644 --- a/build.json +++ b/build.json @@ -498,6 +498,14 @@ "secure": "check", "vs_project_guid": "{C187A093-A0FE-489D-A40A-6E33DE0F9FEB}" }, + { + "name": "grpc++_test_config", + "build": "private", + "language": "c++", + "src": [ + "test/cpp/util/test_config.cc" + ] + }, { "name": "grpc++_test_util", "build": "private", @@ -582,7 +590,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -615,7 +624,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -1929,7 +1939,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -1994,7 +2005,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -2011,7 +2023,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -2042,7 +2055,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -2094,7 +2108,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { @@ -2134,7 +2149,8 @@ "grpc++", "grpc", "gpr_test_util", - "gpr" + "gpr", + "grpc++_test_config" ] }, { diff --git a/examples/pubsub/main.cc b/examples/pubsub/main.cc index cc5076f0a55..8f8eefa9efb 100644 --- a/examples/pubsub/main.cc +++ b/examples/pubsub/main.cc @@ -46,6 +46,7 @@ #include #include #include +#include "test/cpp/util/test_config.h" #include "examples/pubsub/publisher.h" #include "examples/pubsub/subscriber.h" @@ -55,13 +56,6 @@ DEFINE_string(server_host, "pubsub-staging.googleapis.com", "Server host to connect to"); DEFINE_string(project_id, "", "GCE project id such as stoked-keyword-656"); -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - namespace { const char kTopic[] = "testtopics"; @@ -72,7 +66,7 @@ const char kMessageData[] = "Test Data"; int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); gpr_log(GPR_INFO, "Start PUBSUB client"); std::ostringstream ss; diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index 189cdeb0eed..76781632b8e 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -44,6 +44,7 @@ #include #include "test/cpp/interop/client_helper.h" #include "test/cpp/interop/interop_client.h" +#include "test/cpp/util/test_config.h" DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); DEFINE_bool(use_prod_roots, false, "True to use SSL roots for google"); @@ -74,17 +75,10 @@ DEFINE_string(oauth_scope, "", "Scope for OAuth tokens."); using grpc::testing::CreateChannelForTestCase; using grpc::testing::GetServiceAccountJsonKey; -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); grpc::testing::InteropClient client( CreateChannelForTestCase(FLAGS_test_case)); diff --git a/test/cpp/interop/client_helper.cc b/test/cpp/interop/client_helper.cc index 362e6af353b..b4d5b88de53 100644 --- a/test/cpp/interop/client_helper.cc +++ b/test/cpp/interop/client_helper.cc @@ -59,13 +59,6 @@ DECLARE_string(default_service_account); DECLARE_string(service_account_key_file); DECLARE_string(oauth_scope); -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - namespace grpc { namespace testing { diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index d87493b8132..94e266fcbd3 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -52,6 +52,7 @@ #include "test/cpp/interop/empty.grpc.pb.h" #include "test/cpp/interop/messages.grpc.pb.h" #include "test/cpp/interop/server_helper.h" +#include "test/cpp/util/test_config.h" DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); DEFINE_int32(port, 0, "Server port."); @@ -75,13 +76,6 @@ using grpc::testing::StreamingOutputCallResponse; using grpc::testing::TestService; using grpc::Status; -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - static bool got_sigint = false; bool SetPayload(PayloadType type, int size, Payload* payload) { @@ -225,7 +219,7 @@ static void sigint_handler(int x) { got_sigint = true; } int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); signal(SIGINT, sigint_handler); GPR_ASSERT(FLAGS_port != 0); diff --git a/test/cpp/interop/server_helper.cc b/test/cpp/interop/server_helper.cc index 56597c83c4c..c2e750dcf77 100644 --- a/test/cpp/interop/server_helper.cc +++ b/test/cpp/interop/server_helper.cc @@ -42,13 +42,6 @@ DECLARE_bool(enable_ssl); -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - namespace grpc { namespace testing { diff --git a/test/cpp/qps/qps_driver.cc b/test/cpp/qps/qps_driver.cc index 8959f7b97ec..0669ccf808b 100644 --- a/test/cpp/qps/qps_driver.cc +++ b/test/cpp/qps/qps_driver.cc @@ -36,6 +36,7 @@ #include "test/cpp/qps/driver.h" #include "test/cpp/qps/report.h" +#include "test/cpp/util/test_config.h" DEFINE_int32(num_clients, 1, "Number of client binaries"); DEFINE_int32(num_servers, 1, "Number of server binaries"); @@ -67,16 +68,9 @@ using grpc::testing::ServerType; using grpc::testing::RpcType; using grpc::testing::ResourceUsage; -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); RpcType rpc_type; GPR_ASSERT(RpcType_Parse(FLAGS_rpc_type, &rpc_type)); diff --git a/test/cpp/qps/worker.cc b/test/cpp/qps/worker.cc index 1ef5313b661..896a85cc593 100644 --- a/test/cpp/qps/worker.cc +++ b/test/cpp/qps/worker.cc @@ -40,17 +40,11 @@ #include #include "qps_worker.h" +#include "test/cpp/util/test_config.h" DEFINE_int32(driver_port, 0, "Driver server port."); DEFINE_int32(server_port, 0, "Spawned server port."); -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - static bool got_sigint = false; static void sigint_handler(int x) {got_sigint = true;} @@ -71,7 +65,7 @@ static void RunServer() { int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); signal(SIGINT, sigint_handler); diff --git a/test/cpp/util/grpc_cli.cc b/test/cpp/util/grpc_cli.cc index f2271d93654..ee9f2752737 100644 --- a/test/cpp/util/grpc_cli.cc +++ b/test/cpp/util/grpc_cli.cc @@ -63,6 +63,7 @@ #include #include "test/cpp/util/cli_call.h" +#include "test/cpp/util/test_config.h" #include #include #include @@ -70,13 +71,6 @@ #include -// In some distros, gflags is in the namespace google, and in some others, -// in gflags. This hack is enabling us to find both. -namespace google {} -namespace gflags {} -using namespace google; -using namespace gflags; - DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls."); DEFINE_bool(use_auth, false, "Whether to create default google credentials."); DEFINE_string(input_binary_file, "", @@ -87,7 +81,7 @@ DEFINE_string(output_binary_file, "output.bin", int main(int argc, char** argv) { grpc_init(); - ParseCommandLineFlags(&argc, &argv, true); + grpc::testing::InitTest(&argc, &argv, true); if (argc < 4 || grpc::string(argv[1]) != "call") { std::cout << "Usage: grpc_cli call server_host:port full_method_string\n" diff --git a/test/cpp/util/test_config.cc b/test/cpp/util/test_config.cc new file mode 100644 index 00000000000..e74f8fb14f1 --- /dev/null +++ b/test/cpp/util/test_config.cc @@ -0,0 +1,52 @@ +/* + * + * 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 "test/cpp/util/test_config.h" + +// In some distros, gflags is in the namespace google, and in some others, +// in gflags. This hack is enabling us to find both. +namespace google {} +namespace gflags {} +using namespace google; +using namespace gflags; + +namespace grpc { +namespace testing { + +void InitTest(int* argc, char*** argv, bool remove_flags) { + ParseCommandLineFlags(argc, argv, remove_flags); +} + +} // namespace testing +} // namespace grpc diff --git a/test/cpp/util/test_config.h b/test/cpp/util/test_config.h new file mode 100644 index 00000000000..6892d258dea --- /dev/null +++ b/test/cpp/util/test_config.h @@ -0,0 +1,45 @@ +/* + * + * 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_TEST_CPP_UTIL_TEST_CONFIG_H +#define GRPC_TEST_CPP_UTIL_TEST_CONFIG_H + +namespace grpc { +namespace testing { + +void InitTest(int* argc, char*** argv, bool remove_flags); + +} // namespace testing +} // namespace grpc + +#endif // GRPC_TEST_CPP_UTIL_TEST_CONFIG_H From 28d75d934958d5bb33422c9ffec1820ff4dca812 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Wed, 15 Apr 2015 15:52:23 -0700 Subject: [PATCH 04/26] Adding option to add a null terminator when loading a file. - This will take care of a potential issue with default credentials where the slice pointer is casted as const char * for APIs that need a null terminated string. --- .../security/google_default_credentials.c | 4 ++-- src/core/security/security_context.c | 5 ++-- src/core/support/file.c | 7 +++++- src/core/support/file.h | 6 +++-- test/core/security/create_jwt.c | 2 +- test/core/security/fetch_oauth2.c | 4 ++-- test/core/support/file_test.c | 23 +++++++++++++++---- 7 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/core/security/google_default_credentials.c b/src/core/security/google_default_credentials.c index ebea70dad26..d2f46ddd07f 100644 --- a/src/core/security/google_default_credentials.c +++ b/src/core/security/google_default_credentials.c @@ -127,7 +127,7 @@ static grpc_credentials *create_jwt_creds_from_path(char *creds_path) { gpr_slice creds_data; int file_ok = 0; if (creds_path == NULL) return NULL; - creds_data = gpr_load_file(creds_path, &file_ok); + creds_data = gpr_load_file(creds_path, 1, &file_ok); gpr_free(creds_path); if (file_ok) { result = grpc_jwt_credentials_create( @@ -145,7 +145,7 @@ static grpc_credentials *create_refresh_token_creds_from_path( gpr_slice creds_data; int file_ok = 0; if (creds_path == NULL) return NULL; - creds_data = gpr_load_file(creds_path, &file_ok); + creds_data = gpr_load_file(creds_path, 1, &file_ok); gpr_free(creds_path); if (file_ok) { result = grpc_refresh_token_credentials_create( diff --git a/src/core/security/security_context.c b/src/core/security/security_context.c index 08137803a32..dbfcf55655e 100644 --- a/src/core/security/security_context.c +++ b/src/core/security/security_context.c @@ -359,7 +359,6 @@ static int ssl_host_matches_name(const tsi_peer *peer, peer_name = allocated_name; if (!peer_name) return 0; } - r = tsi_ssl_peer_matches_name(peer, peer_name); gpr_free(allocated_name); return r; @@ -453,13 +452,13 @@ static void init_default_pem_root_certs(void) { if (default_root_certs_path == NULL) { default_pem_root_certs = gpr_empty_slice(); } else { - default_pem_root_certs = gpr_load_file(default_root_certs_path, NULL); + default_pem_root_certs = gpr_load_file(default_root_certs_path, 0, NULL); gpr_free(default_root_certs_path); } /* Fall back to installed certs if needed. */ if (GPR_SLICE_IS_EMPTY(default_pem_root_certs)) { - default_pem_root_certs = gpr_load_file(installed_roots_path, NULL); + default_pem_root_certs = gpr_load_file(installed_roots_path, 0, NULL); } } diff --git a/src/core/support/file.c b/src/core/support/file.c index 70100b7e9b5..63aa8262f15 100644 --- a/src/core/support/file.c +++ b/src/core/support/file.c @@ -41,7 +41,8 @@ #include "src/core/support/string.h" -gpr_slice gpr_load_file(const char *filename, int *success) { +gpr_slice gpr_load_file(const char *filename, int add_null_terminator, + int *success) { unsigned char *contents = NULL; size_t contents_size = 0; unsigned char buf[4096]; @@ -76,6 +77,10 @@ gpr_slice gpr_load_file(const char *filename, int *success) { } } if (success != NULL) *success = 1; + if (add_null_terminator) { + contents = gpr_realloc(contents, contents_size + 1); + contents[contents_size++] = 0; + } result = gpr_slice_new(contents, contents_size, gpr_free); end: diff --git a/src/core/support/file.h b/src/core/support/file.h index ee6ca7b2304..1dafe390e37 100644 --- a/src/core/support/file.h +++ b/src/core/support/file.h @@ -44,9 +44,11 @@ extern "C" { /* File utility functions */ -/* Loads the content of a file into a slice. The success parameter, if not NULL, +/* Loads the content of a file into a slice. add_null_terminator will add + a NULL terminator if non-zero. The success parameter, if not NULL, will be set to 1 in case of success and 0 in case of failure. */ -gpr_slice gpr_load_file(const char *filename, int *success); +gpr_slice gpr_load_file(const char *filename, int add_null_terminator, + int *success); /* Creates a temporary file from a prefix. If tmp_filename is not NULL, *tmp_filename is assigned the name of the diff --git a/test/core/security/create_jwt.c b/test/core/security/create_jwt.c index 614dd1e50c6..b02469fb351 100644 --- a/test/core/security/create_jwt.c +++ b/test/core/security/create_jwt.c @@ -48,7 +48,7 @@ void create_jwt(const char *json_key_file_path, const char *service_url, grpc_auth_json_key key; int ok = 0; char *jwt; - gpr_slice json_key_data = gpr_load_file(json_key_file_path, &ok); + gpr_slice json_key_data = gpr_load_file(json_key_file_path, 1, &ok); if (!ok) { fprintf(stderr, "Could not read %s.\n", json_key_file_path); exit(1); diff --git a/test/core/security/fetch_oauth2.c b/test/core/security/fetch_oauth2.c index cc847c82f71..7a40fe0dbb8 100644 --- a/test/core/security/fetch_oauth2.c +++ b/test/core/security/fetch_oauth2.c @@ -77,7 +77,7 @@ static void on_oauth2_response(void *user_data, grpc_mdelem **md_elems, static grpc_credentials *create_service_account_creds( const char *json_key_file_path, const char *scope) { int success; - gpr_slice json_key = gpr_load_file(json_key_file_path, &success); + gpr_slice json_key = gpr_load_file(json_key_file_path, 1, &success); if (!success) { gpr_log(GPR_ERROR, "Could not read file %s.", json_key_file_path); exit(1); @@ -91,7 +91,7 @@ static grpc_credentials *create_refresh_token_creds( const char *json_refresh_token_file_path) { int success; gpr_slice refresh_token = - gpr_load_file(json_refresh_token_file_path, &success); + gpr_load_file(json_refresh_token_file_path, 1, &success); if (!success) { gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path); exit(1); diff --git a/test/core/support/file_test.c b/test/core/support/file_test.c index dd8caf7a80d..56e7a4948e7 100644 --- a/test/core/support/file_test.c +++ b/test/core/support/file_test.c @@ -49,6 +49,7 @@ static const char prefix[] = "file_test"; static void test_load_empty_file(void) { FILE *tmp = NULL; gpr_slice slice; + gpr_slice slice_with_null_term; int success; char *tmp_name; @@ -59,13 +60,19 @@ static void test_load_empty_file(void) { GPR_ASSERT(tmp != NULL); fclose(tmp); - slice = gpr_load_file(tmp_name, &success); + slice = gpr_load_file(tmp_name, 0, &success); GPR_ASSERT(success == 1); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); + slice_with_null_term = gpr_load_file(tmp_name, 1, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1); + GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0); + remove(tmp_name); gpr_free(tmp_name); gpr_slice_unref(slice); + gpr_slice_unref(slice_with_null_term); } static void test_load_failure(void) { @@ -82,7 +89,7 @@ static void test_load_failure(void) { fclose(tmp); remove(tmp_name); - slice = gpr_load_file(tmp_name, &success); + slice = gpr_load_file(tmp_name, 0, &success); GPR_ASSERT(success == 0); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); gpr_free(tmp_name); @@ -92,6 +99,7 @@ static void test_load_failure(void) { static void test_load_small_file(void) { FILE *tmp = NULL; gpr_slice slice; + gpr_slice slice_with_null_term; int success; char *tmp_name; const char *blah = "blah"; @@ -104,14 +112,21 @@ static void test_load_small_file(void) { GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah)); fclose(tmp); - slice = gpr_load_file(tmp_name, &success); + slice = gpr_load_file(tmp_name, 0, &success); GPR_ASSERT(success == 1); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah)); GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah))); + slice_with_null_term = gpr_load_file(tmp_name, 1, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1)); + GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term), + blah) == 0); + remove(tmp_name); gpr_free(tmp_name); gpr_slice_unref(slice); + gpr_slice_unref(slice_with_null_term); } static void test_load_big_file(void) { @@ -135,7 +150,7 @@ static void test_load_big_file(void) { GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer)); fclose(tmp); - slice = gpr_load_file(tmp_name, &success); + slice = gpr_load_file(tmp_name, 0, &success); GPR_ASSERT(success == 1); GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer)); current = GPR_SLICE_START_PTR(slice); From 76801d21903b6cbe08ed456045a087c7ceff1b82 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 14:20:27 +0900 Subject: [PATCH 05/26] Use TypedData_XXX instead of Data_XXX for GRPC::Core::Call Data_XXX family is being deprecated in Ruby 2.x. --- src/ruby/ext/grpc/rb_call.c | 28 ++++++++++++++++++------- src/ruby/ext/grpc/rb_channel_args.c | 5 +++-- src/ruby/ext/grpc/rb_completion_queue.c | 4 ++-- src/ruby/ext/grpc/rb_grpc.c | 10 +++------ src/ruby/ext/grpc/rb_grpc.h | 8 +++++-- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index 1b1958fd263..c8cfc45c699 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -86,7 +86,7 @@ static VALUE sym_cancelled; static VALUE hash_all_calls; /* Destroys a Call. */ -void grpc_rb_call_destroy(void *p) { +static void grpc_rb_call_destroy(void *p) { grpc_call *call = NULL; VALUE ref_count = Qnil; if (p == NULL) { @@ -106,6 +106,18 @@ void grpc_rb_call_destroy(void *p) { } } +/* Describes grpc_call struct for RTypedData */ +static const rb_data_type_t grpc_call_data_type = { + "grpc_call", + {GRPC_RB_GC_NOT_MARKED, grpc_rb_call_destroy, GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, NULL, + /* it is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because grpc_rb_call_destroy + * touches a hash object. + * TODO(yugui) Directly use st_table and call the free function earlier? + */ + 0 +}; + /* Error code details is a hash containing text strings describing errors */ VALUE rb_error_code_details; @@ -124,7 +136,7 @@ const char *grpc_call_error_detail_of(grpc_call_error err) { static VALUE grpc_rb_call_cancel(VALUE self) { grpc_call *call = NULL; grpc_call_error err; - Data_Get_Struct(self, grpc_call, call); + TypedData_Get_Struct(self, grpc_call, &grpc_call_data_type, call); err = grpc_call_cancel(call); if (err != GRPC_CALL_OK) { rb_raise(grpc_rb_eCallError, "cancel failed: %s (code=%d)", @@ -258,8 +270,9 @@ void grpc_rb_md_ary_convert(VALUE md_ary_hash, grpc_metadata_array *md_ary) { /* Initialize the array, compute it's capacity, then fill it. */ grpc_metadata_array_init(md_ary); - md_ary_obj = - Data_Wrap_Struct(grpc_rb_cMdAry, GC_NOT_MARKED, GC_DONT_FREE, md_ary); + md_ary_obj = Data_Wrap_Struct(grpc_rb_cMdAry, + GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, + md_ary); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_capacity_hash_cb, md_ary_obj); md_ary->metadata = gpr_malloc(md_ary->capacity * sizeof(grpc_metadata)); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_fill_hash_cb, md_ary_obj); @@ -543,7 +556,7 @@ static VALUE grpc_rb_call_run_batch(VALUE self, VALUE cqueue, VALUE tag, grpc_event *ev = NULL; grpc_call_error err; VALUE result = Qnil; - Data_Get_Struct(self, grpc_call, call); + TypedData_Get_Struct(self, grpc_call, &grpc_call_data_type, call); /* Validate the ops args, adding them to a ruby array */ if (TYPE(ops_hash) != T_HASH) { @@ -734,7 +747,7 @@ void Init_grpc_call() { /* Gets the call from the ruby object */ grpc_call *grpc_rb_get_wrapped_call(VALUE v) { grpc_call *c = NULL; - Data_Get_Struct(v, grpc_call, c); + TypedData_Get_Struct(v, grpc_call, &grpc_call_data_type, c); return c; } @@ -751,6 +764,5 @@ VALUE grpc_rb_wrap_call(grpc_call *c) { rb_hash_aset(hash_all_calls, OFFT2NUM((VALUE)c), UINT2NUM(NUM2UINT(obj) + 1)); } - return Data_Wrap_Struct(grpc_rb_cCall, GC_NOT_MARKED, - grpc_rb_call_destroy, c); + return TypedData_Wrap_Struct(grpc_rb_cCall, &grpc_call_data_type, c); } diff --git a/src/ruby/ext/grpc/rb_channel_args.c b/src/ruby/ext/grpc/rb_channel_args.c index 9b92ec1514e..0e2cdac1a18 100644 --- a/src/ruby/ext/grpc/rb_channel_args.c +++ b/src/ruby/ext/grpc/rb_channel_args.c @@ -126,8 +126,9 @@ static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) { MEMZERO(params->dst->args, grpc_arg, num_args); rb_hash_foreach(params->src_hash, grpc_rb_channel_create_in_process_add_args_hash_cb, - Data_Wrap_Struct(grpc_rb_cChannelArgs, GC_NOT_MARKED, - GC_DONT_FREE, params->dst)); + Data_Wrap_Struct(grpc_rb_cChannelArgs, + GRPC_RB_GC_NOT_MARKED, + GRPC_RB_GC_DONT_FREE, params->dst)); /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb * decrements it during has processing */ params->dst->num_args = num_args; diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index 20ce1b909c1..bfa2a28a4b9 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -118,8 +118,8 @@ static VALUE grpc_rb_completion_queue_alloc(VALUE cls) { if (cq == NULL) { rb_raise(rb_eArgError, "could not create a completion queue: not sure why"); } - return Data_Wrap_Struct(cls, GC_NOT_MARKED, grpc_rb_completion_queue_destroy, - cq); + return Data_Wrap_Struct(cls, GRPC_RB_GC_NOT_MARKED, + grpc_rb_completion_queue_destroy, cq); } /* Blocks until the next event is available, and returns the event. */ diff --git a/src/ruby/ext/grpc/rb_grpc.c b/src/ruby/ext/grpc/rb_grpc.c index 4f30a6216a3..e721a9b3e77 100644 --- a/src/ruby/ext/grpc/rb_grpc.c +++ b/src/ruby/ext/grpc/rb_grpc.c @@ -46,10 +46,6 @@ #include "rb_credentials.h" #include "rb_server_credentials.h" -/* Define common vars and funcs declared in rb.h */ -const RUBY_DATA_FUNC GC_NOT_MARKED = NULL; -const RUBY_DATA_FUNC GC_DONT_FREE = NULL; - VALUE grpc_rb_cTimeVal = Qnil; /* Alloc func that blocks allocation of a given object by raising an @@ -224,15 +220,15 @@ void Init_grpc_time_consts() { rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject); rb_define_const(grpc_rb_mTimeConsts, "ZERO", Data_Wrap_Struct(grpc_rb_cTimeVal, - GC_NOT_MARKED, GC_DONT_FREE, + GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, (void *)&gpr_time_0)); rb_define_const(grpc_rb_mTimeConsts, "INFINITE_FUTURE", Data_Wrap_Struct(grpc_rb_cTimeVal, - GC_NOT_MARKED, GC_DONT_FREE, + GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, (void *)&gpr_inf_future)); rb_define_const(grpc_rb_mTimeConsts, "INFINITE_PAST", Data_Wrap_Struct(grpc_rb_cTimeVal, - GC_NOT_MARKED, GC_DONT_FREE, + GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, (void *)&gpr_inf_past)); rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0); rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0); diff --git a/src/ruby/ext/grpc/rb_grpc.h b/src/ruby/ext/grpc/rb_grpc.h index 3a93029556f..57db350f5ec 100644 --- a/src/ruby/ext/grpc/rb_grpc.h +++ b/src/ruby/ext/grpc/rb_grpc.h @@ -61,12 +61,16 @@ VALUE sym_metadata; /* GC_NOT_MARKED is used in calls to Data_Wrap_Struct to indicate that the wrapped struct does not need to participate in ruby gc. */ -extern const RUBY_DATA_FUNC GC_NOT_MARKED; +#define GRPC_RB_GC_NOT_MARKED (RUBY_DATA_FUNC)(NULL) /* GC_DONT_FREED is used in calls to Data_Wrap_Struct to indicate that the wrapped struct should not be freed the wrapped ruby object is released by the garbage collector. */ -extern const RUBY_DATA_FUNC GC_DONT_FREE; +#define GRPC_RB_GC_DONT_FREE (RUBY_DATA_FUNC)(NULL) + +/* GRPC_RB_MEMSIZE_UNAVAILABLE is used in rb_data_type_t to indicate that the + * number of bytes used by the wrapped struct is not available. */ +#define GRPC_RB_MEMSIZE_UNAVAILABLE (size_t (*)(const void*))(NULL) /* A ruby object alloc func that fails by raising an exception. */ VALUE grpc_rb_cannot_alloc(VALUE cls); From 961f0bcc15e3b505ff58d8ea343dc99e8613326f Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 14:39:07 +0900 Subject: [PATCH 06/26] Use TypedData for GRPC::Core::MetadataArray --- src/ruby/ext/grpc/rb_call.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index c8cfc45c699..21b933f97f0 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -106,6 +106,25 @@ static void grpc_rb_call_destroy(void *p) { } } +static size_t md_ary_datasize(const void *p) { + const grpc_metadata_array* const ary = (grpc_metadata_array*)p; + size_t i, datasize = sizeof(grpc_metadata_array); + for (i = 0; i < ary->count; ++i) { + const grpc_metadata* const md = &ary->metadata[i]; + datasize += strlen(md->key); + datasize += md->value_length; + } + datasize += ary->capacity * sizeof(grpc_metadata); + return datasize; +} + +static const rb_data_type_t grpc_rb_md_ary_data_type = { + "grpc_metadata_array", + {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, md_ary_datasize}, + NULL, NULL, + 0 +}; + /* Describes grpc_call struct for RTypedData */ static const rb_data_type_t grpc_call_data_type = { "grpc_call", @@ -206,7 +225,8 @@ int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { int i; /* Construct a metadata object from key and value and add it */ - Data_Get_Struct(md_ary_obj, grpc_metadata_array, md_ary); + TypedData_Get_Struct(md_ary_obj, grpc_metadata_array, + &grpc_rb_md_ary_data_type, md_ary); if (TYPE(val) == T_ARRAY) { /* If the value is an array, add capacity for each value in the array */ @@ -243,7 +263,8 @@ int grpc_rb_md_ary_capacity_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { grpc_metadata_array *md_ary = NULL; /* Construct a metadata object from key and value and add it */ - Data_Get_Struct(md_ary_obj, grpc_metadata_array, md_ary); + TypedData_Get_Struct(md_ary_obj, grpc_metadata_array, + &grpc_rb_md_ary_data_type, md_ary); if (TYPE(val) == T_ARRAY) { /* If the value is an array, add capacity for each value in the array */ @@ -270,9 +291,8 @@ void grpc_rb_md_ary_convert(VALUE md_ary_hash, grpc_metadata_array *md_ary) { /* Initialize the array, compute it's capacity, then fill it. */ grpc_metadata_array_init(md_ary); - md_ary_obj = Data_Wrap_Struct(grpc_rb_cMdAry, - GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, - md_ary); + md_ary_obj = TypedData_Wrap_Struct(grpc_rb_cMdAry, &grpc_rb_md_ary_data_type, + md_ary); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_capacity_hash_cb, md_ary_obj); md_ary->metadata = gpr_malloc(md_ary->capacity * sizeof(grpc_metadata)); rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_fill_hash_cb, md_ary_obj); From c9b7d1cb04485111a61e462fc93c52c64cc1007f Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 14:50:09 +0900 Subject: [PATCH 07/26] Use TypedData for GRPC::Core::Channel --- src/ruby/ext/grpc/rb_channel.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c index 3480280a03c..47b85c83ed6 100644 --- a/src/ruby/ext/grpc/rb_channel.c +++ b/src/ruby/ext/grpc/rb_channel.c @@ -103,13 +103,19 @@ static void grpc_rb_channel_mark(void *p) { } } +static rb_data_type_t grpc_channel_data_type = { + "grpc_channel", + {grpc_rb_channel_mark, grpc_rb_channel_free, GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY +}; + /* Allocates grpc_rb_channel instances. */ static VALUE grpc_rb_channel_alloc(VALUE cls) { grpc_rb_channel *wrapper = ALLOC(grpc_rb_channel); wrapper->wrapped = NULL; wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_channel_mark, grpc_rb_channel_free, - wrapper); + return TypedData_Wrap_Struct(cls, &grpc_channel_data_type, wrapper); } /* @@ -133,7 +139,7 @@ static VALUE grpc_rb_channel_init(int argc, VALUE *argv, VALUE self) { /* "21" == 2 mandatory args, 1 (credentials) is optional */ rb_scan_args(argc, argv, "21", &target, &channel_args, &credentials); - Data_Get_Struct(self, grpc_rb_channel, wrapper); + TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); target_chars = StringValueCStr(target); grpc_rb_hash_convert_to_channel_args(channel_args, &args); if (credentials == Qnil) { @@ -174,8 +180,8 @@ static VALUE grpc_rb_channel_init_copy(VALUE copy, VALUE orig) { return Qnil; } - Data_Get_Struct(orig, grpc_rb_channel, orig_ch); - Data_Get_Struct(copy, grpc_rb_channel, copy_ch); + TypedData_Get_Struct(orig, grpc_rb_channel, &grpc_channel_data_type, orig_ch); + TypedData_Get_Struct(copy, grpc_rb_channel, &grpc_channel_data_type, copy_ch); /* use ruby's MEMCPY to make a byte-for-byte copy of the channel wrapper * object. */ @@ -196,7 +202,7 @@ static VALUE grpc_rb_channel_create_call(VALUE self, VALUE cqueue, VALUE method, char *host_chars = StringValueCStr(host); cq = grpc_rb_get_wrapped_completion_queue(cqueue); - Data_Get_Struct(self, grpc_rb_channel, wrapper); + TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); ch = wrapper->wrapped; if (ch == NULL) { rb_raise(rb_eRuntimeError, "closed!"); @@ -229,7 +235,7 @@ static VALUE grpc_rb_channel_destroy(VALUE self) { grpc_rb_channel *wrapper = NULL; grpc_channel *ch = NULL; - Data_Get_Struct(self, grpc_rb_channel, wrapper); + TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); ch = wrapper->wrapped; if (ch != NULL) { grpc_channel_destroy(ch); @@ -278,6 +284,6 @@ void Init_grpc_channel() { /* Gets the wrapped channel from the ruby wrapper */ grpc_channel *grpc_rb_get_wrapped_channel(VALUE v) { grpc_rb_channel *wrapper = NULL; - Data_Get_Struct(v, grpc_rb_channel, wrapper); + TypedData_Get_Struct(v, grpc_rb_channel, &grpc_channel_data_type, wrapper); return wrapper->wrapped; } From d98db4a647cb943f53ea7dd5568c6ed4e809ea9f Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 14:59:59 +0900 Subject: [PATCH 08/26] Use TypedData for TmpChannelArgs --- src/ruby/ext/grpc/rb_channel_args.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ruby/ext/grpc/rb_channel_args.c b/src/ruby/ext/grpc/rb_channel_args.c index 0e2cdac1a18..acd545f5d2f 100644 --- a/src/ruby/ext/grpc/rb_channel_args.c +++ b/src/ruby/ext/grpc/rb_channel_args.c @@ -38,6 +38,13 @@ #include "rb_grpc.h" +static rb_data_type_t grpc_rb_channel_args_data_type = { + "grpc_channel_args", + {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY +}; + /* A callback the processes the hash key values in channel_args hash */ static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key, VALUE val, @@ -60,7 +67,8 @@ static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key, return ST_STOP; } - Data_Get_Struct(args_obj, grpc_channel_args, args); + TypedData_Get_Struct(args_obj, grpc_channel_args, + &grpc_rb_channel_args_data_type, args); if (args->num_args <= 0) { rb_raise(rb_eRuntimeError, "hash_cb bug: num_args is %lu for key:%s", args->num_args, StringValueCStr(key)); @@ -126,9 +134,9 @@ static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) { MEMZERO(params->dst->args, grpc_arg, num_args); rb_hash_foreach(params->src_hash, grpc_rb_channel_create_in_process_add_args_hash_cb, - Data_Wrap_Struct(grpc_rb_cChannelArgs, - GRPC_RB_GC_NOT_MARKED, - GRPC_RB_GC_DONT_FREE, params->dst)); + TypedData_Wrap_Struct(grpc_rb_cChannelArgs, + &grpc_rb_channel_args_data_type, + params->dst)); /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb * decrements it during has processing */ params->dst->num_args = num_args; From bf256ae07a492ddca6ee59164cf27f8a13d6a3db Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 15:17:13 +0900 Subject: [PATCH 09/26] Use TypedData for GRPC::Core::CompletionQueue --- src/ruby/ext/grpc/rb_completion_queue.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index bfa2a28a4b9..db2cc47761d 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -112,21 +112,31 @@ static void grpc_rb_completion_queue_destroy(void *p) { grpc_completion_queue_destroy(cq); } +static rb_data_type_t grpc_rb_completion_queue_data_type = { + "grpc_completion_queue", + {GRPC_RB_GC_NOT_MARKED, grpc_rb_completion_queue_destroy, + GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, NULL, + /* cannot immediately free because grpc_rb_completion_queue_shutdown_drain + * calls rb_thread_call_without_gvl. */ + 0 +}; + /* Allocates a completion queue. */ static VALUE grpc_rb_completion_queue_alloc(VALUE cls) { grpc_completion_queue *cq = grpc_completion_queue_create(); if (cq == NULL) { rb_raise(rb_eArgError, "could not create a completion queue: not sure why"); } - return Data_Wrap_Struct(cls, GRPC_RB_GC_NOT_MARKED, - grpc_rb_completion_queue_destroy, cq); + return TypedData_Wrap_Struct(cls, &grpc_rb_completion_queue_data_type, cq); } /* Blocks until the next event is available, and returns the event. */ static VALUE grpc_rb_completion_queue_next(VALUE self, VALUE timeout) { next_call_stack next_call; MEMZERO(&next_call, next_call_stack, 1); - Data_Get_Struct(self, grpc_completion_queue, next_call.cq); + TypedData_Get_Struct(self, grpc_completion_queue, + &grpc_rb_completion_queue_data_type, next_call.cq); next_call.timeout = grpc_rb_time_timeval(timeout, /* absolute time*/ 0); next_call.event = NULL; rb_thread_call_without_gvl(grpc_rb_completion_queue_next_no_gil, @@ -154,7 +164,8 @@ grpc_event* grpc_rb_completion_queue_pluck_event(VALUE self, VALUE tag, VALUE timeout) { next_call_stack next_call; MEMZERO(&next_call, next_call_stack, 1); - Data_Get_Struct(self, grpc_completion_queue, next_call.cq); + TypedData_Get_Struct(self, grpc_completion_queue, + &grpc_rb_completion_queue_data_type, next_call.cq); next_call.timeout = grpc_rb_time_timeval(timeout, /* absolute time*/ 0); next_call.tag = ROBJECT(tag); next_call.event = NULL; @@ -192,6 +203,7 @@ void Init_grpc_completion_queue() { /* Gets the wrapped completion queue from the ruby wrapper */ grpc_completion_queue *grpc_rb_get_wrapped_completion_queue(VALUE v) { grpc_completion_queue *cq = NULL; - Data_Get_Struct(v, grpc_completion_queue, cq); + TypedData_Get_Struct(v, grpc_completion_queue, + &grpc_rb_completion_queue_data_type, cq); return cq; } From a87924e646673276d5a0f8302800e486be7b0105 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 15:25:16 +0900 Subject: [PATCH 10/26] Use TypedData for GRPC::Core::Credentials --- src/ruby/ext/grpc/rb_credentials.c | 47 ++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/ruby/ext/grpc/rb_credentials.c b/src/ruby/ext/grpc/rb_credentials.c index 1504a4884ea..8170f0d26cb 100644 --- a/src/ruby/ext/grpc/rb_credentials.c +++ b/src/ruby/ext/grpc/rb_credentials.c @@ -83,14 +83,21 @@ static void grpc_rb_credentials_mark(void *p) { } } +static rb_data_type_t grpc_rb_credentials_data_type = { + "grpc_credentials", + {grpc_rb_credentials_mark, grpc_rb_credentials_free, + GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, + NULL, + RUBY_TYPED_FREE_IMMEDIATELY}; + /* Allocates Credential instances. Provides safe initial defaults for the instance fields. */ static VALUE grpc_rb_credentials_alloc(VALUE cls) { grpc_rb_credentials *wrapper = ALLOC(grpc_rb_credentials); wrapper->wrapped = NULL; wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_credentials_mark, - grpc_rb_credentials_free, wrapper); + return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper); } /* Clones Credentials instances. @@ -110,8 +117,10 @@ static VALUE grpc_rb_credentials_init_copy(VALUE copy, VALUE orig) { rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cCredentials)); } - Data_Get_Struct(orig, grpc_rb_credentials, orig_cred); - Data_Get_Struct(copy, grpc_rb_credentials, copy_cred); + TypedData_Get_Struct(orig, grpc_rb_credentials, + &grpc_rb_credentials_data_type, orig_cred); + TypedData_Get_Struct(copy, grpc_rb_credentials, + &grpc_rb_credentials_data_type, copy_cred); /* use ruby's MEMCPY to make a byte-for-byte copy of the credentials * wrapper object. */ @@ -133,8 +142,7 @@ static VALUE grpc_rb_default_credentials_create(VALUE cls) { } wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_credentials_mark, - grpc_rb_credentials_free, wrapper); + return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper); } /* @@ -151,8 +159,7 @@ static VALUE grpc_rb_compute_engine_credentials_create(VALUE cls) { } wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_credentials_mark, - grpc_rb_credentials_free, wrapper); + return TypedData_Wrap_Struct(cls, &grpc_rb_credentials_data_type, wrapper); } /* @@ -166,8 +173,10 @@ static VALUE grpc_rb_composite_credentials_create(VALUE self, VALUE other) { grpc_rb_credentials *other_wrapper = NULL; grpc_rb_credentials *wrapper = NULL; - Data_Get_Struct(self, grpc_rb_credentials, self_wrapper); - Data_Get_Struct(other, grpc_rb_credentials, other_wrapper); + TypedData_Get_Struct(self, grpc_rb_credentials, + &grpc_rb_credentials_data_type, self_wrapper); + TypedData_Get_Struct(other, grpc_rb_credentials, + &grpc_rb_credentials_data_type, other_wrapper); wrapper = ALLOC(grpc_rb_credentials); wrapper->wrapped = grpc_composite_credentials_create(self_wrapper->wrapped, other_wrapper->wrapped); @@ -178,8 +187,8 @@ static VALUE grpc_rb_composite_credentials_create(VALUE self, VALUE other) { } wrapper->mark = Qnil; - return Data_Wrap_Struct(grpc_rb_cCredentials, grpc_rb_credentials_mark, - grpc_rb_credentials_free, wrapper); + return TypedData_Wrap_Struct(grpc_rb_cCredentials, + &grpc_rb_credentials_data_type, wrapper); } /* The attribute used on the mark object to hold the pem_root_certs. */ @@ -214,7 +223,8 @@ static VALUE grpc_rb_credentials_init(int argc, VALUE *argv, VALUE self) { rb_scan_args(argc, argv, "12", &pem_root_certs, &pem_private_key, &pem_cert_chain); - Data_Get_Struct(self, grpc_rb_credentials, wrapper); + TypedData_Get_Struct(self, grpc_rb_credentials, + &grpc_rb_credentials_data_type, wrapper); if (pem_root_certs == Qnil) { rb_raise(rb_eRuntimeError, "could not create a credential: nil pem_root_certs"); @@ -225,8 +235,8 @@ static VALUE grpc_rb_credentials_init(int argc, VALUE *argv, VALUE self) { } else { key_cert_pair.private_key = RSTRING_PTR(pem_private_key); key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain); - creds = grpc_ssl_credentials_create( - RSTRING_PTR(pem_root_certs), &key_cert_pair); + creds = grpc_ssl_credentials_create(RSTRING_PTR(pem_root_certs), + &key_cert_pair); } if (creds == NULL) { rb_raise(rb_eRuntimeError, "could not create a credentials, not sure why"); @@ -253,8 +263,8 @@ void Init_grpc_credentials() { rb_define_alloc_func(grpc_rb_cCredentials, grpc_rb_credentials_alloc); /* Provides a ruby constructor and support for dup/clone. */ - rb_define_method(grpc_rb_cCredentials, "initialize", - grpc_rb_credentials_init, -1); + rb_define_method(grpc_rb_cCredentials, "initialize", grpc_rb_credentials_init, + -1); rb_define_method(grpc_rb_cCredentials, "initialize_copy", grpc_rb_credentials_init_copy, 1); @@ -277,6 +287,7 @@ void Init_grpc_credentials() { /* Gets the wrapped grpc_credentials from the ruby wrapper */ grpc_credentials *grpc_rb_get_wrapped_credentials(VALUE v) { grpc_rb_credentials *wrapper = NULL; - Data_Get_Struct(v, grpc_rb_credentials, wrapper); + TypedData_Get_Struct(v, grpc_rb_credentials, &grpc_rb_credentials_data_type, + wrapper); return wrapper->wrapped; } From d441c2e5b3dcdd2a0eebeeb9fb580da4a875187c Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sat, 11 Apr 2015 15:33:58 +0900 Subject: [PATCH 11/26] Use TypedData for GRPC::Core::TimeSpec --- src/ruby/ext/grpc/rb_grpc.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/ruby/ext/grpc/rb_grpc.c b/src/ruby/ext/grpc/rb_grpc.c index e721a9b3e77..b431636c75e 100644 --- a/src/ruby/ext/grpc/rb_grpc.c +++ b/src/ruby/ext/grpc/rb_grpc.c @@ -48,6 +48,13 @@ VALUE grpc_rb_cTimeVal = Qnil; +static rb_data_type_t grpc_rb_timespec_data_type = { + "gpr_timespec", + {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, + NULL, + RUBY_TYPED_FREE_IMMEDIATELY}; + /* Alloc func that blocks allocation of a given object by raising an * exception. */ VALUE grpc_rb_cannot_alloc(VALUE cls) { @@ -93,7 +100,8 @@ gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) { switch (TYPE(time)) { case T_DATA: if (CLASS_OF(time) == grpc_rb_cTimeVal) { - Data_Get_Struct(time, gpr_timespec, time_const); + TypedData_Get_Struct(time, gpr_timespec, &grpc_rb_timespec_data_type, + time_const); t = *time_const; } else if (CLASS_OF(time) == rb_cTime) { t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0)); @@ -197,7 +205,8 @@ static ID id_to_s; /* Converts a wrapped time constant to a standard time. */ VALUE grpc_rb_time_val_to_time(VALUE self) { gpr_timespec *time_const = NULL; - Data_Get_Struct(self, gpr_timespec, time_const); + TypedData_Get_Struct(self, gpr_timespec, &grpc_rb_timespec_data_type, + time_const); return rb_funcall(rb_cTime, id_at, 2, INT2NUM(time_const->tv_sec), INT2NUM(time_const->tv_nsec)); } @@ -218,18 +227,18 @@ void Init_grpc_time_consts() { rb_define_module_under(grpc_rb_mGrpcCore, "TimeConsts"); grpc_rb_cTimeVal = rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject); - rb_define_const(grpc_rb_mTimeConsts, "ZERO", - Data_Wrap_Struct(grpc_rb_cTimeVal, - GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, - (void *)&gpr_time_0)); - rb_define_const(grpc_rb_mTimeConsts, "INFINITE_FUTURE", - Data_Wrap_Struct(grpc_rb_cTimeVal, - GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, - (void *)&gpr_inf_future)); - rb_define_const(grpc_rb_mTimeConsts, "INFINITE_PAST", - Data_Wrap_Struct(grpc_rb_cTimeVal, - GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, - (void *)&gpr_inf_past)); + rb_define_const( + grpc_rb_mTimeConsts, "ZERO", + TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, + (void *)&gpr_time_0)); + rb_define_const( + grpc_rb_mTimeConsts, "INFINITE_FUTURE", + TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, + (void *)&gpr_inf_future)); + rb_define_const( + grpc_rb_mTimeConsts, "INFINITE_PAST", + TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, + (void *)&gpr_inf_past)); rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0); rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0); rb_define_method(grpc_rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0); From 9232f124e0c06cf2c0523d0b7c09b2dd462449d1 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Sun, 12 Apr 2015 16:39:37 +0900 Subject: [PATCH 12/26] Use TypedData for GRPC::Core::Server --- src/ruby/ext/grpc/rb_server.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ruby/ext/grpc/rb_server.c b/src/ruby/ext/grpc/rb_server.c index 33d9d695008..5d7c8f7a5d9 100644 --- a/src/ruby/ext/grpc/rb_server.c +++ b/src/ruby/ext/grpc/rb_server.c @@ -88,13 +88,23 @@ static void grpc_rb_server_mark(void *p) { } } +static const rb_data_type_t grpc_rb_server_data_type = { + "grpc_server", + {grpc_rb_server_mark, grpc_rb_server_free, GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, + NULL, + /* It is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because the free function would block + * and we might want to unlock GVL + * TODO(yugui) Unlock GVL? + */ + 0}; + /* Allocates grpc_rb_server instances. */ static VALUE grpc_rb_server_alloc(VALUE cls) { grpc_rb_server *wrapper = ALLOC(grpc_rb_server); wrapper->wrapped = NULL; wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_server_mark, grpc_rb_server_free, - wrapper); + return TypedData_Wrap_Struct(cls, &grpc_rb_server_data_type, wrapper); } /* @@ -110,7 +120,8 @@ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) { grpc_channel_args args; MEMZERO(&args, grpc_channel_args, 1); cq = grpc_rb_get_wrapped_completion_queue(cqueue); - Data_Get_Struct(self, grpc_rb_server, wrapper); + TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, + wrapper); grpc_rb_hash_convert_to_channel_args(channel_args, &args); srv = grpc_server_create(cq, &args); @@ -146,8 +157,10 @@ static VALUE grpc_rb_server_init_copy(VALUE copy, VALUE orig) { rb_raise(rb_eTypeError, "not a %s", rb_obj_classname(grpc_rb_cServer)); } - Data_Get_Struct(orig, grpc_rb_server, orig_srv); - Data_Get_Struct(copy, grpc_rb_server, copy_srv); + TypedData_Get_Struct(orig, grpc_rb_server, &grpc_rb_server_data_type, + orig_srv); + TypedData_Get_Struct(copy, grpc_rb_server, &grpc_rb_server_data_type, + copy_srv); /* use ruby's MEMCPY to make a byte-for-byte copy of the server wrapper object. */ @@ -194,7 +207,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue, grpc_call_error err; request_call_stack st; VALUE result; - Data_Get_Struct(self, grpc_rb_server, s); + TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { rb_raise(rb_eRuntimeError, "closed!"); return Qnil; @@ -245,7 +258,7 @@ static VALUE grpc_rb_server_request_call(VALUE self, VALUE cqueue, static VALUE grpc_rb_server_start(VALUE self) { grpc_rb_server *s = NULL; - Data_Get_Struct(self, grpc_rb_server, s); + TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { rb_raise(rb_eRuntimeError, "closed!"); } else { @@ -256,7 +269,7 @@ static VALUE grpc_rb_server_start(VALUE self) { static VALUE grpc_rb_server_destroy(VALUE self) { grpc_rb_server *s = NULL; - Data_Get_Struct(self, grpc_rb_server, s); + TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped != NULL) { grpc_server_shutdown(s->wrapped); grpc_server_destroy(s->wrapped); @@ -288,7 +301,7 @@ static VALUE grpc_rb_server_add_http2_port(int argc, VALUE *argv, VALUE self) { /* "11" == 1 mandatory args, 1 (rb_creds) is optional */ rb_scan_args(argc, argv, "11", &port, &rb_creds); - Data_Get_Struct(self, grpc_rb_server, s); + TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (s->wrapped == NULL) { rb_raise(rb_eRuntimeError, "closed!"); return Qnil; @@ -340,6 +353,6 @@ void Init_grpc_server() { /* Gets the wrapped server from the ruby wrapper */ grpc_server *grpc_rb_get_wrapped_server(VALUE v) { grpc_rb_server *wrapper = NULL; - Data_Get_Struct(v, grpc_rb_server, wrapper); + TypedData_Get_Struct(v, grpc_rb_server, &grpc_rb_server_data_type, wrapper); return wrapper->wrapped; } From 29ee1dbc93d1b97578d39c796bfc19b99e57545f Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Mon, 13 Apr 2015 08:26:09 +0900 Subject: [PATCH 13/26] Use TypedData for GRPC::Core::ServerCredentials --- src/ruby/ext/grpc/rb_server_credentials.c | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ruby/ext/grpc/rb_server_credentials.c b/src/ruby/ext/grpc/rb_server_credentials.c index 8b813eaca1c..ab16c056505 100644 --- a/src/ruby/ext/grpc/rb_server_credentials.c +++ b/src/ruby/ext/grpc/rb_server_credentials.c @@ -82,6 +82,14 @@ static void grpc_rb_server_credentials_mark(void *p) { } } +static const rb_data_type_t grpc_rb_server_credentials_data_type = { + "grpc_server_credentials", + {grpc_rb_server_credentials_mark, grpc_rb_server_credentials_free, + GRPC_RB_MEMSIZE_UNAVAILABLE}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY +}; + /* Allocates ServerCredential instances. Provides safe initial defaults for the instance fields. */ @@ -89,8 +97,8 @@ static VALUE grpc_rb_server_credentials_alloc(VALUE cls) { grpc_rb_server_credentials *wrapper = ALLOC(grpc_rb_server_credentials); wrapper->wrapped = NULL; wrapper->mark = Qnil; - return Data_Wrap_Struct(cls, grpc_rb_server_credentials_mark, - grpc_rb_server_credentials_free, wrapper); + return TypedData_Wrap_Struct(cls, &grpc_rb_server_credentials_data_type, + wrapper); } /* Clones ServerCredentials instances. @@ -112,8 +120,10 @@ static VALUE grpc_rb_server_credentials_init_copy(VALUE copy, VALUE orig) { rb_obj_classname(grpc_rb_cServerCredentials)); } - Data_Get_Struct(orig, grpc_rb_server_credentials, orig_ch); - Data_Get_Struct(copy, grpc_rb_server_credentials, copy_ch); + TypedData_Get_Struct(orig, grpc_rb_server_credentials, + &grpc_rb_server_credentials_data_type, orig_ch); + TypedData_Get_Struct(copy, grpc_rb_server_credentials, + &grpc_rb_server_credentials_data_type, copy_ch); /* use ruby's MEMCPY to make a byte-for-byte copy of the server_credentials wrapper object. */ @@ -149,7 +159,8 @@ static VALUE grpc_rb_server_credentials_init(VALUE self, VALUE pem_root_certs, grpc_rb_server_credentials *wrapper = NULL; grpc_server_credentials *creds = NULL; grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL}; - Data_Get_Struct(self, grpc_rb_server_credentials, wrapper); + TypedData_Get_Struct(self, grpc_rb_server_credentials, + &grpc_rb_server_credentials_data_type, wrapper); if (pem_cert_chain == Qnil) { rb_raise(rb_eRuntimeError, "could not create a server credential: nil pem_cert_chain"); @@ -206,6 +217,7 @@ void Init_grpc_server_credentials() { /* Gets the wrapped grpc_server_credentials from the ruby wrapper */ grpc_server_credentials *grpc_rb_get_wrapped_server_credentials(VALUE v) { grpc_rb_server_credentials *wrapper = NULL; - Data_Get_Struct(v, grpc_rb_server_credentials, wrapper); + TypedData_Get_Struct(v, grpc_rb_server_credentials, + &grpc_rb_server_credentials_data_type, wrapper); return wrapper->wrapped; } From 22887917f54dfa3f1b42f2bc4627fb8dbd968cfb Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Thu, 16 Apr 2015 20:57:17 +0900 Subject: [PATCH 14/26] Avoid implicit function declarations --- src/ruby/ext/grpc/rb_completion_queue.c | 9 ++++++--- src/ruby/ext/grpc/rb_grpc.c | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index 20ce1b909c1..bc5ca109c38 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -33,7 +33,8 @@ #include "rb_completion_queue.h" -#include +#include +#include #include #include @@ -48,14 +49,16 @@ typedef struct next_call_stack { } next_call_stack; /* Calls grpc_completion_queue_next without holding the ruby GIL */ -static void *grpc_rb_completion_queue_next_no_gil(next_call_stack *next_call) { +static void *grpc_rb_completion_queue_next_no_gil(void *param) { + next_call_stack *const next_call = (next_call_stack*)param; next_call->event = grpc_completion_queue_next(next_call->cq, next_call->timeout); return NULL; } /* Calls grpc_completion_queue_pluck without holding the ruby GIL */ -static void *grpc_rb_completion_queue_pluck_no_gil(next_call_stack *next_call) { +static void *grpc_rb_completion_queue_pluck_no_gil(void *param) { + next_call_stack *const next_call = (next_call_stack*)param; next_call->event = grpc_completion_queue_pluck(next_call->cq, next_call->tag, next_call->timeout); return NULL; diff --git a/src/ruby/ext/grpc/rb_grpc.c b/src/ruby/ext/grpc/rb_grpc.c index 4f30a6216a3..2392393ffeb 100644 --- a/src/ruby/ext/grpc/rb_grpc.c +++ b/src/ruby/ext/grpc/rb_grpc.c @@ -34,7 +34,8 @@ #include "rb_grpc.h" #include -#include +#include +#include #include #include @@ -244,7 +245,7 @@ void Init_grpc_time_consts() { id_tv_nsec = rb_intern("tv_nsec"); } -void grpc_rb_shutdown(void *vm) { grpc_shutdown(); } +void grpc_rb_shutdown(ruby_vm_t *vm) { grpc_shutdown(); } /* Initialize the GRPC module structs */ From 9d005cc5e280e4db9c39958ecc0186df30dd619f Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Thu, 16 Apr 2015 09:50:13 -0700 Subject: [PATCH 15/26] Addressing comments. --- src/core/support/file.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/core/support/file.c b/src/core/support/file.c index 63aa8262f15..3a4ac6f2f00 100644 --- a/src/core/support/file.c +++ b/src/core/support/file.c @@ -45,10 +45,10 @@ gpr_slice gpr_load_file(const char *filename, int add_null_terminator, int *success) { unsigned char *contents = NULL; size_t contents_size = 0; - unsigned char buf[4096]; char *error_msg = NULL; gpr_slice result = gpr_empty_slice(); FILE *file = fopen(filename, "rb"); + size_t bytes_read = 0; if (file == NULL) { gpr_asprintf(&error_msg, "Could not open file %s (error = %s).", filename, @@ -56,29 +56,20 @@ gpr_slice gpr_load_file(const char *filename, int add_null_terminator, GPR_ASSERT(error_msg != NULL); goto end; } - - while (1) { - size_t bytes_read = fread(buf, 1, sizeof(buf), file); - if (bytes_read > 0) { - contents = gpr_realloc(contents, contents_size + bytes_read); - memcpy(contents + contents_size, buf, bytes_read); - contents_size += bytes_read; - } - if (bytes_read < sizeof(buf)) { - if (ferror(file)) { - gpr_asprintf(&error_msg, "Error %s occured while reading file %s.", - strerror(errno), filename); - GPR_ASSERT(error_msg != NULL); - goto end; - } else { - GPR_ASSERT(feof(file)); - break; - } - } + fseek(file, 0, SEEK_END); + contents_size = ftell(file); + fseek(file, 0, SEEK_SET); + contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)); + bytes_read = fread(contents, 1, contents_size, file); + if (bytes_read < contents_size) { + GPR_ASSERT(ferror(file)); + gpr_asprintf(&error_msg, "Error %s occured while reading file %s.", + strerror(errno), filename); + GPR_ASSERT(error_msg != NULL); + goto end; } if (success != NULL) *success = 1; if (add_null_terminator) { - contents = gpr_realloc(contents, contents_size + 1); contents[contents_size++] = 0; } result = gpr_slice_new(contents, contents_size, gpr_free); From 9a5a94086bfb9b83ad06a2e7f6104e378e143468 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Thu, 16 Apr 2015 10:39:50 -0700 Subject: [PATCH 16/26] Get run_tests.py forever mode working again --- tools/run_tests/run_tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/run_tests/run_tests.py b/tools/run_tests/run_tests.py index a443b17a971..8c8e78d8597 100755 --- a/tools/run_tests/run_tests.py +++ b/tools/run_tests/run_tests.py @@ -444,6 +444,7 @@ if forever: previous_success = success success = _build_and_run(check_cancelled=have_files_changed, newline_on_success=False, + travis=args.travis, cache=test_cache) == 0 if not previous_success and success: jobset.message('SUCCESS', From 9558460b77d12deca7db0ecb2a43bf87a846baba Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Thu, 16 Apr 2015 12:53:07 -0700 Subject: [PATCH 17/26] Adds a #run_till_terminated func to RpcServer - adds signal handling to RpcServer - ensures that very little behaviour occurs in the signal handling context - adds #run_till_terminated that launches a server in a thread and then handles signals that can be used to stop it. --- src/ruby/.rubocop_todo.yml | 8 +-- src/ruby/lib/grpc/generic/rpc_server.rb | 71 ++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/ruby/.rubocop_todo.yml b/src/ruby/.rubocop_todo.yml index b4d66c517c2..02136a81a91 100644 --- a/src/ruby/.rubocop_todo.yml +++ b/src/ruby/.rubocop_todo.yml @@ -1,18 +1,18 @@ # This configuration was generated by `rubocop --auto-gen-config` -# on 2015-04-15 18:43:23 -0700 using RuboCop version 0.30.0. +# on 2015-04-16 12:30:09 -0700 using RuboCop version 0.30.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 32 +# Offense count: 34 Metrics/AbcSize: Max: 36 # Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 183 + Max: 185 # Offense count: 35 # Configuration parameters: CountComments. @@ -24,7 +24,7 @@ Metrics/MethodLength: Metrics/ParameterLists: Max: 8 -# Offense count: 6 +# Offense count: 9 # Configuration parameters: AllowedVariables. Style/GlobalVars: Enabled: false diff --git a/src/ruby/lib/grpc/generic/rpc_server.rb b/src/ruby/lib/grpc/generic/rpc_server.rb index 30a4bf15325..fa21d770806 100644 --- a/src/ruby/lib/grpc/generic/rpc_server.rb +++ b/src/ruby/lib/grpc/generic/rpc_server.rb @@ -33,6 +33,9 @@ require 'grpc/generic/service' require 'thread' require 'xray/thread_dump_signal_handler' +# A global that contains signals the gRPC servers should respond to. +$grpc_signals = [] + # GRPC contains the General RPC module. module GRPC # RpcServer hosts a number of services and makes them available on the @@ -50,6 +53,23 @@ module GRPC # Default max_waiting_requests size is 20 DEFAULT_MAX_WAITING_REQUESTS = 20 + # Default poll period is 1s + DEFAULT_POLL_PERIOD = 1 + + # Signal check period is 0.25s + SIGNAL_CHECK_PERIOD = 0.25 + + # Sets up a signal handler that adds signals to the signal handling global. + # + # Signal handlers should do as little as humanly possible. + # Here, they just add themselves to $grpc_signals + # + # RpcServer (and later other parts of gRPC) monitors the signals + # $grpc_signals in its own non-signal context. + def self.trap_signals + %w(INT TERM).each { |sig| trap(sig) { $grpc_signals << sig } } + end + # Creates a new RpcServer. # # The RPC server is configured using keyword arguments. @@ -79,7 +99,7 @@ module GRPC # with not available to new requests def initialize(pool_size:DEFAULT_POOL_SIZE, max_waiting_requests:DEFAULT_MAX_WAITING_REQUESTS, - poll_period:INFINITE_FUTURE, + poll_period:DEFAULT_POLL_PERIOD, completion_queue_override:nil, server_override:nil, **kw) @@ -117,6 +137,13 @@ module GRPC return unless @running @stopped = true @pool.stop + + # TODO: uncomment this: + # + # This segfaults in the c layer, so its commented out for now. Shutdown + # still occurs, but the c layer has to do the cleanup. + # + # @server.close end # determines if the server is currently running @@ -139,7 +166,37 @@ module GRPC running? end - # determines if the server is currently stopped + # Runs the server in its own thread, then waits for signal INT or TERM on + # the current thread to terminate it. + def run_till_terminated + self.class.trap_signals + t = Thread.new { run } + wait_till_running + loop do + sleep SIGNAL_CHECK_PERIOD + break unless handle_signals + end + stop + t.join + end + + # Handles the signals in $grpc_signals. + # + # @return false if the server should exit, true if not. + def handle_signals + loop do + sig = $grpc_signals.shift + case sig + when 'INT' + return false + when 'TERM' + return false + end + end + true + end + + # Determines if the server is currently stopped def stopped? @stopped ||= false end @@ -265,7 +322,10 @@ module GRPC # Pool is a simple thread pool for running server requests. class Pool - def initialize(size) + # Default keep alive period is 1s + DEFAULT_KEEP_ALIVE = 1 + + def initialize(size, keep_alive: DEFAULT_KEEP_ALIVE) fail 'pool size must be positive' unless size > 0 @jobs = Queue.new @size = size @@ -273,6 +333,7 @@ module GRPC @stop_mutex = Mutex.new @stop_cond = ConditionVariable.new @workers = [] + @keep_alive = keep_alive end # Returns the number of jobs waiting @@ -325,7 +386,6 @@ module GRPC @workers.size.times { schedule { throw :exit } } @stopped = true - # TODO: allow configuration of the keepalive period keep_alive = 5 @stop_mutex.synchronize do @stop_cond.wait(@stop_mutex, keep_alive) if @workers.size > 0 @@ -333,7 +393,7 @@ module GRPC # Forcibly shutdown any threads that are still alive. if @workers.size > 0 - logger.warn("forcibly terminating #{@workers.size} worker(s)") + logger.info("forcibly terminating #{@workers.size} worker(s)") @workers.each do |t| next unless t.alive? begin @@ -344,7 +404,6 @@ module GRPC end end end - logger.info('stopped, all workers are shutdown') end end From 321871ee6e09373723b400b28e6d08bd18fac629 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Thu, 16 Apr 2015 12:56:11 -0700 Subject: [PATCH 18/26] Updates the example servers to use #run_till_terminated --- src/ruby/bin/interop/interop_server.rb | 2 +- src/ruby/bin/math_server.rb | 2 +- src/ruby/bin/noproto_server.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ruby/bin/interop/interop_server.rb b/src/ruby/bin/interop/interop_server.rb index 0819ba9bbcc..72570d92f3c 100755 --- a/src/ruby/bin/interop/interop_server.rb +++ b/src/ruby/bin/interop/interop_server.rb @@ -185,7 +185,7 @@ def main logger.info("... running insecurely on #{host}") end s.handle(TestTarget) - s.run + s.run_till_terminated end main diff --git a/src/ruby/bin/math_server.rb b/src/ruby/bin/math_server.rb index 5cc76134893..1bfe253b855 100755 --- a/src/ruby/bin/math_server.rb +++ b/src/ruby/bin/math_server.rb @@ -183,7 +183,7 @@ def main end s.handle(Calculator) - s.run + s.run_till_terminated end main diff --git a/src/ruby/bin/noproto_server.rb b/src/ruby/bin/noproto_server.rb index 9979cb7ebbd..f71daeadb37 100755 --- a/src/ruby/bin/noproto_server.rb +++ b/src/ruby/bin/noproto_server.rb @@ -105,7 +105,7 @@ def main end s.handle(NoProto) - s.run + s.run_till_terminated end main From bf6d78c8e4908670de1fe02eaa639dde93166816 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Thu, 16 Apr 2015 14:07:34 -0700 Subject: [PATCH 19/26] Fixes error with the keep-alive --- src/ruby/lib/grpc/generic/rpc_server.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ruby/lib/grpc/generic/rpc_server.rb b/src/ruby/lib/grpc/generic/rpc_server.rb index fa21d770806..bc2211ef7ed 100644 --- a/src/ruby/lib/grpc/generic/rpc_server.rb +++ b/src/ruby/lib/grpc/generic/rpc_server.rb @@ -386,9 +386,8 @@ module GRPC @workers.size.times { schedule { throw :exit } } @stopped = true - keep_alive = 5 @stop_mutex.synchronize do - @stop_cond.wait(@stop_mutex, keep_alive) if @workers.size > 0 + @stop_cond.wait(@stop_mutex, @keep_alive) if @workers.size > 0 end # Forcibly shutdown any threads that are still alive. From 736bd6515c248b3f996edfb0b12dd981b1d3b897 Mon Sep 17 00:00:00 2001 From: Yang Gao Date: Thu, 16 Apr 2015 15:42:42 -0700 Subject: [PATCH 20/26] Avoid generating libs for unknown types. --- BUILD | 23 ----------------------- templates/BUILD.template | 2 +- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/BUILD b/BUILD index e25359d4548..916f24eaf68 100644 --- a/BUILD +++ b/BUILD @@ -765,29 +765,6 @@ cc_library( ) -cc_library( - name = "pubsub_client_lib", - srcs = [ - "examples/pubsub/label.proto", - "examples/pubsub/empty.proto", - "examples/pubsub/pubsub.proto", - "examples/pubsub/publisher.cc", - "examples/pubsub/subscriber.cc", - ], - hdrs = [ - ], - includes = [ - "include", - ".", - ], - deps = [ - ":grpc++", - ":grpc", - ":gpr", - ], -) - - cc_library( name = "grpc_csharp_ext", srcs = [ diff --git a/templates/BUILD.template b/templates/BUILD.template index 8303b9f8e96..fdf87bd29fc 100644 --- a/templates/BUILD.template +++ b/templates/BUILD.template @@ -54,7 +54,7 @@ def get_deps(target_dict): %> % for lib in libs: -% if lib.build != "private": +% if lib.build in ("all", "protoc"): ${cc_library(lib)} % endif % endfor From 1b3e3cd75b8f0e78499d430563619969ca17ef47 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 16 Apr 2015 20:10:29 -0700 Subject: [PATCH 21/26] Moving test.proto to an outer directory so others can depend on it. --- Makefile | 44 +++++++++++++------------- build.json | 4 +-- test/cpp/interop/interop_client.cc | 2 +- test/cpp/interop/server.cc | 2 +- test/{cpp/interop => proto}/test.proto | 0 5 files changed, 26 insertions(+), 26 deletions(-) rename test/{cpp/interop => proto}/test.proto (100%) diff --git a/Makefile b/Makefile index 587f6a46f1e..b05b8e0b4f1 100644 --- a/Makefile +++ b/Makefile @@ -2107,21 +2107,6 @@ $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc: test/cpp/interop/messages.proto $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< endif -ifeq ($(NO_PROTOC),true) -$(GENDIR)/test/cpp/interop/test.pb.cc: protoc_dep_error -$(GENDIR)/test/cpp/interop/test.grpc.pb.cc: protoc_dep_error -else -$(GENDIR)/test/cpp/interop/test.pb.cc: test/cpp/interop/test.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[PROTOC] Generating protobuf CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< - -$(GENDIR)/test/cpp/interop/test.grpc.pb.cc: test/cpp/interop/test.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< -endif - ifeq ($(NO_PROTOC),true) $(GENDIR)/test/cpp/qps/qpstest.pb.cc: protoc_dep_error $(GENDIR)/test/cpp/qps/qpstest.grpc.pb.cc: protoc_dep_error @@ -2182,6 +2167,21 @@ $(GENDIR)/test/cpp/util/messages.grpc.pb.cc: test/cpp/util/messages.proto $(PROT $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< endif +ifeq ($(NO_PROTOC),true) +$(GENDIR)/test/proto/test.pb.cc: protoc_dep_error +$(GENDIR)/test/proto/test.grpc.pb.cc: protoc_dep_error +else +$(GENDIR)/test/proto/test.pb.cc: test/proto/test.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[PROTOC] Generating protobuf CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< + +$(GENDIR)/test/proto/test.grpc.pb.cc: test/proto/test.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< +endif + $(OBJDIR)/$(CONFIG)/%.o : %.c $(E) "[C] Compiling $<" @@ -3900,7 +3900,7 @@ $(OBJDIR)/$(CONFIG)/test/cpp/interop/client_helper.o: LIBINTEROP_CLIENT_MAIN_SRC = \ $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc \ $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc \ - $(GENDIR)/test/cpp/interop/test.pb.cc $(GENDIR)/test/cpp/interop/test.grpc.pb.cc \ + $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc \ test/cpp/interop/client.cc \ test/cpp/interop/interop_client.cc \ @@ -3931,7 +3931,7 @@ ifneq ($(OPENSSL_DEP),) # otherwise parallel compilation will fail if a source is compiled first. test/cpp/interop/empty.proto: $(OPENSSL_DEP) test/cpp/interop/messages.proto: $(OPENSSL_DEP) -test/cpp/interop/test.proto: $(OPENSSL_DEP) +test/proto/test.proto: $(OPENSSL_DEP) test/cpp/interop/client.cc: $(OPENSSL_DEP) test/cpp/interop/interop_client.cc: $(OPENSSL_DEP) endif @@ -3961,8 +3961,8 @@ endif -$(OBJDIR)/$(CONFIG)/test/cpp/interop/client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/cpp/interop/test.pb.cc $(GENDIR)/test/cpp/interop/test.grpc.pb.cc -$(OBJDIR)/$(CONFIG)/test/cpp/interop/interop_client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/cpp/interop/test.pb.cc $(GENDIR)/test/cpp/interop/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/interop_client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc LIBINTEROP_SERVER_HELPER_SRC = \ @@ -4024,7 +4024,7 @@ $(OBJDIR)/$(CONFIG)/test/cpp/interop/server_helper.o: LIBINTEROP_SERVER_MAIN_SRC = \ $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc \ $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc \ - $(GENDIR)/test/cpp/interop/test.pb.cc $(GENDIR)/test/cpp/interop/test.grpc.pb.cc \ + $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc \ test/cpp/interop/server.cc \ @@ -4054,7 +4054,7 @@ ifneq ($(OPENSSL_DEP),) # otherwise parallel compilation will fail if a source is compiled first. test/cpp/interop/empty.proto: $(OPENSSL_DEP) test/cpp/interop/messages.proto: $(OPENSSL_DEP) -test/cpp/interop/test.proto: $(OPENSSL_DEP) +test/proto/test.proto: $(OPENSSL_DEP) test/cpp/interop/server.cc: $(OPENSSL_DEP) endif @@ -4083,7 +4083,7 @@ endif -$(OBJDIR)/$(CONFIG)/test/cpp/interop/server.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/cpp/interop/test.pb.cc $(GENDIR)/test/cpp/interop/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/server.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc LIBPUBSUB_CLIENT_LIB_SRC = \ diff --git a/build.json b/build.json index b885d2eb17e..d0836113396 100644 --- a/build.json +++ b/build.json @@ -580,7 +580,7 @@ "src": [ "test/cpp/interop/empty.proto", "test/cpp/interop/messages.proto", - "test/cpp/interop/test.proto", + "test/proto/test.proto", "test/cpp/interop/client.cc", "test/cpp/interop/interop_client.cc" ], @@ -615,7 +615,7 @@ "src": [ "test/cpp/interop/empty.proto", "test/cpp/interop/messages.proto", - "test/cpp/interop/test.proto", + "test/proto/test.proto", "test/cpp/interop/server.cc" ], "deps": [ diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc index fd9c2e024ab..33053edc97c 100644 --- a/test/cpp/interop/interop_client.cc +++ b/test/cpp/interop/interop_client.cc @@ -43,7 +43,7 @@ #include #include #include -#include "test/cpp/interop/test.grpc.pb.h" +#include "test/proto/test.grpc.pb.h" #include "test/cpp/interop/empty.grpc.pb.h" #include "test/cpp/interop/messages.grpc.pb.h" diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index 94e266fcbd3..d48e9dcfa50 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -48,7 +48,7 @@ #include #include #include -#include "test/cpp/interop/test.grpc.pb.h" +#include "test/proto/test.grpc.pb.h" #include "test/cpp/interop/empty.grpc.pb.h" #include "test/cpp/interop/messages.grpc.pb.h" #include "test/cpp/interop/server_helper.h" diff --git a/test/cpp/interop/test.proto b/test/proto/test.proto similarity index 100% rename from test/cpp/interop/test.proto rename to test/proto/test.proto From 60572d422be5ae6f31c004792b33e1b3069fd697 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 16 Apr 2015 20:45:25 -0700 Subject: [PATCH 22/26] Move .proto files up for reuse --- Makefile | 82 +++++++++++----------- build.json | 8 +-- test/cpp/interop/interop_client.cc | 4 +- test/cpp/interop/interop_client.h | 2 +- test/cpp/interop/server.cc | 4 +- test/{cpp/interop => proto}/empty.proto | 0 test/{cpp/interop => proto}/messages.proto | 0 test/proto/test.proto | 5 +- 8 files changed, 53 insertions(+), 52 deletions(-) rename test/{cpp/interop => proto}/empty.proto (100%) rename test/{cpp/interop => proto}/messages.proto (100%) diff --git a/Makefile b/Makefile index b05b8e0b4f1..e3204fc275d 100644 --- a/Makefile +++ b/Makefile @@ -2077,36 +2077,6 @@ $(GENDIR)/examples/pubsub/pubsub.grpc.pb.cc: examples/pubsub/pubsub.proto $(PROT $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< endif -ifeq ($(NO_PROTOC),true) -$(GENDIR)/test/cpp/interop/empty.pb.cc: protoc_dep_error -$(GENDIR)/test/cpp/interop/empty.grpc.pb.cc: protoc_dep_error -else -$(GENDIR)/test/cpp/interop/empty.pb.cc: test/cpp/interop/empty.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[PROTOC] Generating protobuf CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< - -$(GENDIR)/test/cpp/interop/empty.grpc.pb.cc: test/cpp/interop/empty.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< -endif - -ifeq ($(NO_PROTOC),true) -$(GENDIR)/test/cpp/interop/messages.pb.cc: protoc_dep_error -$(GENDIR)/test/cpp/interop/messages.grpc.pb.cc: protoc_dep_error -else -$(GENDIR)/test/cpp/interop/messages.pb.cc: test/cpp/interop/messages.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[PROTOC] Generating protobuf CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< - -$(GENDIR)/test/cpp/interop/messages.grpc.pb.cc: test/cpp/interop/messages.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) - $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" - $(Q) mkdir -p `dirname $@` - $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< -endif - ifeq ($(NO_PROTOC),true) $(GENDIR)/test/cpp/qps/qpstest.pb.cc: protoc_dep_error $(GENDIR)/test/cpp/qps/qpstest.grpc.pb.cc: protoc_dep_error @@ -2167,6 +2137,36 @@ $(GENDIR)/test/cpp/util/messages.grpc.pb.cc: test/cpp/util/messages.proto $(PROT $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< endif +ifeq ($(NO_PROTOC),true) +$(GENDIR)/test/proto/empty.pb.cc: protoc_dep_error +$(GENDIR)/test/proto/empty.grpc.pb.cc: protoc_dep_error +else +$(GENDIR)/test/proto/empty.pb.cc: test/proto/empty.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[PROTOC] Generating protobuf CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< + +$(GENDIR)/test/proto/empty.grpc.pb.cc: test/proto/empty.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< +endif + +ifeq ($(NO_PROTOC),true) +$(GENDIR)/test/proto/messages.pb.cc: protoc_dep_error +$(GENDIR)/test/proto/messages.grpc.pb.cc: protoc_dep_error +else +$(GENDIR)/test/proto/messages.pb.cc: test/proto/messages.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[PROTOC] Generating protobuf CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --cpp_out=$(GENDIR) $< + +$(GENDIR)/test/proto/messages.grpc.pb.cc: test/proto/messages.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS) + $(E) "[GRPC] Generating gRPC's protobuf service CC file from $<" + $(Q) mkdir -p `dirname $@` + $(Q) $(PROTOC) --grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(BINDIR)/$(CONFIG)/grpc_cpp_plugin $< +endif + ifeq ($(NO_PROTOC),true) $(GENDIR)/test/proto/test.pb.cc: protoc_dep_error $(GENDIR)/test/proto/test.grpc.pb.cc: protoc_dep_error @@ -3898,8 +3898,8 @@ $(OBJDIR)/$(CONFIG)/test/cpp/interop/client_helper.o: LIBINTEROP_CLIENT_MAIN_SRC = \ - $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc \ - $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc \ + $(GENDIR)/test/proto/empty.pb.cc $(GENDIR)/test/proto/empty.grpc.pb.cc \ + $(GENDIR)/test/proto/messages.pb.cc $(GENDIR)/test/proto/messages.grpc.pb.cc \ $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc \ test/cpp/interop/client.cc \ test/cpp/interop/interop_client.cc \ @@ -3929,8 +3929,8 @@ ifneq ($(OPENSSL_DEP),) # This is to ensure the embedded OpenSSL is built beforehand, properly # installing headers to their final destination on the drive. We need this # otherwise parallel compilation will fail if a source is compiled first. -test/cpp/interop/empty.proto: $(OPENSSL_DEP) -test/cpp/interop/messages.proto: $(OPENSSL_DEP) +test/proto/empty.proto: $(OPENSSL_DEP) +test/proto/messages.proto: $(OPENSSL_DEP) test/proto/test.proto: $(OPENSSL_DEP) test/cpp/interop/client.cc: $(OPENSSL_DEP) test/cpp/interop/interop_client.cc: $(OPENSSL_DEP) @@ -3961,8 +3961,8 @@ endif -$(OBJDIR)/$(CONFIG)/test/cpp/interop/client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc -$(OBJDIR)/$(CONFIG)/test/cpp/interop/interop_client.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/client.o: $(GENDIR)/test/proto/empty.pb.cc $(GENDIR)/test/proto/empty.grpc.pb.cc $(GENDIR)/test/proto/messages.pb.cc $(GENDIR)/test/proto/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/interop_client.o: $(GENDIR)/test/proto/empty.pb.cc $(GENDIR)/test/proto/empty.grpc.pb.cc $(GENDIR)/test/proto/messages.pb.cc $(GENDIR)/test/proto/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc LIBINTEROP_SERVER_HELPER_SRC = \ @@ -4022,8 +4022,8 @@ $(OBJDIR)/$(CONFIG)/test/cpp/interop/server_helper.o: LIBINTEROP_SERVER_MAIN_SRC = \ - $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc \ - $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc \ + $(GENDIR)/test/proto/empty.pb.cc $(GENDIR)/test/proto/empty.grpc.pb.cc \ + $(GENDIR)/test/proto/messages.pb.cc $(GENDIR)/test/proto/messages.grpc.pb.cc \ $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc \ test/cpp/interop/server.cc \ @@ -4052,8 +4052,8 @@ ifneq ($(OPENSSL_DEP),) # This is to ensure the embedded OpenSSL is built beforehand, properly # installing headers to their final destination on the drive. We need this # otherwise parallel compilation will fail if a source is compiled first. -test/cpp/interop/empty.proto: $(OPENSSL_DEP) -test/cpp/interop/messages.proto: $(OPENSSL_DEP) +test/proto/empty.proto: $(OPENSSL_DEP) +test/proto/messages.proto: $(OPENSSL_DEP) test/proto/test.proto: $(OPENSSL_DEP) test/cpp/interop/server.cc: $(OPENSSL_DEP) endif @@ -4083,7 +4083,7 @@ endif -$(OBJDIR)/$(CONFIG)/test/cpp/interop/server.o: $(GENDIR)/test/cpp/interop/empty.pb.cc $(GENDIR)/test/cpp/interop/empty.grpc.pb.cc $(GENDIR)/test/cpp/interop/messages.pb.cc $(GENDIR)/test/cpp/interop/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc +$(OBJDIR)/$(CONFIG)/test/cpp/interop/server.o: $(GENDIR)/test/proto/empty.pb.cc $(GENDIR)/test/proto/empty.grpc.pb.cc $(GENDIR)/test/proto/messages.pb.cc $(GENDIR)/test/proto/messages.grpc.pb.cc $(GENDIR)/test/proto/test.pb.cc $(GENDIR)/test/proto/test.grpc.pb.cc LIBPUBSUB_CLIENT_LIB_SRC = \ diff --git a/build.json b/build.json index d0836113396..58837e2c204 100644 --- a/build.json +++ b/build.json @@ -578,8 +578,8 @@ "build": "private", "language": "c++", "src": [ - "test/cpp/interop/empty.proto", - "test/cpp/interop/messages.proto", + "test/proto/empty.proto", + "test/proto/messages.proto", "test/proto/test.proto", "test/cpp/interop/client.cc", "test/cpp/interop/interop_client.cc" @@ -613,8 +613,8 @@ "build": "private", "language": "c++", "src": [ - "test/cpp/interop/empty.proto", - "test/cpp/interop/messages.proto", + "test/proto/empty.proto", + "test/proto/messages.proto", "test/proto/test.proto", "test/cpp/interop/server.cc" ], diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc index 33053edc97c..7f5757d2ba8 100644 --- a/test/cpp/interop/interop_client.cc +++ b/test/cpp/interop/interop_client.cc @@ -44,8 +44,8 @@ #include #include #include "test/proto/test.grpc.pb.h" -#include "test/cpp/interop/empty.grpc.pb.h" -#include "test/cpp/interop/messages.grpc.pb.h" +#include "test/proto/empty.grpc.pb.h" +#include "test/proto/messages.grpc.pb.h" namespace grpc { namespace testing { diff --git a/test/cpp/interop/interop_client.h b/test/cpp/interop/interop_client.h index b0ab320f8dc..3161f7f8756 100644 --- a/test/cpp/interop/interop_client.h +++ b/test/cpp/interop/interop_client.h @@ -38,7 +38,7 @@ #include #include #include -#include "test/cpp/interop/messages.grpc.pb.h" +#include "test/proto/messages.grpc.pb.h" namespace grpc { namespace testing { diff --git a/test/cpp/interop/server.cc b/test/cpp/interop/server.cc index d48e9dcfa50..7888102837a 100644 --- a/test/cpp/interop/server.cc +++ b/test/cpp/interop/server.cc @@ -49,8 +49,8 @@ #include #include #include "test/proto/test.grpc.pb.h" -#include "test/cpp/interop/empty.grpc.pb.h" -#include "test/cpp/interop/messages.grpc.pb.h" +#include "test/proto/empty.grpc.pb.h" +#include "test/proto/messages.grpc.pb.h" #include "test/cpp/interop/server_helper.h" #include "test/cpp/util/test_config.h" diff --git a/test/cpp/interop/empty.proto b/test/proto/empty.proto similarity index 100% rename from test/cpp/interop/empty.proto rename to test/proto/empty.proto diff --git a/test/cpp/interop/messages.proto b/test/proto/messages.proto similarity index 100% rename from test/cpp/interop/messages.proto rename to test/proto/messages.proto diff --git a/test/proto/test.proto b/test/proto/test.proto index b55780e5cde..b9483d84378 100644 --- a/test/proto/test.proto +++ b/test/proto/test.proto @@ -30,10 +30,11 @@ // An integration test service that covers all the method signature permutations // of unary/streaming requests/responses. + syntax = "proto2"; -import "test/cpp/interop/empty.proto"; -import "test/cpp/interop/messages.proto"; +import "test/proto/empty.proto"; +import "test/proto/messages.proto"; package grpc.testing; From 1cc29c1e1dd25797b6ffa9d614f8ac5663403a50 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sat, 18 Apr 2015 00:20:22 +0200 Subject: [PATCH 23/26] Fixing dependency... --- build.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.json b/build.json index 58837e2c204..b141efcb80c 100644 --- a/build.json +++ b/build.json @@ -665,6 +665,9 @@ "test/cpp/qps/server_async.cc", "test/cpp/qps/server_sync.cc", "test/cpp/qps/timer.cc" + ], + "deps": [ + "grpc_test_util" ] }, { From 154db73ff9b8bccbe0d204910a470578d20d72c3 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Fri, 17 Apr 2015 19:40:01 -0700 Subject: [PATCH 24/26] Remove usage of xray - xray was useful during dev testing but is neither a dev or runtime dep - its presence causes issues for libraries that handle signals themselves Also - updates grpc.gemspec with requirements --- src/ruby/grpc.gemspec | 6 ++++-- src/ruby/lib/grpc/generic/client_stub.rb | 1 - src/ruby/lib/grpc/generic/rpc_server.rb | 1 - src/ruby/spec/generic/client_stub_spec.rb | 1 - src/ruby/spec/generic/rpc_server_pool_spec.rb | 1 - src/ruby/spec/generic/rpc_server_spec.rb | 1 - 6 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/ruby/grpc.gemspec b/src/ruby/grpc.gemspec index 12d4ab17f24..ae48213c8f4 100755 --- a/src/ruby/grpc.gemspec +++ b/src/ruby/grpc.gemspec @@ -13,6 +13,9 @@ Gem::Specification.new do |s| s.description = 'Send RPCs from Ruby using GRPC' s.license = 'BSD-3-Clause' + s.required_ruby_version = '>= 2.0.0' + s.requirements << 'libgrpc ~> 0.6.0 needs to be installed' + s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- spec/*`.split("\n") s.executables = `git ls-files -- bin/*.rb`.split("\n").map do |f| @@ -25,13 +28,12 @@ Gem::Specification.new do |s| s.add_dependency 'googleauth', '~> 0.4' # reqd for interop tests s.add_dependency 'logging', '~> 1.8' s.add_dependency 'minitest', '~> 5.4' # reqd for interop tests - s.add_dependency 'xray', '~> 1.1' s.add_development_dependency 'bundler', '~> 1.9' s.add_development_dependency 'rake', '~> 10.4' s.add_development_dependency 'rake-compiler', '~> 0.9' - s.add_development_dependency 'rubocop', '~> 0.30' s.add_development_dependency 'rspec', '~> 3.2' + s.add_development_dependency 'rubocop', '~> 0.30' s.extensions = %w(ext/grpc/extconf.rb) end diff --git a/src/ruby/lib/grpc/generic/client_stub.rb b/src/ruby/lib/grpc/generic/client_stub.rb index dc7672d3595..7b2c04aa226 100644 --- a/src/ruby/lib/grpc/generic/client_stub.rb +++ b/src/ruby/lib/grpc/generic/client_stub.rb @@ -28,7 +28,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'grpc/generic/active_call' -require 'xray/thread_dump_signal_handler' # GRPC contains the General RPC module. module GRPC diff --git a/src/ruby/lib/grpc/generic/rpc_server.rb b/src/ruby/lib/grpc/generic/rpc_server.rb index bc2211ef7ed..c7c8267fa36 100644 --- a/src/ruby/lib/grpc/generic/rpc_server.rb +++ b/src/ruby/lib/grpc/generic/rpc_server.rb @@ -31,7 +31,6 @@ require 'grpc/grpc' require 'grpc/generic/active_call' require 'grpc/generic/service' require 'thread' -require 'xray/thread_dump_signal_handler' # A global that contains signals the gRPC servers should respond to. $grpc_signals = [] diff --git a/src/ruby/spec/generic/client_stub_spec.rb b/src/ruby/spec/generic/client_stub_spec.rb index 193c5f2a038..88c6b44c229 100644 --- a/src/ruby/spec/generic/client_stub_spec.rb +++ b/src/ruby/spec/generic/client_stub_spec.rb @@ -28,7 +28,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'grpc' -require 'xray/thread_dump_signal_handler' # Notifier is useful high-level synchronization primitive. class Notifier diff --git a/src/ruby/spec/generic/rpc_server_pool_spec.rb b/src/ruby/spec/generic/rpc_server_pool_spec.rb index 8383dc1533e..3bbc71b822f 100644 --- a/src/ruby/spec/generic/rpc_server_pool_spec.rb +++ b/src/ruby/spec/generic/rpc_server_pool_spec.rb @@ -28,7 +28,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'grpc' -require 'xray/thread_dump_signal_handler' Pool = GRPC::RpcServer::Pool diff --git a/src/ruby/spec/generic/rpc_server_spec.rb b/src/ruby/spec/generic/rpc_server_spec.rb index 1323bacfa60..e091c589f51 100644 --- a/src/ruby/spec/generic/rpc_server_spec.rb +++ b/src/ruby/spec/generic/rpc_server_spec.rb @@ -28,7 +28,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'grpc' -require 'xray/thread_dump_signal_handler' def load_test_certs test_root = File.join(File.dirname(File.dirname(__FILE__)), 'testdata') From 7d1d9ca7faf6e5cd37df572ab98ff500c07297b9 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Fri, 17 Apr 2015 14:38:48 -0700 Subject: [PATCH 25/26] Refactoring of core security to remove the factories. - Renamed security_context -> security_connector. - Credentials are now responsible for creating their own connectors. --- BUILD | 9 +- Makefile | 15 +- build.json | 9 +- src/core/httpcli/httpcli.c | 13 +- ...context.c => httpcli_security_connector.c} | 42 +-- ...context.h => httpcli_security_connector.h} | 12 +- src/core/security/auth.c | 30 +- src/core/security/credentials.c | 191 ++++++++--- src/core/security/credentials.h | 46 +-- src/core/security/factories.c | 68 ---- src/core/security/secure_transport_setup.c | 14 +- src/core/security/secure_transport_setup.h | 4 +- ...ecurity_context.c => security_connector.c} | 300 ++++++------------ src/core/security/security_connector.h | 201 ++++++++++++ src/core/security/security_context.h | 214 ------------- src/core/security/server_secure_chttp2.c | 19 +- src/core/surface/secure_channel_create.c | 43 ++- .../end2end/fixtures/chttp2_fake_security.c | 1 - .../fixtures/chttp2_simple_ssl_fullstack.c | 1 - .../chttp2_simple_ssl_with_oauth2_fullstack.c | 1 - test/core/security/credentials_test.c | 13 + vsprojects/vs2010/grpc.vcxproj | 10 +- vsprojects/vs2010/grpc.vcxproj.filters | 11 +- vsprojects/vs2013/grpc.vcxproj | 10 +- vsprojects/vs2013/grpc.vcxproj.filters | 11 +- 25 files changed, 620 insertions(+), 668 deletions(-) rename src/core/httpcli/{httpcli_security_context.c => httpcli_security_connector.c} (79%) rename src/core/httpcli/{httpcli_security_context.h => httpcli_security_connector.h} (80%) delete mode 100644 src/core/security/factories.c rename src/core/security/{security_context.c => security_connector.c} (65%) create mode 100644 src/core/security/security_connector.h delete mode 100644 src/core/security/security_context.h diff --git a/BUILD b/BUILD index 916f24eaf68..8bebe1bded3 100644 --- a/BUILD +++ b/BUILD @@ -124,7 +124,7 @@ cc_library( srcs = [ "src/core/httpcli/format_request.h", "src/core/httpcli/httpcli.h", - "src/core/httpcli/httpcli_security_context.h", + "src/core/httpcli/httpcli_security_connector.h", "src/core/httpcli/parser.h", "src/core/security/auth.h", "src/core/security/base64.h", @@ -132,7 +132,7 @@ cc_library( "src/core/security/json_token.h", "src/core/security/secure_endpoint.h", "src/core/security/secure_transport_setup.h", - "src/core/security/security_context.h", + "src/core/security/security_connector.h", "src/core/tsi/fake_transport_security.h", "src/core/tsi/ssl_transport_security.h", "src/core/tsi/transport_security.h", @@ -228,19 +228,18 @@ cc_library( "src/core/transport/transport_impl.h", "src/core/httpcli/format_request.c", "src/core/httpcli/httpcli.c", - "src/core/httpcli/httpcli_security_context.c", + "src/core/httpcli/httpcli_security_connector.c", "src/core/httpcli/parser.c", "src/core/security/auth.c", "src/core/security/base64.c", "src/core/security/credentials.c", "src/core/security/credentials_posix.c", "src/core/security/credentials_win32.c", - "src/core/security/factories.c", "src/core/security/google_default_credentials.c", "src/core/security/json_token.c", "src/core/security/secure_endpoint.c", "src/core/security/secure_transport_setup.c", - "src/core/security/security_context.c", + "src/core/security/security_connector.c", "src/core/security/server_secure_chttp2.c", "src/core/surface/init_secure.c", "src/core/surface/secure_channel_create.c", diff --git a/Makefile b/Makefile index e3204fc275d..4a8ab16cfd8 100644 --- a/Makefile +++ b/Makefile @@ -2591,19 +2591,18 @@ $(OBJDIR)/$(CONFIG)/test/core/util/test_config.o: LIBGRPC_SRC = \ src/core/httpcli/format_request.c \ src/core/httpcli/httpcli.c \ - src/core/httpcli/httpcli_security_context.c \ + src/core/httpcli/httpcli_security_connector.c \ src/core/httpcli/parser.c \ src/core/security/auth.c \ src/core/security/base64.c \ src/core/security/credentials.c \ src/core/security/credentials_posix.c \ src/core/security/credentials_win32.c \ - src/core/security/factories.c \ src/core/security/google_default_credentials.c \ src/core/security/json_token.c \ src/core/security/secure_endpoint.c \ src/core/security/secure_transport_setup.c \ - src/core/security/security_context.c \ + src/core/security/security_connector.c \ src/core/security/server_secure_chttp2.c \ src/core/surface/init_secure.c \ src/core/surface/secure_channel_create.c \ @@ -2740,19 +2739,18 @@ ifneq ($(OPENSSL_DEP),) # otherwise parallel compilation will fail if a source is compiled first. src/core/httpcli/format_request.c: $(OPENSSL_DEP) src/core/httpcli/httpcli.c: $(OPENSSL_DEP) -src/core/httpcli/httpcli_security_context.c: $(OPENSSL_DEP) +src/core/httpcli/httpcli_security_connector.c: $(OPENSSL_DEP) src/core/httpcli/parser.c: $(OPENSSL_DEP) src/core/security/auth.c: $(OPENSSL_DEP) src/core/security/base64.c: $(OPENSSL_DEP) src/core/security/credentials.c: $(OPENSSL_DEP) src/core/security/credentials_posix.c: $(OPENSSL_DEP) src/core/security/credentials_win32.c: $(OPENSSL_DEP) -src/core/security/factories.c: $(OPENSSL_DEP) src/core/security/google_default_credentials.c: $(OPENSSL_DEP) src/core/security/json_token.c: $(OPENSSL_DEP) src/core/security/secure_endpoint.c: $(OPENSSL_DEP) src/core/security/secure_transport_setup.c: $(OPENSSL_DEP) -src/core/security/security_context.c: $(OPENSSL_DEP) +src/core/security/security_connector.c: $(OPENSSL_DEP) src/core/security/server_secure_chttp2.c: $(OPENSSL_DEP) src/core/surface/init_secure.c: $(OPENSSL_DEP) src/core/surface/secure_channel_create.c: $(OPENSSL_DEP) @@ -2905,19 +2903,18 @@ endif $(OBJDIR)/$(CONFIG)/src/core/httpcli/format_request.o: $(OBJDIR)/$(CONFIG)/src/core/httpcli/httpcli.o: -$(OBJDIR)/$(CONFIG)/src/core/httpcli/httpcli_security_context.o: +$(OBJDIR)/$(CONFIG)/src/core/httpcli/httpcli_security_connector.o: $(OBJDIR)/$(CONFIG)/src/core/httpcli/parser.o: $(OBJDIR)/$(CONFIG)/src/core/security/auth.o: $(OBJDIR)/$(CONFIG)/src/core/security/base64.o: $(OBJDIR)/$(CONFIG)/src/core/security/credentials.o: $(OBJDIR)/$(CONFIG)/src/core/security/credentials_posix.o: $(OBJDIR)/$(CONFIG)/src/core/security/credentials_win32.o: -$(OBJDIR)/$(CONFIG)/src/core/security/factories.o: $(OBJDIR)/$(CONFIG)/src/core/security/google_default_credentials.o: $(OBJDIR)/$(CONFIG)/src/core/security/json_token.o: $(OBJDIR)/$(CONFIG)/src/core/security/secure_endpoint.o: $(OBJDIR)/$(CONFIG)/src/core/security/secure_transport_setup.o: -$(OBJDIR)/$(CONFIG)/src/core/security/security_context.o: +$(OBJDIR)/$(CONFIG)/src/core/security/security_connector.o: $(OBJDIR)/$(CONFIG)/src/core/security/server_secure_chttp2.o: $(OBJDIR)/$(CONFIG)/src/core/surface/init_secure.o: $(OBJDIR)/$(CONFIG)/src/core/surface/secure_channel_create.o: diff --git a/build.json b/build.json index 58837e2c204..e74fa649d14 100644 --- a/build.json +++ b/build.json @@ -386,7 +386,7 @@ "headers": [ "src/core/httpcli/format_request.h", "src/core/httpcli/httpcli.h", - "src/core/httpcli/httpcli_security_context.h", + "src/core/httpcli/httpcli_security_connector.h", "src/core/httpcli/parser.h", "src/core/security/auth.h", "src/core/security/base64.h", @@ -394,7 +394,7 @@ "src/core/security/json_token.h", "src/core/security/secure_endpoint.h", "src/core/security/secure_transport_setup.h", - "src/core/security/security_context.h", + "src/core/security/security_connector.h", "src/core/tsi/fake_transport_security.h", "src/core/tsi/ssl_transport_security.h", "src/core/tsi/transport_security.h", @@ -403,19 +403,18 @@ "src": [ "src/core/httpcli/format_request.c", "src/core/httpcli/httpcli.c", - "src/core/httpcli/httpcli_security_context.c", + "src/core/httpcli/httpcli_security_connector.c", "src/core/httpcli/parser.c", "src/core/security/auth.c", "src/core/security/base64.c", "src/core/security/credentials.c", "src/core/security/credentials_posix.c", "src/core/security/credentials_win32.c", - "src/core/security/factories.c", "src/core/security/google_default_credentials.c", "src/core/security/json_token.c", "src/core/security/secure_endpoint.c", "src/core/security/secure_transport_setup.c", - "src/core/security/security_context.c", + "src/core/security/security_connector.c", "src/core/security/server_secure_chttp2.c", "src/core/surface/init_secure.c", "src/core/surface/secure_channel_create.c", diff --git a/src/core/httpcli/httpcli.c b/src/core/httpcli/httpcli.c index d2cf09a8df4..fe7ea6a86b3 100644 --- a/src/core/httpcli/httpcli.c +++ b/src/core/httpcli/httpcli.c @@ -40,9 +40,8 @@ #include "src/core/iomgr/resolve_address.h" #include "src/core/iomgr/tcp_client.h" #include "src/core/httpcli/format_request.h" -#include "src/core/httpcli/httpcli_security_context.h" +#include "src/core/httpcli/httpcli_security_connector.h" #include "src/core/httpcli/parser.h" -#include "src/core/security/security_context.h" #include "src/core/security/secure_transport_setup.h" #include "src/core/support/string.h" #include @@ -180,7 +179,7 @@ static void on_connected(void *arg, grpc_endpoint *tcp) { } req->ep = tcp; if (req->use_ssl) { - grpc_channel_security_context *ctx = NULL; + grpc_channel_security_connector *sc = NULL; const unsigned char *pem_root_certs = NULL; size_t pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs); if (pem_root_certs == NULL || pem_root_certs_size == 0) { @@ -188,12 +187,12 @@ static void on_connected(void *arg, grpc_endpoint *tcp) { finish(req, 0); return; } - GPR_ASSERT(grpc_httpcli_ssl_channel_security_context_create( - pem_root_certs, pem_root_certs_size, req->host, &ctx) == + GPR_ASSERT(grpc_httpcli_ssl_channel_security_connector_create( + pem_root_certs, pem_root_certs_size, req->host, &sc) == GRPC_SECURITY_OK); - grpc_setup_secure_transport(&ctx->base, tcp, on_secure_transport_setup_done, + grpc_setup_secure_transport(&sc->base, tcp, on_secure_transport_setup_done, req); - grpc_security_context_unref(&ctx->base); + grpc_security_connector_unref(&sc->base); } else { start_write(req); } diff --git a/src/core/httpcli/httpcli_security_context.c b/src/core/httpcli/httpcli_security_connector.c similarity index 79% rename from src/core/httpcli/httpcli_security_context.c rename to src/core/httpcli/httpcli_security_connector.c index e97752bbe19..6eed5eaf125 100644 --- a/src/core/httpcli/httpcli_security_context.c +++ b/src/core/httpcli/httpcli_security_connector.c @@ -31,7 +31,7 @@ * */ -#include "src/core/httpcli/httpcli_security_context.h" +#include "src/core/httpcli/httpcli_security_connector.h" #include @@ -42,25 +42,25 @@ #include "src/core/tsi/ssl_transport_security.h" typedef struct { - grpc_channel_security_context base; + grpc_channel_security_connector base; tsi_ssl_handshaker_factory *handshaker_factory; char *secure_peer_name; -} grpc_httpcli_ssl_channel_security_context; +} grpc_httpcli_ssl_channel_security_connector; -static void httpcli_ssl_destroy(grpc_security_context *ctx) { - grpc_httpcli_ssl_channel_security_context *c = - (grpc_httpcli_ssl_channel_security_context *)ctx; +static void httpcli_ssl_destroy(grpc_security_connector *sc) { + grpc_httpcli_ssl_channel_security_connector *c = + (grpc_httpcli_ssl_channel_security_connector *)sc; if (c->handshaker_factory != NULL) { tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); } if (c->secure_peer_name != NULL) gpr_free(c->secure_peer_name); - gpr_free(ctx); + gpr_free(sc); } static grpc_security_status httpcli_ssl_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { - grpc_httpcli_ssl_channel_security_context *c = - (grpc_httpcli_ssl_channel_security_context *)ctx; + grpc_security_connector *sc, tsi_handshaker **handshaker) { + grpc_httpcli_ssl_channel_security_connector *c = + (grpc_httpcli_ssl_channel_security_connector *)sc; tsi_result result = TSI_OK; if (c->handshaker_factory == NULL) return GRPC_SECURITY_ERROR; result = tsi_ssl_handshaker_factory_create_handshaker( @@ -73,12 +73,12 @@ static grpc_security_status httpcli_ssl_create_handshaker( return GRPC_SECURITY_OK; } -static grpc_security_status httpcli_ssl_check_peer(grpc_security_context *ctx, +static grpc_security_status httpcli_ssl_check_peer(grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data) { - grpc_httpcli_ssl_channel_security_context *c = - (grpc_httpcli_ssl_channel_security_context *)ctx; + grpc_httpcli_ssl_channel_security_connector *c = + (grpc_httpcli_ssl_channel_security_connector *)sc; grpc_security_status status = GRPC_SECURITY_OK; /* Check the peer name. */ @@ -92,14 +92,14 @@ static grpc_security_status httpcli_ssl_check_peer(grpc_security_context *ctx, return status; } -static grpc_security_context_vtable httpcli_ssl_vtable = { +static grpc_security_connector_vtable httpcli_ssl_vtable = { httpcli_ssl_destroy, httpcli_ssl_create_handshaker, httpcli_ssl_check_peer}; -grpc_security_status grpc_httpcli_ssl_channel_security_context_create( +grpc_security_status grpc_httpcli_ssl_channel_security_connector_create( const unsigned char *pem_root_certs, size_t pem_root_certs_size, - const char *secure_peer_name, grpc_channel_security_context **ctx) { + const char *secure_peer_name, grpc_channel_security_connector **sc) { tsi_result result = TSI_OK; - grpc_httpcli_ssl_channel_security_context *c; + grpc_httpcli_ssl_channel_security_connector *c; if (secure_peer_name != NULL && pem_root_certs == NULL) { gpr_log(GPR_ERROR, @@ -107,8 +107,8 @@ grpc_security_status grpc_httpcli_ssl_channel_security_context_create( return GRPC_SECURITY_ERROR; } - c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_context)); - memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_context)); + c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_connector)); + memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_connector)); gpr_ref_init(&c->base.base.refcount, 1); c->base.base.is_client_side = 1; @@ -123,9 +123,9 @@ grpc_security_status grpc_httpcli_ssl_channel_security_context_create( gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string(result)); httpcli_ssl_destroy(&c->base.base); - *ctx = NULL; + *sc = NULL; return GRPC_SECURITY_ERROR; } - *ctx = &c->base; + *sc = &c->base; return GRPC_SECURITY_OK; } diff --git a/src/core/httpcli/httpcli_security_context.h b/src/core/httpcli/httpcli_security_connector.h similarity index 80% rename from src/core/httpcli/httpcli_security_context.h rename to src/core/httpcli/httpcli_security_connector.h index a776828a692..c50f25905e6 100644 --- a/src/core/httpcli/httpcli_security_context.h +++ b/src/core/httpcli/httpcli_security_connector.h @@ -31,13 +31,13 @@ * */ -#ifndef GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONTEXT_H -#define GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONTEXT_H +#ifndef GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONNECTOR_H +#define GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONNECTOR_H -#include "src/core/security/security_context.h" +#include "src/core/security/security_connector.h" -grpc_security_status grpc_httpcli_ssl_channel_security_context_create( +grpc_security_status grpc_httpcli_ssl_channel_security_connector_create( const unsigned char *pem_root_certs, size_t pem_root_certs_size, - const char *secure_peer_name, grpc_channel_security_context **ctx); + const char *secure_peer_name, grpc_channel_security_connector **sc); -#endif /* GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONTEXT_H */ +#endif /* GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_SECURITY_CONNECTOR_H */ diff --git a/src/core/security/auth.c b/src/core/security/auth.c index 5fc6d2717f7..130698c11bc 100644 --- a/src/core/security/auth.c +++ b/src/core/security/auth.c @@ -40,7 +40,7 @@ #include "src/core/support/string.h" #include "src/core/channel/channel_stack.h" -#include "src/core/security/security_context.h" +#include "src/core/security/security_connector.h" #include "src/core/security/credentials.h" #include "src/core/surface/call.h" @@ -54,7 +54,7 @@ typedef struct { /* We can have a per-channel credentials. */ typedef struct { - grpc_channel_security_context *security_context; + grpc_channel_security_connector *security_connector; grpc_mdctx *md_ctx; grpc_mdstr *authority_string; grpc_mdstr *path_string; @@ -126,7 +126,7 @@ static void send_security_metadata(grpc_call_element *elem, grpc_call_op *op) { channel_data *channeld = elem->channel_data; grpc_credentials *channel_creds = - channeld->security_context->request_metadata_creds; + channeld->security_connector->request_metadata_creds; /* TODO(jboeuf): Decide on the policy in this case: - populate both channel and call? @@ -138,7 +138,7 @@ static void send_security_metadata(grpc_call_element *elem, grpc_call_op *op) { if (channel_creds != NULL && grpc_credentials_has_request_metadata(channel_creds)) { char *service_url = - build_service_url(channeld->security_context->base.url_scheme, calld); + build_service_url(channeld->security_connector->base.url_scheme, calld); calld->op = *op; /* Copy op (originates from the caller's stack). */ grpc_credentials_get_request_metadata(channel_creds, service_url, on_credentials_metadata, elem); @@ -193,8 +193,8 @@ static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, grpc_security_status status; const char *call_host = grpc_mdstr_as_c_string(calld->host); calld->op = *op; /* Copy op (originates from the caller's stack). */ - status = grpc_channel_security_context_check_call_host( - channeld->security_context, call_host, on_host_checked, elem); + status = grpc_channel_security_connector_check_call_host( + channeld->security_connector, call_host, on_host_checked, elem); if (status != GRPC_SECURITY_OK) { if (status == GRPC_SECURITY_ERROR) { char *error_msg; @@ -255,7 +255,7 @@ static void init_channel_elem(grpc_channel_element *elem, const grpc_channel_args *args, grpc_mdctx *metadata_context, int is_first, int is_last) { - grpc_security_context *ctx = grpc_find_security_context_in_args(args); + grpc_security_connector *ctx = grpc_find_security_connector_in_args(args); /* grab pointers to our data from the channel element */ channel_data *channeld = elem->channel_data; @@ -268,23 +268,24 @@ static void init_channel_elem(grpc_channel_element *elem, /* initialize members */ GPR_ASSERT(ctx->is_client_side); - channeld->security_context = - (grpc_channel_security_context *)grpc_security_context_ref(ctx); + channeld->security_connector = + (grpc_channel_security_connector *)grpc_security_connector_ref(ctx); channeld->md_ctx = metadata_context; channeld->authority_string = grpc_mdstr_from_string(channeld->md_ctx, ":authority"); channeld->path_string = grpc_mdstr_from_string(channeld->md_ctx, ":path"); channeld->error_msg_key = grpc_mdstr_from_string(channeld->md_ctx, "grpc-message"); - channeld->status_key = grpc_mdstr_from_string(channeld->md_ctx, "grpc-status"); + channeld->status_key = + grpc_mdstr_from_string(channeld->md_ctx, "grpc-status"); } /* Destructor for channel data */ static void destroy_channel_elem(grpc_channel_element *elem) { /* grab pointers to our data from the channel element */ channel_data *channeld = elem->channel_data; - grpc_channel_security_context *ctx = channeld->security_context; - if (ctx != NULL) grpc_security_context_unref(&ctx->base); + grpc_channel_security_connector *ctx = channeld->security_connector; + if (ctx != NULL) grpc_security_connector_unref(&ctx->base); if (channeld->authority_string != NULL) { grpc_mdstr_unref(channeld->authority_string); } @@ -300,6 +301,5 @@ static void destroy_channel_elem(grpc_channel_element *elem) { } const grpc_channel_filter grpc_client_auth_filter = { - call_op, channel_op, sizeof(call_data), - init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "auth"}; + call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, + sizeof(channel_data), init_channel_elem, destroy_channel_elem, "auth"}; diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c index e6d2e9e332c..f6366f07505 100644 --- a/src/core/security/credentials.c +++ b/src/core/security/credentials.c @@ -36,11 +36,14 @@ #include #include +#include "src/core/channel/channel_args.h" +#include "src/core/channel/http_client_filter.h" #include "src/core/json/json.h" #include "src/core/httpcli/httpcli.h" #include "src/core/iomgr/iomgr.h" #include "src/core/security/json_token.h" #include "src/core/support/string.h" + #include #include #include @@ -111,9 +114,33 @@ void grpc_credentials_get_request_metadata(grpc_credentials *creds, creds->vtable->get_request_metadata(creds, service_url, cb, user_data); } -grpc_mdctx *grpc_credentials_get_metadata_context(grpc_credentials *creds) { - if (creds == NULL) return NULL; - return creds->vtable->get_metadata_context(creds); +grpc_mdctx *grpc_credentials_get_or_create_metadata_context( + grpc_credentials *creds) { + grpc_mdctx *mdctx = NULL; + if (creds != NULL && creds->vtable->get_metadata_context != NULL) { + mdctx = creds->vtable->get_metadata_context(creds); + } + if (mdctx == NULL) { + return grpc_mdctx_create(); + } else { + grpc_mdctx_ref(mdctx); + return mdctx; + } +} + +grpc_security_status grpc_credentials_create_security_connector( + grpc_credentials *creds, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args) { + *new_args = NULL; + if (creds == NULL || creds->vtable->create_security_connector == NULL || + grpc_credentials_has_request_metadata_only(creds)) { + gpr_log(GPR_ERROR, + "Invalid credentials for creating a security connector."); + return GRPC_SECURITY_ERROR; + } + return creds->vtable->create_security_connector( + creds, target, args, request_metadata_creds, sc, new_args); } void grpc_server_credentials_release(grpc_server_credentials *creds) { @@ -121,6 +148,15 @@ void grpc_server_credentials_release(grpc_server_credentials *creds) { creds->vtable->destroy(creds); } +grpc_security_status grpc_server_credentials_create_security_connector( + grpc_server_credentials *creds, grpc_security_connector **sc) { + if (creds == NULL || creds->vtable->create_security_connector == NULL) { + gpr_log(GPR_ERROR, "Server credentials cannot create security context."); + return GRPC_SECURITY_ERROR; + } + return creds->vtable->create_security_connector(creds, sc); +} + /* -- Ssl credentials. -- */ typedef struct { @@ -176,31 +212,48 @@ static grpc_mdctx *ssl_get_metadata_context(grpc_credentials *creds) { return NULL; } +static grpc_security_status ssl_create_security_connector( + grpc_credentials *creds, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args) { + grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds; + grpc_security_status status = GRPC_SECURITY_OK; + size_t i = 0; + const char *overridden_target_name = NULL; + grpc_arg arg; + + for (i = 0; args && i < args->num_args; i++) { + grpc_arg *arg = &args->args[i]; + if (strcmp(arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 && + arg->type == GRPC_ARG_STRING) { + overridden_target_name = arg->value.string; + break; + } + } + status = grpc_ssl_channel_security_connector_create( + request_metadata_creds, &c->config, target, overridden_target_name, sc); + if (status != GRPC_SECURITY_OK) { + return status; + } + arg.type = GRPC_ARG_STRING; + arg.key = GRPC_ARG_HTTP2_SCHEME; + arg.value.string = "https"; + *new_args = grpc_channel_args_copy_and_add(args, &arg); + return status; +} + +static grpc_security_status ssl_server_create_security_connector( + grpc_server_credentials *creds, grpc_security_connector **sc) { + grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds; + return grpc_ssl_server_security_connector_create(&c->config, sc); +} + static grpc_credentials_vtable ssl_vtable = { ssl_destroy, ssl_has_request_metadata, ssl_has_request_metadata_only, - ssl_get_metadata_context, NULL}; + ssl_get_metadata_context, NULL, ssl_create_security_connector}; -static grpc_server_credentials_vtable ssl_server_vtable = {ssl_server_destroy}; - -const grpc_ssl_config *grpc_ssl_credentials_get_config( - const grpc_credentials *creds) { - if (creds == NULL || strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL)) { - return NULL; - } else { - grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds; - return &c->config; - } -} - -const grpc_ssl_server_config *grpc_ssl_server_credentials_get_config( - const grpc_server_credentials *creds) { - if (creds == NULL || strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL)) { - return NULL; - } else { - grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds; - return &c->config; - } -} +static grpc_server_credentials_vtable ssl_server_vtable = { + ssl_server_destroy, ssl_server_create_security_connector}; static void ssl_copy_key_material(const char *input, unsigned char **output, size_t *output_size) { @@ -388,7 +441,7 @@ static grpc_mdctx *jwt_get_metadata_context(grpc_credentials *creds) { static grpc_credentials_vtable jwt_vtable = { jwt_destroy, jwt_has_request_metadata, jwt_has_request_metadata_only, - jwt_get_metadata_context, jwt_get_request_metadata}; + jwt_get_metadata_context, jwt_get_request_metadata, NULL}; grpc_credentials *grpc_jwt_credentials_create(const char *json_key, gpr_timespec token_lifetime) { @@ -613,7 +666,7 @@ static grpc_credentials_vtable compute_engine_vtable = { oauth2_token_fetcher_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_metadata_context, - oauth2_token_fetcher_get_request_metadata}; + oauth2_token_fetcher_get_request_metadata, NULL}; static void compute_engine_fetch_oauth2( grpc_credentials_metadata_request *metadata_req, @@ -657,7 +710,7 @@ static grpc_credentials_vtable service_account_vtable = { service_account_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_metadata_context, - oauth2_token_fetcher_get_request_metadata}; + oauth2_token_fetcher_get_request_metadata, NULL}; static void service_account_fetch_oauth2( grpc_credentials_metadata_request *metadata_req, @@ -731,7 +784,7 @@ static grpc_credentials_vtable refresh_token_vtable = { refresh_token_destroy, oauth2_token_fetcher_has_request_metadata, oauth2_token_fetcher_has_request_metadata_only, oauth2_token_fetcher_get_metadata_context, - oauth2_token_fetcher_get_request_metadata}; + oauth2_token_fetcher_get_request_metadata, NULL}; static void refresh_token_fetch_oauth2( grpc_credentials_metadata_request *metadata_req, @@ -834,7 +887,7 @@ static grpc_mdctx *fake_oauth2_get_metadata_context(grpc_credentials *creds) { static grpc_credentials_vtable fake_oauth2_vtable = { fake_oauth2_destroy, fake_oauth2_has_request_metadata, fake_oauth2_has_request_metadata_only, fake_oauth2_get_metadata_context, - fake_oauth2_get_request_metadata}; + fake_oauth2_get_request_metadata, NULL}; grpc_credentials *grpc_fake_oauth2_credentials_create( const char *token_md_value, int is_async) { @@ -878,15 +931,33 @@ static grpc_mdctx *fake_transport_security_get_metadata_context( return NULL; } +static grpc_security_status +fake_transport_security_create_security_connector( + grpc_credentials *c, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args) { + *sc = grpc_fake_channel_security_connector_create(request_metadata_creds, 1); + return GRPC_SECURITY_OK; +} + +static grpc_security_status +fake_transport_security_server_create_security_connector( + grpc_server_credentials *c, grpc_security_connector **sc) { + *sc = grpc_fake_server_security_connector_create(); + return GRPC_SECURITY_OK; +} + static grpc_credentials_vtable fake_transport_security_credentials_vtable = { fake_transport_security_credentials_destroy, fake_transport_security_has_request_metadata, fake_transport_security_has_request_metadata_only, - fake_transport_security_get_metadata_context, NULL}; + fake_transport_security_get_metadata_context, NULL, + fake_transport_security_create_security_connector}; static grpc_server_credentials_vtable fake_transport_security_server_credentials_vtable = { - fake_transport_security_server_credentials_destroy}; + fake_transport_security_server_credentials_destroy, + fake_transport_security_server_create_security_connector}; grpc_credentials *grpc_fake_transport_security_credentials_create(void) { grpc_credentials *c = gpr_malloc(sizeof(grpc_credentials)); @@ -911,6 +982,7 @@ grpc_server_credentials *grpc_fake_transport_security_server_credentials_create( typedef struct { grpc_credentials base; grpc_credentials_array inner; + grpc_credentials *connector_creds; } grpc_composite_credentials; typedef struct { @@ -1038,7 +1110,10 @@ static grpc_mdctx *composite_get_metadata_context(grpc_credentials *creds) { size_t i; for (i = 0; i < c->inner.num_creds; i++) { grpc_credentials *inner_creds = c->inner.creds_array[i]; - grpc_mdctx *inner_ctx = grpc_credentials_get_metadata_context(inner_creds); + grpc_mdctx *inner_ctx = NULL; + if (inner_creds->vtable->get_metadata_context != NULL) { + inner_ctx = inner_creds->vtable->get_metadata_context(inner_creds); + } if (inner_ctx) { GPR_ASSERT(ctx == NULL && "can only have one metadata context per composite credential"); @@ -1048,10 +1123,24 @@ static grpc_mdctx *composite_get_metadata_context(grpc_credentials *creds) { return ctx; } +static grpc_security_status composite_create_security_connector( + grpc_credentials *creds, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args) { + grpc_composite_credentials *c = (grpc_composite_credentials *)creds; + if (c->connector_creds == NULL) { + gpr_log(GPR_ERROR, + "Cannot create security connector, missing connector credentials."); + return GRPC_SECURITY_ERROR; + } + return grpc_credentials_create_security_connector(c->connector_creds, target, + args, creds, sc, new_args); +} + static grpc_credentials_vtable composite_credentials_vtable = { composite_destroy, composite_has_request_metadata, composite_has_request_metadata_only, composite_get_metadata_context, - composite_get_request_metadata}; + composite_get_request_metadata, composite_create_security_connector}; static grpc_credentials_array get_creds_array(grpc_credentials **creds_addr) { grpc_credentials_array result; @@ -1067,6 +1156,7 @@ static grpc_credentials_array get_creds_array(grpc_credentials **creds_addr) { grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1, grpc_credentials *creds2) { size_t i; + size_t creds_array_byte_size; grpc_credentials_array creds1_array; grpc_credentials_array creds2_array; grpc_composite_credentials *c; @@ -1080,16 +1170,39 @@ grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1, creds1_array = get_creds_array(&creds1); creds2_array = get_creds_array(&creds2); c->inner.num_creds = creds1_array.num_creds + creds2_array.num_creds; - c->inner.creds_array = - gpr_malloc(c->inner.num_creds * sizeof(grpc_credentials *)); + creds_array_byte_size = c->inner.num_creds * sizeof(grpc_credentials *); + c->inner.creds_array = gpr_malloc(creds_array_byte_size); + memset(c->inner.creds_array, 0, creds_array_byte_size); for (i = 0; i < creds1_array.num_creds; i++) { - c->inner.creds_array[i] = grpc_credentials_ref(creds1_array.creds_array[i]); + grpc_credentials *cur_creds = creds1_array.creds_array[i]; + if (!grpc_credentials_has_request_metadata_only(cur_creds)) { + if (c->connector_creds == NULL) { + c->connector_creds = cur_creds; + } else { + gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials."); + goto fail; + } + } + c->inner.creds_array[i] = grpc_credentials_ref(cur_creds); } for (i = 0; i < creds2_array.num_creds; i++) { + grpc_credentials *cur_creds = creds2_array.creds_array[i]; + if (!grpc_credentials_has_request_metadata_only(cur_creds)) { + if (c->connector_creds == NULL) { + c->connector_creds = cur_creds; + } else { + gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials."); + goto fail; + } + } c->inner.creds_array[i + creds1_array.num_creds] = - grpc_credentials_ref(creds2_array.creds_array[i]); + grpc_credentials_ref(cur_creds); } return &c->base; + +fail: + grpc_credentials_unref(&c->base); + return NULL; } const grpc_credentials_array *grpc_composite_credentials_get_credentials( @@ -1163,7 +1276,7 @@ static grpc_mdctx *iam_get_metadata_context(grpc_credentials *creds) { static grpc_credentials_vtable iam_vtable = { iam_destroy, iam_has_request_metadata, iam_has_request_metadata_only, - iam_get_metadata_context, iam_get_request_metadata}; + iam_get_metadata_context, iam_get_request_metadata, NULL}; grpc_credentials *grpc_iam_credentials_create(const char *token, const char *authority_selector) { diff --git a/src/core/security/credentials.h b/src/core/security/credentials.h index 562b3faa337..87c773e49a1 100644 --- a/src/core/security/credentials.h +++ b/src/core/security/credentials.h @@ -39,6 +39,8 @@ #include #include +#include "src/core/security/security_connector.h" + struct grpc_httpcli_response; /* --- Constants. --- */ @@ -99,6 +101,11 @@ typedef struct { const char *service_url, grpc_credentials_metadata_cb cb, void *user_data); + grpc_security_status (*create_security_connector)( + grpc_credentials *c, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args); + } grpc_credentials_vtable; struct grpc_credentials { @@ -115,19 +122,20 @@ void grpc_credentials_get_request_metadata(grpc_credentials *creds, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data); -grpc_mdctx *grpc_credentials_get_metadata_context(grpc_credentials *creds); -typedef struct { - unsigned char *pem_private_key; - size_t pem_private_key_size; - unsigned char *pem_cert_chain; - size_t pem_cert_chain_size; - unsigned char *pem_root_certs; - size_t pem_root_certs_size; -} grpc_ssl_config; +/* Gets the mdctx from the credentials and increase the refcount if it exists, + otherwise, create a new one. */ +grpc_mdctx *grpc_credentials_get_or_create_metadata_context( + grpc_credentials *creds); -const grpc_ssl_config *grpc_ssl_credentials_get_config( - const grpc_credentials *ssl_creds); +/* Creates a security connector for the channel. May also create new channel + args for the channel to be used in place of the passed in const args if + returned non NULL. In that case the caller is responsible for destroying + new_args after channel creation. */ +grpc_security_status grpc_credentials_create_security_connector( + grpc_credentials *creds, const char *target, const grpc_channel_args *args, + grpc_credentials *request_metadata_creds, + grpc_channel_security_connector **sc, grpc_channel_args **new_args); typedef struct { grpc_credentials **creds_array; @@ -159,6 +167,8 @@ grpc_credentials *grpc_fake_oauth2_credentials_create( typedef struct { void (*destroy)(grpc_server_credentials *c); + grpc_security_status (*create_security_connector)( + grpc_server_credentials *c, grpc_security_connector **sc); } grpc_server_credentials_vtable; struct grpc_server_credentials { @@ -166,17 +176,7 @@ struct grpc_server_credentials { const char *type; }; -typedef struct { - unsigned char **pem_private_keys; - size_t *pem_private_keys_sizes; - unsigned char **pem_cert_chains; - size_t *pem_cert_chains_sizes; - size_t num_key_cert_pairs; - unsigned char *pem_root_certs; - size_t pem_root_certs_size; -} grpc_ssl_server_config; - -const grpc_ssl_server_config *grpc_ssl_server_credentials_get_config( - const grpc_server_credentials *ssl_creds); +grpc_security_status grpc_server_credentials_create_security_connector( + grpc_server_credentials *creds, grpc_security_connector **sc); #endif /* GRPC_INTERNAL_CORE_SECURITY_CREDENTIALS_H */ diff --git a/src/core/security/factories.c b/src/core/security/factories.c deleted file mode 100644 index 3d9216aac40..00000000000 --- a/src/core/security/factories.c +++ /dev/null @@ -1,68 +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. - * - */ - -#include - -#include -#include "src/core/security/credentials.h" -#include "src/core/security/security_context.h" -#include -#include -#include - -grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, - const char *target, - const grpc_channel_args *args) { - grpc_secure_channel_factory factories[] = { - {GRPC_CREDENTIALS_TYPE_SSL, grpc_ssl_channel_create}, - {GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY, - grpc_fake_transport_security_channel_create}}; - return grpc_secure_channel_create_with_factories( - factories, GPR_ARRAY_SIZE(factories), creds, target, args); -} - -grpc_security_status grpc_server_security_context_create( - grpc_server_credentials *creds, grpc_security_context **ctx) { - grpc_security_status status = GRPC_SECURITY_ERROR; - - *ctx = NULL; - if (strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL) == 0) { - status = grpc_ssl_server_security_context_create( - grpc_ssl_server_credentials_get_config(creds), ctx); - } else if (strcmp(creds->type, - GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY) == 0) { - *ctx = grpc_fake_server_security_context_create(); - status = GRPC_SECURITY_OK; - } - return status; -} diff --git a/src/core/security/secure_transport_setup.c b/src/core/security/secure_transport_setup.c index f57d22109ce..3e1db9a12d2 100644 --- a/src/core/security/secure_transport_setup.c +++ b/src/core/security/secure_transport_setup.c @@ -43,7 +43,7 @@ #define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256 typedef struct { - grpc_security_context *ctx; + grpc_security_connector *connector; tsi_handshaker *handshaker; unsigned char *handshake_buffer; size_t handshake_buffer_size; @@ -74,7 +74,7 @@ static void secure_transport_setup_done(grpc_secure_transport_setup *s, if (s->handshaker != NULL) tsi_handshaker_destroy(s->handshaker); if (s->handshake_buffer != NULL) gpr_free(s->handshake_buffer); gpr_slice_buffer_destroy(&s->left_overs); - grpc_security_context_unref(s->ctx); + grpc_security_connector_unref(s->connector); gpr_free(s); } @@ -112,8 +112,8 @@ static void check_peer(grpc_secure_transport_setup *s) { secure_transport_setup_done(s, 0); return; } - peer_status = - grpc_security_context_check_peer(s->ctx, peer, on_peer_checked, s); + peer_status = grpc_security_connector_check_peer(s->connector, peer, + on_peer_checked, s); if (peer_status == GRPC_SECURITY_ERROR) { gpr_log(GPR_ERROR, "Peer check failed."); secure_transport_setup_done(s, 0); @@ -262,7 +262,7 @@ static void on_handshake_data_sent_to_peer(void *setup, } } -void grpc_setup_secure_transport(grpc_security_context *ctx, +void grpc_setup_secure_transport(grpc_security_connector *connector, grpc_endpoint *nonsecure_endpoint, grpc_secure_transport_setup_done_cb cb, void *user_data) { @@ -270,12 +270,12 @@ void grpc_setup_secure_transport(grpc_security_context *ctx, grpc_secure_transport_setup *s = gpr_malloc(sizeof(grpc_secure_transport_setup)); memset(s, 0, sizeof(grpc_secure_transport_setup)); - result = grpc_security_context_create_handshaker(ctx, &s->handshaker); + result = grpc_security_connector_create_handshaker(connector, &s->handshaker); if (result != GRPC_SECURITY_OK) { secure_transport_setup_done(s, 0); return; } - s->ctx = grpc_security_context_ref(ctx); + s->connector = grpc_security_connector_ref(connector); s->handshake_buffer_size = GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE; s->handshake_buffer = gpr_malloc(s->handshake_buffer_size); s->endpoint = nonsecure_endpoint; diff --git a/src/core/security/secure_transport_setup.h b/src/core/security/secure_transport_setup.h index e1f8ed7830d..58701c461d9 100644 --- a/src/core/security/secure_transport_setup.h +++ b/src/core/security/secure_transport_setup.h @@ -35,7 +35,7 @@ #define GRPC_INTERNAL_CORE_SECURITY_SECURE_TRANSPORT_SETUP_H #include "src/core/iomgr/endpoint.h" -#include "src/core/security/security_context.h" +#include "src/core/security/security_connector.h" /* --- Secure transport setup --- */ @@ -45,7 +45,7 @@ typedef void (*grpc_secure_transport_setup_done_cb)( grpc_endpoint *secure_endpoint); /* Calls the callback upon completion. */ -void grpc_setup_secure_transport(grpc_security_context *ctx, +void grpc_setup_secure_transport(grpc_security_connector *connector, grpc_endpoint *nonsecure_endpoint, grpc_secure_transport_setup_done_cb cb, void *user_data); diff --git a/src/core/security/security_context.c b/src/core/security/security_connector.c similarity index 65% rename from src/core/security/security_context.c rename to src/core/security/security_connector.c index dbfcf55655e..dbd79c5b22a 100644 --- a/src/core/security/security_context.c +++ b/src/core/security/security_connector.c @@ -31,12 +31,10 @@ * */ -#include "src/core/security/security_context.h" +#include "src/core/security/security_connector.h" #include -#include "src/core/channel/channel_args.h" -#include "src/core/channel/http_client_filter.h" #include "src/core/security/credentials.h" #include "src/core/security/secure_endpoint.h" #include "src/core/support/env.h" @@ -56,7 +54,8 @@ #ifndef INSTALL_PREFIX static const char *installed_roots_path = "/usr/share/grpc/roots.pem"; #else -static const char *installed_roots_path = INSTALL_PREFIX "/share/grpc/roots.pem"; +static const char *installed_roots_path = + INSTALL_PREFIX "/share/grpc/roots.pem"; #endif /* -- Cipher suites. -- */ @@ -82,75 +81,77 @@ static const char *ssl_cipher_suites(void) { /* -- Common methods. -- */ -grpc_security_status grpc_security_context_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { - if (ctx == NULL || handshaker == NULL) return GRPC_SECURITY_ERROR; - return ctx->vtable->create_handshaker(ctx, handshaker); +grpc_security_status grpc_security_connector_create_handshaker( + grpc_security_connector *sc, tsi_handshaker **handshaker) { + if (sc == NULL || handshaker == NULL) return GRPC_SECURITY_ERROR; + return sc->vtable->create_handshaker(sc, handshaker); } -grpc_security_status grpc_security_context_check_peer( - grpc_security_context *ctx, tsi_peer peer, grpc_security_check_cb cb, +grpc_security_status grpc_security_connector_check_peer( + grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data) { - if (ctx == NULL) { + if (sc == NULL) { tsi_peer_destruct(&peer); return GRPC_SECURITY_ERROR; } - return ctx->vtable->check_peer(ctx, peer, cb, user_data); + return sc->vtable->check_peer(sc, peer, cb, user_data); } -grpc_security_status grpc_channel_security_context_check_call_host( - grpc_channel_security_context *ctx, const char *host, +grpc_security_status grpc_channel_security_connector_check_call_host( + grpc_channel_security_connector *sc, const char *host, grpc_security_check_cb cb, void *user_data) { - if (ctx == NULL || ctx->check_call_host == NULL) return GRPC_SECURITY_ERROR; - return ctx->check_call_host(ctx, host, cb, user_data); + if (sc == NULL || sc->check_call_host == NULL) return GRPC_SECURITY_ERROR; + return sc->check_call_host(sc, host, cb, user_data); } -void grpc_security_context_unref(grpc_security_context *ctx) { - if (ctx == NULL) return; - if (gpr_unref(&ctx->refcount)) ctx->vtable->destroy(ctx); +void grpc_security_connector_unref(grpc_security_connector *sc) { + if (sc == NULL) return; + if (gpr_unref(&sc->refcount)) sc->vtable->destroy(sc); } -grpc_security_context *grpc_security_context_ref(grpc_security_context *ctx) { - if (ctx == NULL) return NULL; - gpr_ref(&ctx->refcount); - return ctx; +grpc_security_connector *grpc_security_connector_ref( + grpc_security_connector *sc) { + if (sc == NULL) return NULL; + gpr_ref(&sc->refcount); + return sc; } -static void context_pointer_arg_destroy(void *p) { - grpc_security_context_unref(p); +static void connector_pointer_arg_destroy(void *p) { + grpc_security_connector_unref(p); } -static void *context_pointer_arg_copy(void *p) { - return grpc_security_context_ref(p); +static void *connector_pointer_arg_copy(void *p) { + return grpc_security_connector_ref(p); } -grpc_arg grpc_security_context_to_arg(grpc_security_context *ctx) { +grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc) { grpc_arg result; result.type = GRPC_ARG_POINTER; - result.key = GRPC_SECURITY_CONTEXT_ARG; - result.value.pointer.destroy = context_pointer_arg_destroy; - result.value.pointer.copy = context_pointer_arg_copy; - result.value.pointer.p = ctx; + result.key = GRPC_SECURITY_CONNECTOR_ARG; + result.value.pointer.destroy = connector_pointer_arg_destroy; + result.value.pointer.copy = connector_pointer_arg_copy; + result.value.pointer.p = sc; return result; } -grpc_security_context *grpc_security_context_from_arg(const grpc_arg *arg) { - if (strcmp(arg->key, GRPC_SECURITY_CONTEXT_ARG)) return NULL; +grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg) { + if (strcmp(arg->key, GRPC_SECURITY_CONNECTOR_ARG)) return NULL; if (arg->type != GRPC_ARG_POINTER) { gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type, - GRPC_SECURITY_CONTEXT_ARG); + GRPC_SECURITY_CONNECTOR_ARG); return NULL; } return arg->value.pointer.p; } -grpc_security_context *grpc_find_security_context_in_args( +grpc_security_connector *grpc_find_security_connector_in_args( const grpc_channel_args *args) { size_t i; if (args == NULL) return NULL; for (i = 0; i < args->num_args; i++) { - grpc_security_context *ctx = grpc_security_context_from_arg(&args->args[i]); - if (ctx != NULL) return ctx; + grpc_security_connector *sc = + grpc_security_connector_from_arg(&args->args[i]); + if (sc != NULL) return sc; } return NULL; } @@ -158,51 +159,41 @@ grpc_security_context *grpc_find_security_context_in_args( static int check_request_metadata_creds(grpc_credentials *creds) { if (creds != NULL && !grpc_credentials_has_request_metadata(creds)) { gpr_log(GPR_ERROR, - "Incompatible credentials for channel security context: needs to " + "Incompatible credentials for channel security connector: needs to " "set request metadata."); return 0; } return 1; } -static grpc_mdctx *get_or_create_mdctx(grpc_credentials *creds) { - grpc_mdctx *mdctx = grpc_credentials_get_metadata_context(creds); - if (mdctx == NULL) { - mdctx = grpc_mdctx_create(); - } else { - grpc_mdctx_ref(mdctx); - } - return mdctx; -} - /* -- Fake implementation. -- */ typedef struct { - grpc_channel_security_context base; + grpc_channel_security_connector base; int call_host_check_is_async; -} grpc_fake_channel_security_context; +} grpc_fake_channel_security_connector; -static void fake_channel_destroy(grpc_security_context *ctx) { - grpc_channel_security_context *c = (grpc_channel_security_context *)ctx; +static void fake_channel_destroy(grpc_security_connector *sc) { + grpc_channel_security_connector *c = (grpc_channel_security_connector *)sc; grpc_credentials_unref(c->request_metadata_creds); - gpr_free(ctx); + gpr_free(sc); } -static void fake_server_destroy(grpc_security_context *ctx) { gpr_free(ctx); } +static void fake_server_destroy(grpc_security_connector *sc) { gpr_free(sc); } static grpc_security_status fake_channel_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { + grpc_security_connector *sc, tsi_handshaker **handshaker) { *handshaker = tsi_create_fake_handshaker(1); return GRPC_SECURITY_OK; } static grpc_security_status fake_server_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { + grpc_security_connector *sc, tsi_handshaker **handshaker) { *handshaker = tsi_create_fake_handshaker(0); return GRPC_SECURITY_OK; } -static grpc_security_status fake_check_peer(grpc_security_context *ctx, +static grpc_security_status fake_check_peer(grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data) { @@ -238,10 +229,10 @@ end: } static grpc_security_status fake_channel_check_call_host( - grpc_channel_security_context *ctx, const char *host, + grpc_channel_security_connector *sc, const char *host, grpc_security_check_cb cb, void *user_data) { - grpc_fake_channel_security_context *c = - (grpc_fake_channel_security_context *)ctx; + grpc_fake_channel_security_connector *c = + (grpc_fake_channel_security_connector *)sc; if (c->call_host_check_is_async) { cb(user_data, GRPC_SECURITY_OK); return GRPC_SECURITY_PENDING; @@ -250,16 +241,16 @@ static grpc_security_status fake_channel_check_call_host( } } -static grpc_security_context_vtable fake_channel_vtable = { +static grpc_security_connector_vtable fake_channel_vtable = { fake_channel_destroy, fake_channel_create_handshaker, fake_check_peer}; -static grpc_security_context_vtable fake_server_vtable = { +static grpc_security_connector_vtable fake_server_vtable = { fake_server_destroy, fake_server_create_handshaker, fake_check_peer}; -grpc_channel_security_context *grpc_fake_channel_security_context_create( +grpc_channel_security_connector *grpc_fake_channel_security_connector_create( grpc_credentials *request_metadata_creds, int call_host_check_is_async) { - grpc_fake_channel_security_context *c = - gpr_malloc(sizeof(grpc_fake_channel_security_context)); + grpc_fake_channel_security_connector *c = + gpr_malloc(sizeof(grpc_fake_channel_security_connector)); gpr_ref_init(&c->base.base.refcount, 1); c->base.base.is_client_side = 1; c->base.base.url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME; @@ -271,8 +262,8 @@ grpc_channel_security_context *grpc_fake_channel_security_context_create( return &c->base; } -grpc_security_context *grpc_fake_server_security_context_create(void) { - grpc_security_context *c = gpr_malloc(sizeof(grpc_security_context)); +grpc_security_connector *grpc_fake_server_security_connector_create(void) { + grpc_security_connector *c = gpr_malloc(sizeof(grpc_security_connector)); gpr_ref_init(&c->refcount, 1); c->vtable = &fake_server_vtable; c->url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME; @@ -282,21 +273,21 @@ grpc_security_context *grpc_fake_server_security_context_create(void) { /* --- Ssl implementation. --- */ typedef struct { - grpc_channel_security_context base; + grpc_channel_security_connector base; tsi_ssl_handshaker_factory *handshaker_factory; char *target_name; char *overridden_target_name; tsi_peer peer; -} grpc_ssl_channel_security_context; +} grpc_ssl_channel_security_connector; typedef struct { - grpc_security_context base; + grpc_security_connector base; tsi_ssl_handshaker_factory *handshaker_factory; -} grpc_ssl_server_security_context; +} grpc_ssl_server_security_connector; -static void ssl_channel_destroy(grpc_security_context *ctx) { - grpc_ssl_channel_security_context *c = - (grpc_ssl_channel_security_context *)ctx; +static void ssl_channel_destroy(grpc_security_connector *sc) { + grpc_ssl_channel_security_connector *c = + (grpc_ssl_channel_security_connector *)sc; grpc_credentials_unref(c->base.request_metadata_creds); if (c->handshaker_factory != NULL) { tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); @@ -304,15 +295,16 @@ static void ssl_channel_destroy(grpc_security_context *ctx) { if (c->target_name != NULL) gpr_free(c->target_name); if (c->overridden_target_name != NULL) gpr_free(c->overridden_target_name); tsi_peer_destruct(&c->peer); - gpr_free(ctx); + gpr_free(sc); } -static void ssl_server_destroy(grpc_security_context *ctx) { - grpc_ssl_server_security_context *c = (grpc_ssl_server_security_context *)ctx; +static void ssl_server_destroy(grpc_security_connector *sc) { + grpc_ssl_server_security_connector *c = + (grpc_ssl_server_security_connector *)sc; if (c->handshaker_factory != NULL) { tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); } - gpr_free(ctx); + gpr_free(sc); } static grpc_security_status ssl_create_handshaker( @@ -331,9 +323,9 @@ static grpc_security_status ssl_create_handshaker( } static grpc_security_status ssl_channel_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { - grpc_ssl_channel_security_context *c = - (grpc_ssl_channel_security_context *)ctx; + grpc_security_connector *sc, tsi_handshaker **handshaker) { + grpc_ssl_channel_security_connector *c = + (grpc_ssl_channel_security_connector *)sc; return ssl_create_handshaker(c->handshaker_factory, 1, c->overridden_target_name != NULL ? c->overridden_target_name @@ -342,13 +334,13 @@ static grpc_security_status ssl_channel_create_handshaker( } static grpc_security_status ssl_server_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker) { - grpc_ssl_server_security_context *c = (grpc_ssl_server_security_context *)ctx; + grpc_security_connector *sc, tsi_handshaker **handshaker) { + grpc_ssl_server_security_connector *c = + (grpc_ssl_server_security_connector *)sc; return ssl_create_handshaker(c->handshaker_factory, 0, NULL, handshaker); } -static int ssl_host_matches_name(const tsi_peer *peer, - const char *peer_name) { +static int ssl_host_matches_name(const tsi_peer *peer, const char *peer_name) { char *allocated_name = NULL; int r; @@ -384,8 +376,7 @@ static grpc_security_status ssl_check_peer(const char *peer_name, } /* Check the peer name if specified. */ - if (peer_name != NULL && - !ssl_host_matches_name(peer, peer_name)) { + if (peer_name != NULL && !ssl_host_matches_name(peer, peer_name)) { gpr_log(GPR_ERROR, "Peer name %s is not in peer certificate", peer_name); return GRPC_SECURITY_ERROR; } @@ -393,12 +384,12 @@ static grpc_security_status ssl_check_peer(const char *peer_name, return GRPC_SECURITY_OK; } -static grpc_security_status ssl_channel_check_peer(grpc_security_context *ctx, +static grpc_security_status ssl_channel_check_peer(grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data) { - grpc_ssl_channel_security_context *c = - (grpc_ssl_channel_security_context *)ctx; + grpc_ssl_channel_security_connector *c = + (grpc_ssl_channel_security_connector *)sc; grpc_security_status status; tsi_peer_destruct(&c->peer); c->peer = peer; @@ -409,7 +400,7 @@ static grpc_security_status ssl_channel_check_peer(grpc_security_context *ctx, return status; } -static grpc_security_status ssl_server_check_peer(grpc_security_context *ctx, +static grpc_security_status ssl_server_check_peer(grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data) { @@ -420,10 +411,10 @@ static grpc_security_status ssl_server_check_peer(grpc_security_context *ctx, } static grpc_security_status ssl_channel_check_call_host( - grpc_channel_security_context *ctx, const char *host, + grpc_channel_security_connector *sc, const char *host, grpc_security_check_cb cb, void *user_data) { - grpc_ssl_channel_security_context *c = - (grpc_ssl_channel_security_context *)ctx; + grpc_ssl_channel_security_connector *c = + (grpc_ssl_channel_security_connector *)sc; if (ssl_host_matches_name(&c->peer, host)) return GRPC_SECURITY_OK; @@ -437,10 +428,10 @@ static grpc_security_status ssl_channel_check_call_host( } } -static grpc_security_context_vtable ssl_channel_vtable = { +static grpc_security_connector_vtable ssl_channel_vtable = { ssl_channel_destroy, ssl_channel_create_handshaker, ssl_channel_check_peer}; -static grpc_security_context_vtable ssl_server_vtable = { +static grpc_security_connector_vtable ssl_server_vtable = { ssl_server_destroy, ssl_server_create_handshaker, ssl_server_check_peer}; static gpr_slice default_pem_root_certs; @@ -471,17 +462,17 @@ size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs) { return GPR_SLICE_LENGTH(default_pem_root_certs); } -grpc_security_status grpc_ssl_channel_security_context_create( +grpc_security_status grpc_ssl_channel_security_connector_create( grpc_credentials *request_metadata_creds, const grpc_ssl_config *config, const char *target_name, const char *overridden_target_name, - grpc_channel_security_context **ctx) { + grpc_channel_security_connector **sc) { size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions(); const unsigned char **alpn_protocol_strings = gpr_malloc(sizeof(const char *) * num_alpn_protocols); unsigned char *alpn_protocol_string_lengths = gpr_malloc(sizeof(unsigned char) * num_alpn_protocols); tsi_result result = TSI_OK; - grpc_ssl_channel_security_context *c; + grpc_ssl_channel_security_connector *c; size_t i; const unsigned char *pem_root_certs; size_t pem_root_certs_size; @@ -502,8 +493,8 @@ grpc_security_status grpc_ssl_channel_security_context_create( goto error; } - c = gpr_malloc(sizeof(grpc_ssl_channel_security_context)); - memset(c, 0, sizeof(grpc_ssl_channel_security_context)); + c = gpr_malloc(sizeof(grpc_ssl_channel_security_connector)); + memset(c, 0, sizeof(grpc_ssl_channel_security_connector)); gpr_ref_init(&c->base.base.refcount, 1); c->base.base.vtable = &ssl_channel_vtable; @@ -535,10 +526,10 @@ grpc_security_status grpc_ssl_channel_security_context_create( gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string(result)); ssl_channel_destroy(&c->base.base); - *ctx = NULL; + *sc = NULL; goto error; } - *ctx = &c->base; + *sc = &c->base; gpr_free(alpn_protocol_strings); gpr_free(alpn_protocol_string_lengths); return GRPC_SECURITY_OK; @@ -549,15 +540,15 @@ error: return GRPC_SECURITY_ERROR; } -grpc_security_status grpc_ssl_server_security_context_create( - const grpc_ssl_server_config *config, grpc_security_context **ctx) { +grpc_security_status grpc_ssl_server_security_connector_create( + const grpc_ssl_server_config *config, grpc_security_connector **sc) { size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions(); const unsigned char **alpn_protocol_strings = gpr_malloc(sizeof(const char *) * num_alpn_protocols); unsigned char *alpn_protocol_string_lengths = gpr_malloc(sizeof(unsigned char) * num_alpn_protocols); tsi_result result = TSI_OK; - grpc_ssl_server_security_context *c; + grpc_ssl_server_security_connector *c; size_t i; for (i = 0; i < num_alpn_protocols; i++) { @@ -571,8 +562,8 @@ grpc_security_status grpc_ssl_server_security_context_create( gpr_log(GPR_ERROR, "An SSL server needs a key and a cert."); goto error; } - c = gpr_malloc(sizeof(grpc_ssl_server_security_context)); - memset(c, 0, sizeof(grpc_ssl_server_security_context)); + c = gpr_malloc(sizeof(grpc_ssl_server_security_connector)); + memset(c, 0, sizeof(grpc_ssl_server_security_connector)); gpr_ref_init(&c->base.refcount, 1); c->base.url_scheme = GRPC_SSL_URL_SCHEME; @@ -582,17 +573,17 @@ grpc_security_status grpc_ssl_server_security_context_create( config->pem_private_keys_sizes, (const unsigned char **)config->pem_cert_chains, config->pem_cert_chains_sizes, config->num_key_cert_pairs, - config->pem_root_certs, config->pem_root_certs_size, - ssl_cipher_suites(), alpn_protocol_strings, - alpn_protocol_string_lengths, num_alpn_protocols, &c->handshaker_factory); + config->pem_root_certs, config->pem_root_certs_size, ssl_cipher_suites(), + alpn_protocol_strings, alpn_protocol_string_lengths, num_alpn_protocols, + &c->handshaker_factory); if (result != TSI_OK) { gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string(result)); ssl_server_destroy(&c->base); - *ctx = NULL; + *sc = NULL; goto error; } - *ctx = &c->base; + *sc = &c->base; gpr_free(alpn_protocol_strings); gpr_free(alpn_protocol_string_lengths); return GRPC_SECURITY_OK; @@ -603,84 +594,3 @@ error: return GRPC_SECURITY_ERROR; } -/* -- High level objects. -- */ - -grpc_channel *grpc_ssl_channel_create(grpc_credentials *ssl_creds, - grpc_credentials *request_metadata_creds, - const char *target, - const grpc_channel_args *args) { - grpc_channel_security_context *ctx = NULL; - grpc_channel *channel = NULL; - grpc_security_status status = GRPC_SECURITY_OK; - size_t i = 0; - const char *overridden_target_name = NULL; - grpc_arg arg; - grpc_channel_args *new_args; - - for (i = 0; args && i < args->num_args; i++) { - grpc_arg *arg = &args->args[i]; - if (strcmp(arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 && - arg->type == GRPC_ARG_STRING) { - overridden_target_name = arg->value.string; - break; - } - } - status = grpc_ssl_channel_security_context_create( - request_metadata_creds, grpc_ssl_credentials_get_config(ssl_creds), - target, overridden_target_name, &ctx); - if (status != GRPC_SECURITY_OK) { - return grpc_lame_client_channel_create(); - } - arg.type = GRPC_ARG_STRING; - arg.key = GRPC_ARG_HTTP2_SCHEME; - arg.value.string = "https"; - new_args = grpc_channel_args_copy_and_add(args, &arg); - channel = grpc_secure_channel_create_internal( - target, new_args, ctx, get_or_create_mdctx(request_metadata_creds)); - grpc_security_context_unref(&ctx->base); - grpc_channel_args_destroy(new_args); - return channel; -} - -grpc_channel *grpc_fake_transport_security_channel_create( - grpc_credentials *fake_creds, grpc_credentials *request_metadata_creds, - const char *target, const grpc_channel_args *args) { - grpc_channel_security_context *ctx = - grpc_fake_channel_security_context_create(request_metadata_creds, 1); - grpc_channel *channel = grpc_secure_channel_create_internal( - target, args, ctx, get_or_create_mdctx(request_metadata_creds)); - grpc_security_context_unref(&ctx->base); - return channel; -} - -grpc_channel *grpc_secure_channel_create_with_factories( - const grpc_secure_channel_factory *factories, size_t num_factories, - grpc_credentials *creds, const char *target, - const grpc_channel_args *args) { - size_t i; - if (creds == NULL) { - gpr_log(GPR_ERROR, "No credentials to create a secure channel."); - return grpc_lame_client_channel_create(); - } - if (grpc_credentials_has_request_metadata_only(creds)) { - gpr_log(GPR_ERROR, - "Credentials is insufficient to create a secure channel."); - return grpc_lame_client_channel_create(); - } - - for (i = 0; i < num_factories; i++) { - grpc_credentials *composite_creds = NULL; - grpc_credentials *transport_security_creds = NULL; - transport_security_creds = grpc_credentials_contains_type( - creds, factories[i].creds_type, &composite_creds); - if (transport_security_creds != NULL) { - return factories[i].factory(transport_security_creds, composite_creds, - target, args); - } - } - - gpr_log(GPR_ERROR, - "Unknown credentials type %s for creating a secure channel.", - creds->type); - return grpc_lame_client_channel_create(); -} diff --git a/src/core/security/security_connector.h b/src/core/security/security_connector.h new file mode 100644 index 00000000000..47abe05cffc --- /dev/null +++ b/src/core/security/security_connector.h @@ -0,0 +1,201 @@ +/* + * + * 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_CORE_SECURITY_SECURITY_CONNECTOR_H +#define GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H + +#include +#include "src/core/iomgr/endpoint.h" +#include "src/core/tsi/transport_security_interface.h" + +/* --- status enum. --- */ + +typedef enum { + GRPC_SECURITY_OK = 0, + GRPC_SECURITY_PENDING, + GRPC_SECURITY_ERROR +} grpc_security_status; + +/* --- URL schemes. --- */ + +#define GRPC_SSL_URL_SCHEME "https" +#define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security" + +/* --- security_connector object. --- + + A security connector object represents away to configure the underlying + transport security mechanism and check the resulting trusted peer. */ + +typedef struct grpc_security_connector grpc_security_connector; + +#define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector" + +typedef void (*grpc_security_check_cb)(void *user_data, + grpc_security_status status); + +typedef struct { + void (*destroy)(grpc_security_connector *sc); + grpc_security_status (*create_handshaker)(grpc_security_connector *sc, + tsi_handshaker **handshaker); + grpc_security_status (*check_peer)(grpc_security_connector *sc, tsi_peer peer, + grpc_security_check_cb cb, + void *user_data); +} grpc_security_connector_vtable; + +struct grpc_security_connector { + const grpc_security_connector_vtable *vtable; + gpr_refcount refcount; + int is_client_side; + const char *url_scheme; +}; + +/* Increments the refcount. */ +grpc_security_connector *grpc_security_connector_ref( + grpc_security_connector *sc); + +/* Decrements the refcount and destroys the object if it reaches 0. */ +void grpc_security_connector_unref(grpc_security_connector *sc); + +/* Handshake creation. */ +grpc_security_status grpc_security_connector_create_handshaker( + grpc_security_connector *sc, tsi_handshaker **handshaker); + +/* Check the peer. + Implementations can choose to check the peer either synchronously or + asynchronously. In the first case, a successful call will return + GRPC_SECURITY_OK. In the asynchronous case, the call will return + GRPC_SECURITY_PENDING unless an error is detected early on. + Ownership of the peer is transfered. +*/ +grpc_security_status grpc_security_connector_check_peer( + grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb, + void *user_data); + +/* Util to encapsulate the connector in a channel arg. */ +grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc); + +/* Util to get the connector from a channel arg. */ +grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg); + +/* Util to find the connector from channel args. */ +grpc_security_connector *grpc_find_security_connector_in_args( + const grpc_channel_args *args); + +/* --- channel_security_connector object. --- + + A channel security connector object represents away to configure the + underlying transport security mechanism on the client side. */ + +typedef struct grpc_channel_security_connector grpc_channel_security_connector; + +struct grpc_channel_security_connector { + grpc_security_connector base; /* requires is_client_side to be non 0. */ + grpc_credentials *request_metadata_creds; + grpc_security_status (*check_call_host)(grpc_channel_security_connector *sc, + const char *host, + grpc_security_check_cb cb, + void *user_data); +}; + +/* Checks that the host that will be set for a call is acceptable. + Implementations can choose do the check either synchronously or + asynchronously. In the first case, a successful call will return + GRPC_SECURITY_OK. In the asynchronous case, the call will return + GRPC_SECURITY_PENDING unless an error is detected early on. */ +grpc_security_status grpc_channel_security_connector_check_call_host( + grpc_channel_security_connector *sc, const char *host, + grpc_security_check_cb cb, void *user_data); + +/* --- Creation security connectors. --- */ + +/* For TESTING ONLY! + Creates a fake connector that emulates real channel security. */ +grpc_channel_security_connector *grpc_fake_channel_security_connector_create( + grpc_credentials *request_metadata_creds, int call_host_check_is_async); + +/* For TESTING ONLY! + Creates a fake connector that emulates real server security. */ +grpc_security_connector *grpc_fake_server_security_connector_create(void); + +/* Config for ssl clients. */ +typedef struct { + unsigned char *pem_private_key; + size_t pem_private_key_size; + unsigned char *pem_cert_chain; + size_t pem_cert_chain_size; + unsigned char *pem_root_certs; + size_t pem_root_certs_size; +} grpc_ssl_config; + +/* Creates an SSL channel_security_connector. + - request_metadata_creds is the credentials object which metadata + will be sent with each request. This parameter can be NULL. + - config is the SSL config to be used for the SSL channel establishment. + - is_client should be 0 for a server or a non-0 value for a client. + - secure_peer_name is the secure peer name that should be checked in + grpc_channel_security_connector_check_peer. This parameter may be NULL in + which case the peer name will not be checked. Note that if this parameter + is not NULL, then, pem_root_certs should not be NULL either. + - sc is a pointer on the connector to be created. + This function returns GRPC_SECURITY_OK in case of success or a + specific error code otherwise. +*/ +grpc_security_status grpc_ssl_channel_security_connector_create( + grpc_credentials *request_metadata_creds, + const grpc_ssl_config *config, const char *target_name, + const char *overridden_target_name, grpc_channel_security_connector **sc); + +/* Gets the default ssl roots. */ +size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs); + +/* Config for ssl servers. */ +typedef struct { + unsigned char **pem_private_keys; + size_t *pem_private_keys_sizes; + unsigned char **pem_cert_chains; + size_t *pem_cert_chains_sizes; + size_t num_key_cert_pairs; + unsigned char *pem_root_certs; + size_t pem_root_certs_size; +} grpc_ssl_server_config; + +/* Creates an SSL server_security_connector. + - config is the SSL config to be used for the SSL channel establishment. + - sc is a pointer on the connector to be created. + This function returns GRPC_SECURITY_OK in case of success or a + specific error code otherwise. +*/ +grpc_security_status grpc_ssl_server_security_connector_create( + const grpc_ssl_server_config *config, grpc_security_connector **sc); + +#endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H */ diff --git a/src/core/security/security_context.h b/src/core/security/security_context.h deleted file mode 100644 index 8e7ba34cac2..00000000000 --- a/src/core/security/security_context.h +++ /dev/null @@ -1,214 +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_CORE_SECURITY_SECURITY_CONTEXT_H -#define GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONTEXT_H - -#include -#include "src/core/iomgr/endpoint.h" -#include "src/core/security/credentials.h" -#include "src/core/tsi/transport_security_interface.h" - -/* --- status enum. --- */ - -typedef enum { - GRPC_SECURITY_OK = 0, - GRPC_SECURITY_PENDING, - GRPC_SECURITY_ERROR -} grpc_security_status; - -/* --- URL schemes. --- */ - -#define GRPC_SSL_URL_SCHEME "https" -#define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security" - -/* --- security_context object. --- - - A security context object represents away to configure the underlying - transport security mechanism and check the resulting trusted peer. */ - -typedef struct grpc_security_context grpc_security_context; - -#define GRPC_SECURITY_CONTEXT_ARG "grpc.security_context" - -typedef void (*grpc_security_check_cb)(void *user_data, - grpc_security_status status); - -typedef struct { - void (*destroy)(grpc_security_context *ctx); - grpc_security_status (*create_handshaker)(grpc_security_context *ctx, - tsi_handshaker **handshaker); - grpc_security_status (*check_peer)(grpc_security_context *ctx, tsi_peer peer, - grpc_security_check_cb cb, - void *user_data); -} grpc_security_context_vtable; - -struct grpc_security_context { - const grpc_security_context_vtable *vtable; - gpr_refcount refcount; - int is_client_side; - const char *url_scheme; -}; - -/* Increments the refcount. */ -grpc_security_context *grpc_security_context_ref(grpc_security_context *ctx); - -/* Decrements the refcount and destroys the object if it reaches 0. */ -void grpc_security_context_unref(grpc_security_context *ctx); - -/* Handshake creation. */ -grpc_security_status grpc_security_context_create_handshaker( - grpc_security_context *ctx, tsi_handshaker **handshaker); - -/* Check the peer. - Implementations can choose to check the peer either synchronously or - asynchronously. In the first case, a successful call will return - GRPC_SECURITY_OK. In the asynchronous case, the call will return - GRPC_SECURITY_PENDING unless an error is detected early on. - Ownership of the peer is transfered. -*/ -grpc_security_status grpc_security_context_check_peer( - grpc_security_context *ctx, tsi_peer peer, - grpc_security_check_cb cb, void *user_data); - -/* Util to encapsulate the context in a channel arg. */ -grpc_arg grpc_security_context_to_arg(grpc_security_context *ctx); - -/* Util to get the context from a channel arg. */ -grpc_security_context *grpc_security_context_from_arg(const grpc_arg *arg); - -/* Util to find the context from channel args. */ -grpc_security_context *grpc_find_security_context_in_args( - const grpc_channel_args *args); - -/* --- channel_security_context object. --- - - A channel security context object represents away to configure the - underlying transport security mechanism on the client side. */ - -typedef struct grpc_channel_security_context grpc_channel_security_context; - -struct grpc_channel_security_context { - grpc_security_context base; /* requires is_client_side to be non 0. */ - grpc_credentials *request_metadata_creds; - grpc_security_status (*check_call_host)( - grpc_channel_security_context *ctx, const char *host, - grpc_security_check_cb cb, void *user_data); -}; - -/* Checks that the host that will be set for a call is acceptable. - Implementations can choose do the check either synchronously or - asynchronously. In the first case, a successful call will return - GRPC_SECURITY_OK. In the asynchronous case, the call will return - GRPC_SECURITY_PENDING unless an error is detected early on. */ -grpc_security_status grpc_channel_security_context_check_call_host( - grpc_channel_security_context *ctx, const char *host, - grpc_security_check_cb cb, void *user_data); - -/* --- Creation security contexts. --- */ - -/* For TESTING ONLY! - Creates a fake context that emulates real channel security. */ -grpc_channel_security_context *grpc_fake_channel_security_context_create( - grpc_credentials *request_metadata_creds, int call_host_check_is_async); - -/* For TESTING ONLY! - Creates a fake context that emulates real server security. */ -grpc_security_context *grpc_fake_server_security_context_create(void); - -/* Creates an SSL channel_security_context. - - request_metadata_creds is the credentials object which metadata - will be sent with each request. This parameter can be NULL. - - config is the SSL config to be used for the SSL channel establishment. - - is_client should be 0 for a server or a non-0 value for a client. - - secure_peer_name is the secure peer name that should be checked in - grpc_channel_security_context_check_peer. This parameter may be NULL in - which case the peer name will not be checked. Note that if this parameter - is not NULL, then, pem_root_certs should not be NULL either. - - ctx is a pointer on the context to be created. - This function returns GRPC_SECURITY_OK in case of success or a - specific error code otherwise. -*/ -grpc_security_status grpc_ssl_channel_security_context_create( - grpc_credentials *request_metadata_creds, const grpc_ssl_config *config, - const char *target_name, const char *overridden_target_name, - grpc_channel_security_context **ctx); - -/* Creates an SSL server_security_context. - - config is the SSL config to be used for the SSL channel establishment. - - ctx is a pointer on the context to be created. - This function returns GRPC_SECURITY_OK in case of success or a - specific error code otherwise. -*/ -grpc_security_status grpc_ssl_server_security_context_create( - const grpc_ssl_server_config *config, grpc_security_context **ctx); - -/* --- Creation of high level objects. --- */ - -/* Secure client channel creation. */ - -size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs); - -grpc_channel *grpc_ssl_channel_create(grpc_credentials *ssl_creds, - grpc_credentials *request_metadata_creds, - const char *target, - const grpc_channel_args *args); - -grpc_channel *grpc_fake_transport_security_channel_create( - grpc_credentials *fake_creds, grpc_credentials *request_metadata_creds, - const char *target, const grpc_channel_args *args); - -grpc_channel *grpc_secure_channel_create_internal( - const char *target, const grpc_channel_args *args, - grpc_channel_security_context *ctx, grpc_mdctx *mdctx); - -typedef grpc_channel *(*grpc_secure_channel_factory_func)( - grpc_credentials *transport_security_creds, - grpc_credentials *request_metadata_creds, const char *target, - const grpc_channel_args *args); - -typedef struct { - const char *creds_type; - grpc_secure_channel_factory_func factory; -} grpc_secure_channel_factory; - -grpc_channel *grpc_secure_channel_create_with_factories( - const grpc_secure_channel_factory *factories, size_t num_factories, - grpc_credentials *creds, const char *target, const grpc_channel_args *args); - -/* Secure server context creation. */ - -grpc_security_status grpc_server_security_context_create( - grpc_server_credentials *creds, grpc_security_context **ctx); - -#endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONTEXT_H */ diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c index 165ed5474fe..5b7d99ce401 100644 --- a/src/core/security/server_secure_chttp2.c +++ b/src/core/security/server_secure_chttp2.c @@ -40,7 +40,8 @@ #include "src/core/iomgr/endpoint.h" #include "src/core/iomgr/resolve_address.h" #include "src/core/iomgr/tcp_server.h" -#include "src/core/security/security_context.h" +#include "src/core/security/credentials.h" +#include "src/core/security/security_connector.h" #include "src/core/security/secure_transport_setup.h" #include "src/core/surface/server.h" #include "src/core/transport/chttp2_transport.h" @@ -52,7 +53,7 @@ typedef struct grpc_server_secure_state { grpc_server *server; grpc_tcp_server *tcp; - grpc_security_context *ctx; + grpc_security_connector *sc; int is_shutdown; gpr_mu mu; gpr_refcount refcount; @@ -64,7 +65,7 @@ static void state_ref(grpc_server_secure_state *state) { static void state_unref(grpc_server_secure_state *state) { if (gpr_unref(&state->refcount)) { - grpc_security_context_unref(state->ctx); + grpc_security_connector_unref(state->sc); gpr_free(state); } } @@ -104,7 +105,7 @@ static void on_secure_transport_setup_done(void *statep, static void on_accept(void *statep, grpc_endpoint *tcp) { grpc_server_secure_state *state = statep; state_ref(state); - grpc_setup_secure_transport(state->ctx, tcp, on_secure_transport_setup_done, + grpc_setup_secure_transport(state->sc, tcp, on_secure_transport_setup_done, state); } @@ -137,11 +138,11 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, int port_num = -1; int port_temp; grpc_security_status status = GRPC_SECURITY_ERROR; - grpc_security_context *ctx = NULL; + grpc_security_connector *sc = NULL; /* create security context */ if (creds == NULL) goto error; - status = grpc_server_security_context_create(creds, &ctx); + status = grpc_server_credentials_create_security_connector(creds, &sc); if (status != GRPC_SECURITY_OK) { gpr_log(GPR_ERROR, "Unable to create secure server with credentials of type %s.", @@ -188,7 +189,7 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, state = gpr_malloc(sizeof(*state)); state->server = server; state->tcp = tcp; - state->ctx = ctx; + state->sc = sc; state->is_shutdown = 0; gpr_mu_init(&state->mu); gpr_ref_init(&state->refcount, 1); @@ -200,8 +201,8 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, /* Error path: cleanup and return */ error: - if (ctx) { - grpc_security_context_unref(ctx); + if (sc) { + grpc_security_connector_unref(sc); } if (resolved) { grpc_resolved_addresses_destroy(resolved); diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c index 96b2fe04fa5..e7d223bfda9 100644 --- a/src/core/surface/secure_channel_create.c +++ b/src/core/surface/secure_channel_create.c @@ -48,7 +48,7 @@ #include "src/core/iomgr/resolve_address.h" #include "src/core/iomgr/tcp_client.h" #include "src/core/security/auth.h" -#include "src/core/security/security_context.h" +#include "src/core/security/credentials.h" #include "src/core/security/secure_transport_setup.h" #include "src/core/support/string.h" #include "src/core/surface/channel.h" @@ -74,7 +74,7 @@ typedef struct { } request; struct setup { - grpc_channel_security_context *security_context; + grpc_channel_security_connector *security_connector; const char *target; grpc_transport_setup_callback setup_callback; void *setup_user_data; @@ -130,7 +130,7 @@ static void on_connect(void *rp, grpc_endpoint *tcp) { return; } } else { - grpc_setup_secure_transport(&r->setup->security_context->base, tcp, + grpc_setup_secure_transport(&r->setup->security_connector->base, tcp, on_secure_transport_setup_done, r); } } @@ -185,7 +185,7 @@ static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) { static void done_setup(void *sp) { setup *s = sp; gpr_free((void *)s->target); - grpc_security_context_unref(&s->security_context->base); + grpc_security_connector_unref(&s->security_connector->base); gpr_free(s); } @@ -203,23 +203,37 @@ static grpc_transport_setup_result complete_setup(void *channel_stack, Asynchronously: - resolve target - connect to it (trying alternatives as presented) - perform handshakes */ -grpc_channel *grpc_secure_channel_create_internal( - const char *target, const grpc_channel_args *args, - grpc_channel_security_context *context, grpc_mdctx *mdctx) { +grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, + const char *target, + const grpc_channel_args *args) { setup *s; grpc_channel *channel; - grpc_arg context_arg; + grpc_arg connector_arg; grpc_channel_args *args_copy; + grpc_channel_args *new_args_from_connector; + grpc_channel_security_connector* connector; + grpc_mdctx *mdctx; #define MAX_FILTERS 3 const grpc_channel_filter *filters[MAX_FILTERS]; int n = 0; - if (grpc_find_security_context_in_args(args) != NULL) { + + if (grpc_find_security_connector_in_args(args) != NULL) { gpr_log(GPR_ERROR, "Cannot set security context in channel args."); + return grpc_lame_client_channel_create(); } + if (grpc_credentials_create_security_connector( + creds, target, args, NULL, &connector, &new_args_from_connector) != + GRPC_SECURITY_OK) { + return grpc_lame_client_channel_create(); + } + mdctx = grpc_credentials_get_or_create_metadata_context(creds); + s = gpr_malloc(sizeof(setup)); - context_arg = grpc_security_context_to_arg(&context->base); - args_copy = grpc_channel_args_copy_and_add(args, &context_arg); + connector_arg = grpc_security_connector_to_arg(&connector->base); + args_copy = grpc_channel_args_copy_and_add( + new_args_from_connector != NULL ? new_args_from_connector : args, + &connector_arg); filters[n++] = &grpc_client_surface_filter; if (grpc_channel_args_is_census_enabled(args)) { filters[n++] = &grpc_client_census_filter; @@ -228,13 +242,14 @@ grpc_channel *grpc_secure_channel_create_internal( GPR_ASSERT(n <= MAX_FILTERS); channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1); grpc_channel_args_destroy(args_copy); + if (new_args_from_connector != NULL) { + grpc_channel_args_destroy(new_args_from_connector); + } s->target = gpr_strdup(target); s->setup_callback = complete_setup; s->setup_user_data = grpc_channel_get_channel_stack(channel); - s->security_context = - (grpc_channel_security_context *)grpc_security_context_ref( - &context->base); + s->security_connector = connector; grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel), args, mdctx, initiate_setup, done_setup, s); diff --git a/test/core/end2end/fixtures/chttp2_fake_security.c b/test/core/end2end/fixtures/chttp2_fake_security.c index 047d482be34..929f1f50db5 100644 --- a/test/core/end2end/fixtures/chttp2_fake_security.c +++ b/test/core/end2end/fixtures/chttp2_fake_security.c @@ -38,7 +38,6 @@ #include "src/core/channel/channel_args.h" #include "src/core/security/credentials.h" -#include "src/core/security/security_context.h" #include #include #include diff --git a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c index 16433f5f87c..9c4086d79d4 100644 --- a/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c +++ b/test/core/end2end/fixtures/chttp2_simple_ssl_fullstack.c @@ -38,7 +38,6 @@ #include "src/core/channel/channel_args.h" #include "src/core/security/credentials.h" -#include "src/core/security/security_context.h" #include "src/core/support/env.h" #include "src/core/support/file.h" #include "src/core/support/string.h" diff --git a/test/core/end2end/fixtures/chttp2_simple_ssl_with_oauth2_fullstack.c b/test/core/end2end/fixtures/chttp2_simple_ssl_with_oauth2_fullstack.c index 99031df8e54..e9e1c5f838a 100644 --- a/test/core/end2end/fixtures/chttp2_simple_ssl_with_oauth2_fullstack.c +++ b/test/core/end2end/fixtures/chttp2_simple_ssl_with_oauth2_fullstack.c @@ -39,7 +39,6 @@ #include "src/core/channel/channel_args.h" #include "src/core/iomgr/iomgr.h" #include "src/core/security/credentials.h" -#include "src/core/security/security_context.h" #include #include #include diff --git a/test/core/security/credentials_test.c b/test/core/security/credentials_test.c index d1d1ec15623..1b657e3d89c 100644 --- a/test/core/security/credentials_test.c +++ b/test/core/security/credentials_test.c @@ -313,6 +313,19 @@ static void test_ssl_oauth2_composite_creds(void) { composite_creds); } +void test_ssl_fake_transport_security_composite_creds_failure(void) { + grpc_credentials *ssl_creds = + grpc_ssl_credentials_create(NULL, NULL); + grpc_credentials *fake_transport_security_creds = + grpc_fake_transport_security_credentials_create(); + + /* 2 connector credentials: should not work. */ + GPR_ASSERT(grpc_composite_credentials_create( + ssl_creds, fake_transport_security_creds) == NULL); + grpc_credentials_unref(ssl_creds); + grpc_credentials_unref(fake_transport_security_creds); +} + static void check_ssl_oauth2_iam_composite_metadata( void *user_data, grpc_mdelem **md_elems, size_t num_md, grpc_credentials_status status) { diff --git a/vsprojects/vs2010/grpc.vcxproj b/vsprojects/vs2010/grpc.vcxproj index d18c33bf276..ce973e82824 100644 --- a/vsprojects/vs2010/grpc.vcxproj +++ b/vsprojects/vs2010/grpc.vcxproj @@ -86,7 +86,7 @@ - + @@ -94,7 +94,7 @@ - + @@ -194,7 +194,7 @@ - + @@ -208,8 +208,6 @@ - - @@ -218,7 +216,7 @@ - + diff --git a/vsprojects/vs2010/grpc.vcxproj.filters b/vsprojects/vs2010/grpc.vcxproj.filters index 7774a780402..7619bc1f4f2 100644 --- a/vsprojects/vs2010/grpc.vcxproj.filters +++ b/vsprojects/vs2010/grpc.vcxproj.filters @@ -7,7 +7,7 @@ src\core\httpcli - + src\core\httpcli @@ -28,9 +28,6 @@ src\core\security - - src\core\security - src\core\security @@ -43,7 +40,7 @@ src\core\security - + src\core\security @@ -386,7 +383,7 @@ src\core\httpcli - + src\core\httpcli @@ -410,7 +407,7 @@ src\core\security - + src\core\security diff --git a/vsprojects/vs2013/grpc.vcxproj b/vsprojects/vs2013/grpc.vcxproj index 4c7f0fad2e2..250d480afd0 100644 --- a/vsprojects/vs2013/grpc.vcxproj +++ b/vsprojects/vs2013/grpc.vcxproj @@ -88,7 +88,7 @@ - + @@ -96,7 +96,7 @@ - + @@ -196,7 +196,7 @@ - + @@ -210,8 +210,6 @@ - - @@ -220,7 +218,7 @@ - + diff --git a/vsprojects/vs2013/grpc.vcxproj.filters b/vsprojects/vs2013/grpc.vcxproj.filters index 7774a780402..7619bc1f4f2 100644 --- a/vsprojects/vs2013/grpc.vcxproj.filters +++ b/vsprojects/vs2013/grpc.vcxproj.filters @@ -7,7 +7,7 @@ src\core\httpcli - + src\core\httpcli @@ -28,9 +28,6 @@ src\core\security - - src\core\security - src\core\security @@ -43,7 +40,7 @@ src\core\security - + src\core\security @@ -386,7 +383,7 @@ src\core\httpcli - + src\core\httpcli @@ -410,7 +407,7 @@ src\core\security - + src\core\security From aab1edc5f6456117d201e73103a12efb4c114556 Mon Sep 17 00:00:00 2001 From: Julien Boeuf Date: Sat, 18 Apr 2015 22:12:48 -0700 Subject: [PATCH 26/26] Fixing inaccurate ruby test. --- src/ruby/spec/credentials_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ruby/spec/credentials_spec.rb b/src/ruby/spec/credentials_spec.rb index fc97d11a878..8e72e85d540 100644 --- a/src/ruby/spec/credentials_spec.rb +++ b/src/ruby/spec/credentials_spec.rb @@ -61,11 +61,11 @@ describe Credentials do end describe '#compose' do - it 'can be completed OK' do + it 'cannot be completed OK with 2 SSL creds' do certs = load_test_certs cred1 = Credentials.new(*certs) cred2 = Credentials.new(*certs) - expect { cred1.compose(cred2) }.to_not raise_error + expect { cred1.compose(cred2) }.to raise_error end end end