test_credentials_provider.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. *
  3. * Copyright 2016 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 GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
  19. #define GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
  20. #include <memory>
  21. #include <grpcpp/security/credentials.h>
  22. #include <grpcpp/security/server_credentials.h>
  23. #include <grpcpp/support/channel_arguments.h>
  24. namespace grpc {
  25. namespace testing {
  26. const char kInsecureCredentialsType[] = "INSECURE_CREDENTIALS";
  27. // For real credentials, like tls/ssl, this name should match the AuthContext
  28. // property "transport_security_type".
  29. const char kTlsCredentialsType[] = "ssl";
  30. const char kAltsCredentialsType[] = "alts";
  31. const char kGoogleDefaultCredentialsType[] = "google_default_credentials";
  32. // Provide test credentials of a particular type.
  33. class CredentialTypeProvider {
  34. public:
  35. virtual ~CredentialTypeProvider() {}
  36. virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
  37. ChannelArguments* args) = 0;
  38. virtual std::shared_ptr<ServerCredentials> GetServerCredentials() = 0;
  39. };
  40. // Provide test credentials. Thread-safe.
  41. class CredentialsProvider {
  42. public:
  43. virtual ~CredentialsProvider() {}
  44. // Add a secure type in addition to the defaults. The default provider has
  45. // (kInsecureCredentialsType, kTlsCredentialsType).
  46. virtual void AddSecureType(
  47. const std::string& type,
  48. std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
  49. // Provide channel credentials according to the given type. Alter the channel
  50. // arguments if needed. Return nullptr if type is not registered.
  51. virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
  52. const std::string& type, ChannelArguments* args) = 0;
  53. // Provide server credentials according to the given type.
  54. // Return nullptr if type is not registered.
  55. virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
  56. const std::string& type) = 0;
  57. // Provide a list of secure credentials type.
  58. virtual std::vector<std::string> GetSecureCredentialsTypeList() = 0;
  59. };
  60. // Get the current provider. Create a default one if not set.
  61. // Not thread-safe.
  62. CredentialsProvider* GetCredentialsProvider();
  63. // Set the global provider. Takes ownership. The previous set provider will be
  64. // destroyed.
  65. // Not thread-safe.
  66. void SetCredentialsProvider(CredentialsProvider* provider);
  67. } // namespace testing
  68. } // namespace grpc
  69. #endif // GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H