authorization_policy_provider.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef GRPCPP_SECURITY_AUTHORIZATION_POLICY_PROVIDER_H
  15. #define GRPCPP_SECURITY_AUTHORIZATION_POLICY_PROVIDER_H
  16. #include <memory>
  17. #include <grpc/grpc_security.h>
  18. #include <grpc/status.h>
  19. #include <grpcpp/impl/codegen/status.h>
  20. namespace grpc {
  21. namespace experimental {
  22. // Wrapper around C-core grpc_authorization_policy_provider. Internally, it
  23. // handles creating and updating authorization engine objects, using SDK
  24. // authorization policy.
  25. class AuthorizationPolicyProviderInterface {
  26. public:
  27. virtual ~AuthorizationPolicyProviderInterface() = default;
  28. virtual grpc_authorization_policy_provider* c_provider() = 0;
  29. };
  30. // Implementation obtains authorization policy from static string. This provider
  31. // will always return the same authorization engines.
  32. class StaticDataAuthorizationPolicyProvider
  33. : public AuthorizationPolicyProviderInterface {
  34. public:
  35. static std::shared_ptr<StaticDataAuthorizationPolicyProvider> Create(
  36. const TString& authz_policy, grpc::Status* status);
  37. // Use factory method "Create" to create an instance of
  38. // StaticDataAuthorizationPolicyProvider.
  39. explicit StaticDataAuthorizationPolicyProvider(
  40. grpc_authorization_policy_provider* provider)
  41. : c_provider_(provider) {}
  42. ~StaticDataAuthorizationPolicyProvider() override;
  43. grpc_authorization_policy_provider* c_provider() override {
  44. return c_provider_;
  45. }
  46. private:
  47. grpc_authorization_policy_provider* c_provider_ = nullptr;
  48. };
  49. // Implementation obtains authorization policy by watching for changes in
  50. // filesystem.
  51. class FileWatcherAuthorizationPolicyProvider
  52. : public AuthorizationPolicyProviderInterface {
  53. public:
  54. static std::shared_ptr<FileWatcherAuthorizationPolicyProvider> Create(
  55. const TString& authz_policy_path, unsigned int refresh_interval_sec,
  56. grpc::Status* status);
  57. // Use factory method "Create" to create an instance of
  58. // FileWatcherAuthorizationPolicyProvider.
  59. explicit FileWatcherAuthorizationPolicyProvider(
  60. grpc_authorization_policy_provider* provider)
  61. : c_provider_(provider) {}
  62. ~FileWatcherAuthorizationPolicyProvider() override;
  63. grpc_authorization_policy_provider* c_provider() override {
  64. return c_provider_;
  65. }
  66. private:
  67. grpc_authorization_policy_provider* c_provider_ = nullptr;
  68. };
  69. } // namespace experimental
  70. } // namespace grpc
  71. #endif // GRPCPP_SECURITY_AUTHORIZATION_POLICY_PROVIDER_H