 Chromium Code Reviews
 Chromium Code Reviews Issue 1696563002:
  Blimp: add support for SSL connections.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1696563002:
  Blimp: add support for SSL connections.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: blimp/client/session/assignment_source.cc | 
| diff --git a/blimp/client/session/assignment_source.cc b/blimp/client/session/assignment_source.cc | 
| index 242d7839122af23edbbeef36cf87feeedd5821d2..e1ddc9b2eb8dec2380b8d0a8af29821bbb5491c7 100644 | 
| --- a/blimp/client/session/assignment_source.cc | 
| +++ b/blimp/client/session/assignment_source.cc | 
| @@ -7,11 +7,14 @@ | 
| #include "base/bind.h" | 
| #include "base/callback_helpers.h" | 
| #include "base/command_line.h" | 
| +#include "base/files/file_util.h" | 
| #include "base/json/json_reader.h" | 
| #include "base/json/json_writer.h" | 
| #include "base/location.h" | 
| +#include "base/memory/ref_counted.h" | 
| #include "base/numerics/safe_conversions.h" | 
| #include "base/strings/string_number_conversions.h" | 
| +#include "base/task_runner_util.h" | 
| #include "base/values.h" | 
| #include "blimp/client/app/blimp_client_switches.h" | 
| #include "blimp/common/protocol_version.h" | 
| @@ -40,7 +43,6 @@ const char kProtocolVersionKey[] = "protocol_version"; | 
| const char kClientTokenKey[] = "clientToken"; | 
| const char kHostKey[] = "host"; | 
| const char kPortKey[] = "port"; | 
| -const char kCertificateFingerprintKey[] = "certificateFingerprint"; | 
| const char kCertificateKey[] = "certificate"; | 
| // URL scheme constants for custom assignments. See the '--blimplet-endpoint' | 
| @@ -49,47 +51,6 @@ const char kCustomSSLScheme[] = "ssl"; | 
| const char kCustomTCPScheme[] = "tcp"; | 
| const char kCustomQUICScheme[] = "quic"; | 
| -Assignment GetCustomBlimpletAssignment() { | 
| - GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 
| - switches::kBlimpletEndpoint)); | 
| - | 
| - std::string host; | 
| - int port; | 
| - if (url.is_empty() || !url.is_valid() || !url.has_scheme() || | 
| - !net::ParseHostAndPort(url.path(), &host, &port)) { | 
| - return Assignment(); | 
| - } | 
| - | 
| - net::IPAddress ip_address; | 
| - if (!ip_address.AssignFromIPLiteral(host)) { | 
| - CHECK(false) << "Invalid BlimpletAssignment host " << host; | 
| - } | 
| - | 
| - if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { | 
| - CHECK(false) << "Invalid BlimpletAssignment port " << port; | 
| - } | 
| - | 
| - Assignment::TransportProtocol protocol = | 
| - Assignment::TransportProtocol::UNKNOWN; | 
| - if (url.has_scheme()) { | 
| - if (url.SchemeIs(kCustomSSLScheme)) { | 
| - protocol = Assignment::TransportProtocol::SSL; | 
| - } else if (url.SchemeIs(kCustomTCPScheme)) { | 
| - protocol = Assignment::TransportProtocol::TCP; | 
| - } else if (url.SchemeIs(kCustomQUICScheme)) { | 
| - protocol = Assignment::TransportProtocol::QUIC; | 
| - } else { | 
| - CHECK(false) << "Invalid BlimpletAssignment scheme " << url.scheme(); | 
| - } | 
| - } | 
| - | 
| - Assignment assignment; | 
| - assignment.transport_protocol = protocol; | 
| - assignment.ip_endpoint = net::IPEndPoint(ip_address, port); | 
| - assignment.client_token = kDummyClientToken; | 
| - return assignment; | 
| -} | 
| - | 
| GURL GetBlimpAssignerURL() { | 
| // TODO(dtrainor): Add a way to specify another assigner. | 
| return GURL(kDefaultAssignerURL); | 
| @@ -148,17 +109,15 @@ bool Assignment::is_null() const { | 
| } | 
| AssignmentSource::AssignmentSource( | 
| - const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, | 
| const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 
| - : main_task_runner_(main_task_runner), | 
| - url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)) {} | 
| + : io_task_runner_(io_task_runner), | 
| + url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)), | 
| + file_reader_(new FileReader) {} | 
| AssignmentSource::~AssignmentSource() {} | 
| void AssignmentSource::GetAssignment(const std::string& client_auth_token, | 
| const AssignmentCallback& callback) { | 
| - DCHECK(main_task_runner_->BelongsToCurrentThread()); | 
| - | 
| // Cancel any outstanding callback. | 
| if (!callback_.is_null()) { | 
| base::ResetAndReturn(&callback_) | 
| @@ -166,12 +125,83 @@ void AssignmentSource::GetAssignment(const std::string& client_auth_token, | 
| } | 
| callback_ = AssignmentCallback(callback); | 
| - Assignment assignment = GetCustomBlimpletAssignment(); | 
| - if (!assignment.is_null()) { | 
| - // Post the result so that the behavior of this function is consistent. | 
| - main_task_runner_->PostTask( | 
| - FROM_HERE, base::Bind(base::ResetAndReturn(&callback_), | 
| - AssignmentSource::Result::RESULT_OK, assignment)); | 
| + // Try to get a custom assignment on the IO thread first. | 
| + PostTaskAndReplyWithResult( | 
| + io_task_runner_.get(), FROM_HERE, | 
| + base::Bind(&AssignmentSource::GetCustomAssignment, | 
| + base::Unretained(this)), | 
| + base::Bind(&AssignmentSource::OnGetCustomAssignmentDone, | 
| + base::Unretained(this), client_auth_token)); | 
| +} | 
| + | 
| +scoped_ptr<Assignment> AssignmentSource::GetCustomAssignment() { | 
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 
| 
Ryan Sleevi
2016/02/23 00:49:45
It makes me deeply nervous to have an object that
 
Kevin M
2016/02/23 01:58:25
Made it an anonymous namespaced function and added
 | 
| + scoped_ptr<Assignment> assignment(new Assignment); | 
| + GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 
| + switches::kEngineEndpoint)); | 
| + | 
| + std::string host; | 
| + int port; | 
| + if (url.is_empty() || !url.is_valid() || !url.has_scheme() || | 
| + !net::ParseHostAndPort(url.path(), &host, &port)) { | 
| + return nullptr; | 
| + } | 
| + | 
| + net::IPAddress ip_address; | 
| + CHECK(ip_address.AssignFromIPLiteral(host)) << "Invalid Assignment host " | 
| + << host; | 
| + CHECK(port > 0 && port < 65535) << "Invalid IP port number: " << port; | 
| 
Ryan Sleevi
2016/02/23 00:49:45
Why do you check this, given url.is_valid()?
 
Kevin M
2016/02/23 01:58:25
Done.
 | 
| + | 
| + Assignment::TransportProtocol protocol = | 
| + Assignment::TransportProtocol::UNKNOWN; | 
| + if (url.has_scheme()) { | 
| 
Ryan Sleevi
2016/02/23 00:49:45
Why do you check this, given line 145?
 
Kevin M
2016/02/23 01:58:25
Done.
 | 
| + if (url.SchemeIs(kCustomSSLScheme)) { | 
| + protocol = Assignment::TransportProtocol::SSL; | 
| + } else if (url.SchemeIs(kCustomTCPScheme)) { | 
| + protocol = Assignment::TransportProtocol::TCP; | 
| + } else if (url.SchemeIs(kCustomQUICScheme)) { | 
| + protocol = Assignment::TransportProtocol::QUIC; | 
| + } else { | 
| + CHECK(false) << "Invalid engine protocol scheme " << url.scheme(); | 
| + } | 
| + } | 
| + | 
| + scoped_refptr<net::X509Certificate> cert; | 
| + if (protocol == Assignment::TransportProtocol::SSL || | 
| + protocol == Assignment::TransportProtocol::QUIC) { | 
| + base::FilePath cert_path = | 
| + base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 
| + switches::kEngineCertPath); | 
| + CHECK(!cert_path.empty()) << "Missing required parameter --" | 
| + << switches::kEngineCertPath << "."; | 
| + std::string cert_str; | 
| + CHECK(file_reader_->ReadFileToString(cert_path, &cert_str)) | 
| + << "Couldn't read from file: " << cert_path.LossyDisplayName(); | 
| + net::CertificateList cert_list = | 
| + net::X509Certificate::CreateCertificateListFromBytes( | 
| + cert_str.data(), cert_str.size(), | 
| + net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); | 
| + CHECK_EQ(1u, cert_list.size()) | 
| + << "Only one cert is allowed in PEM cert list."; | 
| + cert = cert_list[0]; | 
| + } | 
| + | 
| + assignment->transport_protocol = protocol; | 
| + assignment->ip_endpoint = | 
| + net::IPEndPoint(ip_address, base::checked_cast<uint16_t>(port)); | 
| 
Ryan Sleevi
2016/02/23 00:49:45
Why do you do this, given line 153?
 
