1 //
2 // Copyright 2023 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
17 #include "test/core/test_util/audit_logging_utils.h"
18
19 #include <grpc/grpc_audit_logging.h>
20 #include <grpc/support/json.h>
21 #include <grpc/support/port_platform.h>
22
23 #include <algorithm>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include "absl/status/statusor.h"
29 #include "absl/strings/string_view.h"
30 #include "src/core/util/json/json_writer.h"
31
32 namespace grpc_core {
33 namespace testing {
34
35 namespace {
36
37 constexpr absl::string_view kLoggerName = "test_logger";
38
39 using experimental::AuditContext;
40 using experimental::AuditLogger;
41 using experimental::AuditLoggerFactory;
42 using experimental::Json;
43
44 } // namespace
45
name() const46 absl::string_view TestAuditLogger::name() const { return kLoggerName; }
Log(const AuditContext & context)47 void TestAuditLogger::Log(const AuditContext& context) {
48 audit_logs_->push_back(JsonDump(Json::FromObject({
49 {"rpc_method", Json::FromString(std::string(context.rpc_method()))},
50 {"principal", Json::FromString(std::string(context.principal()))},
51 {"policy_name", Json::FromString(std::string(context.policy_name()))},
52 {"matched_rule", Json::FromString(std::string(context.matched_rule()))},
53 {"authorized", Json::FromBool(context.authorized())},
54 })));
55 }
56
name() const57 absl::string_view TestAuditLoggerFactory::Config::name() const {
58 return kLoggerName;
59 }
60
name() const61 absl::string_view TestAuditLoggerFactory::name() const { return kLoggerName; }
62
63 absl::StatusOr<std::unique_ptr<AuditLoggerFactory::Config>>
ParseAuditLoggerConfig(const Json &)64 TestAuditLoggerFactory::ParseAuditLoggerConfig(const Json&) {
65 return std::make_unique<Config>();
66 }
67
CreateAuditLogger(std::unique_ptr<AuditLoggerFactory::Config>)68 std::unique_ptr<AuditLogger> TestAuditLoggerFactory::CreateAuditLogger(
69 std::unique_ptr<AuditLoggerFactory::Config>) {
70 return std::make_unique<TestAuditLogger>(audit_logs_);
71 }
72
73 } // namespace testing
74 } // namespace grpc_core
75