Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: blimp/client/session/assignment_source.cc

Issue 1696563002: Blimp: add support for SSL connections. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed extra deps from Dockerfile Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "blimp/client/session/assignment_source.h" 5 #include "blimp/client/session/assignment_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h"
8 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
9 #include "base/location.h" 11 #include "base/location.h"
10 #include "base/numerics/safe_conversions.h" 12 #include "base/numerics/safe_conversions.h"
11 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
12 #include "blimp/client/app/blimp_client_switches.h" 14 #include "blimp/client/app/blimp_client_switches.h"
15 #include "net/base/hash_value.h"
13 #include "net/base/ip_address.h" 16 #include "net/base/ip_address.h"
14 #include "net/base/ip_endpoint.h" 17 #include "net/base/ip_endpoint.h"
18 #include "net/cert/pem_tokenizer.h"
15 19
16 namespace blimp { 20 namespace blimp {
21 namespace client {
17 namespace { 22 namespace {
18 23
19 // TODO(kmarshall): Take values from configuration data. 24 // TODO(kmarshall): Take values from configuration data.
20 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; 25 const char kDummyClientToken[] = "MyVoiceIsMyPassport";
21 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; 26 const std::string kDefaultBlimpletIPAddress = "127.0.0.1";
22 const uint16_t kDefaultBlimpletTCPPort = 25467;
23 27
24 net::IPAddress GetBlimpletIPAddress() { 28 net::IPAddress GetBlimpletIPAddress() {
25 std::string host; 29 std::string host;
26 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 30 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
27 switches::kBlimpletHost)) { 31 switches::kEngineHost)) {
28 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 32 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
29 switches::kBlimpletHost); 33 switches::kEngineHost);
Wez 2016/02/18 00:40:50 This is super verbose; can't you just say: string
Kevin M 2016/02/18 23:35:46 Done.
30 } else { 34 } else {
31 host = kDefaultBlimpletIPAddress; 35 host = kDefaultBlimpletIPAddress;
32 } 36 }
33 net::IPAddress ip_address; 37 net::IPAddress ip_address;
34 if (!ip_address.AssignFromIPLiteral(host)) 38 if (!ip_address.AssignFromIPLiteral(host))
35 CHECK(false) << "Invalid BlimpletAssignment host " << host; 39 CHECK(false) << "Invalid BlimpletAssignment host " << host;
Wez 2016/02/18 00:40:50 Do you mean CHECK(ip_address.AssignFromIPLiteral(.
Kevin M 2016/02/18 23:35:46 Done.
36 return ip_address; 40 return ip_address;
37 } 41 }
38 42
39 uint16_t GetBlimpletTCPPort() { 43 // Puts the value of the command line parameter |param| in |output|.
40 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 44 // Returns true if the parameter was provided.
Wez 2016/02/18 00:40:50 This doesn't make clear what it does if the parame
Kevin M 2016/02/18 23:35:46 Done.
41 switches::kBlimpletTCPPort)) { 45 // CHECK-fails if the parameter was provided but invalid (must be numeric and
42 std::string port_str = 46 // within the range [1, 65535]
Wez 2016/02/18 00:40:50 Suggest: "CHECK()s that |param| decodes to a valid
Kevin M 2016/02/18 23:35:46 Done.
43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 47 bool GetUint16Parameter(const std::string& param, uint16_t* output) {
44 switches::kBlimpletTCPPort); 48 std::string param_str =
45 uint port_64t; 49 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(param);
46 if (!base::StringToUint(port_str, &port_64t) || 50 if (param_str.empty()) {
47 !base::IsValueInRangeForNumericType<uint16_t>(port_64t)) { 51 return false;
48 CHECK(false) << "Invalid BlimpletAssignment port " << port_str;
49 }
50 return base::checked_cast<uint16_t>(port_64t);
51 } else {
52 return kDefaultBlimpletTCPPort;
53 } 52 }
53
54 uint param_parsed = 0;
55 bool is_valid = base::StringToUint(param_str, &param_parsed) &&
56 base::IsValueInRangeForNumericType<uint16_t>(param_parsed) &&
57 param_parsed > 0;
58 CHECK(is_valid) << "Invalid range for parameter " << param;
Wez 2016/02/18 00:40:50 nit: Misleading; may also not be a number. nit: W
Kevin M 2016/02/18 23:35:46 Done.
59 *output = base::checked_cast<uint16_t>(param_parsed);
Wez 2016/02/18 00:40:50 No need to use checked_cast<> here, since |param_p
Kevin M 2016/02/18 23:35:46 Done.
60 return true;
61 }
62
63 // Reads the contents of |path| into |output|.
64 void ReadFromDisk(const base::FilePath& path, std::string* output) {
65 DCHECK(output);
66 CHECK(base::ReadFileToString(path, output)) << "Couldn't read from file: "
67 << path.LossyDisplayName();
68 }
69
70 // Parses a certificate from PEM-encoded |cert_str| and attaches it to
71 // |assignment|. Returns the populated assignment object via |callback|.
72 void ParseCertForAssignment(
73 scoped_ptr<std::string> cert_str,
74 scoped_ptr<Assignment> assignment,
75 const AssignmentSource::AssignmentCallback& callback) {
76 DCHECK(assignment);
Wez 2016/02/18 00:40:50 nit: This code dereferences |assignment| directly,
Kevin M 2016/02/18 23:35:46 Done.
77 DCHECK(cert_str);
78 DCHECK(!cert_str->empty());
79
80 net::PEMTokenizer pem_tokenizer(*cert_str, {"CERTIFICATE"});
81 while (pem_tokenizer.GetNext()) {
82 CHECK(!assignment->cert.get());
Wez 2016/02/18 00:40:50 nit: No need for .get() when testing a scoped[ref]
Kevin M 2016/02/18 23:35:47 Done.
83 assignment->cert = net::X509Certificate::CreateFromBytes(
84 pem_tokenizer.data().data(), pem_tokenizer.data().length());
85 CHECK(assignment->cert) << "Couldn't parse CERTIFICATE entry.";
86 }
87
88 callback.Run(*assignment);
54 } 89 }
55 90
56 } // namespace 91 } // namespace
57 92
58 namespace client { 93 Assignment::Assignment() {}
94
95 Assignment::~Assignment() {}
59 96
60 AssignmentSource::AssignmentSource( 97 AssignmentSource::AssignmentSource(
61 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) 98 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner)
62 : main_task_runner_(main_task_runner) {} 99 : main_task_runner_(main_task_runner) {}
63 100
64 AssignmentSource::~AssignmentSource() {} 101 AssignmentSource::~AssignmentSource() {}
65 102
103 scoped_refptr<base::SingleThreadTaskRunner>
104 AssignmentSource::GetIOTaskRunner() {
105 if (!io_thread_) {
106 io_thread_.reset(new base::Thread("CertFileThrad"));
107 base::Thread::Options options;
108 options.message_loop_type = base::MessageLoop::TYPE_IO;
Wez 2016/02/18 00:40:50 nit: Why not use the type+size constructor with th
Kevin M 2016/02/18 23:35:47 Done.
109 io_thread_->StartWithOptions(options);
110 }
111 return io_thread_->task_runner();
112 }
113
66 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { 114 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) {
67 DCHECK(main_task_runner_->BelongsToCurrentThread()); 115 DCHECK(main_task_runner_->BelongsToCurrentThread());
68 Assignment assignment; 116
69 assignment.ip_endpoint = 117 scoped_ptr<Assignment> assignment(new Assignment);
70 net::IPEndPoint(GetBlimpletIPAddress(), GetBlimpletTCPPort()); 118 assignment->client_token = kDummyClientToken;
71 assignment.client_token = kDummyClientToken; 119 assignment->ip_addresses.push_back(GetBlimpletIPAddress());
72 main_task_runner_->PostTask(FROM_HERE, base::Bind(callback, assignment)); 120 GetUint16Parameter(switches::kEngineTCPPort, &assignment->tcp_port);
121 if (GetUint16Parameter(switches::kEngineSSLPort, &assignment->ssl_port)) {
122 base::FilePath cert_path =
123 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
124 switches::kEngineCertPath);
125 CHECK(!cert_path.empty()) << "Missing required parameter --"
126 << switches::kEngineCertPath << ".";
127
128 scoped_ptr<std::string> cert_str(new std::string);
129 GetIOTaskRunner()->PostTaskAndReply(
130 FROM_HERE, base::Bind(&ReadFromDisk, cert_path, cert_str.get()),
131 base::Bind(&ParseCertForAssignment, base::Passed(std::move(cert_str)),
132 base::Passed(std::move(assignment)), callback));
Wez 2016/02/18 00:40:50 Eeek; you'll need to take the bare pointer to |cer
Kevin M 2016/02/18 23:35:47 Good catch, thanks! Done
133 } else {
134 callback.Run(*assignment);
135 }
73 } 136 }
74 137
75 } // namespace client 138 } // namespace client
76 } // namespace blimp 139 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698