File: dlp_reporting_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (389 lines) | stat: -rw-r--r-- 15,493 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/enterprise/data_controls/dlp_reporting_manager.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "components/enterprise/common/proto/synced/dlp_policy_event.pb.h"
#include "components/enterprise/data_controls/core/browser/dlp_histogram_helper.h"
#include "components/reporting/client/report_queue.h"
#include "components/reporting/client/report_queue_configuration.h"
#include "components/reporting/client/report_queue_factory.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/util/status.h"
#include "content/public/common/content_constants.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace data_controls {
// TODO(1187477, marcgrimme): revisit if this should be refactored.
DlpPolicyEvent_Mode RuleLevel2DlpEventMode(
    Rule::Level level) {
  switch (level) {
    case Rule::Level::kBlock:
      return DlpPolicyEvent_Mode_BLOCK;
    case Rule::Level::kWarn:
      return DlpPolicyEvent_Mode_WARN;
    case Rule::Level::kReport:
      return DlpPolicyEvent_Mode_REPORT;
    case Rule::Level::kNotSet:
    case Rule::Level::kAllow:
      return DlpPolicyEvent_Mode_UNDEFINED_MODE;
  }
}

// TODO(1187477, marcgrimme): revisit if this should be refactored.
DlpPolicyEvent_Restriction RuleRestriction2DlpEventRestriction(
    Rule::Restriction restriction) {
  switch (restriction) {
    case Rule::Restriction::kPrinting:
      return DlpPolicyEvent_Restriction_PRINTING;
    case Rule::Restriction::kScreenshot:
      return DlpPolicyEvent_Restriction_SCREENSHOT;
    case Rule::Restriction::kScreenShare:
      return DlpPolicyEvent_Restriction_SCREENCAST;
    case Rule::Restriction::kPrivacyScreen:
      return DlpPolicyEvent_Restriction_EPRIVACY;
    case Rule::Restriction::kClipboard:
      return DlpPolicyEvent_Restriction_CLIPBOARD;
    case Rule::Restriction::kFiles:
      return DlpPolicyEvent_Restriction_FILES;
    case Rule::Restriction::kUnknownRestriction:
      return DlpPolicyEvent_Restriction_UNDEFINED_RESTRICTION;
  }
}

