server_credentials.h 4.7 KB

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