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 15 #ifndef GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_ENGINE_H 16 #define GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_ENGINE_H 17 18 #include <grpc/grpc_audit_logging.h> 19 #include <grpc/support/port_platform.h> 20 #include <stddef.h> 21 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "src/core/lib/security/authorization/authorization_engine.h" 27 #include "src/core/lib/security/authorization/evaluate_args.h" 28 #include "src/core/lib/security/authorization/matchers.h" 29 #include "src/core/lib/security/authorization/rbac_policy.h" 30 31 namespace grpc_core { 32 33 using experimental::AuditLogger; 34 35 // GrpcAuthorizationEngine can be either an Allow engine or Deny engine. This 36 // engine makes authorization decisions to Allow or Deny incoming RPC request 37 // based on permission and principal configs in the provided RBAC policy and the 38 // engine type. This engine ignores condition field in RBAC config. It is the 39 // caller's responsibility to provide RBAC policies that are compatible with 40 // this engine. 41 class GrpcAuthorizationEngine : public AuthorizationEngine { 42 public: 43 // Builds GrpcAuthorizationEngine without any policies. GrpcAuthorizationEngine(Rbac::Action action)44 explicit GrpcAuthorizationEngine(Rbac::Action action) 45 : action_(action), audit_condition_(Rbac::AuditCondition::kNone) {} 46 // Builds GrpcAuthorizationEngine with allow/deny RBAC policy. 47 explicit GrpcAuthorizationEngine(Rbac policy); 48 49 GrpcAuthorizationEngine(GrpcAuthorizationEngine&& other) noexcept; 50 GrpcAuthorizationEngine& operator=(GrpcAuthorizationEngine&& other) noexcept; 51 action()52 Rbac::Action action() const { return action_; } 53 54 // Required only for testing purpose. num_policies()55 size_t num_policies() const { return policies_.size(); } 56 57 // Required only for testing purpose. audit_condition()58 Rbac::AuditCondition audit_condition() const { return audit_condition_; } 59 60 // Required only for testing purpose. audit_loggers()61 const std::vector<std::unique_ptr<AuditLogger>>& audit_loggers() const { 62 return audit_loggers_; 63 } 64 65 // Evaluates incoming request against RBAC policy and makes a decision to 66 // whether allow/deny this request. 67 Decision Evaluate(const EvaluateArgs& args) const override; 68 69 private: 70 struct Policy { 71 std::string name; 72 std::unique_ptr<AuthorizationMatcher> matcher; 73 }; 74 75 std::string name_; 76 Rbac::Action action_; 77 std::vector<Policy> policies_; 78 Rbac::AuditCondition audit_condition_; 79 std::vector<std::unique_ptr<AuditLogger>> audit_loggers_; 80 }; 81 82 } // namespace grpc_core 83 84 #endif // GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_GRPC_AUTHORIZATION_ENGINE_H 85