grpc_tls_certificate_provider.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_PROVIDER_H
18 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_PROVIDER_H
19 
21 
22 #include <map>
23 #include <string>
24 
25 #include "absl/base/thread_annotations.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/types/optional.h"
29 
30 #include <grpc/grpc_security.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
33 
38 #include "src/core/lib/gprpp/thd.h"
43 
44 // Interface for a grpc_tls_certificate_provider that handles the process to
45 // fetch credentials and validation contexts. Implementations are free to rely
46 // on local or remote sources to fetch the latest secrets, and free to share any
47 // state among different instances as they deem fit.
48 //
49 // On creation, grpc_tls_certificate_provider creates a
50 // grpc_tls_certificate_distributor object. When the credentials and validation
51 // contexts become valid or changed, a grpc_tls_certificate_provider should
52 // notify its distributor so as to propagate the update to the watchers.
54  : public grpc_core::RefCounted<grpc_tls_certificate_provider> {
55  public:
56  virtual grpc_pollset_set* interested_parties() const { return nullptr; }
57 
59  distributor() const = 0;
60 
61  // Compares this grpc_tls_certificate_provider object with \a other.
62  // If this method returns 0, it means that gRPC can treat the two certificate
63  // providers as effectively the same. This method is used to compare
64  // `grpc_tls_certificate_provider` objects when they are present in
65  // channel_args. One important usage of this is when channel args are used in
66  // SubchannelKey, which leads to a useful property that allows subchannels to
67  // be reused when two different `grpc_tls_certificate_provider` objects are
68  // used but they compare as equal (assuming other channel args match).
69  int Compare(const grpc_tls_certificate_provider* other) const {
70  GPR_ASSERT(other != nullptr);
71  int r = type().Compare(other->type());
72  if (r != 0) return r;
73  return CompareImpl(other);
74  }
75 
76  // The pointer value \a type is used to uniquely identify a creds
77  // implementation for down-casting purposes. Every provider implementation
78  // should use a unique string instance, which should be returned by all
79  // instances of that provider implementation.
80  virtual grpc_core::UniqueTypeName type() const = 0;
81 
82  private:
83  // Implementation for `Compare` method intended to be overridden by
84  // subclasses. Only invoked if `type()` and `other->type()` point to the same
85  // string.
86  virtual int CompareImpl(const grpc_tls_certificate_provider* other) const = 0;
87 };
88 
89 namespace grpc_core {
90 
91 // A basic provider class that will get credentials from string during
92 // initialization.
95  public:
97  PemKeyCertPairList pem_key_cert_pairs);
98 
100 
102  return distributor_;
103  }
104 
105  UniqueTypeName type() const override;
106 
107  private:
108  struct WatcherInfo {
109  bool root_being_watched = false;
111  };
112 
113  int CompareImpl(const grpc_tls_certificate_provider* other) const override {
114  // TODO(yashykt): Maybe do something better here.
115  return QsortCompare(static_cast<const grpc_tls_certificate_provider*>(this),
116  other);
117  }
118 
122  // Guards members below.
124  // Stores each cert_name we get from the distributor callback and its watcher
125  // information.
126  std::map<std::string, WatcherInfo> watcher_info_;
127 };
128 
129 // A provider class that will watch the credential changes on the file system.
132  public:
134  std::string identity_certificate_path,
135  std::string root_cert_path,
136  unsigned int refresh_interval_sec);
137 
139 
141  return distributor_;
142  }
143 
144  UniqueTypeName type() const override;
145 
146  private:
147  struct WatcherInfo {
148  bool root_being_watched = false;
150  };
151 
152  int CompareImpl(const grpc_tls_certificate_provider* other) const override {
153  // TODO(yashykt): Maybe do something better here.
154  return QsortCompare(static_cast<const grpc_tls_certificate_provider*>(this),
155  other);
156  }
157 
158  // Force an update from the file system regardless of the interval.
159  void ForceUpdate();
160  // Read the root certificates from files and update the distributor.
162  const std::string& root_cert_full_path);
163  // Read the private key and the certificate chain from files and update the
164  // distributor.
166  const std::string& private_key_path,
167  const std::string& identity_certificate_path);
168 
169  // Information that is used by the refreshing thread.
173  unsigned int refresh_interval_sec_ = 0;
174 
178 
179  // Guards members below.
181  // The most-recent credential data. It will be empty if the most recent read
182  // attempt failed.
183  std::string root_certificate_ ABSL_GUARDED_BY(mu_);
184  PemKeyCertPairList pem_key_cert_pairs_ ABSL_GUARDED_BY(mu_);
185  // Stores each cert_name we get from the distributor callback and its watcher
186  // information.
187  std::map<std::string, WatcherInfo> watcher_info_ ABSL_GUARDED_BY(mu_);
188 };
189 
190 // Checks if the private key matches the certificate's public key.
191 // Returns a not-OK status on failure, or a bool indicating
192 // whether the key/cert pair matches.
195 
196 } // namespace grpc_core
197 
198 #endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_TLS_GRPC_TLS_CERTIFICATE_PROVIDER_H
grpc_tls_certificate_provider::CompareImpl
virtual int CompareImpl(const grpc_tls_certificate_provider *other) const =0
grpc_core::StaticDataCertificateProvider::mu_
Mutex mu_
Definition: grpc_tls_certificate_provider.h:123
grpc_core::FileWatcherCertificateProvider::private_key_path_
std::string private_key_path_
Definition: grpc_tls_certificate_provider.h:170
log.h
grpc_core::StaticDataCertificateProvider::type
UniqueTypeName type() const override
Definition: grpc_tls_certificate_provider.cc:107
grpc_tls_certificate_distributor.h
grpc_core::FileWatcherCertificateProvider::mu_
Mutex mu_
Definition: grpc_tls_certificate_provider.h:180
grpc_core::UniqueTypeName::Compare
int Compare(const UniqueTypeName &other) const
Definition: unique_type_name.h:90
grpc_core
Definition: call_metric_recorder.h:31
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::FileWatcherCertificateProvider::ReadRootCertificatesFromFile
absl::optional< std::string > ReadRootCertificatesFromFile(const std::string &root_cert_full_path)
Definition: grpc_tls_certificate_provider.cc:286
grpc_core::StaticDataCertificateProvider::~StaticDataCertificateProvider
~StaticDataCertificateProvider() override
Definition: grpc_tls_certificate_provider.cc:101
grpc_core::FileWatcherCertificateProvider::WatcherInfo
Definition: grpc_tls_certificate_provider.h:147
grpc_core::FileWatcherCertificateProvider::distributor
RefCountedPtr< grpc_tls_certificate_distributor > distributor() const override
Definition: grpc_tls_certificate_provider.h:140
grpc_core::StaticDataCertificateProvider::CompareImpl
int CompareImpl(const grpc_tls_certificate_provider *other) const override
Definition: grpc_tls_certificate_provider.h:113
grpc_core::FileWatcherCertificateProvider::ABSL_GUARDED_BY
std::string root_certificate_ ABSL_GUARDED_BY(mu_)
grpc_security.h
grpc_core::StaticDataCertificateProvider::distributor_
RefCountedPtr< grpc_tls_certificate_distributor > distributor_
Definition: grpc_tls_certificate_provider.h:119
grpc_core::FileWatcherCertificateProvider::distributor_
RefCountedPtr< grpc_tls_certificate_distributor > distributor_
Definition: grpc_tls_certificate_provider.h:175
grpc_core::StaticDataCertificateProvider::WatcherInfo
Definition: grpc_tls_certificate_provider.h:108
grpc_core::FileWatcherCertificateProvider::ForceUpdate
void ForceUpdate()
Definition: grpc_tls_certificate_provider.cc:209
grpc_core::RefCountedPtr< grpc_tls_certificate_distributor >
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::FileWatcherCertificateProvider::WatcherInfo::root_being_watched
bool root_being_watched
Definition: grpc_tls_certificate_provider.h:148
absl::optional< std::string >
grpc_core::FileWatcherCertificateProvider::type
UniqueTypeName type() const override
Definition: grpc_tls_certificate_provider.cc:204
grpc_tls_certificate_provider::Compare
int Compare(const grpc_tls_certificate_provider *other) const
Definition: grpc_tls_certificate_provider.h:69
grpc_core::FileWatcherCertificateProvider::shutdown_event_
gpr_event shutdown_event_
Definition: grpc_tls_certificate_provider.h:177
grpc_core::FileWatcherCertificateProvider::refresh_thread_
Thread refresh_thread_
Definition: grpc_tls_certificate_provider.h:176
grpc_core::StaticDataCertificateProvider::WatcherInfo::identity_being_watched
bool identity_being_watched
Definition: grpc_tls_certificate_provider.h:110
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_core::FileWatcherCertificateProvider::refresh_interval_sec_
unsigned int refresh_interval_sec_
Definition: grpc_tls_certificate_provider.h:173
grpc_core::FileWatcherCertificateProvider::identity_certificate_path_
std::string identity_certificate_path_
Definition: grpc_tls_certificate_provider.h:171
grpc_core::FileWatcherCertificateProvider::ReadIdentityKeyCertPairFromFiles
absl::optional< PemKeyCertPairList > ReadIdentityKeyCertPairFromFiles(const std::string &private_key_path, const std::string &identity_certificate_path)
Definition: grpc_tls_certificate_provider.cc:317
grpc_core::FileWatcherCertificateProvider::WatcherInfo::identity_being_watched
bool identity_being_watched
Definition: grpc_tls_certificate_provider.h:149
grpc_core::PrivateKeyAndCertificateMatch
absl::StatusOr< bool > PrivateKeyAndCertificateMatch(absl::string_view private_key, absl::string_view cert_chain)
Definition: grpc_tls_certificate_provider.cc:397
grpc_core::StaticDataCertificateProvider::pem_key_cert_pairs_
PemKeyCertPairList pem_key_cert_pairs_
Definition: grpc_tls_certificate_provider.h:121
grpc_core::FileWatcherCertificateProvider::~FileWatcherCertificateProvider
~FileWatcherCertificateProvider() override
Definition: grpc_tls_certificate_provider.cc:196
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
gpr_event
Definition: impl/codegen/sync_generic.h:31
grpc_core::StaticDataCertificateProvider::distributor
RefCountedPtr< grpc_tls_certificate_distributor > distributor() const override
Definition: grpc_tls_certificate_provider.h:101
grpc_core::UniqueTypeName
Definition: unique_type_name.h:56
grpc_tls_certificate_provider::distributor
virtual grpc_core::RefCountedPtr< grpc_tls_certificate_distributor > distributor() const =0
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
ref_counted.h
grpc_core::StaticDataCertificateProvider::root_certificate_
std::string root_certificate_
Definition: grpc_tls_certificate_provider.h:120
grpc_core::PemKeyCertPairList
std::vector< PemKeyCertPair > PemKeyCertPairList
Definition: ssl_utils.h:183
grpc_core::StaticDataCertificateProvider::StaticDataCertificateProvider
StaticDataCertificateProvider(std::string root_certificate, PemKeyCertPairList pem_key_cert_pairs)
Definition: grpc_tls_certificate_provider.cc:52
private_key
Definition: hrss.c:1885
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc_core::FileWatcherCertificateProvider::FileWatcherCertificateProvider
FileWatcherCertificateProvider(std::string private_key_path, std::string identity_certificate_path, std::string root_cert_path, unsigned int refresh_interval_sec)
Definition: grpc_tls_certificate_provider.cc:121
thd.h
grpc_tls_certificate_provider::type
virtual grpc_core::UniqueTypeName type() const =0
unique_type_name.h
grpc_core::Thread
Definition: thd.h:43
grpc_core::FileWatcherCertificateProvider::CompareImpl
int CompareImpl(const grpc_tls_certificate_provider *other) const override
Definition: grpc_tls_certificate_provider.h:152
ref_counted_ptr.h
grpc_core::StaticDataCertificateProvider
Definition: grpc_tls_certificate_provider.h:93
grpc_core::FileWatcherCertificateProvider::root_cert_path_
std::string root_cert_path_
Definition: grpc_tls_certificate_provider.h:172
grpc_core::StaticDataCertificateProvider::watcher_info_
std::map< std::string, WatcherInfo > watcher_info_
Definition: grpc_tls_certificate_provider.h:126
grpc_tls_certificate_provider
Definition: grpc_tls_certificate_provider.h:53
grpc_core::FileWatcherCertificateProvider
Definition: grpc_tls_certificate_provider.h:130
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
iomgr_fwd.h
grpc_core::StaticDataCertificateProvider::WatcherInfo::root_being_watched
bool root_being_watched
Definition: grpc_tls_certificate_provider.h:109
grpc_tls_certificate_provider::interested_parties
virtual grpc_pollset_set * interested_parties() const
Definition: grpc_tls_certificate_provider.h:56
sync.h
root_certificate
std::string root_certificate
Definition: xds_end2end_test.cc:142
sync.h
ssl_utils.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:48