tls_certificate_provider.h 5.3 KB

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