summaryrefslogtreecommitdiffstats
path: root/third_party/content_analysis_sdk/demo/client.cc
blob: 84ca6e2356b1afe0a228a936e431943cef34f1da (plain)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2022 The Chromium Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <time.h>

#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include <vector>

#include "content_analysis/sdk/analysis_client.h"
#include "demo/atomic_output.h"

using content_analysis::sdk::Client;
using content_analysis::sdk::ContentAnalysisRequest;
using content_analysis::sdk::ContentAnalysisResponse;
using content_analysis::sdk::ContentAnalysisAcknowledgement;

// Different paths are used depending on whether this agent should run as a
// use specific agent or not.  These values are chosen to match the test
// values in chrome browser.
constexpr char kPathUser[] = "path_user";
constexpr char kPathSystem[] = "brcm_chrm_cas";

// Global app config.
std::string path = kPathSystem;
bool user_specific = false;
bool group = false;
std::unique_ptr<Client> client;

// Paramters used to build the request.
content_analysis::sdk::AnalysisConnector connector =
    content_analysis::sdk::FILE_ATTACHED;
time_t request_token_number = time(nullptr);
std::string request_token;
std::string tag = "dlp";
bool threaded = false;
std::string digest = "sha256-123456";
std::string url = "https://upload.example.com";
std::string email = "me@example.com";
std::string machine_user = "DOMAIN\\me";
std::vector<std::string> datas;

// When grouping, remember the tokens of all requests/responses in order to
// acknowledge them all with the same final action.
//
// This global state.  It may be access from multiple thread so must be
// accessed from a critical section.
std::mutex global_mutex;
ContentAnalysisAcknowledgement::FinalAction global_final_action =
    ContentAnalysisAcknowledgement::ALLOW;
std::vector<std::string> request_tokens;

// Command line parameters.
constexpr const char* kArgConnector = "--connector=";
constexpr const char* kArgDigest = "--digest=";
constexpr const char* kArgEmail = "--email=";
constexpr const char* kArgGroup = "--group";
constexpr const char* kArgMachineUser = "--machine-user=";
constexpr const char* kArgPath = "--path=";
constexpr const char* kArgRequestToken = "--request-token=";
constexpr const char* kArgTag = "--tag=";
constexpr const char* kArgThreaded = "--threaded";
constexpr const char* kArgUrl = "--url=";
constexpr const char* kArgUserSpecific = "--user";
constexpr const char* kArgHelp = "--help";

bool ParseCommandLine(int argc, char* argv[]) {
  for (int i = 1; i < argc; ++i) {
    const std::string arg = argv[i];
    if (arg.find(kArgConnector) == 0) {
      std::string connector_str = arg.substr(strlen(kArgConnector));
      if (connector_str == "download") {
        connector = content_analysis::sdk::FILE_DOWNLOADED;
      } else if (connector_str == "attach") {
        connector = content_analysis::sdk::FILE_ATTACHED;
      } else if (connector_str == "bulk-data-entry") {
        connector = content_analysis::sdk::BULK_DATA_ENTRY;
      } else if (connector_str == "print") {
        connector = content_analysis::sdk::PRINT;
      } else if (connector_str == "file-transfer") {
        connector = content_analysis::sdk::FILE_TRANSFER;
      } else {
        std::cout << "[Demo] Incorrect command line arg: " << arg << std::endl;
        return false;
      }
    } else if (arg.find(kArgRequestToken) == 0) {
      request_token = arg.substr(strlen(kArgRequestToken));
    } else if (arg.find(kArgTag) == 0) {
      tag = arg.substr(strlen(kArgTag));
    } else if (arg.find(kArgThreaded) == 0) {
      threaded = true;
    } else if (arg.find(kArgDigest) == 0) {
      digest = arg.substr(strlen(kArgDigest));
    } else if (arg.find(kArgUrl) == 0) {
      url = arg.substr(strlen(kArgUrl));
    } else if (arg.find(kArgMachineUser) == 0) {
      machine_user = arg.substr(strlen(kArgMachineUser));
    } else if (arg.find(kArgEmail) == 0) {
      email = arg.substr(strlen(kArgEmail));
    } else if (arg.find(kArgPath) == 0) {
      path = arg.substr(strlen(kArgPath));
    } else if (arg.find(kArgUserSpecific) == 0) {
      // If kArgPath was already used, abort.
      if (path != kPathSystem) {
        std::cout << std::endl << "ERROR: use --path=<path> after --user";
        return false;
      }
      path = kPathUser;
      user_specific = true;
    } else if (arg.find(kArgGroup) == 0) {
      group = true;
    } else if (arg.find(kArgHelp) == 0) {
      return false;
    } else {
      datas.push_back(arg);
    }
  }

  return true;
}