// TODO(1187477, marcgrimme): revisit if this should be refactored.
Rule::Restriction DlpEventRestriction2RuleRestriction(
    DlpPolicyEvent_Restriction restriction) {
  switch (restriction) {
    case DlpPolicyEvent_Restriction_PRINTING:
      return Rule::Restriction::kPrinting;
    case DlpPolicyEvent_Restriction_SCREENSHOT:
      return Rule::Restriction::kScreenshot;
    case DlpPolicyEvent_Restriction_SCREENCAST:
      return Rule::Restriction::kScreenShare;
    case DlpPolicyEvent_Restriction_EPRIVACY:
      return Rule::Restriction::kPrivacyScreen;
    case DlpPolicyEvent_Restriction_CLIPBOARD:
      return Rule::Restriction::kClipboard;
    case DlpPolicyEvent_Restriction_FILES:
      return Rule::Restriction::kFiles;
    case DlpPolicyEvent_Restriction_UNDEFINED_RESTRICTION:
      return Rule::Restriction::kUnknownRestriction;
    case DlpPolicyEvent_Restriction_DlpPolicyEvent_Restriction_INT_MIN_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEvent_Restriction_DlpPolicyEvent_Restriction_INT_MAX_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
}

DlpPolicyEvent_UserType GetCurrentUserType() {
#if BUILDFLAG(IS_CHROMEOS)
  // Could be not initialized in tests.
  if (!user_manager::UserManager::IsInitialized() ||
      !user_manager::UserManager::Get()->GetPrimaryUser()) {
    return DlpPolicyEvent_UserType_UNDEFINED_USER_TYPE;
  }
  const user_manager::User* const user =
      user_manager::UserManager::Get()->GetPrimaryUser();
  DCHECK(user);
  switch (user->GetType()) {
    case user_manager::UserType::kRegular:
      return DlpPolicyEvent_UserType_REGULAR;
    case user_manager::UserType::kPublicAccount:
      return DlpPolicyEvent_UserType_MANAGED_GUEST;
    case user_manager::UserType::kKioskChromeApp:
    case user_manager::UserType::kKioskWebApp:
    case user_manager::UserType::kKioskIWA:
      return DlpPolicyEvent_UserType_KIOSK;
    case user_manager::UserType::kGuest:
    case user_manager::UserType::kChild:
      return DlpPolicyEvent_UserType_UNDEFINED_USER_TYPE;
  }
#else
  // TODO(b/303640183): Revisit what this should return for non-CrOS platforms.
  return DlpPolicyEvent_UserType_UNDEFINED_USER_TYPE;
#endif  // BUILDFLAG(IS_CHROMEOS)
}

// static
std::unique_ptr<DlpPolicyEventBuilder> DlpPolicyEventBuilder::Event(
    const std::string& src_url,
    const std::string& rule_name,
    const std::string& rule_id,
    Rule::Restriction restriction,
    Rule::Level level) {
  std::unique_ptr<DlpPolicyEventBuilder> event_builder(
      new DlpPolicyEventBuilder());
  event_builder->SetSourceUrl(src_url);
  event_builder->SetRestriction(restriction);
  event_builder->event.set_mode(RuleLevel2DlpEventMode(level));

  if (!rule_name.empty()) {
    event_builder->event.set_triggered_rule_name(rule_name);
  }
  if (!rule_id.empty()) {
    event_builder->event.set_triggered_rule_id(rule_id);
  }
  return event_builder;
}

// static
std::unique_ptr<DlpPolicyEventBuilder>
DlpPolicyEventBuilder::WarningProceededEvent(const std::string& src_url,
                                             const std::string& rule_name,
                                             const std::string& rule_id,
                                             Rule::Restriction restriction) {
  std::unique_ptr<DlpPolicyEventBuilder> event_builder(
      new DlpPolicyEventBuilder());
  event_builder->SetSourceUrl(src_url);
  event_builder->SetRestriction(restriction);
  event_builder->event.set_mode(DlpPolicyEvent_Mode_WARN_PROCEED);
  return event_builder;
}

DlpPolicyEventBuilder::DlpPolicyEventBuilder() {
  int64_t timestamp_micro =
      (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds();
  event.set_timestamp_micro(timestamp_micro);
  event.set_user_type(GetCurrentUserType());
}

void DlpPolicyEventBuilder::SetDestinationUrl(const std::string& dst_url) {
  DlpPolicyEventDestination* event_destination = new DlpPolicyEventDestination;
  event_destination->set_url(dst_url.substr(0, content::kMaxURLDisplayChars));
  event.set_allocated_destination(event_destination);
}

#if BUILDFLAG(IS_CHROMEOS)
void DlpPolicyEventBuilder::SetDestinationComponent(
    Component dst_component) {
  DlpPolicyEventDestination* event_destination = new DlpPolicyEventDestination;
  switch (dst_component) {
    case (Component::kArc):
      event_destination->set_component(DlpPolicyEventDestination_Component_ARC);
      break;
    case (Component::kCrostini):
      event_destination->set_component(
          DlpPolicyEventDestination_Component_CROSTINI);
      break;
    case (Component::kPluginVm):
      event_destination->set_component(
          DlpPolicyEventDestination_Component_PLUGIN_VM);
      break;
    case (Component::kUsb):
      event_destination->set_component(DlpPolicyEventDestination_Component_USB);
      break;
    case (Component::kDrive):
      event_destination->set_component(
          DlpPolicyEventDestination_Component_DRIVE);
      break;
    case (Component::kOneDrive):
      event_destination->set_component(
          DlpPolicyEventDestination_Component_ONEDRIVE);
      break;
    case (Component::kUnknownComponent):
      event_destination->set_component(
          DlpPolicyEventDestination_Component_UNDEFINED_COMPONENT);
      break;
  }
  event.set_allocated_destination(event_destination);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void DlpPolicyEventBuilder::SetContentName(const std::string& content_name) {
  event.set_content_name(content_name);
}

DlpPolicyEvent DlpPolicyEventBuilder::Create() {
  return event;
}

void DlpPolicyEventBuilder::SetSourceUrl(const std::string& src_url) {
  DlpPolicyEventSource* event_source = new DlpPolicyEventSource;
  event_source->set_url(src_url.substr(0, content::kMaxURLDisplayChars));
  event.set_allocated_source(event_source);
}

void DlpPolicyEventBuilder::SetRestriction(
    Rule::Restriction restriction) {
  event.set_restriction(
      RuleRestriction2DlpEventRestriction(restriction));
}

DlpPolicyEvent CreateDlpPolicyEvent(const std::string& src_url,
                                    Rule::Restriction restriction,
                                    const std::string& rule_name,
                                    const std::string& rule_id,
                                    Rule::Level level) {
  auto event_builder = DlpPolicyEventBuilder::Event(src_url, rule_name, rule_id,
                                                    restriction, level);
  return event_builder->Create();
}

DlpPolicyEvent CreateDlpPolicyEvent(const std::string& src_url,
                                    const std::string& dst_url,
                                    Rule::Restriction restriction,
                                    const std::string& rule_name,
                                    const std::string& rule_id,
                                    Rule::Level level) {
  auto event_builder = DlpPolicyEventBuilder::Event(src_url, rule_name, rule_id,
                                                    restriction, level);
  event_builder->SetDestinationUrl(dst_url);
  return event_builder->Create();
}

#if BUILDFLAG(IS_CHROMEOS)
DlpPolicyEvent CreateDlpPolicyEvent(const std::string& src_url,
                                    Component dst_component,
                                    Rule::Restriction restriction,
                                    const std::string& rule_name,
                                    const std::string& rule_id,
                                    Rule::Level level) {
  auto event_builder = DlpPolicyEventBuilder::Event(src_url, rule_name, rule_id,
                                                    restriction, level);
  event_builder->SetDestinationComponent(dst_component);

  return event_builder->Create();
}
#endif  // BUILDFLAG(IS_CHROMEOS)

DlpReportingManager::DlpReportingManager()
    : report_queue_(
          ::reporting::ReportQueueFactory::CreateSpeculativeReportQueue([]() {
            ::reporting::SourceInfo source_info;
            source_info.set_source(::reporting::SourceInfo::ASH);
            return ::reporting::ReportQueueConfiguration::Create(
                       {.event_type = ::reporting::EventType::kUser,
                        .destination = ::reporting::Destination::DLP_EVENTS})
                .SetSourceInfo(std::move(source_info));
          }())) {}

DlpReportingManager::~DlpReportingManager() = default;

void DlpReportingManager::SetReportQueueForTest(
    std::unique_ptr<::reporting::ReportQueue, base::OnTaskRunnerDeleter>
        report_queue) {
  report_queue_.reset();
  report_queue_ = std::move(report_queue);
}

void DlpReportingManager::ReportEvent(const std::string& src_url,
                                      Rule::Restriction restriction,
                                      Rule::Level level,
                                      const std::string& rule_name,
                                      const std::string& rule_id) {
  auto event =
      CreateDlpPolicyEvent(src_url, restriction, rule_name, rule_id, level);
  ReportEvent(std::move(event));
}

void DlpReportingManager::ReportEvent(const std::string& src_url,
                                      const std::string& dst_url,
                                      Rule::Restriction restriction,
                                      Rule::Level level,
                                      const std::string& rule_name,
                                      const std::string& rule_id) {
  auto event = CreateDlpPolicyEvent(src_url, dst_url, restriction, rule_name,
                                    rule_id, level);
  ReportEvent(std::move(event));
}

#if BUILDFLAG(IS_CHROMEOS)
void DlpReportingManager::ReportEvent(const std::string& src_url,
                                      const Component dst_component,
                                      Rule::Restriction restriction,
                                      Rule::Level level,
                                      const std::string& rule_name,
                                      const std::string& rule_id) {
  auto event = CreateDlpPolicyEvent(src_url, dst_component, restriction,
                                    rule_name, rule_id, level);
  ReportEvent(std::move(event));
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void DlpReportingManager::OnEventEnqueued(reporting::Status status) {
  if (!status.ok()) {
    VLOG(1) << "Could not enqueue event to DLP reporting queue because of "
            << status;
  }
  events_reported_++;
  base::UmaHistogramEnumeration(GetDlpHistogramPrefix() +
                                    dlp::kReportedEventStatus,
                                status.code(),
                                reporting::error::Code::MAX_VALUE);
}

void DlpReportingManager::ReportEvent(DlpPolicyEvent event) {
  // TODO(1187506, marcgrimme) Refactor to handle gracefully with user
  // interaction when queue is not ready.
  DlpBooleanHistogram(
      dlp::kErrorsReportQueueNotReady, !report_queue_.get());
  if (!report_queue_.get()) {
    DLOG(WARNING) << "Report queue could not be initialized. DLP reporting "
                     "functionality will be disabled.";
    return;
  }

  for (auto& observer : observers_) {
    observer.OnReportEvent(event);
  }

  reporting::ReportQueue::EnqueueCallback callback = base::BindOnce(
      &DlpReportingManager::OnEventEnqueued, weak_factory_.GetWeakPtr());

  switch (event.mode()) {
    case DlpPolicyEvent_Mode_BLOCK:
      base::UmaHistogramEnumeration(
          GetDlpHistogramPrefix() +
              dlp::kReportedBlockLevelRestriction,
          DlpEventRestriction2RuleRestriction(event.restriction()));
      break;
    case DlpPolicyEvent_Mode_REPORT:
      base::UmaHistogramEnumeration(
          GetDlpHistogramPrefix() +
              dlp::kReportedReportLevelRestriction,
          DlpEventRestriction2RuleRestriction(event.restriction()));
      break;
    case DlpPolicyEvent_Mode_WARN:
      base::UmaHistogramEnumeration(
          GetDlpHistogramPrefix() +
              dlp::kReportedWarnLevelRestriction,
          DlpEventRestriction2RuleRestriction(event.restriction()));
      break;
    case DlpPolicyEvent_Mode_WARN_PROCEED:
      base::UmaHistogramEnumeration(
          GetDlpHistogramPrefix() +
              dlp::kReportedWarnProceedLevelRestriction,
          DlpEventRestriction2RuleRestriction(event.restriction()));
      break;
    case DlpPolicyEvent_Mode_UNDEFINED_MODE:
    case DlpPolicyEvent_Mode_DlpPolicyEvent_Mode_INT_MIN_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEvent_Mode_DlpPolicyEvent_Mode_INT_MAX_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
  report_queue_->Enqueue(std::make_unique<DlpPolicyEvent>(std::move(event)),
                         reporting::Priority::SLOW_BATCH, std::move(callback));
  VLOG(1) << "DLP event sent to reporting infrastructure.";
}

void DlpReportingManager::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void DlpReportingManager::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

}  // namespace data_controls