server_credentials.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. class Server;
  29. class ServerCredentials;
  30. class SecureServerCredentials;
  31. /// Options to create ServerCredentials with SSL
  32. struct SslServerCredentialsOptions {
  33. /// \warning Deprecated
  34. SslServerCredentialsOptions()
  35. : force_client_auth(false),
  36. client_certificate_request(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE) {}
  37. explicit SslServerCredentialsOptions(
  38. grpc_ssl_client_certificate_request_type request_type)
  39. : force_client_auth(false), client_certificate_request(request_type) {}
  40. struct PemKeyCertPair {
  41. std::string private_key;
  42. std::string cert_chain;
  43. };
  44. std::string pem_root_certs;
  45. std::vector<PemKeyCertPair> pem_key_cert_pairs;
  46. /// \warning Deprecated
  47. bool force_client_auth;
  48. /// If both \a force_client_auth and \a client_certificate_request
  49. /// fields are set, \a force_client_auth takes effect, i.e.
  50. /// \a REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
  51. /// will be enforced.
  52. grpc_ssl_client_certificate_request_type client_certificate_request;
  53. };
  54. /// Builds Xds ServerCredentials given fallback credentials
  55. std::shared_ptr<ServerCredentials> XdsServerCredentials(
  56. const std::shared_ptr<ServerCredentials>& fallback_credentials);
  57. namespace experimental {
  58. GRPC_DEPRECATED(
  59. "Use grpc::XdsServerCredentials instead. The experimental version will be "
  60. "deleted after the 1.41 release.")
  61. std::shared_ptr<ServerCredentials> XdsServerCredentials(
  62. const std::shared_ptr<ServerCredentials>& fallback_credentials);
  63. } // namespace experimental
  64. /// Wrapper around \a grpc_server_credentials, a way to authenticate a server.
  65. class ServerCredentials : private grpc::GrpcLibraryCodegen {
  66. public:
  67. ServerCredentials();
  68. ~ServerCredentials() override;
  69. /// This method is not thread-safe and has to be called before the server is
  70. /// started. The last call to this function wins.
  71. virtual void SetAuthMetadataProcessor(
  72. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) = 0;
  73. private:
  74. friend class Server;
  75. // We need this friend declaration for access to Insecure() and
  76. // AsSecureServerCredentials(). When these two functions are no longer
  77. // necessary, this friend declaration can be removed too.
  78. friend std::shared_ptr<ServerCredentials> grpc::XdsServerCredentials(
  79. const std::shared_ptr<ServerCredentials>& fallback_credentials);
  80. /// Tries to bind \a server to the given \a addr (eg, localhost:1234,
  81. /// 192.168.1.1:31416, [::1]:27182, etc.)
  82. ///
  83. /// \return bound port number on success, 0 on failure.
  84. // TODO(dgq): the "port" part seems to be a misnomer.
  85. virtual int AddPortToServer(const std::string& addr, grpc_server* server) = 0;
  86. // TODO(yashykt): This is a hack since InsecureServerCredentials() cannot use
  87. // grpc_insecure_server_credentials_create() and should be removed after
  88. // insecure builds are removed from gRPC.
  89. virtual bool IsInsecure() const { return false; }
  90. // TODO(yashkt): This is a hack that should be removed once we remove insecure
  91. // builds and the indirect method of adding ports to a server.
  92. virtual SecureServerCredentials* AsSecureServerCredentials() {
  93. return nullptr;
  94. }
  95. };
  96. /// Builds SSL ServerCredentials given SSL specific options
  97. std::shared_ptr<ServerCredentials> SslServerCredentials(
  98. const grpc::SslServerCredentialsOptions& options);
  99. std::shared_ptr<ServerCredentials> InsecureServerCredentials();
  100. namespace experimental {
  101. /// Options to create ServerCredentials with ALTS
  102. struct AltsServerCredentialsOptions {
  103. /// Add fields if needed.
  104. };
  105. /// Builds ALTS ServerCredentials given ALTS specific options
  106. std::shared_ptr<ServerCredentials> AltsServerCredentials(
  107. const AltsServerCredentialsOptions& options);
  108. /// Builds Local ServerCredentials.
  109. std::shared_ptr<ServerCredentials> AltsServerCredentials(
  110. const AltsServerCredentialsOptions& options);
  111. std::shared_ptr<ServerCredentials> LocalServerCredentials(
  112. grpc_local_connect_type type);
  113. /// Builds TLS ServerCredentials given TLS options.
  114. std::shared_ptr<ServerCredentials> TlsServerCredentials(
  115. const experimental::TlsServerCredentialsOptions& options);
  116. } // namespace experimental
  117. } // namespace grpc
  118. #endif // GRPCPP_SECURITY_SERVER_CREDENTIALS_H