• 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 #include "src/core/lib/security/authorization/audit_logging.h"
20 
21 #include <grpc/grpc_audit_logging.h>
22 #include <grpc/support/json.h>
23 #include <grpc/support/port_platform.h>
24 
25 #include <map>
26 #include <memory>
27 #include <utility>
28 
29 #include "absl/log/check.h"
30 #include "absl/status/status.h"
31 #include "absl/status/statusor.h"
32 #include "absl/strings/str_format.h"
33 #include "absl/strings/string_view.h"
34 #include "src/core/lib/security/authorization/stdout_logger.h"
35 #include "src/core/util/sync.h"
36 
37 namespace grpc_core {
38 namespace experimental {
39 
40 Mutex* AuditLoggerRegistry::mu = new Mutex();
41 
42 AuditLoggerRegistry* AuditLoggerRegistry::registry = new AuditLoggerRegistry();
43 
AuditLoggerRegistry()44 AuditLoggerRegistry::AuditLoggerRegistry() {
45   auto factory = std::make_unique<StdoutAuditLoggerFactory>();
46   absl::string_view name = factory->name();
47   CHECK(logger_factories_map_.emplace(name, std::move(factory)).second);
48 }
49 
RegisterFactory(std::unique_ptr<AuditLoggerFactory> factory)50 void AuditLoggerRegistry::RegisterFactory(
51     std::unique_ptr<AuditLoggerFactory> factory) {
52   CHECK(factory != nullptr);
53   MutexLock lock(mu);
54   absl::string_view name = factory->name();
55   CHECK(
56       registry->logger_factories_map_.emplace(name, std::move(factory)).second);
57 }
58 
FactoryExists(absl::string_view name)59 bool AuditLoggerRegistry::FactoryExists(absl::string_view name) {
60   MutexLock lock(mu);
61   return registry->logger_factories_map_.find(name) !=
62          registry->logger_factories_map_.end();
63 }
64 
65 absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
ParseConfig(absl::string_view name,const Json & json)66 AuditLoggerRegistry::ParseConfig(absl::string_view name, const Json& json) {
67   MutexLock lock(mu);
68   auto it = registry->logger_factories_map_.find(name);
69   if (it == registry->logger_factories_map_.end()) {
70     return absl::NotFoundError(
71         absl::StrFormat("audit logger factory for %s does not exist", name));
72   }
73   return it->second->ParseAuditLoggerConfig(json);
74 }
75 
CreateAuditLogger(std::unique_ptr<AuditLoggerFactory::Config> config)76 std::unique_ptr<AuditLogger> AuditLoggerRegistry::CreateAuditLogger(
77     std::unique_ptr<AuditLoggerFactory::Config> config) {
78   MutexLock lock(mu);
79   auto it = registry->logger_factories_map_.find(config->name());
80   CHECK(it != registry->logger_factories_map_.end());
81   return it->second->CreateAuditLogger(std::move(config));
82 }
83 
TestOnlyResetRegistry()84 void AuditLoggerRegistry::TestOnlyResetRegistry() {
85   MutexLock lock(mu);
86   delete registry;
87   registry = new AuditLoggerRegistry();
88 }
89 
RegisterAuditLoggerFactory(std::unique_ptr<AuditLoggerFactory> factory)90 void RegisterAuditLoggerFactory(std::unique_ptr<AuditLoggerFactory> factory) {
91   AuditLoggerRegistry::RegisterFactory(std::move(factory));
92 }
93 
94 }  // namespace experimental
95 }  // namespace grpc_core
96