void PrintHelp() {
  std::cout
    << std::endl << std::endl
    << "Usage: client [OPTIONS] [@]content_or_file ..." << std::endl
    << "A simple client to send content analysis requests to a running agent." << std::endl
    << "Without @ the content to analyze is the argument itself." << std::endl
    << "Otherwise the content is read from a file called 'content_or_file'." << std::endl
    << "Multiple [@]content_or_file arguments may be specified, each generates one request." << std::endl
    << std::endl << "Options:"  << std::endl
    << kArgConnector << "<connector> : one of 'download', 'attach' (default), 'bulk-data-entry', 'print', or 'file-transfer'" << std::endl
    << kArgRequestToken << "<unique-token> : defaults to 'req-<number>' which auto increments" << std::endl
    << kArgTag << "<tag> : defaults to 'dlp'" << std::endl
    << kArgThreaded << " : handled multiple requests using threads" << std::endl
    << kArgUrl << "<url> : defaults to 'https://upload.example.com'" << std::endl
    << kArgMachineUser << "<machine-user> : defaults to 'DOMAIN\\me'" << std::endl
    << kArgEmail << "<email> : defaults to 'me@example.com'" << std::endl
    << kArgPath << " <path> : Used the specified path instead of default. Must come after --user." << std::endl
    << kArgUserSpecific << " : Connects to an OS user specific agent" << std::endl
    << kArgDigest << "<digest> : defaults to 'sha256-123456'" << std::endl
    << kArgGroup << " : Generate the same final action for all requests" << std::endl
    << kArgHelp << " : prints this help message" << std::endl;
}

std::string GenerateRequestToken() {
  std::stringstream stm;
  stm << "req-" << request_token_number++;
  return stm.str();
}

ContentAnalysisRequest BuildRequest(const std::string& data) {
  std::string filepath;
  std::string filename;
  if (data[0] == '@') {
    filepath = data.substr(1);
    filename = filepath.substr(filepath.find_last_of("/\\") + 1);
  }

  ContentAnalysisRequest request;

  // Set request to expire 5 minutes into the future.
  request.set_expires_at(time(nullptr) + 5 * 60);
  request.set_analysis_connector(connector);
  request.set_request_token(!request_token.empty()
      ? request_token : GenerateRequestToken());
  *request.add_tags() = tag;

  auto request_data = request.mutable_request_data();
  request_data->set_url(url);
  request_data->set_email(email);
  request_data->set_digest(digest);
  if (!filename.empty()) {
    request_data->set_filename(filename);
  }

  auto client_metadata = request.mutable_client_metadata();
  auto browser = client_metadata->mutable_browser();
  browser->set_machine_user(machine_user);

  if (!filepath.empty()) {
    request.set_file_path(filepath);
  } else if (!data.empty()) {
    request.set_text_content(data);
  } else {
    std::cout << "[Demo] Specify text content or a file path." << std::endl;
    PrintHelp();
    exit(1);
  }

  return request;
}

// Gets the most severe action within the result.
ContentAnalysisResponse::Result::TriggeredRule::Action
GetActionFromResult(const ContentAnalysisResponse::Result& result) {
  auto action =
    ContentAnalysisResponse::Result::TriggeredRule::ACTION_UNSPECIFIED;
  for (auto rule : result.triggered_rules()) {
    if (rule.has_action() && rule.action() > action)
      action = rule.action();
  }
  return action;
}

// Gets the most severe action within all the the results of a response.
ContentAnalysisResponse::Result::TriggeredRule::Action
GetActionFromResponse(const ContentAnalysisResponse& response) {
  auto action =
    ContentAnalysisResponse::Result::TriggeredRule::ACTION_UNSPECIFIED;
  for (auto result : response.results()) {
    auto action2 = GetActionFromResult(result);
    if (action2 > action)
      action = action2;
  }
  return action;
}

void DumpResponse(
    std::stringstream& stream,
    const ContentAnalysisResponse& response) {
  for (auto result : response.results()) {
    auto tag = result.has_tag() ? result.tag() : "<no-tag>";

    auto status = result.has_status()
      ? result.status()
      : ContentAnalysisResponse::Result::STATUS_UNKNOWN;
    std::string status_str;
    switch (status) {
    case ContentAnalysisResponse::Result::STATUS_UNKNOWN:
      status_str = "Unknown";
      break;
    case ContentAnalysisResponse::Result::SUCCESS:
      status_str = "Success";
      break;
    case ContentAnalysisResponse::Result::FAILURE:
      status_str = "Failure";
      break;
    default:
      status_str = "<Uknown>";
      break;
    }

    auto action = GetActionFromResult(result);
    std::string action_str;
    switch (action) {
    case ContentAnalysisResponse::Result::TriggeredRule::ACTION_UNSPECIFIED:
      action_str = "allowed";
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::REPORT_ONLY:
      action_str = "reported only";
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::WARN:
      action_str = "warned";
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::BLOCK:
      action_str = "blocked";
      break;
    }

    time_t now = time(nullptr);
    stream << "[Demo] Request " << response.request_token()  << " is " << action_str
           << " after " << tag
           << " analysis, status=" << status_str
           << " at " << ctime(&now);
  }
}

