grpc_tls_certificate_distributor.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H
18 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H
19 
21 
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <set>
26 #include <string>
27 #include <utility>
28 
29 #include "absl/base/thread_annotations.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/types/optional.h"
32 
37 
40 };
41 
42 // TLS certificate distributor.
44  : public grpc_core::RefCounted<grpc_tls_certificate_distributor> {
45  public:
46  // Interface for watching TLS certificates update.
48  public:
49  virtual ~TlsCertificatesWatcherInterface() = default;
50 
51  // Handles the delivery of the updated root and identity certificates.
52  // An absl::nullopt value indicates no corresponding contents for
53  // root_certs or key_cert_pairs. Note that we will send updates of the
54  // latest contents for both root and identity certificates, even when only
55  // one side of it got updated.
56  //
57  // @param root_certs the contents of the reloaded root certs.
58  // @param key_cert_pairs the contents of the reloaded identity key-cert
59  // pairs.
60  virtual void OnCertificatesChanged(
63 
64  // Handles an error that occurs while attempting to fetch certificate data.
65  // Note that if a watcher sees an error, it simply means the Provider is
66  // having problems renewing new data. If the watcher has previously received
67  // several OnCertificatesChanged, all the data received from that function
68  // is valid.
69  // In that case, watcher might simply log the error. If the watcher hasn't
70  // received any OnCertificatesChanged before the error occurs, no valid
71  // data is available yet, and the watcher should either fail or "waiting"
72  // for the valid data in a non-blocking way.
73  //
74  // @param root_cert_error the error occurred while reloading root
75  // certificates.
76  // @param identity_cert_error the error occurred while reloading identity
77  // certificates.
78  virtual void OnError(grpc_error_handle root_cert_error,
79  grpc_error_handle identity_cert_error) = 0;
80  };
81 
82  // Sets the key materials based on their certificate name.
83  //
84  // @param cert_name The name of the certificates being updated.
85  // @param pem_root_certs The content of root certificates.
86  // @param pem_key_cert_pairs The content of identity key-cert pairs.
87  void SetKeyMaterials(
90 
91  bool HasRootCerts(const std::string& root_cert_name);
92 
93  bool HasKeyCertPairs(const std::string& identity_cert_name);
94 
95  // Propagates the error that the caller (e.g. Producer) encounters to all the
96  // watchers watching a particular certificate name.
97  //
98  // @param cert_name The watching cert name of the watchers that the caller
99  // wants to notify when encountering error.
100  // @param root_cert_error The error that the caller encounters when reloading
101  // root certs.
102  // @param identity_cert_error The error that the caller encounters when
103  // reloading identity certs.
104  void SetErrorForCert(const std::string& cert_name,
105  absl::optional<grpc_error_handle> root_cert_error,
106  absl::optional<grpc_error_handle> identity_cert_error);
107 
108  // Propagates the error that the caller (e.g. Producer) encounters to all
109  // watchers.
110  //
111  // @param error The error that the caller encounters.
113 
114  // Sets the TLS certificate watch status callback function. The
115  // grpc_tls_certificate_distributor will invoke this callback when a new
116  // certificate name is watched by a newly registered watcher, or when a
117  // certificate name is no longer watched by any watchers.
118  // Note that when the callback shows a cert is no longer being watched, the
119  // distributor will delete the corresponding certificate data from its cache,
120  // and clear the corresponding error, if there is any. This means that if the
121  // callback subsequently says the same cert is now being watched again, the
122  // provider must re-provide the credentials or re-invoke the errors to the
123  // distributor, to indicate a successful or failed reloading.
124  // @param callback The callback function being set by the caller, e.g the
125  // Producer. Note that this callback will be invoked for each certificate
126  // name.
127  //
128  // For the parameters in the callback function:
129  // string_value The name of the certificates being watched.
130  // bool_value_1 If the root certificates with the specific name are being
131  // watched. bool_value_2 If the identity certificates with the specific name
132  // are being watched.
134  std::function<void(std::string, bool, bool)> callback) {
136  watch_status_callback_ = std::move(callback);
137  };
138 
139  // Registers a watcher. The caller may keep a raw pointer to the watcher,
140  // which may be used only for cancellation. (Because the caller does not own
141  // the watcher, the pointer must not be used for any other purpose.) At least
142  // one of root_cert_name and identity_cert_name must be specified.
143  //
144  // @param watcher The watcher being registered.
145  // @param root_cert_name The name of the root certificates that will be
146  // watched. If set to absl::nullopt, the root certificates won't be watched.
147  // @param identity_cert_name The name of the identity certificates that will
148  // be watched. If set to absl::nullopt, the identity certificates won't be
149  // watched.
151  std::unique_ptr<TlsCertificatesWatcherInterface> watcher,
152  absl::optional<std::string> root_cert_name,
153  absl::optional<std::string> identity_cert_name);
154 
155  // Cancels a watcher.
156  //
157  // @param watcher The watcher being cancelled.
158  void CancelTlsCertificatesWatch(TlsCertificatesWatcherInterface* watcher);
159 
160  private:
161  // Contains the information about each watcher.
162  struct WatcherInfo {
163  std::unique_ptr<TlsCertificatesWatcherInterface> watcher;
166  };
167  // CertificateInfo contains the credential contents and some additional
168  // watcher information.
169  // Note that having errors doesn't indicate the corresponding credentials are
170  // invalid. For example, if root_cert_error != nullptr but pem_root_certs has
171  // value, it simply means an error occurs while trying to fetch the latest
172  // root certs, while pem_root_certs still contains the valid old data.
174  // The contents of the root certificates.
176  // The contents of the identity key-certificate pairs.
178  // The root cert reloading error propagated by the caller.
180  // The identity cert reloading error propagated by the caller.
182  // The set of watchers watching root certificates.
183  // This is mainly used for quickly looking up the affected watchers while
184  // performing a credential reloading.
185  std::set<TlsCertificatesWatcherInterface*> root_cert_watchers;
186  // The set of watchers watching identity certificates. This is mainly used
187  // for quickly looking up the affected watchers while performing a
188  // credential reloading.
189  std::set<TlsCertificatesWatcherInterface*> identity_cert_watchers;
190 
194  }
198  }
202  }
203  };
204 
206  // We need a dedicated mutex for watch_status_callback_ for allowing
207  // callers(e.g. Producer) to directly set key materials in the callback
208  // functions.
210  // Stores information about each watcher.
211  std::map<TlsCertificatesWatcherInterface*, WatcherInfo> watchers_
213  // The callback to notify the caller, e.g. the Producer, that the watch status
214  // is changed.
215  std::function<void(std::string, bool, bool)> watch_status_callback_
217  // Stores the names of each certificate, and their corresponding credential
218  // contents as well as some additional watcher information.
219  std::map<std::string, CertificateInfo> certificate_info_map_
221 };
222 
223 #endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_DISTRIBUTOR_H
grpc_tls_certificate_distributor::CertificateInfo::pem_key_cert_pairs
grpc_core::PemKeyCertPairList pem_key_cert_pairs
Definition: grpc_tls_certificate_distributor.h:177
grpc_tls_certificate_distributor::WatcherInfo::root_cert_name
absl::optional< std::string > root_cert_name
Definition: grpc_tls_certificate_distributor.h:164
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
grpc_tls_certificate_distributor::CertificateInfo::root_cert_watchers
std::set< TlsCertificatesWatcherInterface * > root_cert_watchers
Definition: grpc_tls_certificate_distributor.h:185
pem_root_certs
static char * pem_root_certs
Definition: rb_channel_credentials.c:38
grpc_tls_certificate_distributor::CertificateInfo::pem_root_certs
std::string pem_root_certs
Definition: grpc_tls_certificate_distributor.h:175
grpc_tls_certificate_distributor::callback_mu_
grpc_core::Mutex callback_mu_
Definition: grpc_tls_certificate_distributor.h:209
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_tls_identity_pairs
Definition: grpc_tls_certificate_distributor.h:38
grpc_tls_identity_pairs::pem_key_cert_pairs
grpc_core::PemKeyCertPairList pem_key_cert_pairs
Definition: grpc_tls_certificate_distributor.h:39
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_tls_certificate_distributor::mu_
grpc_core::Mutex mu_
Definition: grpc_tls_certificate_distributor.h:205
grpc_tls_certificate_distributor
Definition: grpc_tls_certificate_distributor.h:43
grpc_tls_certificate_distributor::HasRootCerts
bool HasRootCerts(const std::string &root_cert_name)
Definition: grpc_tls_certificate_distributor.cc:86
grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface
Definition: grpc_tls_certificate_distributor.h:47
grpc_tls_certificate_distributor::CertificateInfo::SetRootError
void SetRootError(grpc_error_handle error)
Definition: grpc_tls_certificate_distributor.h:195
grpc_tls_certificate_distributor::WatcherInfo::watcher
std::unique_ptr< TlsCertificatesWatcherInterface > watcher
Definition: grpc_tls_certificate_distributor.h:163
grpc_tls_certificate_distributor::WatchTlsCertificates
void WatchTlsCertificates(std::unique_ptr< TlsCertificatesWatcherInterface > watcher, absl::optional< std::string > root_cert_name, absl::optional< std::string > identity_cert_name)
Definition: grpc_tls_certificate_distributor.cc:176
grpc_tls_certificate_distributor::SetKeyMaterials
void SetKeyMaterials(const std::string &cert_name, absl::optional< std::string > pem_root_certs, absl::optional< grpc_core::PemKeyCertPairList > pem_key_cert_pairs)
Definition: grpc_tls_certificate_distributor.cc:27
grpc_tls_certificate_distributor::CertificateInfo::root_cert_error
grpc_error_handle root_cert_error
Definition: grpc_tls_certificate_distributor.h:179
grpc_tls_certificate_distributor::CertificateInfo::identity_cert_error
grpc_error_handle identity_cert_error
Definition: grpc_tls_certificate_distributor.h:181
grpc_tls_certificate_distributor::WatcherInfo
Definition: grpc_tls_certificate_distributor.h:162
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_tls_certificate_distributor::CertificateInfo::~CertificateInfo
~CertificateInfo()
Definition: grpc_tls_certificate_distributor.h:191
grpc_tls_certificate_distributor::CertificateInfo::SetIdentityError
void SetIdentityError(grpc_error_handle error)
Definition: grpc_tls_certificate_distributor.h:199
grpc_tls_certificate_distributor::CertificateInfo
Definition: grpc_tls_certificate_distributor.h:173
watchers_
std::map< SubchannelInterface::ConnectivityStateWatcherInterface *, WatcherWrapper * > watchers_
Definition: outlier_detection.cc:226
grpc_tls_certificate_distributor::SetError
void SetError(grpc_error_handle error)
Definition: grpc_tls_certificate_distributor.cc:155
absl::optional< absl::string_view >
grpc_tls_certificate_distributor::SetErrorForCert
void SetErrorForCert(const std::string &cert_name, absl::optional< grpc_error_handle > root_cert_error, absl::optional< grpc_error_handle > identity_cert_error)
Definition: grpc_tls_certificate_distributor.cc:102
error.h
grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface::~TlsCertificatesWatcherInterface
virtual ~TlsCertificatesWatcherInterface()=default
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface::OnCertificatesChanged
virtual void OnCertificatesChanged(absl::optional< absl::string_view > root_certs, absl::optional< grpc_core::PemKeyCertPairList > key_cert_pairs)=0
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface::OnError
virtual void OnError(grpc_error_handle root_cert_error, grpc_error_handle identity_cert_error)=0
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_tls_certificate_distributor::CancelTlsCertificatesWatch
void CancelTlsCertificatesWatch(TlsCertificatesWatcherInterface *watcher)
Definition: grpc_tls_certificate_distributor.cc:264
ref_counted.h
grpc_core::PemKeyCertPairList
std::vector< PemKeyCertPair > PemKeyCertPairList
Definition: ssl_utils.h:183
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
watcher
ClusterWatcher * watcher
Definition: cds.cc:148
grpc_tls_certificate_distributor::ABSL_GUARDED_BY
std::map< TlsCertificatesWatcherInterface *, WatcherInfo > watchers_ ABSL_GUARDED_BY(mu_)
grpc_error
Definition: error_internal.h:42
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_tls_certificate_distributor::WatcherInfo::identity_cert_name
absl::optional< std::string > identity_cert_name
Definition: grpc_tls_certificate_distributor.h:165
grpc_tls_certificate_distributor::HasKeyCertPairs
bool HasKeyCertPairs(const std::string &identity_cert_name)
Definition: grpc_tls_certificate_distributor.cc:94
grpc_tls_certificate_distributor::CertificateInfo::identity_cert_watchers
std::set< TlsCertificatesWatcherInterface * > identity_cert_watchers
Definition: grpc_tls_certificate_distributor.h:189
sync.h
ssl_utils.h
grpc_tls_certificate_distributor::SetWatchStatusCallback
void SetWatchStatusCallback(std::function< void(std::string, bool, bool)> callback)
Definition: grpc_tls_certificate_distributor.h:133
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:45