File: cloud_service_client.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 (517 lines) | stat: -rw-r--r-- 20,694 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/cloud_service_client.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_split.h"
#include "base/strings/stringize_macros.h"
#include "google_apis/google_api_keys.h"
#include "net/http/http_request_headers.h"
#include "remoting/base/protobuf_http_request.h"
#include "remoting/base/protobuf_http_request_config.h"
#include "remoting/base/service_urls.h"
#include "remoting/base/version.h"
#include "remoting/proto/google/internal/remoting/cloud/v1alpha/empty.pb.h"
#include "remoting/proto/google/internal/remoting/cloud/v1alpha/network_traversal_service.pb.h"
#include "remoting/proto/google/internal/remoting/cloud/v1alpha/remote_access_host.pb.h"
#include "remoting/proto/google/internal/remoting/cloud/v1alpha/remote_access_service.pb.h"
#include "remoting/proto/google/internal/remoting/cloud/v1alpha/session_authz_service.pb.h"
#include "remoting/proto/google/remoting/cloud/v1/provisioning_service.pb.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace {
constexpr net::NetworkTrafficAnnotationTag kGenerateHostTokenTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("remoting_cloud_generate_host_token",
                                        R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Generates a host token which is used to authorize a Chrome Remote "
            "Desktop connection between a user and a 'Cloud host' running on "
            "GCE (this machine)."
          trigger:
            "User connects to a remote access host instance running on GCE "
            "which has been configured as a 'Cloud' host."
          user_data {
            type: OTHER
          }
          data:
            "An access token for the device robot account associated with this "
            "machine."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-09-23"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the Chrome Remote Desktop host is not registered as a Cloud "
            "host running on GCE."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag
    kProvisionGceInstanceTrafficAnnotation =
        net::DefineNetworkTrafficAnnotation(
            "remoting_cloud_provision_gce_instance",
            R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Registers a new Chrome Remote Desktop host for a GCE instance."
          trigger:
            "User runs the remoting_start_host command by typing it on the "
            "terminal. Third party administrators might implement scripts to "
            "run this automatically, but the Chrome Remote Desktop product "
            "does not come with such scripts."
          user_data {
            type: EMAIL
            type: OTHER
          }
          data:
            "The email address of the account to configure CRD for and the "
            "display name of the new remote access host instance which will be "
            "shown in the Chrome Remote Desktop client website."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-03-29"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the start-host utility is not run with the cloud-user flag."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag kReauthorizeHostTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("remoting_cloud_reauthorize_host",
                                        R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Reauthorizes the Chrome Remote Desktop connection between a user "
            "and a 'Cloud host' running on GCE (this machine)."
          trigger:
            "User connects to a remote access host instance running on GCE "
            "which has been configured as a 'Cloud' host."
          user_data {
            type: OTHER
          }
          data:
            "An access token for the device robot account associated with this "
            "machine and an opaque session token generated by the backend "
            "service."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-09-23"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the Chrome Remote Desktop host is not registered as a Cloud "
            "host running on GCE."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag kSendHeartbeatTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("remoting_cloud_send_heartbeat",
                                        R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Updates the last seen time in the Chrome Remote Desktop Directory "
            "service for a given remote access host instance."
          trigger:
            "Configuring a CRD remote access host on a GCE Instance."
          user_data {
            type: OTHER
          }
          data:
            "An internal UUID to identify the remote access host instance."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-09-18"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the CRD host is not configured and run as a Cloud host."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag
    kUpdateRemoteAccessHostTrafficAnnotation =
        net::DefineNetworkTrafficAnnotation(
            "remoting_cloud_update_remote_access_host",
            R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Updates the Chrome Remote Desktop Directory service with "
            "environment details and signaling information for a given remote "
            "access host instance."
          trigger:
            "Configuring a CRD remote access host on a GCE Instance."
          user_data {
            type: OTHER
          }
          data:
            "Includes an internal UUID to identify the remote access host "
            "instance, the name and version of the operating system, the "
            "version of the CRD package installed, and a set of signaling ids "
            "which the CRD client can use to send the host messages."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-09-18"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the CRD host is not configured and run as a Cloud host."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag
    kVerifySessionTokenTrafficAnnotation = net::DefineNetworkTrafficAnnotation(
        "remoting_cloud_verify_session_token",
        R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Verifies a session token returned by the client device and "
            "decodes the shared secret from the session token, to be used to "
            "authorize a Chrome Remote Desktop connection between a user and a "
            "Cloud host running on GCE (this device)."
          trigger:
            "User connects to a remote access host instance running on GCE "
            "which has been configured as a 'Cloud' host."
          user_data {
            type: OTHER
          }
          data:
            "An access token for the device robot account associated with this "
            "machine and an opaque session token generated by the backend "
            "service."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-09-23"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the Chrome Remote Desktop host is not registered as a Cloud "
            "host running on GCE."
          policy_exception_justification:
            "Not implemented."
        })");

constexpr net::NetworkTrafficAnnotationTag kGenerateIceConfigTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("remoting_cloud_generate_ice_config",
                                        R"(
        semantics {
          sender: "Chrome Remote Desktop"
          description:
            "Request used by Chrome Remote Desktop to fetch an ICE "
            "(Interactive Connectivity Establishment) configuration which "
            "contains a list of STUN (Session Traversal Utilities for NAT) & "
            "TURN (Traversal Using Relay NAT) servers and TURN credentials. "
            "Please see https://tools.ietf.org/html/rfc5245 for more details."
          trigger:
            "When a user connects to a remote access host instance running on "
            "GCE which has been configured as a 'Cloud' host."
          user_data {
            type: OTHER
          }
          data:
            "An access token for the device robot account associated with this "
            "machine."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts { owners: "//remoting/OWNERS" }
          }
          last_reviewed: "2024-10-08"
        }
        policy {
          cookies_allowed: NO
          setting:
            "This request cannot be stopped in settings, but will not be sent "
            "if the Chrome Remote Desktop host is not registered as a Cloud "
            "host running on GCE."
          policy_exception_justification:
            "Not implemented."
        })");

// Remoting Cloud API using statements.
using ProvisionGceInstanceRequest =
    google::remoting::cloud::v1::ProvisionGceInstanceRequest;

// Remoting Cloud Private API using statements.
using Empty = google::internal::remoting::cloud::v1alpha::Empty;
using GenerateHostTokenRequest =
    google::internal::remoting::cloud::v1alpha::GenerateHostTokenRequest;
using GenerateIceConfigRequest =
    google::internal::remoting::cloud::v1alpha::GenerateIceConfigRequest;
using ReauthorizeHostRequest =
    google::internal::remoting::cloud::v1alpha::ReauthorizeHostRequest;
using RemoteAccessHost =
    google::internal::remoting::cloud::v1alpha::RemoteAccessHost;
using SendHeartbeatRequest =
    google::internal::remoting::cloud::v1alpha::SendHeartbeatRequest;
using VerifySessionTokenRequest =
    google::internal::remoting::cloud::v1alpha::VerifySessionTokenRequest;

constexpr char kFtlResourceSeparator[] = "/chromoting_ftl_";

}  // namespace

namespace remoting {

CloudServiceClient::CloudServiceClient(
    const std::string& api_key,
    OAuthTokenGetter* oauth_token_getter,
    const std::string& base_service_url,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
    : api_key_(api_key),
      http_client_(base_service_url, oauth_token_getter, url_loader_factory) {}

CloudServiceClient::~CloudServiceClient() = default;

void CloudServiceClient::ProvisionGceInstance(
    const std::string& owner_email,
    const std::string& display_name,
    const std::string& public_key,
    const std::optional<std::string>& existing_directory_id,
    const std::optional<std::string>& instance_identity_token,
    ProvisionGceInstanceCallback callback) {
  constexpr char path[] = "/v1/provisioning:provisionGceInstance";

  auto request = std::make_unique<ProvisionGceInstanceRequest>();
  request->set_owner_email(owner_email);
  request->set_display_name(display_name);
  request->set_public_key(public_key);
  request->set_version(STRINGIZE(VERSION));
  if (existing_directory_id.has_value() && !existing_directory_id->empty()) {
    request->set_existing_directory_id(*existing_directory_id);
  }
  if (instance_identity_token.has_value() &&
      !instance_identity_token->empty()) {
    request->set_instance_identity_token(*instance_identity_token);
  }

  ExecuteRequest(kProvisionGceInstanceTrafficAnnotation, path, api_key_,
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback));
}

void CloudServiceClient::SendHeartbeat(const std::string& directory_id,
                                       std::string_view instance_identity_token,
                                       SendHeartbeatCallback callback) {
  constexpr char path[] = "/v1alpha/access:sendHeartbeat";

  auto request = std::make_unique<SendHeartbeatRequest>();
  request->set_directory_id(directory_id);
  request->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kSendHeartbeatTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback));
}

void CloudServiceClient::UpdateRemoteAccessHost(
    const std::string& directory_id,
    std::optional<std::string> host_version,
    std::optional<std::string> signaling_id,
    std::optional<std::string> offline_reason,
    std::optional<std::string> os_name,
    std::optional<std::string> os_version,
    std::string_view instance_identity_token,
    UpdateRemoteAccessHostCallback callback) {
  constexpr char path[] = "/v1alpha/access:updateRemoteAccessHost";

  auto host = std::make_unique<RemoteAccessHost>();

  host->set_directory_id(directory_id);
  if (host_version.has_value()) {
    host->set_version(*host_version);
  }
  if (signaling_id.has_value()) {
    auto parts = base::SplitStringUsingSubstr(
        *signaling_id, kFtlResourceSeparator, base::TRIM_WHITESPACE,
        base::SPLIT_WANT_ALL);
    if (parts.size() == 2) {
      host->mutable_tachyon_account_info()->set_account_id(std::move(parts[0]));
      host->mutable_tachyon_account_info()->set_registration_id(
          std::move(parts[1]));
    } else {
      LOG(WARNING) << "Invalid signaling_id provided: " << *signaling_id;
    }
  }
  if (offline_reason.has_value()) {
    host->set_offline_reason(*offline_reason);
  }
  if (os_name.has_value() && os_version.has_value()) {
    host->mutable_operating_system_info()->set_name(*os_name);
    host->mutable_operating_system_info()->set_version(*os_version);
  }
  host->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kUpdateRemoteAccessHostTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPatchMethod, std::move(host),
                 std::move(callback));
}

