You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server_credentials.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPCPP_SECURITY_SERVER_CREDENTIALS_H
  19. #define GRPCPP_SECURITY_SERVER_CREDENTIALS_H
  20. #include <memory>
  21. #include <vector>
  22. #include <grpc/grpc_security_constants.h>
  23. #include <grpcpp/security/auth_metadata_processor.h>
  24. #include <grpcpp/security/tls_credentials_options.h>
  25. #include <grpcpp/support/config.h>
  26. struct grpc_server;
  27. namespace grpc
  28. {
  29. class Server;
  30. class ServerCredentials;
  31. class SecureServerCredentials;
  32. /// Options to create ServerCredentials with SSL
  33. struct SslServerCredentialsOptions
  34. {
  35. /// \warning Deprecated
  36. SslServerCredentialsOptions() :
  37. force_client_auth(false),
  38. client_certificate_request(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE)
  39. {
  40. }
  41. explicit SslServerCredentialsOptions(
  42. grpc_ssl_client_certificate_request_type request_type
  43. ) :
  44. force_client_auth(false),
  45. client_certificate_request(request_type)
  46. {
  47. }
  48. struct PemKeyCertPair
  49. {
  50. std::string private_key;
  51. std::string cert_chain;
  52. };
  53. std::string pem_root_certs;
  54. std::vector<PemKeyCertPair> pem_key_cert_pairs;
  55. /// \warning Deprecated
  56. bool force_client_auth;
  57. /// If both \a force_client_auth and \a client_certificate_request
  58. /// fields are set, \a force_client_auth takes effect, i.e.
  59. /// \a REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
  60. /// will be enforced.
  61. grpc_ssl_client_certificate_request_type client_certificate_request;
  62. };
  63. /// Builds Xds ServerCredentials given fallback credentials
  64. std::shared_ptr<ServerCredentials> XdsServerCredentials(
  65. const std::shared_ptr<ServerCredentials>& fallback_credentials
  66. );
  67. namespace experimental
  68. {
  69. GRPC_DEPRECATED(
  70. "Use grpc::XdsServerCredentials instead. The experimental version will be "
  71. "deleted after the 1.41 release."
  72. )
  73. std::shared_ptr<ServerCredentials> XdsServerCredentials(
  74. const std::shared_ptr<ServerCredentials>& fallback_credentials
  75. );
  76. } // namespace experimental
  77. /// Wrapper around \a grpc_server_credentials, a way to authenticate a server.
  78. class ServerCredentials : private grpc::GrpcLibraryCodegen
  79. {
  80. public:
  81. ServerCredentials();
  82. ~ServerCredentials() override;
  83. /// This method is not thread-safe and has to be called before the server is
  84. /// started. The last call to this function wins.
  85. virtual void SetAuthMetadataProcessor(
  86. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor
  87. ) = 0;
  88. private:
  89. friend class Server;
  90. // We need this friend declaration for access to Insecure() and
  91. // AsSecureServerCredentials(). When these two functions are no longer
  92. // necessary, this friend declaration can be removed too.
  93. friend std::shared_ptr<ServerCredentials> grpc::XdsServerCredentials(
  94. const std::shared_ptr<ServerCredentials>& fallback_credentials
  95. );
  96. /// Tries to bind \a server to the given \a addr (eg, localhost:1234,
  97. /// 192.168.1.1:31416, [::1]:27182, etc.)
  98. ///
  99. /// \return bound port number on success, 0 on failure.
  100. // TODO(dgq): the "port" part seems to be a misnomer.
  101. virtual int AddPortToServer(const std::string& addr, grpc_server* server) = 0;
  102. // TODO(yashykt): This is a hack since InsecureServerCredentials() cannot use
  103. // grpc_insecure_server_credentials_create() and should be removed after
  104. // insecure builds are removed from gRPC.
  105. virtual bool IsInsecure() const
  106. {
  107. return false;
  108. }
  109. // TODO(yashkt): This is a hack that should be removed once we remove insecure
  110. // builds and the indirect method of adding ports to a server.
  111. virtual SecureServerCredentials* AsSecureServerCredentials()
  112. {
  113. return nullptr;
  114. }
  115. };
  116. /// Builds SSL ServerCredentials given SSL specific options
  117. std::shared_ptr<ServerCredentials> SslServerCredentials(
  118. const grpc::SslServerCredentialsOptions& options
  119. );
  120. std::shared_ptr<ServerCredentials> InsecureServerCredentials();
  121. namespace experimental
  122. {
  123. /// Options to create ServerCredentials with ALTS
  124. struct AltsServerCredentialsOptions
  125. {
  126. /// Add fields if needed.
  127. };
  128. /// Builds ALTS ServerCredentials given ALTS specific options
  129. std::shared_ptr<ServerCredentials> AltsServerCredentials(
  130. const AltsServerCredentialsOptions& options
  131. );
  132. /// Builds Local ServerCredentials.
  133. std::shared_ptr<ServerCredentials> AltsServerCredentials(
  134. const AltsServerCredentialsOptions& options
  135. );
  136. std::shared_ptr<ServerCredentials> LocalServerCredentials(
  137. grpc_local_connect_type type
  138. );
  139. /// Builds TLS ServerCredentials given TLS options.
  140. std::shared_ptr<ServerCredentials> TlsServerCredentials(
  141. const experimental::TlsServerCredentialsOptions& options
  142. );
  143. } // namespace experimental
  144. } // namespace grpc
  145. #endif // GRPCPP_SECURITY_SERVER_CREDENTIALS_H