From c392be6ca0316f469a8d9b68176fe5153b24a93e Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 5 Aug 2020 21:37:58 +0000 Subject: [PATCH] created mock versions of Activation and CelValue for use with AuthorizationEngine --- BUILD | 2 + build_autogenerated.yaml | 2 + .../authorization/mock_cel/activation.h | 61 ++++++++++++++ .../authorization/mock_cel/cel_value.h | 83 +++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 src/core/lib/security/authorization/mock_cel/activation.h create mode 100644 src/core/lib/security/authorization/mock_cel/cel_value.h diff --git a/BUILD b/BUILD index e3b5f4a1903..abf1272e276 100644 --- a/BUILD +++ b/BUILD @@ -1861,6 +1861,8 @@ grpc_cc_library( ], hdrs = [ "src/core/lib/security/authorization/authorization_engine.h", + "src/core/lib/security/authorization/mock_cel/activation.h", + "src/core/lib/security/authorization/mock_cel/cel_value.h", ], language = "c++", deps = [ diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 5fd7e2ee544..4d2d22038b6 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -4779,6 +4779,8 @@ targets: language: c++ headers: - src/core/lib/security/authorization/authorization_engine.h + - src/core/lib/security/authorization/mock_cel/activation.h + - src/core/lib/security/authorization/mock_cel/cel_value.h src: - src/core/lib/security/authorization/authorization_engine.cc - test/core/security/authorization_engine_test.cc diff --git a/src/core/lib/security/authorization/mock_cel/activation.h b/src/core/lib/security/authorization/mock_cel/activation.h new file mode 100644 index 00000000000..626923d4b19 --- /dev/null +++ b/src/core/lib/security/authorization/mock_cel/activation.h @@ -0,0 +1,61 @@ +// Copyright 2020 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_ACTIVATION_H +#define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_ACTIVATION_H + +#include + +#include "absl/strings/string_view.h" + +#include "src/core/lib/security/authorization/mock_cel/cel_value.h" + +namespace google { +namespace api { +namespace expr { +namespace runtime { + +// Base class for an activation. This is a temporary stub implementation of CEL +// APIs. Once gRPC imports the CEL library, this class will be removed. +class BaseActivation { + public: + BaseActivation() = default; + + // Non-copyable/non-assignable + BaseActivation(const BaseActivation&) = delete; + BaseActivation& operator=(const BaseActivation&) = delete; +}; + +// Instance of Activation class is used by evaluator. +// It provides binding between references used in expressions +// and actual values. This is a temporary stub implementation of CEL APIs. +// Once gRPC imports the CEL library, this class will be removed. +class Activation : public BaseActivation { + public: + Activation() = default; + + // Non-copyable/non-assignable + Activation(const Activation&) = delete; + Activation& operator=(const Activation&) = delete; + + // Insert value into Activation. + void InsertValue(absl::string_view name, const CelValue& value) {} +}; + +} // namespace runtime +} // namespace expr +} // namespace api +} // namespace google + +#endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_ACTIVATION_H diff --git a/src/core/lib/security/authorization/mock_cel/cel_value.h b/src/core/lib/security/authorization/mock_cel/cel_value.h new file mode 100644 index 00000000000..65f0e1ea532 --- /dev/null +++ b/src/core/lib/security/authorization/mock_cel/cel_value.h @@ -0,0 +1,83 @@ +// Copyright 2020 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H +#define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H + +// CelValue is a holder, capable of storing all kinds of data +// supported by CEL. +// CelValue defines explicitly typed/named getters/setters. +// When storing pointers to objects, CelValue does not accept ownership +// to them and does not control their lifecycle. Instead objects are expected +// to be either external to expression evaluation, and controlled beyond the +// scope or to be allocated and associated with some allocation/ownership +// controller (Arena). +// Usage examples: +// (a) For primitive types: +// CelValue value = CelValue::CreateInt64(1); +// (b) For string: +// std::string* msg("test"); +// CelValue value = CelValue::CreateString(msg); + +#include + +#include "absl/strings/string_view.h" + +namespace google { +namespace api { +namespace expr { +namespace runtime { + +// Break cyclic depdendencies for container types. +class CelMap; + +// This is a temporary stub implementation of CEL APIs. +// Once gRPC imports the CEL library, this class will be removed. +class CelValue { + public: + // Default constructor. + // Creates CelValue with null data type. + CelValue() : CelValue(nullptr) {} + + // We will use factory methods instead of public constructors + // The reason for this is the high risk of implicit type conversions + // between bool/int/pointer types. + // We rely on copy elision to avoid extra copying. + static CelValue CreateNull() { return CelValue(nullptr); } + + static CelValue CreateInt64(int64_t value) { return CreateNull(); } + + static CelValue CreateUint64(uint64_t value) { return CreateNull(); } + + static CelValue CreateStringView(absl::string_view value) { + return CreateNull(); + } + + static CelValue CreateString(const std::string* str) { return CreateNull(); } + + static CelValue CreateMap(const CelMap* value) { return CreateNull(); } + + private: + // Constructs CelValue wrapping value supplied as argument. + // Value type T should be supported by specification of ValueHolder. + template + explicit CelValue(T value) {} +}; + +} // namespace runtime +} // namespace expr +} // namespace api +} // namespace google + +#endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H