GRPC C++  1.80.0
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 GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
18 #define GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
19 
20 #include <grpc/credentials.h>
21 #include <grpc/grpc_security.h>
23 #include <grpc/status.h>
26 #include <grpcpp/support/config.h>
27 
28 #include <memory>
29 #include <string>
30 #include <variant>
31 #include <vector>
32 
33 namespace grpc {
34 namespace experimental {
35 
36 // Interface for a class that handles the process to fetch credential data.
37 // Implementations should be a wrapper class of an internal provider
38 // implementation.
40  public:
41  virtual ~CertificateProviderInterface() = default;
42  virtual grpc_tls_certificate_provider* c_provider() = 0;
43 };
44 
45 // A struct that stores the credential data presented to the peer in handshake
46 // to show local identity. The private_key and certificate_chain should always
47 // match.
48 struct [[deprecated("Use IdentityKeyOrSignerCertPair instead")]] GRPCXX_DLL
50  std::string private_key;
51  std::string certificate_chain;
52 };
53 
54 // A struct that stores the credential data presented to the peer in handshake
55 // to show local identity. The private_key and certificate_chain should always
56 // match. The private_key can be either a PEM string or a PrivateKeySigner.
57 // The PrivateKeySigner will only work with gRPC binaries compiled with
58 // BoringSSL.
60  std::variant<std::string, std::shared_ptr<PrivateKeySigner>> private_key;
61  std::string certificate_chain;
62 };
63 
64 // A basic CertificateProviderInterface implementation that will load credential
65 // data from static string during initialization. This provider will always
66 // return the same cert data for all cert names, and reloading is not supported.
67 class [[deprecated("Use InMemoryCertificateProvider instead")]] GRPCXX_DLL
69  public:
71  const std::string& root_certificate,
72  const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs);
73 
74  explicit StaticDataCertificateProvider(const std::string& root_certificate)
75  : StaticDataCertificateProvider(root_certificate, {}) {}
76 
78  const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs)
79  : StaticDataCertificateProvider("", identity_key_cert_pairs) {}
80 
82 
83  grpc_tls_certificate_provider* c_provider() override { return c_provider_; }
84 
85  // Returns an OK status if the following conditions hold:
86  // - the root certificates consist of one or more valid PEM blocks, and
87  // - every identity key-cert pair has a certificate chain that consists of
88  // valid PEM blocks and has a private key that is either a valid PEM block
89  // or a non-null PrivateKeySigner instance.
90  absl::Status ValidateCredentials() const;
91 
92  private:
93  grpc_tls_certificate_provider* c_provider_ = nullptr;
94 };
95 
96 // A CertificateProviderInterface implementation that will watch the credential
97 // changes on the file system. This provider will always return the up-to-date
98 // cert data for all the cert names callers set through |TlsCredentialsOptions|.
99 // Several things to note:
100 // 1. This API only supports one key-cert file and hence one set of identity
101 // key-cert pair, so SNI(Server Name Indication) is not supported.
102 // 2. The private key and identity certificate should always match. This API
103 // guarantees atomic read, and it is the callers' responsibility to do atomic
104 // updates. There are many ways to atomically update the key and certs in the
105 // file system. To name a few:
106 // 1) creating a new directory, renaming the old directory to a new name, and
107 // then renaming the new directory to the original name of the old directory.
108 // 2) using a symlink for the directory. When need to change, put new
109 // credential data in a new directory, and change symlink.
112  public:
113  // Constructor to get credential updates from root and identity file paths.
114  //
115  // @param private_key_path is the file path of the private key.
116  // @param identity_certificate_path is the file path of the identity
117  // certificate chain.
118  // @param root_cert_path is the file path to the root certificate bundle.
119  // @param refresh_interval_sec is the refreshing interval that we will check
120  // the files for updates.
121  FileWatcherCertificateProvider(const std::string& private_key_path,
122  const std::string& identity_certificate_path,
123  const std::string& root_cert_path,
124  const std::string& spiffe_bundle_map_path,
125  unsigned int refresh_interval_sec);
126  // Constructor to get credential updates from identity file paths only.
127  FileWatcherCertificateProvider(const std::string& private_key_path,
128  const std::string& identity_certificate_path,
129  unsigned int refresh_interval_sec)
130  : FileWatcherCertificateProvider(private_key_path,
131  identity_certificate_path, "", "",
132  refresh_interval_sec) {}
133  // Constructor to get credential updates from root file path only.
134  FileWatcherCertificateProvider(const std::string& root_cert_path,
135  unsigned int refresh_interval_sec)
136  : FileWatcherCertificateProvider("", "", root_cert_path, "",
137  refresh_interval_sec) {}
138  FileWatcherCertificateProvider(const std::string& private_key_path,
139  const std::string& identity_certificate_path,
140  const std::string& root_cert_path,
141  unsigned int refresh_interval_sec)
143  private_key_path, identity_certificate_path, root_cert_path, "",
144  refresh_interval_sec) {}
145 
146  ~FileWatcherCertificateProvider() override;
147 
148  grpc_tls_certificate_provider* c_provider() override { return c_provider_; }
149 
150  // Returns an OK status if the following conditions hold:
151  // - the currently-loaded root certificates, if any, consist of one or more
152  // valid PEM blocks, and
153  // - every currently-loaded identity key-cert pair, if any, has a certificate
154  // chain that consists of valid PEM blocks and has a private key that is
155  // either a valid PEM block or a non-null PrivateKeySigner instance.
156  absl::Status ValidateCredentials() const;
157 
158  private:
159  grpc_tls_certificate_provider* c_provider_ = nullptr;
160 };
161 
162 // "A CertificateProviderInterface implementation that stores credentials
163 // in-memory and allows the user to update credentials on-demand.
166  public:
168 
169  ~InMemoryCertificateProvider() override;
170 
171  grpc_tls_certificate_provider* c_provider() override { return c_provider_; }
172 
173  // TODO(anasalazar): Expose some API for callers to pass in a SPIFFE bundle
174  // map for the root cert.
175  // Users should verify the status retuned to confirm that the update was
176  // successful.
177  absl::Status UpdateRoot(const std::string& root_certificate);
178  absl::Status UpdateIdentityKeyCertPair(
179  std::vector<IdentityKeyCertPair> identity_key_cert_pairs);
180  absl::Status UpdateIdentityKeyCertPair(
181  std::vector<IdentityKeyOrSignerCertPair>
182  identity_key_or_signer_cert_pairs);
183 
184  // Returns an OK status if the following conditions hold:
185  // - the root certificates consist of one or more valid PEM blocks, and
186  // - every identity key-cert pair has a certificate chain that consists of
187  // chain that consists of valid PEM blocks and has a private key is a valid
188  // PEM block.
189  absl::Status ValidateCredentials() const;
190 
191  private:
192  grpc_tls_certificate_provider* c_provider_ = nullptr;
193 };
194 
195 } // namespace experimental
196 } // namespace grpc
197 
198 #endif // GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
tls_private_key_signer.h
grpc::experimental::FileWatcherCertificateProvider
Definition: tls_certificate_provider.h:110
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::experimental::FileWatcherCertificateProvider::FileWatcherCertificateProvider
FileWatcherCertificateProvider(const std::string &private_key_path, const std::string &identity_certificate_path, unsigned int refresh_interval_sec)
Definition: tls_certificate_provider.h:127
grpc_tls_certificate_provider
struct grpc_tls_certificate_provider grpc_tls_certificate_provider
— TLS channel/server credentials — It is used for experimental purpose for now and subject to change.
Definition: credentials.h:609
credentials.h
grpc_security.h
status.h
grpc::experimental::StaticDataCertificateProvider::StaticDataCertificateProvider
StaticDataCertificateProvider(const std::vector< IdentityKeyCertPair > &identity_key_cert_pairs)
Definition: tls_certificate_provider.h:77
grpc::experimental::FileWatcherCertificateProvider::FileWatcherCertificateProvider
FileWatcherCertificateProvider(const std::string &root_cert_path, unsigned int refresh_interval_sec)
Definition: tls_certificate_provider.h:134
GRPCXX_DLL
#define GRPCXX_DLL
Definition: port_platform.h:131
grpc::experimental::IdentityKeyCertPair::certificate_chain
std::string certificate_chain
Definition: tls_certificate_provider.h:51
grpc::experimental::StaticDataCertificateProvider::c_provider
grpc_tls_certificate_provider * c_provider() override
Definition: tls_certificate_provider.h:83
grpc::experimental::IdentityKeyCertPair
Definition: tls_certificate_provider.h:48
grpc::experimental::CertificateProviderInterface
Definition: tls_certificate_provider.h:39
grpc::experimental::InMemoryCertificateProvider
Definition: tls_certificate_provider.h:164
grpc::experimental::IdentityKeyCertPair::private_key
std::string private_key
Definition: tls_certificate_provider.h:50
config.h
grpc::experimental::InMemoryCertificateProvider::c_provider
grpc_tls_certificate_provider * c_provider() override
Definition: tls_certificate_provider.h:171
grpc::experimental::FileWatcherCertificateProvider::c_provider
grpc_tls_certificate_provider * c_provider() override
Definition: tls_certificate_provider.h:148
grpc::experimental::StaticDataCertificateProvider
Definition: tls_certificate_provider.h:67
grpc_security_constants.h
grpc::experimental::FileWatcherCertificateProvider::FileWatcherCertificateProvider
FileWatcherCertificateProvider(const std::string &private_key_path, const std::string &identity_certificate_path, const std::string &root_cert_path, unsigned int refresh_interval_sec)
Definition: tls_certificate_provider.h:138
grpc::experimental::StaticDataCertificateProvider::StaticDataCertificateProvider
StaticDataCertificateProvider(const std::string &root_certificate)
Definition: tls_certificate_provider.h:74
grpc::experimental::IdentityKeyOrSignerCertPair::private_key
std::variant< std::string, std::shared_ptr< PrivateKeySigner > > private_key
Definition: tls_certificate_provider.h:60
grpc::experimental::IdentityKeyOrSignerCertPair
Definition: tls_certificate_provider.h:59
grpc::experimental::IdentityKeyOrSignerCertPair::certificate_chain
std::string certificate_chain
Definition: tls_certificate_provider.h:61
grpc::protobuf::util::Status
::absl::Status Status
Definition: config_protobuf.h:107
port_platform.h