ContentAnalysisAcknowledgement BuildAcknowledgement(
    const std::string& request_token,
    ContentAnalysisAcknowledgement::FinalAction final_action) {
  ContentAnalysisAcknowledgement ack;
  ack.set_request_token(request_token);
  ack.set_status(ContentAnalysisAcknowledgement::SUCCESS);
  ack.set_final_action(final_action);
  return ack;
}

void HandleRequest(const ContentAnalysisRequest& request) {
  AtomicCout aout;
  ContentAnalysisResponse response;
  int err = client->Send(request, &response);
  if (err != 0) {
    aout.stream() << "[Demo] Error sending request " << request.request_token()
           << std::endl;
  } else if (response.results_size() == 0) {
    aout.stream() << "[Demo] Response " << request.request_token() << " is missing a result"
                  << std::endl;
  } else {
    DumpResponse(aout.stream(), response);

    auto final_action = ContentAnalysisAcknowledgement::ALLOW;
    switch (GetActionFromResponse(response)) {
    case ContentAnalysisResponse::Result::TriggeredRule::ACTION_UNSPECIFIED:
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::REPORT_ONLY:
      final_action = ContentAnalysisAcknowledgement::REPORT_ONLY;
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::WARN:
      final_action = ContentAnalysisAcknowledgement::WARN;
      break;
    case ContentAnalysisResponse::Result::TriggeredRule::BLOCK:
      final_action = ContentAnalysisAcknowledgement::BLOCK;
      break;
    }

    // If grouping, remember the request's token in order to ack the response
    // later.
    if (group) {
      std::unique_lock<std::mutex> lock(global_mutex);
      request_tokens.push_back(request.request_token());
      if (final_action > global_final_action)
        global_final_action = final_action;
    } else {
      int err = client->Acknowledge(
        BuildAcknowledgement(response.request_token(), final_action));
      if (err != 0) {
        aout.stream() << "[Demo] Error sending ack " << request.request_token()
                      << std::endl;
      }
    }
  }
}

void ProcessRequest(size_t i) {
  auto request = BuildRequest(datas[i]);

  {
    AtomicCout aout;
    aout.stream() << "[Demo] Sending request " << request.request_token() << std::endl;
  }

  HandleRequest(request);
}

int main(int argc, char* argv[]) {
  if (!ParseCommandLine(argc, argv)) {
    PrintHelp();
    return 1;
  }

  // Each client uses a unique name to identify itself with Google Chrome.
  client = Client::Create({path, user_specific});
  if (!client) {
    std::cout << "[Demo] Error starting client" << std::endl;
    return 1;
  };

  auto info = client->GetAgentInfo();
  std::cout << "Agent pid=" << info.pid
            << " path=" << info.binary_path << std::endl;

  if (threaded) {
    std::vector<std::unique_ptr<std::thread>> threads;
    for (int i = 0; i < datas.size(); ++i) {
      AtomicCout aout;
      aout.stream() << "Start thread " << i << std::endl;
      threads.emplace_back(std::make_unique<std::thread>(ProcessRequest, i));
    }

    // Make sure all threads have terminated.
    for (auto& thread : threads) {
      thread->join();
    }
  }
  else {
    for (size_t i = 0; i < datas.size(); ++i) {
      ProcessRequest(i);
    }
  }
  // It's safe to access global state beyond this point without locking since
  // all no more responses will be touching them.

  if (group) {
    std::cout << std::endl;
    std::cout << "[Demo] Final action for all requests is ";
    switch (global_final_action) {
    // Google Chrome fails open, so if no action is specified that is the same
    // as ALLOW.
    case ContentAnalysisAcknowledgement::ACTION_UNSPECIFIED:
    case ContentAnalysisAcknowledgement::ALLOW:
      std::cout << "allowed";
      break;
    case ContentAnalysisAcknowledgement::REPORT_ONLY:
      std::cout << "reported only";
      break;
    case ContentAnalysisAcknowledgement::WARN:
      std::cout << "warned";
      break;
    case ContentAnalysisAcknowledgement::BLOCK:
      std::cout << "blocked";
      break;
    }
    std::cout << std::endl << std::endl;

    for (auto token : request_tokens) {
      std::cout << "[Demo] Sending group Ack" << std::endl;
      int err = client->Acknowledge(
        BuildAcknowledgement(token, global_final_action));
      if (err != 0) {
        std::cout << "[Demo] Error sending ack for " << token << std::endl;
      }
    }
  }

  return 0;
};