void CloudServiceClient::GenerateHostToken(
    std::string_view instance_identity_token,
    GenerateHostTokenCallback callback) {
  constexpr char path[] = "/v1alpha/sessionAuthz:generateHostToken";

  auto request = std::make_unique<GenerateHostTokenRequest>();
  request->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kGenerateHostTokenTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback));
}

void CloudServiceClient::GenerateIceConfig(
    std::string_view instance_identity_token,
    GenerateIceConfigCallback callback) {
  constexpr char path[] = "/v1alpha/networkTraversal:generateIceConfig";

  auto request = std::make_unique<GenerateIceConfigRequest>();
  request->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kGenerateIceConfigTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback));
}

void CloudServiceClient::VerifySessionToken(
    const std::string& session_token,
    std::string_view instance_identity_token,
    VerifySessionTokenCallback callback) {
  constexpr char path[] = "/v1alpha/sessionAuthz:verifySessionToken";

  auto request = std::make_unique<VerifySessionTokenRequest>();
  request->set_session_token(session_token);
  request->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kVerifySessionTokenTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback));
}

void CloudServiceClient::ReauthorizeHost(
    const std::string& session_reauth_token,
    const std::string& session_id,
    std::string_view instance_identity_token,
    scoped_refptr<const ProtobufHttpRequestConfig::RetryPolicy> retry_policy,
    ReauthorizeHostCallback callback) {
  constexpr char path[] = "/v1alpha/sessionAuthz:reauthorizeHost";

  auto request = std::make_unique<ReauthorizeHostRequest>();
  request->set_session_reauth_token(session_reauth_token);
  request->set_session_id(session_id);
  request->set_instance_identity_token(instance_identity_token);

  ExecuteRequest(kReauthorizeHostTrafficAnnotation, path, /*api_key=*/"",
                 net::HttpRequestHeaders::kPostMethod, std::move(request),
                 std::move(callback), std::move(retry_policy));
}

