GRPC C++  1.66.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 <memory>
21 #include <vector>
22 
23 #include <grpc/credentials.h>
24 #include <grpc/grpc_security.h>
26 #include <grpc/status.h>
27 #include <grpc/support/log.h>
28 #include <grpcpp/support/config.h>
29 
30 namespace grpc {
31 namespace experimental {
32 
33 // Interface for a class that handles the process to fetch credential data.
34 // Implementations should be a wrapper class of an internal provider
35 // implementation.
37  public:
38  virtual ~CertificateProviderInterface() = default;
39  virtual grpc_tls_certificate_provider* c_provider() = 0;
40 };
41 
42 // A struct that stores the credential data presented to the peer in handshake
43 // to show local identity. The private_key and certificate_chain should always
44 // match.
46  std::string private_key;
47  std::string certificate_chain;
48 };
49 
50 // A basic CertificateProviderInterface implementation that will load credential
51 // data from static string during initialization. This provider will always
52 // return the same cert data for all cert names, and reloading is not supported.
55  public:
57  const std::string& root_certificate,
58  const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs);
59 
60  explicit StaticDataCertificateProvider(const std::string& root_certificate)
61  : StaticDataCertificateProvider(root_certificate, {}) {}
62 
64  const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs)
65  : StaticDataCertificateProvider("", identity_key_cert_pairs) {}
66 
68 
69  grpc_tls_certificate_provider* c_provider() override { return c_provider_; }
70 
71  private:
72  grpc_tls_certificate_provider* c_provider_ = nullptr;
73 };
74 
75 // A CertificateProviderInterface implementation that will watch the credential
76 // changes on the file system. This provider will always return the up-to-date
77 // cert data for all the cert names callers set through |TlsCredentialsOptions|.
78 // Several things to note:
79 // 1. This API only supports one key-cert file and hence one set of identity
80 // key-cert pair, so SNI(Server Name Indication) is not supported.
81 // 2. The private key and identity certificate should always match. This API
82 // guarantees atomic read, and it is the callers' responsibility to do atomic
83 // updates. There are many ways to atomically update the key and certs in the
84 // file system. To name a few:
85 // 1) creating a new directory, renaming the old directory to a new name, and
86 // then renaming the new directory to the original name of the old directory.
87 // 2) using a symlink for the directory. When need to change, put new
88 // credential data in a new directory, and change symlink.
91  public:
92  // Constructor to get credential updates from root and identity file paths.
93  //
94  // @param private_key_path is the file path of the private key.
95  // @param identity_certificate_path is the file path of the identity
96  // certificate chain.
97  // @param root_cert_path is the file path to the root certificate bundle.
98  // @param refresh_interval_sec is the refreshing interval that we will check
99  // the files for updates.
100  FileWatcherCertificateProvider(const std::string& private_key_path,
101  const std::string& identity_certificate_path,
102  const std::string& root_cert_path,
103  unsigned int refresh_interval_sec);
104  // Constructor to get credential updates from identity file paths only.
105  FileWatcherCertificateProvider(const std::string& private_key_path,
106  const std::string& identity_certificate_path,
107  unsigned int refresh_interval_sec)
108  : FileWatcherCertificateProvider(private_key_path,
109  identity_certificate_path, "",
110  refresh_interval_sec) {}
111  // Constructor to get credential updates from root file path only.
112  FileWatcherCertificateProvider(const std::string& root_cert_path,
113  unsigned int refresh_interval_sec)
114  : FileWatcherCertificateProvider("", "", root_cert_path,
115  refresh_interval_sec) {}
116 
117  ~FileWatcherCertificateProvider() override;
118 
119  grpc_tls_certificate_provider* c_provider() override { return c_provider_; }
120 
121  private:
122  grpc_tls_certificate_provider* c_provider_ = nullptr;
123 };
124 
125 } // namespace experimental
126 } // namespace grpc
127 
128 #endif // GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
grpc::experimental::FileWatcherCertificateProvider
Definition: tls_certificate_provider.h:89
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:105
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:583
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:63
grpc::experimental::FileWatcherCertificateProvider::FileWatcherCertificateProvider
FileWatcherCertificateProvider(const std::string &root_cert_path, unsigned int refresh_interval_sec)
Definition: tls_certificate_provider.h:112
GRPCXX_DLL
#define GRPCXX_DLL
Definition: port_platform.h:128
log.h
grpc::experimental::IdentityKeyCertPair::certificate_chain
std::string certificate_chain
Definition: tls_certificate_provider.h:47
grpc::experimental::StaticDataCertificateProvider::c_provider
grpc_tls_certificate_provider * c_provider() override
Definition: tls_certificate_provider.h:69
grpc::experimental::IdentityKeyCertPair
Definition: tls_certificate_provider.h:45
grpc::experimental::CertificateProviderInterface
Definition: tls_certificate_provider.h:36
grpc::experimental::IdentityKeyCertPair::private_key
std::string private_key
Definition: tls_certificate_provider.h:46
config.h
grpc::experimental::FileWatcherCertificateProvider::c_provider
grpc_tls_certificate_provider * c_provider() override
Definition: tls_certificate_provider.h:119
grpc::experimental::StaticDataCertificateProvider
Definition: tls_certificate_provider.h:53
grpc_security_constants.h
grpc::experimental::StaticDataCertificateProvider::StaticDataCertificateProvider
StaticDataCertificateProvider(const std::string &root_certificate)
Definition: tls_certificate_provider.h:60