tls_certificate_provider.h 5.3 KB

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