void CloudServiceClient::CancelPendingRequests() {
  http_client_.CancelPendingRequests();
}

template <typename CallbackType>
void CloudServiceClient::ExecuteRequest(
    const net::NetworkTrafficAnnotationTag& traffic_annotation,
    const std::string& path,
    const std::string& api_key,
    const std::string& method,
    std::unique_ptr<google::protobuf::MessageLite> request_message,
    CallbackType callback,
    scoped_refptr<const ProtobufHttpRequestConfig::RetryPolicy> retry_policy) {
  auto request_config =
      std::make_unique<ProtobufHttpRequestConfig>(traffic_annotation);
  request_config->path = path;
  if (api_key.empty()) {
    request_config->authenticated = true;
  } else {
    request_config->api_key = api_key;
    request_config->authenticated = false;
  }
  request_config->method = method;
  request_config->retry_policy = std::move(retry_policy);
  request_config->request_message = std::move(request_message);
  auto request =
      std::make_unique<ProtobufHttpRequest>(std::move(request_config));
  request->SetResponseCallback(std::move(callback));
  http_client_.ExecuteRequest(std::move(request));
}

// static
std::unique_ptr<CloudServiceClient>
CloudServiceClient::CreateForChromotingRobotAccount(
    OAuthTokenGetter* oauth_token_getter,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
  return base::WrapUnique(new CloudServiceClient(
      /*api_key=*/std::string(), oauth_token_getter,
      ServiceUrls::GetInstance()->remoting_cloud_private_endpoint(),
      url_loader_factory));
}

// static
std::unique_ptr<CloudServiceClient> CloudServiceClient::CreateForGcpProject(
    const std::string& api_key,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
  return base::WrapUnique(new CloudServiceClient(
      api_key,
      /*oauth_token_getter=*/nullptr,
      ServiceUrls::GetInstance()->remoting_cloud_public_endpoint(),
      url_loader_factory));
}

// static
std::unique_ptr<CloudServiceClient>
CloudServiceClient::CreateForGceDefaultServiceAccount(
    OAuthTokenGetter* oauth_token_getter,
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
  return base::WrapUnique(new CloudServiceClient(
      /*api_key=*/std::string(), oauth_token_getter,
      ServiceUrls::GetInstance()->remoting_cloud_public_endpoint(),
      url_loader_factory));
}

}  // namespace remoting