Changes requested by Yihua.

pull/19778/head
Matthew Stevenson 5 years ago
parent e89efbc1ac
commit 00cce90adf
  1. 24
      include/grpcpp/security/tls_credentials_options.h
  2. 26
      src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h
  3. 2
      src/cpp/common/tls_credentials_options.cc
  4. 124
      test/cpp/client/credentials_test.cc

@ -139,6 +139,11 @@ class TlsCredentialReloadConfig {
int Schedule(TlsCredentialReloadArg* arg) const {
if (credential_reload_interface_ == nullptr) {
gpr_log(GPR_ERROR, "credential reload interface is nullptr");
if (arg != nullptr) {
arg->set_status(GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL);
arg->set_error_details(
"the interface of the credential reload config is nullptr");
}
return 1;
}
return credential_reload_interface_->Schedule(arg);
@ -147,6 +152,11 @@ class TlsCredentialReloadConfig {
void Cancel(TlsCredentialReloadArg* arg) const {
if (credential_reload_interface_ == nullptr) {
gpr_log(GPR_ERROR, "credential reload interface is nullptr");
if (arg != nullptr) {
arg->set_status(GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL);
arg->set_error_details(
"the interface of the credential reload config is nullptr");
}
return;
}
credential_reload_interface_->Cancel(arg);
@ -233,6 +243,12 @@ class TlsServerAuthorizationCheckConfig {
int Schedule(TlsServerAuthorizationCheckArg* arg) const {
if (server_authorization_check_interface_ == nullptr) {
gpr_log(GPR_ERROR, "server authorization check interface is nullptr");
if (arg != nullptr) {
arg->set_status(GRPC_STATUS_NOT_FOUND);
arg->set_error_details(
"the interface of the server authorization check config is "
"nullptr");
}
return 1;
}
return server_authorization_check_interface_->Schedule(arg);
@ -241,12 +257,18 @@ class TlsServerAuthorizationCheckConfig {
void Cancel(TlsServerAuthorizationCheckArg* arg) const {
if (server_authorization_check_interface_ == nullptr) {
gpr_log(GPR_ERROR, "server authorization check interface is nullptr");
if (arg != nullptr) {
arg->set_status(GRPC_STATUS_NOT_FOUND);
arg->set_error_details(
"the interface of the server authorization check config is "
"nullptr");
}
return;
}
server_authorization_check_interface_->Cancel(arg);
}
/** Creates C struct for the server authorization check config. **/
/** Returns C struct for the server authorization check config. **/
grpc_tls_server_authorization_check_config* c_config() const {
return c_config_;
}

@ -77,9 +77,14 @@ struct grpc_tls_credential_reload_config
int Schedule(grpc_tls_credential_reload_arg* arg) const {
if (schedule_ == nullptr) {
gpr_log(GPR_ERROR, "schedule API is nullptr");
if (arg != nullptr) {
arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL;
arg->error_details =
gpr_strdup("schedule API in credential reload config is nullptr");
}
return 1;
}
if (arg != nullptr && context_ != nullptr) {
if (arg != nullptr) {
arg->config = const_cast<grpc_tls_credential_reload_config*>(this);
}
return schedule_(config_user_data_, arg);
@ -87,9 +92,14 @@ struct grpc_tls_credential_reload_config
void Cancel(grpc_tls_credential_reload_arg* arg) const {
if (cancel_ == nullptr) {
gpr_log(GPR_ERROR, "cancel API is nullptr.");
if (arg != nullptr) {
arg->status = GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL;
arg->error_details =
gpr_strdup("cancel API in credential reload config is nullptr");
}
return;
}
if (arg != nullptr && context_ != nullptr) {
if (arg != nullptr) {
arg->config = const_cast<grpc_tls_credential_reload_config*>(this);
}
cancel_(config_user_data_, arg);
@ -143,6 +153,11 @@ struct grpc_tls_server_authorization_check_config
int Schedule(grpc_tls_server_authorization_check_arg* arg) const {
if (schedule_ == nullptr) {
gpr_log(GPR_ERROR, "schedule API is nullptr");
if (arg != nullptr) {
arg->status = GRPC_STATUS_NOT_FOUND;
arg->error_details = gpr_strdup(
"schedule API in server authorization check config is nullptr");
}
return 1;
}
if (arg != nullptr && context_ != nullptr) {
@ -154,9 +169,14 @@ struct grpc_tls_server_authorization_check_config
void Cancel(grpc_tls_server_authorization_check_arg* arg) const {
if (cancel_ == nullptr) {
gpr_log(GPR_ERROR, "cancel API is nullptr.");
if (arg != nullptr) {
arg->status = GRPC_STATUS_NOT_FOUND;
arg->error_details = gpr_strdup(
"schedule API in server authorization check config is nullptr");
}
return;
}
if (arg != nullptr && context_ != nullptr) {
if (arg != nullptr) {
arg->config =
const_cast<grpc_tls_server_authorization_check_config*>(this);
}

@ -19,6 +19,8 @@
#include <grpcpp/security/tls_credentials_options.h>
#include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h"
#include <grpc/support/alloc.h>
#include "src/cpp/common/tls_credentials_options_util.h"
namespace grpc_impl {

@ -314,12 +314,16 @@ typedef class ::grpc_impl::experimental::TlsCredentialReloadConfig
TlsCredentialReloadConfig;
TEST_F(CredentialsTest, TlsCredentialReloadArgCallback) {
grpc_tls_credential_reload_arg c_arg;
c_arg.cb = tls_credential_reload_callback;
TlsCredentialReloadArg arg = TlsCredentialReloadArg(&c_arg);
arg.set_status(GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW);
arg.OnCredentialReloadDoneCallback();
EXPECT_EQ(arg.status(), GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_UNCHANGED);
grpc_tls_credential_reload_arg* c_arg = new grpc_tls_credential_reload_arg;
c_arg->cb = tls_credential_reload_callback;
TlsCredentialReloadArg* arg = new TlsCredentialReloadArg(c_arg);
arg->set_status(GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW);
arg->OnCredentialReloadDoneCallback();
EXPECT_EQ(arg->status(), GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_UNCHANGED);
// Cleanup.
delete arg;
delete c_arg;
}
TEST_F(CredentialsTest, TlsCredentialReloadConfigSchedule) {
@ -427,35 +431,39 @@ typedef class ::grpc_impl::experimental::TlsServerAuthorizationCheckConfig
TlsServerAuthorizationCheckConfig;
TEST_F(CredentialsTest, TlsServerAuthorizationCheckArgCallback) {
grpc_tls_server_authorization_check_arg c_arg;
c_arg.cb = tls_server_authorization_check_callback;
TlsServerAuthorizationCheckArg arg(&c_arg);
arg.set_cb_user_data(nullptr);
arg.set_success(0);
arg.set_target_name("target_name");
arg.set_peer_cert("peer_cert");
arg.set_status(GRPC_STATUS_UNAUTHENTICATED);
arg.set_error_details("error_details");
const char* target_name_before_callback = c_arg.target_name;
const char* peer_cert_before_callback = c_arg.peer_cert;
const char* error_details_before_callback = c_arg.error_details;
arg.OnServerAuthorizationCheckDoneCallback();
EXPECT_STREQ(static_cast<char*>(arg.cb_user_data()), "cb_user_data");
gpr_free(arg.cb_user_data());
EXPECT_EQ(arg.success(), 1);
EXPECT_STREQ(arg.target_name().c_str(), "callback_target_name");
EXPECT_STREQ(arg.peer_cert().c_str(), "callback_peer_cert");
EXPECT_EQ(arg.status(), GRPC_STATUS_OK);
EXPECT_STREQ(arg.error_details().c_str(), "callback_error_details");
grpc_tls_server_authorization_check_arg* c_arg =
new grpc_tls_server_authorization_check_arg;
c_arg->cb = tls_server_authorization_check_callback;
TlsServerAuthorizationCheckArg* arg =
new TlsServerAuthorizationCheckArg(c_arg);
arg->set_cb_user_data(nullptr);
arg->set_success(0);
arg->set_target_name("target_name");
arg->set_peer_cert("peer_cert");
arg->set_status(GRPC_STATUS_UNAUTHENTICATED);
arg->set_error_details("error_details");
const char* target_name_before_callback = c_arg->target_name;
const char* peer_cert_before_callback = c_arg->peer_cert;
const char* error_details_before_callback = c_arg->error_details;
arg->OnServerAuthorizationCheckDoneCallback();
EXPECT_STREQ(static_cast<char*>(arg->cb_user_data()), "cb_user_data");
gpr_free(arg->cb_user_data());
EXPECT_EQ(arg->success(), 1);
EXPECT_STREQ(arg->target_name().c_str(), "callback_target_name");
EXPECT_STREQ(arg->peer_cert().c_str(), "callback_peer_cert");
EXPECT_EQ(arg->status(), GRPC_STATUS_OK);
EXPECT_STREQ(arg->error_details().c_str(), "callback_error_details");
// Cleanup.
gpr_free(const_cast<char*>(target_name_before_callback));
gpr_free(const_cast<char*>(peer_cert_before_callback));
gpr_free(const_cast<char*>(error_details_before_callback));
gpr_free(const_cast<char*>(c_arg.target_name));
gpr_free(const_cast<char*>(c_arg.peer_cert));
gpr_free(const_cast<char*>(c_arg.error_details));
gpr_free(const_cast<char*>(c_arg->target_name));
gpr_free(const_cast<char*>(c_arg->peer_cert));
gpr_free(const_cast<char*>(c_arg->error_details));
delete arg;
delete c_arg;
}
TEST_F(CredentialsTest, TlsServerAuthorizationCheckConfigSchedule) {
@ -654,6 +662,62 @@ TEST_F(CredentialsTest, LoadSpiffeChannelCredentials) {
GPR_ASSERT(channel_credentials != nullptr);
}
TEST_F(CredentialsTest, TlsCredentialReloadConfigErrorMessages) {
std::shared_ptr<TlsCredentialReloadConfig> config(
new TlsCredentialReloadConfig(nullptr));
grpc_tls_credential_reload_arg* c_arg = new grpc_tls_credential_reload_arg;
TlsCredentialReloadArg* arg = new TlsCredentialReloadArg(c_arg);
int schedule_output = config->Schedule(arg);
EXPECT_EQ(schedule_output, 1);
EXPECT_EQ(arg->status(), GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL);
EXPECT_STREQ(arg->error_details().c_str(),
"the interface of the credential reload config is nullptr");
gpr_free(const_cast<char*>(c_arg->error_details));
arg->set_status(GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_UNCHANGED);
config->Cancel(arg);
EXPECT_EQ(arg->status(), GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL);
EXPECT_STREQ(arg->error_details().c_str(),
"the interface of the credential reload config is nullptr");
// Cleanup.
gpr_free(const_cast<char*>(c_arg->error_details));
delete arg;
delete c_arg;
gpr_free(config->c_config());
}
TEST_F(CredentialsTest, TlsServerAuthorizationCheckConfigErrorMessages) {
std::shared_ptr<TlsServerAuthorizationCheckConfig> config(
new TlsServerAuthorizationCheckConfig(nullptr));
grpc_tls_server_authorization_check_arg* c_arg =
new grpc_tls_server_authorization_check_arg;
TlsServerAuthorizationCheckArg* arg =
new TlsServerAuthorizationCheckArg(c_arg);
int schedule_output = config->Schedule(arg);
EXPECT_EQ(schedule_output, 1);
EXPECT_EQ(arg->status(), GRPC_STATUS_NOT_FOUND);
EXPECT_STREQ(
arg->error_details().c_str(),
"the interface of the server authorization check config is nullptr");
gpr_free(const_cast<char*>(c_arg->error_details));
arg->set_status(GRPC_STATUS_OK);
config->Cancel(arg);
EXPECT_EQ(arg->status(), GRPC_STATUS_NOT_FOUND);
EXPECT_STREQ(
arg->error_details().c_str(),
"the interface of the server authorization check config is nullptr");
// Cleanup.
gpr_free(const_cast<char*>(c_arg->error_details));
delete arg;
delete c_arg;
gpr_free(config->c_config());
}
} // namespace testing
} // namespace grpc

Loading…
Cancel
Save