Hid some key materials API's and refactored credential reload arg wrapper.

pull/19778/head
Matthew Stevenson 6 years ago
parent f3f8bf05b2
commit 1e46b38d66
  1. 47
      include/grpcpp/security/tls_credentials_options.h
  2. 80
      src/cpp/common/tls_credentials_options.cc
  3. 20
      test/cpp/client/credentials_test.cc

@ -44,66 +44,39 @@ class TlsKeyMaterialsConfig {
return pem_key_cert_pair_list_;
}
/**Setter for member fields. **/
/** Setter for key materials that will be called by the user. The setter
* transfers ownership of the arguments to the config. **/
void set_key_materials(grpc::string pem_root_certs,
::std::vector<PemKeyCertPair> pem_key_cert_pair_list);
/** Creates C struct for key materials. **/
grpc_tls_key_materials_config* c_key_materials() const;
private:
::std::vector<PemKeyCertPair> pem_key_cert_pair_list_;
grpc::string pem_root_certs_;
};
/** Creates smart pointer to a C++ version of the C key materials. **/
::std::shared_ptr<TlsKeyMaterialsConfig> tls_key_materials_c_to_cpp(
const grpc_tls_key_materials_config* config);
/** TLS credential reload arguments, wraps grpc_tls_credential_reload_arg. **/
typedef class TlsCredentialReloadArg TlsCredentialReloadArg;
typedef void (*grpcpp_tls_on_credential_reload_done_cb)(
TlsCredentialReloadArg* arg);
class TlsCredentialReloadArg {
public:
/** Getters for member fields. **/
grpcpp_tls_on_credential_reload_done_cb cb() const { return cb_; }
void* cb_user_data() const { return cb_user_data_; }
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config() const {
return key_materials_config_;
}
grpc_ssl_certificate_config_reload_status status() const { return status_; }
grpc::string error_details() const { return error_details_; }
/** Getters for member fields. The callback function is not exposed. **/
void* cb_user_data() const;
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config() const;
grpc_ssl_certificate_config_reload_status status() const;
grpc::string error_details() const;
/** Setters for member fields. **/
void set_cb(grpcpp_tls_on_credential_reload_done_cb cb);
void set_cb_user_data(void* cb_user_data);
void set_key_materials_config(
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config);
void set_status(grpc_ssl_certificate_config_reload_status status);
void set_error_details(grpc::string error_details);
/** Creates C struct for credential reload arg. **/
grpc_tls_credential_reload_arg* c_credential_reload_arg() const;
/** Creates C callback function from C++ callback function. **/
grpc_tls_on_credential_reload_done_cb c_callback() const;
/** Calls the C arg's callback function. **/
void callback() ;
private:
grpcpp_tls_on_credential_reload_done_cb cb_;
void* cb_user_data_;
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config_;
grpc_ssl_certificate_config_reload_status status_;
grpc::string error_details_;
grpc_tls_credential_reload_arg* c_arg_;
};
/** Creates a smart pointer to a C++ version of the credential reload argument,
* with the callback function set to a nullptr. **/
::std::unique_ptr<TlsCredentialReloadArg> tls_credential_reload_arg_c_to_cpp(
const grpc_tls_credential_reload_arg* arg);
/** TLS credential reloag config, wraps grpc_tls_credential_reload_config. **/
class TlsCredentialReloadConfig {
public:

@ -23,7 +23,7 @@
namespace grpc_impl {
namespace experimental {
/** gRPC TLS key materials config API implementation **/
/** TLS key materials config API implementation **/
void TlsKeyMaterialsConfig::set_key_materials(
grpc::string pem_root_certs,
::std::vector<PemKeyCertPair> pem_key_cert_pair_list) {
@ -31,25 +31,28 @@ void TlsKeyMaterialsConfig::set_key_materials(
pem_root_certs_ = ::std::move(pem_root_certs);
}
grpc_tls_key_materials_config* TlsKeyMaterialsConfig::c_key_materials() const {
namespace {
/** Creates a new C struct for the key materials. **/
grpc_tls_key_materials_config* c_key_materials(::std::shared_ptr<TlsKeyMaterialsConfig> config) {
grpc_tls_key_materials_config* c_config =
grpc_tls_key_materials_config_create();
::grpc_core::InlinedVector<::grpc_core::PemKeyCertPair, 1>
c_pem_key_cert_pair_list;
for (auto key_cert_pair = pem_key_cert_pair_list_.begin();
key_cert_pair != pem_key_cert_pair_list_.end(); key_cert_pair++) {
for (auto key_cert_pair = config->pem_key_cert_pair_list().begin();
key_cert_pair != config->pem_key_cert_pair_list().end(); key_cert_pair++) {
::grpc_core::PemKeyCertPair c_pem_key_cert_pair =
::grpc_core::PemKeyCertPair(key_cert_pair->private_key.c_str(),
key_cert_pair->cert_chain.c_str());
c_pem_key_cert_pair_list.push_back(::std::move(c_pem_key_cert_pair));
}
::grpc_core::UniquePtr<char> c_pem_root_certs(
gpr_strdup(pem_root_certs_.c_str()));
gpr_strdup(config->pem_root_certs().c_str()));
c_config->set_key_materials(::std::move(c_pem_root_certs),
::std::move(c_pem_key_cert_pair_list));
return c_config;
}
/** Creates a new TlsKeyMaterialsConfig from a C struct config. **/
::std::shared_ptr<TlsKeyMaterialsConfig> tls_key_materials_c_to_cpp(
const grpc_tls_key_materials_config* config) {
::std::shared_ptr<TlsKeyMaterialsConfig> cpp_config(
@ -70,37 +73,51 @@ grpc_tls_key_materials_config* TlsKeyMaterialsConfig::c_key_materials() const {
::std::move(cpp_pem_key_cert_pair_list));
return cpp_config;
}
} // namespace
/** gRPC TLS credential reload arg API implementation **/
void TlsCredentialReloadArg::set_cb(
grpcpp_tls_on_credential_reload_done_cb cb) {
cb_ = cb;
/** TLS credential reload arg API implementation **/
void* TlsCredentialReloadArg::cb_user_data() const {
return c_arg_->cb_user_data;
}
/** This function creates a new TlsKeyMaterialsConfig instance whose fields are
* not shared with the corresponding key materials config fields of the
* TlsCredentialReloadArg instance. **/
::std::shared_ptr<TlsKeyMaterialsConfig> TlsCredentialReloadArg::key_materials_config() const {
return tls_key_materials_c_to_cpp(c_arg_->key_materials_config);
}
grpc_ssl_certificate_config_reload_status TlsCredentialReloadArg::status() const {
return c_arg_->status;
}
grpc::string TlsCredentialReloadArg::error_details() const {
return static_cast<grpc::string>(c_arg_->error_details);
}
void TlsCredentialReloadArg::set_cb_user_data(void* cb_user_data) {
cb_user_data_ = cb_user_data;
c_arg_->cb_user_data = cb_user_data;
}
void TlsCredentialReloadArg::set_key_materials_config(
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config) {
key_materials_config_ = ::std::move(key_materials_config);
c_arg_->key_materials_config = c_key_materials(key_materials_config);
}
void TlsCredentialReloadArg::set_status(
grpc_ssl_certificate_config_reload_status status) {
status_ = status;
c_arg_->status = status;
}
void TlsCredentialReloadArg::set_error_details(grpc::string error_details) {
error_details_ = ::std::move(error_details);
c_arg_->error_details = gpr_strdup(error_details.c_str());
}
::std::unique_ptr<TlsCredentialReloadArg> tls_credential_reload_arg_c_to_cpp(
const grpc_tls_credential_reload_arg* arg) {
::std::unique_ptr<TlsCredentialReloadArg> cpp_arg(
new TlsCredentialReloadArg());
cpp_arg->set_cb(
static_cast<grpcpp_tls_on_credential_reload_done_cb>(nullptr));
namespace {
/** Creates a smart pointer to a C++ version of the credential reload argument,
* with the callback function set to a nullptr. **/
::std::unique_ptr<TlsCredentialReloadArg> tls_credential_reload_arg_c_to_cpp(const grpc_tls_credential_reload_arg* arg) {
::std::unique_ptr<TlsCredentialReloadArg> cpp_arg(new TlsCredentialReloadArg());
cpp_arg->set_cb_user_data(arg->cb_user_data);
cpp_arg->set_key_materials_config(
tls_key_materials_c_to_cpp(arg->key_materials_config));
@ -108,26 +125,10 @@ void TlsCredentialReloadArg::set_error_details(grpc::string error_details) {
cpp_arg->set_error_details(arg->error_details);
return cpp_arg;
}
} // namespace
grpc_tls_on_credential_reload_done_cb TlsCredentialReloadArg::c_callback()
const {
grpcpp_tls_on_credential_reload_done_cb cpp_cb = cb_;
std::function<void(grpc_tls_credential_reload_arg*)> c_cb =
[cpp_cb](grpc_tls_credential_reload_arg* arg) {
return cpp_cb(tls_credential_reload_arg_c_to_cpp(arg).get());
};
return *(c_cb.target<grpc_tls_on_credential_reload_done_cb>());
}
grpc_tls_credential_reload_arg*
TlsCredentialReloadArg::c_credential_reload_arg() const {
grpc_tls_credential_reload_arg* c_arg = new grpc_tls_credential_reload_arg();
c_arg->cb = this->c_callback();
c_arg->cb_user_data = cb_user_data_;
c_arg->key_materials_config = key_materials_config_->c_key_materials();
c_arg->status = status_;
c_arg->error_details = gpr_strdup(error_details_.c_str());
return c_arg;
void TlsCredentialReloadArg::callback() {
c_arg_->cb(c_arg_);
}
/** gRPC TLS credential reload config API implementation **/
@ -276,8 +277,7 @@ grpc_tls_credentials_options* TlsCredentialsOptions::c_credentials_options()
grpc_tls_credentials_options_create();
c_options->set_cert_request_type(cert_request_type_);
c_options->set_key_materials_config(
::grpc_core::RefCountedPtr<grpc_tls_key_materials_config>(
key_materials_config_->c_key_materials()));
::grpc_core::RefCountedPtr<grpc_tls_key_materials_config>(c_key_materials(key_materials_config_)));
c_options->set_credential_reload_config(
::grpc_core::RefCountedPtr<grpc_tls_credential_reload_config>(
credential_reload_config_->c_credential_reload()));

@ -198,6 +198,7 @@ TEST_F(CredentialsTest, StsCredentialsOptionsFromEnv) {
gpr_unsetenv("STS_CREDENTIALS");
}
/**
typedef class ::grpc_impl::experimental::TlsKeyMaterialsConfig
TlsKeyMaterialsConfig;
@ -216,7 +217,9 @@ TEST_F(CredentialsTest, TlsKeyMaterialsConfigCppToC) {
c_config->pem_key_cert_pair_list()[0].cert_chain());
gpr_free(c_config);
}
**/
/**
TEST_F(CredentialsTest, TlsKeyMaterialsCtoCpp) {
grpc_tls_key_materials_config c_config;
::grpc_core::PemKeyCertPair pem_key_cert_pair =
@ -236,17 +239,18 @@ TEST_F(CredentialsTest, TlsKeyMaterialsCtoCpp) {
EXPECT_STREQ("private_key", cpp_pair_list[0].private_key.c_str());
EXPECT_STREQ("cert_chain", cpp_pair_list[0].cert_chain.c_str());
}
**/
typedef class ::grpc_impl::experimental::TlsCredentialReloadArg
TlsCredentialReloadArg;
typedef class ::grpc_impl::experimental::TlsCredentialReloadConfig
TlsCredentialReloadConfig;
typedef void (*grpcpp_tls_on_credential_reload_done_cb)(
TlsCredentialReloadArg* arg);
//typedef class ::grpc_impl::experimental::TlsCredentialReloadArg
// TlsCredentialReloadArg;
//typedef class ::grpc_impl::experimental::TlsCredentialReloadConfig
// TlsCredentialReloadConfig;
//typedef void (*grpcpp_tls_on_credential_reload_done_cb)(
// TlsCredentialReloadArg* arg);
/**
TEST_F(CredentialsTest, TlsCredentialReloadArgCppToC) {
TlsCredentialReloadArg arg;
arg.set_cb(static_cast<grpcpp_tls_on_credential_reload_done_cb>(nullptr));
arg.set_cb_user_data(nullptr);
::std::shared_ptr<TlsKeyMaterialsConfig> key_materials_config;
struct TlsKeyMaterialsConfig::PemKeyCertPair pair1 = {"private_key1",
@ -280,6 +284,8 @@ TEST_F(CredentialsTest, TlsCredentialReloadArgCppToC) {
EXPECT_STREQ(c_arg->error_details, "error_details");
}
**/
/**
TEST_F(CredentialsTest, TlsCredentialReloadConfigCppToC) {
TlsCredentialReloadConfig config =

Loading…
Cancel
Save