1 2 // Copyright 2020 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_CEL_AUTHORIZATION_ENGINE_H 17 #define GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_CEL_AUTHORIZATION_ENGINE_H 18 19 #include <grpc/support/port_platform.h> 20 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "absl/container/flat_hash_set.h" 27 #include "envoy/config/rbac/v3/rbac.upb.h" 28 #include "google/api/expr/v1alpha1/syntax.upb.h" 29 #include "src/core/lib/security/authorization/evaluate_args.h" 30 #include "src/core/lib/security/authorization/mock_cel/activation.h" 31 #include "src/core/lib/security/authorization/mock_cel/cel_value.h" 32 #include "upb/mem/arena.hpp" 33 34 namespace grpc_core { 35 36 // CelAuthorizationEngine makes an AuthorizationDecision to ALLOW or DENY the 37 // current action based on the condition fields in provided RBAC policies. 38 // The engine may be constructed with one or two policies. If two policies, 39 // the first policy is deny-if-matched and the second is allow-if-matched. 40 // The engine returns UNDECIDED decision if it fails to find a match in any 41 // policy. This engine ignores the principal and permission fields in RBAC 42 // policies. It is the caller's responsibility to provide RBAC policies that 43 // are compatible with this engine. 44 // 45 // Example: 46 // CelAuthorizationEngine* engine = 47 // CelAuthorizationEngine::CreateCelAuthorizationEngine(rbac_policies); 48 // engine->Evaluate(evaluate_args); // returns authorization decision. 49 class CelAuthorizationEngine { 50 public: 51 // rbac_policies must be a vector containing either a single policy of any 52 // kind, or one deny policy and one allow policy, in that order. 53 static std::unique_ptr<CelAuthorizationEngine> CreateCelAuthorizationEngine( 54 const std::vector<envoy_config_rbac_v3_RBAC*>& rbac_policies); 55 56 // Users should use the CreateCelAuthorizationEngine factory function 57 // instead of calling the CelAuthorizationEngine constructor directly. 58 explicit CelAuthorizationEngine( 59 const std::vector<envoy_config_rbac_v3_RBAC*>& rbac_policies); 60 // TODO(mywang@google.com): add an Evaluate member function. 61 62 private: 63 enum Action { 64 kAllow, 65 kDeny, 66 }; 67 68 std::unique_ptr<mock_cel::Activation> CreateActivation( 69 const EvaluateArgs& args); 70 71 std::map<const std::string, const google_api_expr_v1alpha1_Expr*> 72 deny_if_matched_; 73 std::map<const std::string, const google_api_expr_v1alpha1_Expr*> 74 allow_if_matched_; 75 upb::Arena arena_; 76 absl::flat_hash_set<std::string> envoy_attributes_; 77 absl::flat_hash_set<std::string> header_keys_; 78 std::unique_ptr<mock_cel::CelMap> headers_; 79 }; 80 81 } // namespace grpc_core 82 83 #endif // GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_CEL_AUTHORIZATION_ENGINE_H 84