1
- #include " audit_log.h"
2
- #include " audit_log_impl.h"
3
-
4
1
#include < library/cpp/json/json_value.h>
5
2
#include < library/cpp/json/json_writer.h>
3
+ #include < library/cpp/logger/record.h>
4
+ #include < library/cpp/logger/backend.h>
5
+
6
+ #include < ydb/library/actors/core/log.h>
7
+ #include < ydb/library/actors/core/actor.h>
8
+ #include < ydb/library/actors/core/events.h>
9
+ #include < ydb/library/actors/core/hfunc.h>
10
+ #include < ydb/library/services/services.pb.h>
11
+
12
+ #include < ydb/core/base/events.h>
13
+
14
+ #include " audit_log_service.h"
15
+ #include " audit_log.h"
16
+
17
+ #if defined LOG_T || \
18
+ defined LOG_D || \
19
+ defined LOG_I || \
20
+ defined LOG_N || \
21
+ defined LOG_W || \
22
+ defined LOG_E
23
+ # error log macro redefinition
24
+ #endif
25
+
26
+ #define LOG_T (stream ) LOG_TRACE_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
27
+ #define LOG_D (stream ) LOG_DEBUG_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
28
+ #define LOG_I (stream ) LOG_INFO_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
29
+ #define LOG_N (stream ) LOG_NOTICE_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
30
+ #define LOG_W (stream ) LOG_WARN_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
31
+ #define LOG_E (stream ) LOG_ERROR_S((TlsActivationContext->AsActorContext ()), NKikimrServices::AUDIT_LOG_WRITER, stream)
6
32
7
33
namespace NKikimr::NAudit {
8
34
9
- using namespace NActors ;
35
+ // TAuditLogActor
36
+ //
10
37
11
- void TAuditLogActor::HandlePoisonPill (
12
- const TEvents::TEvPoisonPill::TPtr& ev,
13
- const TActorContext& ctx)
14
- {
15
- Y_UNUSED (ev);
16
- AUDIT_LOG_ENABLED.store (false );
17
- Die (ctx);
18
- }
38
+ struct TEvAuditLog {
39
+ //
40
+ // Events declaration
41
+ //
19
42
20
- STFUNC (TAuditLogActor::StateWork)
21
- {
22
- switch (ev->GetTypeRewrite ()) {
23
- HFunc (TEvents::TEvPoisonPill, HandlePoisonPill);
24
- HFunc (TEvAuditLog::TEvWriteAuditLog, HandleWriteAuditLog);
25
- default :
26
- HandleUnexpectedEvent (ev);
27
- break ;
28
- }
29
- }
43
+ enum EEvents {
44
+ EvBegin = EventSpaceBegin (TKikimrEvents::ES_YDB_AUDIT_LOG),
45
+
46
+ // Request actors
47
+ EvWriteAuditLog = EvBegin + 0 ,
48
+
49
+ EvEnd
50
+ };
51
+
52
+ static_assert (EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG),
53
+ " expected EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG)"
54
+ );
55
+
56
+ struct TEvWriteAuditLog : public NActors ::TEventLocal<TEvWriteAuditLog, EvWriteAuditLog> {
57
+ TInstant Time;
58
+ TVector<std::pair<TString, TString>> Parts;
59
+
60
+ TEvWriteAuditLog (TInstant time, TVector<std::pair<TString, TString>>&& parts)
61
+ : Time(time)
62
+ , Parts(std::move(parts))
63
+ {}
64
+ };
65
+ };
30
66
31
- void TAuditLogActor:: WriteLog (const TString& log, const TVector<THolder<TLogBackend>>& logBackends) {
67
+ void WriteLog (const TString& log , const TVector<THolder<TLogBackend>>& logBackends) {
32
68
for (auto & logBackend : logBackends) {
33
69
try {
34
- logBackend->WriteData (
35
- TLogRecord (
36
- ELogPriority::TLOG_INFO ,
37
- log .data (),
38
- log . length () ));
70
+ logBackend->WriteData (TLogRecord (
71
+ ELogPriority::TLOG_INFO,
72
+ log . data () ,
73
+ log .length ()
74
+ ));
39
75
} catch (const yexception& e) {
40
- LOG_W (" WriteLog:"
41
- << " unable to write audit log (error: " << e.what () << " )" );
76
+ LOG_W (" WriteLog: unable to write audit log (error: " << e.what () << " )" );
42
77
}
43
78
}
44
79
}
45
80
46
- TString TAuditLogActor:: GetJsonLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
81
+ TString GetJsonLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
47
82
const auto * msg = ev->Get ();
48
83
TStringStream ss;
49
84
ss << msg->Time << " : " ;
@@ -56,7 +91,7 @@ TString TAuditLogActor::GetJsonLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev
56
91
return ss.Str ();
57
92
}
58
93
59
- TString TAuditLogActor:: GetTxtLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
94
+ TString GetTxtLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
60
95
const auto * msg = ev->Get ();
61
96
TStringStream ss;
62
97
ss << msg->Time << " : " ;
@@ -69,29 +104,81 @@ TString TAuditLogActor::GetTxtLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev)
69
104
return ss.Str ();
70
105
}
71
106
72
- void TAuditLogActor::HandleWriteAuditLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev, const TActorContext& ctx) {
73
- Y_UNUSED (ctx);
74
-
75
- for (auto & logBackends : LogBackends) {
76
- switch (logBackends.first ) {
77
- case NKikimrConfig::TAuditConfig::JSON:
78
- WriteLog (GetJsonLog (ev), logBackends.second );
79
- break ;
80
- case NKikimrConfig::TAuditConfig::TXT:
81
- WriteLog (GetTxtLog (ev), logBackends.second );
82
- break ;
83
- default :
84
- WriteLog (GetJsonLog (ev), logBackends.second );
85
- break ;
107
+ class TAuditLogActor final : public TActor<TAuditLogActor> {
108
+ private:
109
+ const TAuditLogBackends LogBackends;
110
+
111
+ public:
112
+ TAuditLogActor (TAuditLogBackends&& logBackends)
113
+ : TActor(&TThis::StateWork)
114
+ , LogBackends(std::move(logBackends))
115
+ {}
116
+
117
+ static constexpr NKikimrServices::TActivity::EType ActorActivityType () {
118
+ return NKikimrServices::TActivity::AUDIT_WRITER_ACTOR;
119
+ }
120
+
121
+ private:
122
+ STFUNC (StateWork) {
123
+ switch (ev->GetTypeRewrite ()) {
124
+ HFunc (TEvents::TEvPoisonPill, HandlePoisonPill);
125
+ HFunc (TEvAuditLog::TEvWriteAuditLog, HandleWriteAuditLog);
126
+ default :
127
+ HandleUnexpectedEvent (ev);
128
+ break ;
86
129
}
87
130
}
131
+
132
+ void HandlePoisonPill (const TEvents::TEvPoisonPill::TPtr& ev, const TActorContext& ctx) {
133
+ Y_UNUSED (ev);
134
+ AUDIT_LOG_ENABLED.store (false );
135
+ Die (ctx);
136
+ }
137
+
138
+ void HandleWriteAuditLog (const TEvAuditLog::TEvWriteAuditLog::TPtr& ev, const TActorContext& ctx) {
139
+ Y_UNUSED (ctx);
140
+
141
+ for (auto & logBackends : LogBackends) {
142
+ switch (logBackends.first ) {
143
+ case NKikimrConfig::TAuditConfig::JSON:
144
+ WriteLog (GetJsonLog (ev), logBackends.second );
145
+ break ;
146
+ case NKikimrConfig::TAuditConfig::TXT:
147
+ WriteLog (GetTxtLog (ev), logBackends.second );
148
+ break ;
149
+ default :
150
+ WriteLog (GetJsonLog (ev), logBackends.second );
151
+ break ;
152
+ }
153
+ }
154
+ }
155
+
156
+ void HandleUnexpectedEvent (STFUNC_SIG) {
157
+ LOG_W (" TAuditLogActor:"
158
+ << " unhandled event type: " << ev->GetTypeRewrite ()
159
+ << " event: " << ev->GetTypeName ()
160
+ );
161
+ }
162
+ };
163
+
164
+ // Client interface implementation
165
+ //
166
+
167
+ std::atomic<bool > AUDIT_LOG_ENABLED = false ;
168
+
169
+ void SendAuditLog (const NActors::TActorSystem* sys, TVector<std::pair<TString, TString>>&& parts)
170
+ {
171
+ auto request = MakeHolder<TEvAuditLog::TEvWriteAuditLog>(Now (), std::move (parts));
172
+ sys->Send (MakeAuditServiceID (), request.Release ());
88
173
}
89
174
90
- void TAuditLogActor::HandleUnexpectedEvent (STFUNC_SIG)
175
+ // Service interface implementation
176
+ //
177
+
178
+ THolder<NActors::IActor> CreateAuditWriter (TAuditLogBackends&& logBackends)
91
179
{
92
- LOG_W (" TAuditLogActor:"
93
- << " unhandled event type: " << ev->GetTypeRewrite ()
94
- << " event: " << ev->GetTypeName ());
180
+ AUDIT_LOG_ENABLED.store (true );
181
+ return MakeHolder<TAuditLogActor>(std::move (logBackends));
95
182
}
96
183
97
184
} // namespace NKikimr::NAudit
0 commit comments