[Audit Logging] Logger and factory APIs in C-Core and C++. (#32750)

Audit logging APIs for both built-in loggers and third-party logger
implementations.

C++ uses using decls referring to C-Core APIs.

---------

Co-authored-by: rockspore <rockspore@users.noreply.github.com>
pull/32947/head
Luwei Ge 2 years ago committed by GitHub
parent da5dbc068d
commit dcfc5d6904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      BUILD
  2. 51
      CMakeLists.txt
  3. 2
      Makefile
  4. 30
      build_autogenerated.yaml
  5. 4
      gRPC-C++.podspec
  6. 1
      gRPC-Core.podspec
  7. 1
      grpc.gemspec
  8. 1
      grpc.gyp
  9. 96
      include/grpc/grpc_audit_logging.h
  10. 1
      include/grpc/module.modulemap
  11. 44
      include/grpcpp/security/audit_logging.h
  12. 1
      package.xml
  13. 22
      src/core/BUILD
  14. 91
      src/core/lib/security/authorization/audit_logging.cc
  15. 74
      src/core/lib/security/authorization/audit_logging.h
  16. 13
      test/core/security/BUILD
  17. 114
      test/core/security/grpc_audit_logging_test.cc
  18. 2
      tools/doxygen/Doxyfile.c++
  19. 4
      tools/doxygen/Doxyfile.c++.internal
  20. 1
      tools/doxygen/Doxyfile.core
  21. 1
      tools/doxygen/Doxyfile.core.internal
  22. 24
      tools/run_tests/generated/tests.json

@ -220,6 +220,7 @@ GPR_PUBLIC_HDRS = [
]
GRPC_PUBLIC_HDRS = [
"include/grpc/grpc_audit_logging.h",
"include/grpc/byte_buffer.h",
"include/grpc/byte_buffer_reader.h",
"include/grpc/compression.h",
@ -388,6 +389,7 @@ GRPCXX_PUBLIC_HDRS = [
"include/grpcpp/impl/status.h",
"include/grpcpp/impl/sync.h",
"include/grpcpp/resource_quota.h",
"include/grpcpp/security/audit_logging.h",
"include/grpcpp/security/auth_context.h",
"include/grpcpp/security/auth_metadata_processor.h",
"include/grpcpp/security/credentials.h",
@ -790,6 +792,10 @@ grpc_cc_library(
grpc_cc_library(
name = "grpc_public_hdrs",
hdrs = GRPC_PUBLIC_HDRS,
external_deps = [
"absl/status:statusor",
"absl/strings",
],
tags = [
"avoid_dep",
"nofixdeps",
@ -1734,6 +1740,7 @@ grpc_cc_library(
"//src/core:handshaker_factory",
"//src/core:handshaker_registry",
"//src/core:iomgr_fwd",
"//src/core:json",
"//src/core:memory_quota",
"//src/core:poll",
"//src/core:ref_counted",
@ -1886,6 +1893,7 @@ grpc_cc_library(
"//src/core:error",
"//src/core:gpr_atm",
"//src/core:gpr_manual_constructor",
"//src/core:grpc_audit_logging",
"//src/core:grpc_backend_metric_provider",
"//src/core:grpc_service_config",
"//src/core:grpc_transport_inproc",

51
CMakeLists.txt generated

@ -982,6 +982,7 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx google_mesh_ca_certificate_provider_factory_test)
add_dependencies(buildtests_cxx graceful_shutdown_test)
add_dependencies(buildtests_cxx grpc_alts_credentials_options_test)
add_dependencies(buildtests_cxx grpc_audit_logging_test)
add_dependencies(buildtests_cxx grpc_authorization_engine_test)
add_dependencies(buildtests_cxx grpc_authorization_policy_provider_test)
add_dependencies(buildtests_cxx grpc_authz_end2end_test)
@ -2409,6 +2410,7 @@ foreach(_hdr
include/grpc/event_engine/slice_buffer.h
include/grpc/fork.h
include/grpc/grpc.h
include/grpc/grpc_audit_logging.h
include/grpc/grpc_posix.h
include/grpc/grpc_security.h
include/grpc/grpc_security_constants.h
@ -3043,6 +3045,7 @@ foreach(_hdr
include/grpc/event_engine/slice_buffer.h
include/grpc/fork.h
include/grpc/grpc.h
include/grpc/grpc_audit_logging.h
include/grpc/grpc_posix.h
include/grpc/grpc_security.h
include/grpc/grpc_security_constants.h
@ -3219,6 +3222,7 @@ add_library(grpc++
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -3472,6 +3476,7 @@ foreach(_hdr
include/grpcpp/impl/status.h
include/grpcpp/impl/sync.h
include/grpcpp/resource_quota.h
include/grpcpp/security/audit_logging.h
include/grpcpp/security/auth_context.h
include/grpcpp/security/auth_metadata_processor.h
include/grpcpp/security/authorization_policy_provider.h
@ -4175,6 +4180,7 @@ foreach(_hdr
include/grpcpp/impl/status.h
include/grpcpp/impl/sync.h
include/grpcpp/resource_quota.h
include/grpcpp/security/audit_logging.h
include/grpcpp/security/auth_context.h
include/grpcpp/security/auth_metadata_processor.h
include/grpcpp/security/authorization_policy_provider.h
@ -4545,6 +4551,7 @@ foreach(_hdr
include/grpc/event_engine/slice_buffer.h
include/grpc/fork.h
include/grpc/grpc.h
include/grpc/grpc_audit_logging.h
include/grpc/grpc_posix.h
include/grpc/grpc_security.h
include/grpc/grpc_security_constants.h
@ -6846,6 +6853,7 @@ add_executable(binder_transport_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -9918,6 +9926,7 @@ add_executable(endpoint_binder_pool_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -10610,6 +10619,7 @@ add_executable(fake_binder_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -11944,6 +11954,44 @@ target_link_libraries(grpc_alts_credentials_options_test
)
endif()
if(gRPC_BUILD_TESTS)
add_executable(grpc_audit_logging_test
src/core/lib/security/authorization/audit_logging.cc
test/core/security/grpc_audit_logging_test.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
target_compile_features(grpc_audit_logging_test PUBLIC cxx_std_14)
target_include_directories(grpc_audit_logging_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(grpc_audit_logging_test
${_gRPC_BASELIB_LIBRARIES}
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ZLIB_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
grpc_test_util
)
endif()
if(gRPC_BUILD_TESTS)
@ -21782,6 +21830,7 @@ add_executable(transport_stream_receiver_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -22496,6 +22545,7 @@ add_executable(wire_reader_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc
@ -22597,6 +22647,7 @@ add_executable(wire_writer_test
src/core/ext/transport/binder/wire_format/transaction.cc
src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
src/core/ext/transport/binder/wire_format/wire_writer.cc
src/core/lib/security/authorization/audit_logging.cc
src/cpp/client/channel_cc.cc
src/cpp/client/client_callback.cc
src/cpp/client/client_context.cc

2
Makefile generated

@ -1733,6 +1733,7 @@ PUBLIC_HEADERS_C += \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \
@ -2222,6 +2223,7 @@ PUBLIC_HEADERS_C += \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \

@ -155,6 +155,7 @@ libs:
- include/grpc/event_engine/slice_buffer.h
- include/grpc/fork.h
- include/grpc/grpc.h
- include/grpc/grpc_audit_logging.h
- include/grpc/grpc_posix.h
- include/grpc/grpc_security.h
- include/grpc/grpc_security_constants.h
@ -1847,6 +1848,7 @@ libs:
- include/grpc/event_engine/slice_buffer.h
- include/grpc/fork.h
- include/grpc/grpc.h
- include/grpc/grpc_audit_logging.h
- include/grpc/grpc_posix.h
- include/grpc/grpc_security.h
- include/grpc/grpc_security_constants.h
@ -2896,6 +2898,7 @@ libs:
- include/grpcpp/impl/status.h
- include/grpcpp/impl/sync.h
- include/grpcpp/resource_quota.h
- include/grpcpp/security/audit_logging.h
- include/grpcpp/security/auth_context.h
- include/grpcpp/security/auth_metadata_processor.h
- include/grpcpp/security/authorization_policy_provider.h
@ -2957,6 +2960,7 @@ libs:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -2988,6 +2992,7 @@ libs:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -3318,6 +3323,7 @@ libs:
- include/grpcpp/impl/status.h
- include/grpcpp/impl/sync.h
- include/grpcpp/resource_quota.h
- include/grpcpp/security/audit_logging.h
- include/grpcpp/security/auth_context.h
- include/grpcpp/security/auth_metadata_processor.h
- include/grpcpp/security/authorization_policy_provider.h
@ -3424,6 +3430,7 @@ libs:
- include/grpc/event_engine/slice_buffer.h
- include/grpc/fork.h
- include/grpc/grpc.h
- include/grpc/grpc_audit_logging.h
- include/grpc/grpc_posix.h
- include/grpc/grpc_security.h
- include/grpc/grpc_security_constants.h
@ -4972,6 +4979,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -5004,6 +5012,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -6450,6 +6459,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -6482,6 +6492,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -6872,6 +6883,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -6904,6 +6916,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -7991,6 +8004,17 @@ targets:
- test/core/util/tracer_util.cc
deps:
- grpc_test_util
- name: grpc_audit_logging_test
gtest: true
build: test
language: c++
headers:
- src/core/lib/security/authorization/audit_logging.h
src:
- src/core/lib/security/authorization/audit_logging.cc
- test/core/security/grpc_audit_logging_test.cc
deps:
- grpc_test_util
- name: grpc_authorization_engine_test
gtest: true
build: test
@ -12250,6 +12274,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -12281,6 +12306,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -12563,6 +12589,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -12595,6 +12622,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc
@ -12670,6 +12698,7 @@ targets:
- src/core/ext/transport/binder/wire_format/wire_reader.h
- src/core/ext/transport/binder/wire_format/wire_reader_impl.h
- src/core/ext/transport/binder/wire_format/wire_writer.h
- src/core/lib/security/authorization/audit_logging.h
- src/cpp/client/client_stats_interceptor.h
- src/cpp/client/create_channel_internal.h
- src/cpp/client/secure_credentials.h
@ -12702,6 +12731,7 @@ targets:
- src/core/ext/transport/binder/wire_format/transaction.cc
- src/core/ext/transport/binder/wire_format/wire_reader_impl.cc
- src/core/ext/transport/binder/wire_format/wire_writer.cc
- src/core/lib/security/authorization/audit_logging.cc
- src/cpp/client/channel_cc.cc
- src/cpp/client/client_callback.cc
- src/cpp/client/client_context.cc

4
gRPC-C++.podspec generated

@ -167,6 +167,7 @@ Pod::Spec.new do |s|
'include/grpcpp/impl/status.h',
'include/grpcpp/impl/sync.h',
'include/grpcpp/resource_quota.h',
'include/grpcpp/security/audit_logging.h',
'include/grpcpp/security/auth_context.h',
'include/grpcpp/security/auth_metadata_processor.h',
'include/grpcpp/security/authorization_policy_provider.h',
@ -957,6 +958,8 @@ Pod::Spec.new do |s|
'src/core/lib/resource_quota/resource_quota.h',
'src/core/lib/resource_quota/thread_quota.h',
'src/core/lib/resource_quota/trace.h',
'src/core/lib/security/authorization/audit_logging.cc',
'src/core/lib/security/authorization/audit_logging.h',
'src/core/lib/security/authorization/authorization_engine.h',
'src/core/lib/security/authorization/authorization_policy_provider.h',
'src/core/lib/security/authorization/evaluate_args.h',
@ -1982,6 +1985,7 @@ Pod::Spec.new do |s|
'src/core/lib/resource_quota/resource_quota.h',
'src/core/lib/resource_quota/thread_quota.h',
'src/core/lib/resource_quota/trace.h',
'src/core/lib/security/authorization/audit_logging.h',
'src/core/lib/security/authorization/authorization_engine.h',
'src/core/lib/security/authorization/authorization_policy_provider.h',
'src/core/lib/security/authorization/evaluate_args.h',

1
gRPC-Core.podspec generated

@ -122,6 +122,7 @@ Pod::Spec.new do |s|
'include/grpc/event_engine/slice_buffer.h',
'include/grpc/fork.h',
'include/grpc/grpc.h',
'include/grpc/grpc_audit_logging.h',
'include/grpc/grpc_posix.h',
'include/grpc/grpc_security.h',
'include/grpc/grpc_security_constants.h',

1
grpc.gemspec generated

@ -62,6 +62,7 @@ Gem::Specification.new do |s|
s.files += %w( include/grpc/event_engine/slice_buffer.h )
s.files += %w( include/grpc/fork.h )
s.files += %w( include/grpc/grpc.h )
s.files += %w( include/grpc/grpc_audit_logging.h )
s.files += %w( include/grpc/grpc_posix.h )
s.files += %w( include/grpc/grpc_security.h )
s.files += %w( include/grpc/grpc_security_constants.h )

1
grpc.gyp generated

@ -1506,6 +1506,7 @@
'src/core/ext/transport/binder/wire_format/transaction.cc',
'src/core/ext/transport/binder/wire_format/wire_reader_impl.cc',
'src/core/ext/transport/binder/wire_format/wire_writer.cc',
'src/core/lib/security/authorization/audit_logging.cc',
'src/cpp/client/channel_cc.cc',
'src/cpp/client/client_callback.cc',
'src/cpp/client/client_context.cc',

@ -0,0 +1,96 @@
//
//
// Copyright 2023 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_GRPC_AUDIT_LOGGING_H
#define GRPC_GRPC_AUDIT_LOGGING_H
#include <grpc/support/port_platform.h>
#include <memory>
#include <string>
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
// TODO(lwge): Switch to public header when it's ready.
#include "src/core/lib/json/json.h"
namespace grpc_core {
namespace experimental {
// The class containing the context for an audited RPC.
class AuditContext {
public:
AuditContext(absl::string_view rpc_method, absl::string_view principal,
absl::string_view policy_name, absl::string_view matched_rule,
bool authorized)
: rpc_method_(rpc_method),
principal_(principal),
policy_name_(policy_name),
matched_rule_(matched_rule),
authorized_(authorized) {}
absl::string_view rpc_method() const { return rpc_method_; }
absl::string_view principal() const { return principal_; }
absl::string_view policy_name() const { return policy_name_; }
absl::string_view matched_rule() const { return matched_rule_; }
bool authorized() const { return authorized_; }
private:
absl::string_view rpc_method_;
absl::string_view principal_;
absl::string_view policy_name_;
absl::string_view matched_rule_;
bool authorized_;
};
// This base class for audit logger implementations.
class AuditLogger {
public:
virtual ~AuditLogger() = default;
virtual void Log(const AuditContext& audit_context) = 0;
};
// This is the base class for audit logger factory implementations.
class AuditLoggerFactory {
public:
class Config {
public:
virtual ~Config() = default;
virtual absl::string_view name() const = 0;
virtual std::string ToString() const = 0;
};
virtual ~AuditLoggerFactory() = default;
virtual absl::string_view name() const = 0;
virtual absl::StatusOr<std::unique_ptr<Config>> ParseAuditLoggerConfig(
const Json& json) = 0;
virtual std::unique_ptr<AuditLogger> CreateAuditLogger(
std::unique_ptr<AuditLoggerFactory::Config>) = 0;
};
// Registers an audit logger factory. This should only be called during
// initialization.
void RegisterAuditLoggerFactory(std::unique_ptr<AuditLoggerFactory> factory);
} // namespace experimental
} // namespace grpc_core
#endif /* GRPC_GRPC_AUDIT_LOGGING_H */

@ -8,6 +8,7 @@ header "byte_buffer.h"
header "compression.h"
header "fork.h"
header "grpc.h"
header "grpc_audit_logging.h"
header "grpc_posix.h"
header "grpc_security.h"
header "grpc_security_constants.h"

@ -0,0 +1,44 @@
//
//
// Copyright 2023 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 GRPCPP_SECURITY_AUDIT_LOGGING_H
#define GRPCPP_SECURITY_AUDIT_LOGGING_H
#include <memory>
#include <string>
#include <utility>
#include "absl/status/statusor.h"
#include <grpc/grpc_audit_logging.h>
#include <grpcpp/support/string_ref.h>
namespace grpc {
namespace experimental {
using grpc_core::experimental::AuditContext; // NOLINT(misc-unused-using-decls)
using grpc_core::experimental::AuditLogger; // NOLINT(misc-unused-using-decls)
using grpc_core::experimental::
AuditLoggerFactory; // NOLINT(misc-unused-using-decls)
using grpc_core::experimental::
RegisterAuditLoggerFactory; // NOLINT(misc-unused-using-decls)
} // namespace experimental
} // namespace grpc
#endif // GRPCPP_SECURITY_AUDIT_LOGGING_H

1
package.xml generated

@ -44,6 +44,7 @@
<file baseinstalldir="/" name="include/grpc/event_engine/slice_buffer.h" role="src" />
<file baseinstalldir="/" name="include/grpc/fork.h" role="src" />
<file baseinstalldir="/" name="include/grpc/grpc.h" role="src" />
<file baseinstalldir="/" name="include/grpc/grpc_audit_logging.h" role="src" />
<file baseinstalldir="/" name="include/grpc/grpc_posix.h" role="src" />
<file baseinstalldir="/" name="include/grpc/grpc_security.h" role="src" />
<file baseinstalldir="/" name="include/grpc/grpc_security_constants.h" role="src" />

@ -2716,6 +2716,28 @@ grpc_cc_library(
],
)
grpc_cc_library(
name = "grpc_audit_logging",
srcs = [
"lib/security/authorization/audit_logging.cc",
],
hdrs = [
"lib/security/authorization/audit_logging.h",
],
external_deps = [
"absl/base:core_headers",
"absl/status",
"absl/status:statusor",
"absl/strings",
"absl/strings:str_format",
],
deps = [
"json",
"//:gpr",
"//:grpc_base",
],
)
grpc_cc_library(
name = "grpc_authorization_base",
srcs = [

@ -0,0 +1,91 @@
//
//
// Copyright 2023 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.
//
//
#include <grpc/support/port_platform.h>
#include "src/core/lib/security/authorization/audit_logging.h"
#include <initializer_list>
#include <map>
#include <memory>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include <grpc/grpc_audit_logging.h>
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/json/json.h"
namespace grpc_core {
namespace experimental {
Mutex* AuditLoggerRegistry::mu = new Mutex();
AuditLoggerRegistry* AuditLoggerRegistry::registry = new AuditLoggerRegistry();
void AuditLoggerRegistry::RegisterFactory(
std::unique_ptr<AuditLoggerFactory> factory) {
GPR_ASSERT(factory != nullptr);
MutexLock lock(mu);
absl::string_view name = factory->name();
GPR_ASSERT(
registry->logger_factories_map_.emplace(name, std::move(factory)).second);
}
bool AuditLoggerRegistry::FactoryExists(absl::string_view name) {
MutexLock lock(mu);
return registry->logger_factories_map_.find(name) !=
registry->logger_factories_map_.end();
}
absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
AuditLoggerRegistry::ParseConfig(absl::string_view name, const Json& json) {
MutexLock lock(mu);
auto it = registry->logger_factories_map_.find(name);
if (it == registry->logger_factories_map_.end()) {
return absl::NotFoundError(
absl::StrFormat("audit logger factory for %s does not exist", name));
}
return it->second->ParseAuditLoggerConfig(json);
}
std::unique_ptr<AuditLogger> AuditLoggerRegistry::CreateAuditLogger(
std::unique_ptr<AuditLoggerFactory::Config> config) {
MutexLock lock(mu);
auto it = registry->logger_factories_map_.find(config->name());
GPR_ASSERT(it != registry->logger_factories_map_.end());
return it->second->CreateAuditLogger(std::move(config));
}
void AuditLoggerRegistry::TestOnlyResetRegistry() {
MutexLock lock(mu);
delete registry;
registry = new AuditLoggerRegistry();
}
void RegisterAuditLoggerFactory(std::unique_ptr<AuditLoggerFactory> factory) {
AuditLoggerRegistry::RegisterFactory(std::move(factory));
}
} // namespace experimental
} // namespace grpc_core

@ -0,0 +1,74 @@
//
//
// Copyright 2023 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_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H
#define GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H
#include <grpc/support/port_platform.h>
#include <map>
#include <memory>
#include "absl/base/thread_annotations.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include <grpc/grpc_audit_logging.h>
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/json/json.h"
namespace grpc_core {
namespace experimental {
class AuditLoggerRegistry {
public:
static void RegisterFactory(std::unique_ptr<AuditLoggerFactory>);
static bool FactoryExists(absl::string_view name);
static absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
ParseConfig(absl::string_view name, const Json& json);
// This assume the given config is parsed and validated already.
// Therefore, it should always succeed in creating a logger.
static std::unique_ptr<AuditLogger> CreateAuditLogger(
std::unique_ptr<AuditLoggerFactory::Config>);
// Factories are registered during initialization. They should never be
// unregistered since they will be looked up at any time till the program
// exits. This function should only be used in tests to clear the registry.
static void TestOnlyResetRegistry();
private:
// TODO(lwge): Add built-in logger registrations once avaialble.
AuditLoggerRegistry() = default;
static Mutex* mu;
static AuditLoggerRegistry* registry ABSL_GUARDED_BY(mu);
// The key is owned by the factory.
std::map<absl::string_view, std::unique_ptr<AuditLoggerFactory>>
logger_factories_map_ ABSL_GUARDED_BY(mu);
};
} // namespace experimental
} // namespace grpc_core
#endif // GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H

@ -559,3 +559,16 @@ grpc_cc_test(
"//test/core/util:grpc_test_util",
],
)
grpc_cc_test(
name = "grpc_audit_logging_test",
srcs = ["grpc_audit_logging_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
"//:grpc",
"//src/core:grpc_audit_logging",
"//test/core/util:grpc_test_util",
],
)

@ -0,0 +1,114 @@
//
//
// Copyright 2023 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.
//
//
#include <grpc/support/port_platform.h>
#include <memory>
#include <string>
#include <gtest/gtest.h>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include <grpc/grpc_audit_logging.h>
#include "src/core/lib/json/json.h"
#include "src/core/lib/security/authorization/audit_logging.h"
#include "test/core/util/test_config.h"
#include "test/core/util/tls_utils.h"
namespace grpc_core {
namespace testing {
constexpr absl::string_view kName = "test_logger";
using experimental::AuditContext;
using experimental::AuditLogger;
using experimental::AuditLoggerFactory;
using experimental::AuditLoggerRegistry;
using experimental::RegisterAuditLoggerFactory;
namespace {
class TestAuditLogger : public AuditLogger {
public:
void Log(const AuditContext&) override {}
};
class TestAuditLoggerFactory : public AuditLoggerFactory {
public:
class TestConfig : public Config {
public:
absl::string_view name() const override { return kName; }
std::string ToString() const override { return "test_config"; }
};
absl::string_view name() const override { return kName; }
std::unique_ptr<AuditLogger> CreateAuditLogger(
std::unique_ptr<AuditLoggerFactory::Config>) override {
return std::make_unique<TestAuditLogger>();
}
absl::StatusOr<std::unique_ptr<Config>> ParseAuditLoggerConfig(
const Json&) override {
return std::make_unique<TestConfig>();
}
};
class AuditLoggingTest : public ::testing::Test {
protected:
void SetUp() override {
RegisterAuditLoggerFactory(std::make_unique<TestAuditLoggerFactory>());
}
void TearDown() override { AuditLoggerRegistry::TestOnlyResetRegistry(); }
};
} // namespace
TEST_F(AuditLoggingTest, SuccessfulLoggerCreation) {
auto result = AuditLoggerRegistry::ParseConfig(kName, Json());
ASSERT_TRUE(result.ok());
ASSERT_NE(AuditLoggerRegistry::CreateAuditLogger(std::move(result.value())),
nullptr);
}
TEST_F(AuditLoggingTest, UnknownLogger) {
auto result = AuditLoggerRegistry::ParseConfig("unknown_logger", Json());
EXPECT_EQ(result.status().code(), absl::StatusCode::kNotFound);
EXPECT_EQ(result.status().message(),
"audit logger factory for unknown_logger does not exist")
<< result.status();
}
TEST_F(AuditLoggingTest, AuditLoggerFactoryExistenceChecks) {
EXPECT_TRUE(AuditLoggerRegistry::FactoryExists(kName));
EXPECT_FALSE(AuditLoggerRegistry::FactoryExists("unknown_logger"));
}
} // namespace testing
} // namespace grpc_core
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
grpc_init();
int ret = RUN_ALL_TESTS();
grpc_shutdown();
return ret;
}

@ -890,6 +890,7 @@ include/grpc/event_engine/slice.h \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \
@ -1032,6 +1033,7 @@ include/grpcpp/impl/service_type.h \
include/grpcpp/impl/status.h \
include/grpcpp/impl/sync.h \
include/grpcpp/resource_quota.h \
include/grpcpp/security/audit_logging.h \
include/grpcpp/security/auth_context.h \
include/grpcpp/security/auth_metadata_processor.h \
include/grpcpp/security/authorization_policy_provider.h \

@ -890,6 +890,7 @@ include/grpc/event_engine/slice.h \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \
@ -1032,6 +1033,7 @@ include/grpcpp/impl/service_type.h \
include/grpcpp/impl/status.h \
include/grpcpp/impl/sync.h \
include/grpcpp/resource_quota.h \
include/grpcpp/security/audit_logging.h \
include/grpcpp/security/auth_context.h \
include/grpcpp/security/auth_metadata_processor.h \
include/grpcpp/security/authorization_policy_provider.h \
@ -2452,6 +2454,8 @@ src/core/lib/resource_quota/thread_quota.cc \
src/core/lib/resource_quota/thread_quota.h \
src/core/lib/resource_quota/trace.cc \
src/core/lib/resource_quota/trace.h \
src/core/lib/security/authorization/audit_logging.cc \
src/core/lib/security/authorization/audit_logging.h \
src/core/lib/security/authorization/authorization_engine.h \
src/core/lib/security/authorization/authorization_policy_provider.h \
src/core/lib/security/authorization/authorization_policy_provider_vtable.cc \

@ -823,6 +823,7 @@ include/grpc/event_engine/slice.h \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \

@ -823,6 +823,7 @@ include/grpc/event_engine/slice.h \
include/grpc/event_engine/slice_buffer.h \
include/grpc/fork.h \
include/grpc/grpc.h \
include/grpc/grpc_audit_logging.h \
include/grpc/grpc_posix.h \
include/grpc/grpc_security.h \
include/grpc/grpc_security_constants.h \

@ -3317,6 +3317,30 @@
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "grpc_audit_logging_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,

Loading…
Cancel
Save