• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H
20 #define GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H
21 
22 #include <grpc/grpc_audit_logging.h>
23 #include <grpc/support/json.h>
24 #include <grpc/support/port_platform.h>
25 
26 #include <map>
27 #include <memory>
28 
29 #include "absl/base/thread_annotations.h"
30 #include "absl/status/statusor.h"
31 #include "absl/strings/string_view.h"
32 #include "src/core/util/sync.h"
33 
34 namespace grpc_core {
35 namespace experimental {
36 
37 class AuditLoggerRegistry {
38  public:
39   static void RegisterFactory(std::unique_ptr<AuditLoggerFactory>);
40 
41   static bool FactoryExists(absl::string_view name);
42 
43   static absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
44   ParseConfig(absl::string_view name, const Json& json);
45 
46   // This assume the given config is parsed and validated already.
47   // Therefore, it should always succeed in creating a logger.
48   static std::unique_ptr<AuditLogger> CreateAuditLogger(
49       std::unique_ptr<AuditLoggerFactory::Config>);
50 
51   // Factories are registered during initialization. They should never be
52   // unregistered since they will be looked up at any time till the program
53   // exits. This function should only be used in tests to clear the registry.
54   static void TestOnlyResetRegistry();
55 
56  private:
57   AuditLoggerRegistry();
58 
59   static Mutex* mu;
60 
61   static AuditLoggerRegistry* registry ABSL_GUARDED_BY(mu);
62 
63   // The key is owned by the factory.
64   std::map<absl::string_view, std::unique_ptr<AuditLoggerFactory>>
65       logger_factories_map_ ABSL_GUARDED_BY(mu);
66 };
67 
68 }  // namespace experimental
69 }  // namespace grpc_core
70 
71 #endif  // GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_AUDIT_LOGGING_H
72