diff --git a/BUILD b/BUILD index f079f9dcba0..63744760bfd 100644 --- a/BUILD +++ b/BUILD @@ -137,6 +137,7 @@ GRPCXX_SRCS = [ "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", + "src/cpp/common/validate_service_config.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/channel_argument_option.cc", "src/cpp/server/create_default_thread_pool.cc", @@ -286,6 +287,7 @@ GRPCXX_PUBLIC_HDRS = [ "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", "include/grpcpp/support/time.h", + "include/grpcpp/support/validate_service_config.h", ] grpc_cc_library( diff --git a/BUILD.gn b/BUILD.gn index c0ff5dc2417..14e5c3f0b5f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1139,6 +1139,7 @@ config("grpc_config") { "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", "include/grpcpp/support/time.h", + "include/grpcpp/support/validate_service_config.h", "src/core/ext/transport/inproc/inproc_transport.h", "src/core/lib/avl/avl.h", "src/core/lib/backoff/backoff.h", @@ -1338,6 +1339,7 @@ config("grpc_config") { "src/cpp/common/secure_auth_context.h", "src/cpp/common/secure_channel_arguments.cc", "src/cpp/common/secure_create_auth_context.cc", + "src/cpp/common/validate_service_config.cc", "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/channel_argument_option.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f31f21702c..e9d2168b8de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3043,6 +3043,7 @@ add_library(grpc++ src/cpp/common/core_codegen.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc + src/cpp/common/validate_service_config.cc src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/channel_argument_option.cc @@ -3218,6 +3219,7 @@ foreach(_hdr include/grpcpp/support/stub_options.h include/grpcpp/support/sync_stream.h include/grpcpp/support/time.h + include/grpcpp/support/validate_service_config.h include/grpc/support/alloc.h include/grpc/support/atm.h include/grpc/support/atm_gcc_atomic.h @@ -3440,6 +3442,7 @@ add_library(grpc++_cronet src/cpp/common/core_codegen.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc + src/cpp/common/validate_service_config.cc src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/channel_argument_option.cc @@ -3835,6 +3838,7 @@ foreach(_hdr include/grpcpp/support/stub_options.h include/grpcpp/support/sync_stream.h include/grpcpp/support/time.h + include/grpcpp/support/validate_service_config.h include/grpc/support/alloc.h include/grpc/support/atm.h include/grpc/support/atm_gcc_atomic.h @@ -4652,6 +4656,7 @@ add_library(grpc++_unsecure src/cpp/common/core_codegen.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc + src/cpp/common/validate_service_config.cc src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/channel_argument_option.cc @@ -4826,6 +4831,7 @@ foreach(_hdr include/grpcpp/support/stub_options.h include/grpcpp/support/sync_stream.h include/grpcpp/support/time.h + include/grpcpp/support/validate_service_config.h include/grpc/support/alloc.h include/grpc/support/atm.h include/grpc/support/atm_gcc_atomic.h diff --git a/Makefile b/Makefile index e3804df86d4..e56c5ec4e47 100644 --- a/Makefile +++ b/Makefile @@ -5435,6 +5435,7 @@ LIBGRPC++_SRC = \ src/cpp/common/core_codegen.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ + src/cpp/common/validate_service_config.cc \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/channel_argument_option.cc \ @@ -5575,6 +5576,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ include/grpcpp/support/time.h \ + include/grpcpp/support/validate_service_config.h \ include/grpc/support/alloc.h \ include/grpc/support/atm.h \ include/grpc/support/atm_gcc_atomic.h \ @@ -5841,6 +5843,7 @@ LIBGRPC++_CRONET_SRC = \ src/cpp/common/core_codegen.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ + src/cpp/common/validate_service_config.cc \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/channel_argument_option.cc \ @@ -6200,6 +6203,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ include/grpcpp/support/time.h \ + include/grpcpp/support/validate_service_config.h \ include/grpc/support/alloc.h \ include/grpc/support/atm.h \ include/grpc/support/atm_gcc_atomic.h \ @@ -7000,6 +7004,7 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/common/core_codegen.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ + src/cpp/common/validate_service_config.cc \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/channel_argument_option.cc \ @@ -7140,6 +7145,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ include/grpcpp/support/time.h \ + include/grpcpp/support/validate_service_config.h \ include/grpc/support/alloc.h \ include/grpc/support/atm.h \ include/grpc/support/atm_gcc_atomic.h \ diff --git a/build.yaml b/build.yaml index 86b8c406607..529afecbb33 100644 --- a/build.yaml +++ b/build.yaml @@ -1426,6 +1426,7 @@ filegroups: - include/grpcpp/support/stub_options.h - include/grpcpp/support/sync_stream.h - include/grpcpp/support/time.h + - include/grpcpp/support/validate_service_config.h headers: - src/cpp/client/create_channel_internal.h - src/cpp/common/channel_filter.h @@ -1450,6 +1451,7 @@ filegroups: - src/cpp/common/core_codegen.cc - src/cpp/common/resource_quota_cc.cc - src/cpp/common/rpc_method.cc + - src/cpp/common/validate_service_config.cc - src/cpp/common/version_cc.cc - src/cpp/server/async_generic_service.cc - src/cpp/server/channel_argument_option.cc diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 0e9a50ddcae..0ce31532629 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -148,6 +148,7 @@ Pod::Spec.new do |s| 'include/grpcpp/support/stub_options.h', 'include/grpcpp/support/sync_stream.h', 'include/grpcpp/support/time.h', + 'include/grpcpp/support/validate_service_config.h', 'include/grpcpp/impl/codegen/async_generic_service.h', 'include/grpcpp/impl/codegen/async_stream.h', 'include/grpcpp/impl/codegen/async_unary_call.h', @@ -234,6 +235,7 @@ Pod::Spec.new do |s| 'src/cpp/common/core_codegen.cc', 'src/cpp/common/resource_quota_cc.cc', 'src/cpp/common/rpc_method.cc', + 'src/cpp/common/validate_service_config.cc', 'src/cpp/common/version_cc.cc', 'src/cpp/server/async_generic_service.cc', 'src/cpp/server/channel_argument_option.cc', diff --git a/grpc.gyp b/grpc.gyp index eff5ab816b8..6268bed7bb0 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -1463,6 +1463,7 @@ 'src/cpp/common/core_codegen.cc', 'src/cpp/common/resource_quota_cc.cc', 'src/cpp/common/rpc_method.cc', + 'src/cpp/common/validate_service_config.cc', 'src/cpp/common/version_cc.cc', 'src/cpp/server/async_generic_service.cc', 'src/cpp/server/channel_argument_option.cc', @@ -1619,6 +1620,7 @@ 'src/cpp/common/core_codegen.cc', 'src/cpp/common/resource_quota_cc.cc', 'src/cpp/common/rpc_method.cc', + 'src/cpp/common/validate_service_config.cc', 'src/cpp/common/version_cc.cc', 'src/cpp/server/async_generic_service.cc', 'src/cpp/server/channel_argument_option.cc', diff --git a/include/grpcpp/support/validate_service_config.h b/include/grpcpp/support/validate_service_config.h new file mode 100644 index 00000000000..41f2c636bd9 --- /dev/null +++ b/include/grpcpp/support/validate_service_config.h @@ -0,0 +1,36 @@ +/* + * + * Copyright 2019 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPCPP_SUPPORT_VALIDATE_SERVICE_CONFIG_H +#define GRPCPP_SUPPORT_VALIDATE_SERVICE_CONFIG_H + +#include + +namespace grpc { + +namespace experimental { +/// Validates \a service_config_json. If valid, returns an empty string. +/// Otherwise, returns the validation error. +/// TODO(yashykt): Promote it to out of experimental once it is proved useful +/// and gRFC is accepted. +grpc::string ValidateServiceConfigJSON(const grpc::string& service_config_json); +} // namespace experimental + +} // namespace grpc + +#endif // GRPCPP_SUPPORT_VALIDATE_SERVICE_CONFIG_H diff --git a/src/cpp/common/validate_service_config.cc b/src/cpp/common/validate_service_config.cc new file mode 100644 index 00000000000..0f193cf1c42 --- /dev/null +++ b/src/cpp/common/validate_service_config.cc @@ -0,0 +1,40 @@ +/* + * + * Copyright 2019 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +#include "src/core/ext/filters/client_channel/service_config.h" + +namespace grpc { +namespace experimental { +grpc::string ValidateServiceConfigJSON( + const grpc::string& service_config_json) { + grpc_init(); + grpc_error* error = GRPC_ERROR_NONE; + grpc_core::ServiceConfig::Create(service_config_json.c_str(), &error); + grpc::string return_value; + if (error != GRPC_ERROR_NONE) { + return_value = grpc_error_string(error); + GRPC_ERROR_UNREF(error); + } + grpc_shutdown(); + return return_value; +} +} // namespace experimental +} // namespace grpc diff --git a/test/cpp/end2end/service_config_end2end_test.cc b/test/cpp/end2end/service_config_end2end_test.cc index b3299232763..14786b98f85 100644 --- a/test/cpp/end2end/service_config_end2end_test.cc +++ b/test/cpp/end2end/service_config_end2end_test.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include "src/core/ext/filters/client_channel/backup_poller.h" #include "src/core/ext/filters/client_channel/global_subchannel_pool.h" @@ -231,6 +232,9 @@ class ServiceConfigEnd2endTest : public ::testing::Test { std::shared_ptr BuildChannelWithDefaultServiceConfig() { ChannelArguments args; + EXPECT_THAT(grpc::experimental::ValidateServiceConfigJSON( + ValidDefaultServiceConfig()), + ::testing::StrEq("")); args.SetServiceConfigJSON(ValidDefaultServiceConfig()); args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR, response_generator_.get()); @@ -239,6 +243,10 @@ class ServiceConfigEnd2endTest : public ::testing::Test { std::shared_ptr BuildChannelWithInvalidDefaultServiceConfig() { ChannelArguments args; + EXPECT_THAT( + grpc::experimental::ValidateServiceConfigJSON( + InvalidDefaultServiceConfig()), + ::testing::HasSubstr("failed to parse JSON for service config")); args.SetServiceConfigJSON(InvalidDefaultServiceConfig()); args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR, response_generator_.get()); diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 667b9b3541e..913d7be993b 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -1040,7 +1040,8 @@ include/grpcpp/support/status_code_enum.h \ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ -include/grpcpp/support/time.h +include/grpcpp/support/time.h \ +include/grpcpp/support/validate_service_config.h # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index f4e6caa418a..8b664d8ac01 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1043,6 +1043,7 @@ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ include/grpcpp/support/time.h \ +include/grpcpp/support/validate_service_config.h \ src/core/ext/filters/client_channel/health/health.pb.c \ src/core/ext/filters/client_channel/health/health.pb.h \ src/core/ext/transport/inproc/inproc_transport.h \ @@ -1245,6 +1246,7 @@ src/cpp/common/secure_auth_context.cc \ src/cpp/common/secure_auth_context.h \ src/cpp/common/secure_channel_arguments.cc \ src/cpp/common/secure_create_auth_context.cc \ +src/cpp/common/validate_service_config.cc \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/channel_argument_option.cc \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index dd4341c2736..a9dc9fcfe3f 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -10404,6 +10404,7 @@ "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", "include/grpcpp/support/time.h", + "include/grpcpp/support/validate_service_config.h", "src/cpp/client/create_channel_internal.h", "src/cpp/common/channel_filter.h", "src/cpp/server/dynamic_thread_pool.h", @@ -10530,6 +10531,7 @@ "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", "include/grpcpp/support/time.h", + "include/grpcpp/support/validate_service_config.h", "src/cpp/client/channel_cc.cc", "src/cpp/client/client_context.cc", "src/cpp/client/client_interceptor.cc", @@ -10547,6 +10549,7 @@ "src/cpp/common/core_codegen.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", + "src/cpp/common/validate_service_config.cc", "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/channel_argument_option.cc",