Skip to content

Commit e1a142c

Browse files
authored
24-3: auditlog: add logins (#8104)
Add audit logging for login operation. merge #8027 (81ca5b1), #7546 (f972e3a) from main. KIKIMR-21774
1 parent d358371 commit e1a142c

18 files changed

+357
-205
lines changed

ydb/core/audit/audit_log.cpp

-24
This file was deleted.

ydb/core/audit/audit_log.h

+10-93
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
#pragma once
22

3-
#include <ydb/core/base/events.h>
3+
#include <utility>
4+
#include <atomic>
45

5-
#include <ydb/library/actors/core/actor.h>
6-
#include <ydb/library/actors/core/events.h>
7-
#include <library/cpp/logger/backend.h>
8-
#include <ydb/core/protos/config.pb.h>
9-
#include <ydb/library/services/services.pb.h>
10-
11-
#include <library/cpp/logger/record.h>
12-
#include <ydb/library/actors/core/hfunc.h>
13-
#include <ydb/library/actors/core/log.h>
6+
#include <util/generic/string.h>
7+
#include <util/generic/vector.h>
148

15-
#include <util/generic/strbuf.h>
16-
#include <util/datetime/base.h>
9+
#include <ydb/library/actors/core/actor.h>
1710

1811
#define AUDIT_LOG_S(sys, expr) \
1912
do { \
@@ -24,7 +17,7 @@
2417
} \
2518
} while (0) /**/
2619

27-
#define AUDIT_LOG(expr) AUDIT_LOG_S((TlsActivationContext->ExecutorThread.ActorSystem), expr)
20+
#define AUDIT_LOG(expr) AUDIT_LOG_S((::NActors::TlsActivationContext->ExecutorThread.ActorSystem), expr)
2821

2922
#define AUDIT_PART_NO_COND(key, value) AUDIT_PART_COND(key, value, true)
3023
#define AUDIT_PART_COND(key, value, condition) \
@@ -37,90 +30,14 @@
3730
#define GET_AUDIT_PART_MACRO(_1, _2, _3, NAME,...) NAME
3831
#define AUDIT_PART(...) GET_AUDIT_PART_MACRO(__VA_ARGS__, AUDIT_PART_COND, AUDIT_PART_NO_COND)(__VA_ARGS__)
3932

33+
namespace NActors {
34+
class TActorSystem;
35+
}
36+
4037
namespace NKikimr::NAudit {
4138

4239
extern std::atomic<bool> AUDIT_LOG_ENABLED;
4340

44-
struct TEvAuditLog
45-
{
46-
//
47-
// Events declaration
48-
//
49-
50-
enum EEvents
51-
{
52-
EvBegin = EventSpaceBegin(TKikimrEvents::ES_YDB_AUDIT_LOG),
53-
54-
// Request actors
55-
EvWriteAuditLog = EvBegin + 0,
56-
57-
EvEnd
58-
};
59-
60-
static_assert(EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG),
61-
"expected EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG)");
62-
63-
struct TEvWriteAuditLog
64-
: public NActors::TEventLocal<TEvWriteAuditLog, EvWriteAuditLog>
65-
{
66-
TInstant Time;
67-
TVector<std::pair<TString, TString>> Parts;
68-
69-
TEvWriteAuditLog(TInstant time, TVector<std::pair<TString, TString>>&& parts)
70-
: Time(time)
71-
, Parts(std::move(parts))
72-
{}
73-
};
74-
};
75-
76-
class TAuditLogActor final
77-
: public TActor<TAuditLogActor>
78-
{
79-
private:
80-
const TMap<NKikimrConfig::TAuditConfig::EFormat, TVector<THolder<TLogBackend>>> LogBackends;
81-
public:
82-
TAuditLogActor(TMap<NKikimrConfig::TAuditConfig::EFormat, TVector<THolder<TLogBackend>>> logBackends)
83-
: TActor(&TThis::StateWork)
84-
, LogBackends(std::move(logBackends))
85-
{
86-
}
87-
88-
static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
89-
return NKikimrServices::TActivity::AUDIT_WRITER_ACTOR;
90-
}
91-
92-
private:
93-
STFUNC(StateWork);
94-
95-
void HandlePoisonPill(
96-
const TEvents::TEvPoisonPill::TPtr& ev,
97-
const TActorContext& ctx);
98-
99-
void HandleWriteAuditLog(
100-
const TEvAuditLog::TEvWriteAuditLog::TPtr& ev,
101-
const TActorContext& ctx);
102-
103-
static void WriteLog(
104-
const TString& log,
105-
const TVector<THolder<TLogBackend>>& logBackends);
106-
107-
static TString GetJsonLog(
108-
const TEvAuditLog::TEvWriteAuditLog::TPtr& ev);
109-
110-
static TString GetTxtLog(
111-
const TEvAuditLog::TEvWriteAuditLog::TPtr& ev);
112-
113-
void HandleUnexpectedEvent(STFUNC_SIG);
114-
};
115-
116-
////////////////////////////////////////////////////////////////////////////////
117-
11841
void SendAuditLog(const NActors::TActorSystem* sys, TVector<std::pair<TString, TString>>&& parts);
11942

120-
inline NActors::TActorId MakeAuditServiceID() {
121-
return NActors::TActorId(0, TStringBuf("YDB_AUDIT"));
122-
}
123-
124-
THolder<NActors::IActor> CreateAuditWriter(TMap<NKikimrConfig::TAuditConfig::EFormat, TVector<THolder<TLogBackend>>> logBackends);
125-
12643
} // namespace NKikimr::NAudit

ydb/core/audit/audit_log_impl.cpp

+137-50
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,84 @@
1-
#include "audit_log.h"
2-
#include "audit_log_impl.h"
3-
41
#include <library/cpp/json/json_value.h>
52
#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)
632

733
namespace NKikimr::NAudit {
834

9-
using namespace NActors;
35+
// TAuditLogActor
36+
//
1037

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+
//
1942

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+
};
3066

31-
void TAuditLogActor::WriteLog(const TString& log, const TVector<THolder<TLogBackend>>& logBackends) {
67+
void WriteLog(const TString& log, const TVector<THolder<TLogBackend>>& logBackends) {
3268
for (auto& logBackend : logBackends) {
3369
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+
));
3975
} 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() << ")");
4277
}
4378
}
4479
}
4580

46-
TString TAuditLogActor::GetJsonLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
81+
TString GetJsonLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
4782
const auto* msg = ev->Get();
4883
TStringStream ss;
4984
ss << msg->Time << ": ";
@@ -56,7 +91,7 @@ TString TAuditLogActor::GetJsonLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev
5691
return ss.Str();
5792
}
5893

59-
TString TAuditLogActor::GetTxtLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
94+
TString GetTxtLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev) {
6095
const auto* msg = ev->Get();
6196
TStringStream ss;
6297
ss << msg->Time << ": ";
@@ -69,29 +104,81 @@ TString TAuditLogActor::GetTxtLog(const TEvAuditLog::TEvWriteAuditLog::TPtr& ev)
69104
return ss.Str();
70105
}
71106

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;
86129
}
87130
}
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());
88173
}
89174

90-
void TAuditLogActor::HandleUnexpectedEvent(STFUNC_SIG)
175+
// Service interface implementation
176+
//
177+
178+
THolder<NActors::IActor> CreateAuditWriter(TAuditLogBackends&& logBackends)
91179
{
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));
95182
}
96183

97184
} // namespace NKikimr::NAudit

ydb/core/audit/audit_log_impl.h

-15
This file was deleted.

0 commit comments

Comments
 (0)