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