Kevin M
2016/02/23 01:58:25
In the previous patch you recommended using checke
 | 
| + assignment->client_token = kDummyClientToken; | 
| + assignment->cert = cert; | 
| + return assignment; | 
| +} | 
| + | 
| +void AssignmentSource::OnGetCustomAssignmentDone( | 
| + const std::string& client_auth_token, | 
| + scoped_ptr<Assignment> custom_assignment) { | 
| + // If GetCustomAssignment succeeded, then return the custom assignment | 
| + // directly. | 
| + if (custom_assignment && !custom_assignment->is_null()) { | 
| + base::ResetAndReturn(&callback_) | 
| + .Run(AssignmentSource::RESULT_OK, *custom_assignment); | 
| 
Ryan Sleevi
2016/02/23 00:49:45
The API contract for using a heap-allocated custom
 
Kevin M
2016/02/23 01:58:25
Done.
 | 
| return; | 
| } | 
| @@ -191,12 +221,14 @@ void AssignmentSource::GetAssignment(const std::string& client_auth_token, | 
| std::string json; | 
| base::JSONWriter::Write(dictionary, &json); | 
| url_fetcher_->SetUploadData("application/json", json); | 
| - | 
| url_fetcher_->Start(); | 
| } | 
| +void AssignmentSource::SetFileReaderForTest(scoped_ptr<FileReader> reader) { | 
| + file_reader_ = std::move(reader); | 
| +} | 
| + | 
| void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { | 
| - DCHECK(main_task_runner_->BelongsToCurrentThread()); | 
| DCHECK(!callback_.is_null()); | 
| DCHECK_EQ(url_fetcher_.get(), source); | 
| @@ -272,12 +304,10 @@ void AssignmentSource::ParseAssignerResponse() { | 
| std::string client_token; | 
| std::string host; | 
| int port; | 
| - std::string cert_fingerprint; | 
| - std::string cert; | 
| + std::string cert_str; | 
| if (!(dict->GetString(kClientTokenKey, &client_token) && | 
| dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && | 
| - dict->GetString(kCertificateFingerprintKey, &cert_fingerprint) && | 
| - dict->GetString(kCertificateKey, &cert))) { | 
| + dict->GetString(kCertificateKey, &cert_str))) { | 
| base::ResetAndReturn(&callback_) | 
| .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); | 
| return; | 
| @@ -296,18 +326,37 @@ void AssignmentSource::ParseAssignerResponse() { | 
| return; | 
| } | 
| - Assignment assignment; | 
| + net::CertificateList cert_list = | 
| + net::X509Certificate::CreateCertificateListFromBytes( | 
| + cert_str.data(), cert_str.size(), | 
| + net::X509Certificate::FORMAT_PEM_CERT_SEQUENCE); | 
| + if (cert_list.size() != 1) { | 
| + base::ResetAndReturn(&callback_) | 
| 
Ryan Sleevi
2016/02/23 00:49:44
The heavy use of base::ResetAndReturn(...) makes m
 
Kevin M
2016/02/23 01:58:25
No, it's a safety practice my team has been using
 | 
| + .Run(AssignmentSource::Result::RESULT_INVALID_CERT, Assignment()); | 
| + return; | 
| + } | 
| + scoped_refptr<net::X509Certificate> cert = std::move(cert_list[0]); | 
| 
Ryan Sleevi
2016/02/23 00:49:45
Why do you std::move() here, only to copy-assign o
 
Kevin M
2016/02/23 01:58:25
:P Good point.
 | 
| + | 
| // The assigner assumes SSL-only and all engines it assigns only communicate | 
| // over SSL. | 
| + Assignment assignment; | 
| assignment.transport_protocol = Assignment::TransportProtocol::SSL; | 
| assignment.ip_endpoint = net::IPEndPoint(ip_address, port); | 
| assignment.client_token = client_token; | 
| - assignment.certificate = cert; | 
| - assignment.certificate_fingerprint = cert_fingerprint; | 
| + assignment.cert = cert; | 
| base::ResetAndReturn(&callback_) | 
| .Run(AssignmentSource::Result::RESULT_OK, assignment); | 
| } | 
| +FileReader::FileReader() {} | 
| + | 
| +FileReader::~FileReader() {} | 
| + | 
| +bool FileReader::ReadFileToString(const base::FilePath& path, | 
| + std::string* output) { | 
| + return base::ReadFileToString(path, output); | 
| +} | 
| + | 
| } // namespace client | 
| } // namespace blimp |