Skip to content

Commit

Permalink
Add File Watcher Certificate Provider API
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenLian committed Nov 19, 2020
1 parent e6e6be4 commit d74e43d
Show file tree
Hide file tree
Showing 26 changed files with 1,949 additions and 147 deletions.
42 changes: 42 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx google_mesh_ca_certificate_provider_factory_test)
add_dependencies(buildtests_cxx grpc_cli)
add_dependencies(buildtests_cxx grpc_tls_certificate_distributor_test)
add_dependencies(buildtests_cxx grpc_tls_certificate_provider_test)
add_dependencies(buildtests_cxx grpc_tls_credentials_options_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_cxx grpc_tool_test)
Expand Down Expand Up @@ -11696,6 +11697,7 @@ if(gRPC_BUILD_TESTS)

add_executable(grpc_tls_certificate_distributor_test
test/core/security/grpc_tls_certificate_distributor_test.cc
test/core/security/tls_utils.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
Expand Down Expand Up @@ -11729,11 +11731,51 @@ target_link_libraries(grpc_tls_certificate_distributor_test
)


endif()
if(gRPC_BUILD_TESTS)

add_executable(grpc_tls_certificate_provider_test
test/core/security/grpc_tls_certificate_provider_test.cc
test/core/security/tls_utils.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)

target_include_directories(grpc_tls_certificate_provider_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)

target_link_libraries(grpc_tls_certificate_provider_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
grpc
gpr
address_sorting
upb
)


endif()
if(gRPC_BUILD_TESTS)

add_executable(grpc_tls_credentials_options_test
test/core/security/grpc_tls_credentials_options_test.cc
test/core/security/tls_utils.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
Expand Down
23 changes: 21 additions & 2 deletions build_autogenerated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6208,9 +6208,26 @@ targets:
gtest: true
build: test
language: c++
headers: []
headers:
- test/core/security/tls_utils.h
src:
- test/core/security/grpc_tls_certificate_distributor_test.cc
- test/core/security/tls_utils.cc
deps:
- grpc_test_util
- grpc
- gpr
- address_sorting
- upb
- name: grpc_tls_certificate_provider_test
gtest: true
build: test
language: c++
headers:
- test/core/security/tls_utils.h
src:
- test/core/security/grpc_tls_certificate_provider_test.cc
- test/core/security/tls_utils.cc
deps:
- grpc_test_util
- grpc
Expand All @@ -6221,9 +6238,11 @@ targets:
gtest: true
build: test
language: c++
headers: []
headers:
- test/core/security/tls_utils.h
src:
- test/core/security/grpc_tls_credentials_options_test.cc
- test/core/security/tls_utils.cc
deps:
- grpc_test_util
- grpc
Expand Down
1 change: 1 addition & 0 deletions grpc.def
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ EXPORTS
grpc_tls_identity_pairs_add_pair
grpc_tls_identity_pairs_destroy
grpc_tls_certificate_provider_static_data_create
grpc_tls_certificate_provider_file_watcher_create
grpc_tls_certificate_provider_release
grpc_tls_credentials_options_create
grpc_tls_credentials_options_set_cert_request_type
Expand Down
25 changes: 25 additions & 0 deletions include/grpc/grpc_security.h
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,31 @@ GRPCAPI grpc_tls_certificate_provider*
grpc_tls_certificate_provider_static_data_create(
const char* root_certificate, grpc_tls_identity_pairs* pem_key_cert_pairs);

/**
* Creates a grpc_tls_certificate_provider that will watch the credential
* changes on the file system. This provider will always return the up-to-date
* cert data for all the cert names callers set through
* |grpc_tls_credentials_options|. Note that this API only supports one key-cert
* file and hence one set of identity key-cert pair, so SNI(Server Name
* Indication) is not supported.
* - private_key_path is the file path of the private key. This must be set if
* |identity_certificate_path| is set. Otherwise, it could be null if no
* identity credentials are needed.
* - identity_certificate_path is the file path of the identity certificate
* chain. This must be set if |private_key_path| is set. Otherwise, it could
* be null if no identity credentials are needed.
* - root_cert_path is the file path to the root certificate bundle. This
* may be null if no root certs are needed.
* - refresh_interval_sec is the refreshing interval that we will check the
* files for updates.
* It does not take ownership of parameters.
* It is used for experimental purpose for now and subject to change.
*/
GRPCAPI grpc_tls_certificate_provider*
grpc_tls_certificate_provider_file_watcher_create(
const char* private_key_path, const char* identity_certificate_path,
const char* root_cert_path, unsigned int refresh_interval_sec);

/**
* Releases a grpc_tls_certificate_provider object. The creator of the
* grpc_tls_certificate_provider object is responsible for its release. It is
Expand Down
52 changes: 51 additions & 1 deletion include/grpcpp/security/tls_certificate_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,57 @@ class StaticDataCertificateProvider : public CertificateProviderInterface {
const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs)
: StaticDataCertificateProvider("", identity_key_cert_pairs) {}

~StaticDataCertificateProvider();
~StaticDataCertificateProvider() override;

grpc_tls_certificate_provider* c_provider() override { return c_provider_; }

private:
grpc_tls_certificate_provider* c_provider_ = nullptr;
};

// A CertificateProviderInterface implementation that will watch the credential
// changes on the file system. This provider will always return the up-to-date
// cert data for all the cert names callers set through |TlsCredentialsOptions|.
// Several things to note:
// 1. This API only supports one key-cert file and hence one set of identity
// key-cert pair, so SNI(Server Name Indication) is not supported.
// 2. The private key and identity certificate should always match. This API
// guarantees atomic read, and it is the callers' responsibility to do atomic
// updates. There are many ways to atomically update the key and certs in the
// file system. To name a few:
// 1) creating a new directory, renaming the old directory to a new name, and
// then renaming the new directory to the original name of the old directory.
// 2) using a symlink for the directory. When need to change, put new
// credential data in a new directory, and change symlink.
class FileWatcherCertificateProvider final
: public CertificateProviderInterface {
public:
// Constructor to get credential updates from root and identity file paths.
//
// @param private_key_path is the file path of the private key.
// @param identity_certificate_path is the file path of the identity
// certificate chain.
// @param root_cert_path is the file path to the root certificate bundle.
// @param refresh_interval_sec is the refreshing interval that we will check
// the files for updates.
FileWatcherCertificateProvider(const std::string& private_key_path,
const std::string& identity_certificate_path,
const std::string& root_cert_path,
unsigned int refresh_interval_sec);
// Constructor to get credential updates from identity file paths only.
FileWatcherCertificateProvider(const std::string& private_key_path,
const std::string& identity_certificate_path,
unsigned int refresh_interval_sec)
: FileWatcherCertificateProvider(private_key_path,
identity_certificate_path, "",
refresh_interval_sec) {}
// Constructor to get credential updates from root file path only.
FileWatcherCertificateProvider(const std::string& root_cert_path,
unsigned int refresh_interval_sec)
: FileWatcherCertificateProvider("", "", root_cert_path,
refresh_interval_sec) {}

~FileWatcherCertificateProvider() override;

grpc_tls_certificate_provider* c_provider() override { return c_provider_; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ struct grpc_tls_certificate_distributor
grpc_error* identity_cert_error) = 0;
};

// Sets the key materials based on their certificate name. Note that we are
// not doing any copies for pem_root_certs and pem_key_cert_pairs. For
// pem_root_certs, the original string contents need to outlive the
// distributor; for pem_key_cert_pairs, internally it is taking two
// unique_ptr(s) to the credential string, so the ownership is actually
// transferred.
// Sets the key materials based on their certificate name.
//
// @param cert_name The name of the certificates being updated.
// @param pem_root_certs The content of root certificates.
Expand Down
Loading

0 comments on commit d74e43d

Please sign in to comment.