File: request_handler_for_remote_commands.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 (102 lines) | stat: -rw-r--r-- 4,092 bytes parent folder | download | duplicates (6)
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
// 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 "components/policy/test_support/request_handler_for_remote_commands.h"

#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/policy/test_support/client_storage.h"
#include "components/policy/test_support/policy_storage.h"
#include "components/policy/test_support/remote_commands_state.h"
#include "components/policy/test_support/test_server_helpers.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

using net::test_server::HttpRequest;
using net::test_server::HttpResponse;

namespace em = enterprise_management;

namespace policy {

RequestHandlerForRemoteCommands::RequestHandlerForRemoteCommands(
    EmbeddedPolicyTestServer* parent)
    : EmbeddedPolicyTestServer::RequestHandler(parent) {}

RequestHandlerForRemoteCommands::~RequestHandlerForRemoteCommands() = default;

std::string RequestHandlerForRemoteCommands::RequestType() {
  return dm_protocol::kValueRequestRemoteCommands;
}

std::unique_ptr<HttpResponse> RequestHandlerForRemoteCommands::HandleRequest(
    const HttpRequest& request) {
  em::DeviceManagementResponse response;
  const ClientStorage::ClientInfo* client_info =
      client_storage()->GetClientOrNull(
          KeyValueFromUrl(request.GetURL(), dm_protocol::kParamDeviceID));
  if (!client_info) {
    return CreateHttpResponse(net::HTTP_GONE, response);
  }

  RemoteCommandsState* state = remote_commands_state();

  em::DeviceManagementRequest device_management_request;
  device_management_request.ParseFromString(request.content);
  const em::DeviceRemoteCommandRequest remote_command_request =
      device_management_request.remote_command_request();

  if (remote_command_request.has_last_command_unique_id()) {
    state->AddRemoteCommandAcked(
        remote_command_request.last_command_unique_id());
  }

  for (auto result : remote_command_request.command_results()) {
    LOG(INFO) << "remote command result: " << result.command_id() << " "
              << result.result();
    state->AddRemoteCommandResult(result);
  }

  std::vector<em::RemoteCommand> pending_commands =
      state->ExtractPendingRemoteCommands();
  ProcessSecureRemoteCommands(remote_command_request, client_info,
                              pending_commands,
                              response.mutable_remote_command_response());
  LOG(INFO) << "serialized string: " << response.SerializeAsString();

  return CreateHttpResponse(net::HTTP_OK, response);
}

void RequestHandlerForRemoteCommands::ProcessSecureRemoteCommands(
    const em::DeviceRemoteCommandRequest& request,
    const ClientStorage::ClientInfo* client_info,
    const std::vector<em::RemoteCommand>& pending_commands,
    em::DeviceRemoteCommandResponse* response) {
  const em::PolicyFetchRequest::SignatureType signature_type =
      request.signature_type();
  const SignatureProvider* signature_provider =
      policy_storage()->signature_provider();
  const SignatureProvider::SigningKey* signing_key =
      signature_provider->GetCurrentKey();

  for (auto command : pending_commands) {
    LOG(INFO) << "pending command type and id: " << command.type() << " "
              << command.command_id();
    em::SignedData* signed_data = response->add_secure_commands();
    em::RemoteCommand command_copy(command);
    command_copy.set_target_device_id(client_info->device_id);

    em::PolicyData policy_data;
    policy_data.set_policy_type(dm_protocol::kChromeRemoteCommandPolicyType);
    policy_data.set_device_id(client_info->device_id);

    command_copy.SerializeToString(policy_data.mutable_policy_value());

    policy_data.SerializeToString(signed_data->mutable_data());
    signing_key->Sign(signed_data->data(), signature_type,
                      signed_data->mutable_signature());
  }
}
}  // namespace policy