Skip to content

Commit

Permalink
Add BlimpEngineConfig to verify command line switches and store config
Browse files Browse the repository at this point in the history
Initially this has the shared secret between the client and the engine.

BUG=575421

Review URL: https://codereview.chromium.org/1489413005

Cr-Commit-Position: refs/heads/master@{#369861}
  • Loading branch information
marcinjb authored and Commit bot committed Jan 15, 2016
1 parent 0918e57 commit 446acf6
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 5 deletions.
1 change: 1 addition & 0 deletions PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
(
r"^base[\\\/]process[\\\/]process_linux\.cc$",
r"^base[\\\/]process[\\\/]process_metrics_linux\.cc$",
r"^blimp[\\\/]engine[\\\/]browser[\\\/]blimp_browser_main_parts\.cc$",
r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]boot_times_recorder\.cc$",
r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]"
"customization_document_browsertest\.cc$",
Expand Down
12 changes: 12 additions & 0 deletions blimp/docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ docker run blimp_engine --with-my-flags

See the [blimp engine `Dockerfile`](../engine/Dockerfile) to find out what flags
are passed by default.

### Mapping Volumes into the Docker Container

If you need to map a directory into the Docker container (eg. for necessary
files):

```bash
docker run -v /path/to/srcdir:/path/to/docker/destdir blimp_engine
```

NB: The permissions of the directory and the files outside of the Docker
container will be carried over into the container.
4 changes: 4 additions & 0 deletions blimp/docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ client device via USB, you'll need remote port forwarding to allow the Blimp
client to talk to your computer. Follow the instructions
[here](https://developer.chrome.com/devtools/docs/remote-debugging) to get
started. You'll probably want to remap 25467 to "localhost:25467".

### Required flags
* `--blimp-client-token-path=$PATH`: Path to a file containing a nonempty
token string. If this is not present, the engine will fail to boot.
11 changes: 6 additions & 5 deletions blimp/engine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ RUN chown -R blimp_user /engine
USER blimp_user
WORKDIR "/engine"

ENTRYPOINT ["/engine/blimp_engine_app", "--disable-gpu", \
"--use-remote-compositing", \
# Retains display items for
# serialization on the engine.
"--disable-cached-picture-raster"]
ENTRYPOINT ["/engine/blimp_engine_app", \
"--disable-gpu", \
"--use-remote-compositing", \
# Retains display items for serialization on the engine.
"--disable-cached-picture-raster", \
"--blimp-client-token-path=/engine/data/client_token"]
5 changes: 5 additions & 0 deletions blimp/engine/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ source_set("browser") {
"blimp_browser_main_parts.h",
"blimp_content_browser_client.cc",
"blimp_content_browser_client.h",
"blimp_engine_config.cc",
"blimp_engine_config.h",
"blimp_engine_session.cc",
"blimp_engine_session.h",
"blimp_network_delegate.cc",
Expand All @@ -20,6 +22,8 @@ source_set("browser") {
"blimp_url_request_context_getter.h",
"engine_render_widget_feature.cc",
"engine_render_widget_feature.h",
"switches.cc",
"switches.h",
]

deps = [
Expand All @@ -44,6 +48,7 @@ source_set("unit_tests") {
testonly = true

sources = [
"blimp_engine_config_unittest.cc",
"engine_render_widget_feature_unittest.cc",
]

Expand Down
12 changes: 12 additions & 0 deletions blimp/engine/browser/blimp_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

#include "blimp/engine/browser/blimp_browser_main_parts.h"

#include "base/command_line.h"
#include "base/threading/thread_restrictions.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/engine/browser/blimp_browser_context.h"
#include "blimp/engine/browser/blimp_engine_config.h"
#include "blimp/engine/browser/blimp_engine_session.h"
#include "blimp/net/blimp_connection.h"
#include "content/public/browser/browser_thread.h"
Expand All @@ -30,6 +33,15 @@ void BlimpBrowserMainParts::PreEarlyInitialization() {
// TODO(haibinlu): Rename the method below. crbug/548330.
ui::InitializeInputMethodForTesting();
#endif
// Fetch the engine config from the command line, and crash if invalid. Allow
// IO operations even though this is not in the FILE thread as this is
// necessary for Blimp startup and occurs before any user interaction.
{
const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
base::ThreadRestrictions::ScopedAllowIO allow_io;
engine_config_ = BlimpEngineConfig::Create(*cmd_line);
CHECK(engine_config_);
}
}

void BlimpBrowserMainParts::PreMainMessageLoopRun() {
Expand Down
2 changes: 2 additions & 0 deletions blimp/engine/browser/blimp_browser_main_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace blimp {
namespace engine {

class BlimpBrowserContext;
class BlimpEngineConfig;
class BlimpEngineSession;

class BlimpBrowserMainParts : public content::BrowserMainParts {
Expand All @@ -37,6 +38,7 @@ class BlimpBrowserMainParts : public content::BrowserMainParts {
BlimpBrowserContext* GetBrowserContext();

private:
scoped_ptr<BlimpEngineConfig> engine_config_;
scoped_ptr<net::NetLog> net_log_;
scoped_ptr<BlimpEngineSession> engine_session_;

Expand Down
59 changes: 59 additions & 0 deletions blimp/engine/browser/blimp_engine_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "blimp/engine/browser/blimp_engine_config.h"

#include <string>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "blimp/engine/browser/switches.h"

namespace blimp {
namespace engine {

namespace {
// Gets the client token from the file provided by the command line. If a read
// does not succeed, or the switch is malformed, an empty string is returned.
std::string GetClientToken(const base::CommandLine& cmd_line) {
std::string file_contents;
const base::FilePath path = cmd_line.GetSwitchValuePath(kClientTokenPath);
if (!base::ReadFileToString(path, &file_contents)) {
LOG(ERROR) << "Could not read client token file at "
<< (path.empty() ? "(not provided)" : path.AsUTF8Unsafe());
}
return base::CollapseWhitespaceASCII(file_contents, true);
}
} // namespace

BlimpEngineConfig::~BlimpEngineConfig() {}

// static
scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::Create(
const base::CommandLine& cmd_line) {
const std::string client_token = GetClientToken(cmd_line);
if (!client_token.empty()) {
return make_scoped_ptr(new BlimpEngineConfig(client_token));
}
return nullptr;
}

// static
scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::CreateForTest(
const std::string& client_token) {
return make_scoped_ptr(new BlimpEngineConfig(client_token));
}

const std::string& BlimpEngineConfig::client_token() const {
return client_token_;
}

BlimpEngineConfig::BlimpEngineConfig(const std::string& client_token)
: client_token_(client_token) {}

} // namespace engine
} // namespace blimp
58 changes: 58 additions & 0 deletions blimp/engine/browser/blimp_engine_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_
#define BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_

#include <string>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"

namespace base {
class CommandLine;
} // namespace base

namespace blimp {
namespace engine {

// Class to hold all of the configuration bits necessary for the Blimp engine.
//
// The BlimpEngineConfig class is initialized from parameters provided on the
// command line. For the switches to pass verification:
// * A client token filepath must be provided and the file must have a
// non-empty token.
//
// The BlimpEngineConfig object is intended to live as long as the engine is
// running. It should also be one of the first things to be set up.
class BlimpEngineConfig {
public:
~BlimpEngineConfig();

// Attempts to create a BlimpEngineConfig based on the parameters in the
// specified CommandLine. This validates all of the command line switches
// and parses all files specified. Returns a non-null scoped_ptr on success.
static scoped_ptr<BlimpEngineConfig> Create(
const base::CommandLine& cmd_line);

// Creates a BlimpEngineConfig based on individual components. Should only
// be used for testing.
static scoped_ptr<BlimpEngineConfig> CreateForTest(
const std::string& client_token);

// Returns the client token.
const std::string& client_token() const;

private:
explicit BlimpEngineConfig(const std::string& client_token);

const std::string client_token_;

DISALLOW_COPY_AND_ASSIGN(BlimpEngineConfig);
};

} // namespace engine
} // namespace blimp

#endif // BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_
86 changes: 86 additions & 0 deletions blimp/engine/browser/blimp_engine_config_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "blimp/engine/browser/blimp_engine_config.h"

#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "blimp/engine/browser/switches.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blimp {
namespace engine {
namespace {

// Reference client token.
static const char kTestClientToken[] = "Reference client token";

class BlimpEngineConfigTest : public testing::Test {
protected:
void SetUp() override {
// Create a temporary directory and populate it with all of the switches
// If a test requires a switch's file to be missing, call
// RemoveFileForSwitch().
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
CreateFileForSwitch(kClientTokenPath, kTestClientToken);
}

// Creates a file in the temp directory for a given filepath switch.
void CreateFileForSwitch(const std::string& filepath_switch,
const std::string& data) const {
ASSERT_TRUE(base::WriteFile(GetFilepathForSwitch(filepath_switch),
data.c_str(), data.size()));
}

// Removes the associated file for a given filepath switch.
void RemoveFileForSwitch(const std::string& filepath_switch) const {
base::DeleteFile(GetFilepathForSwitch(filepath_switch), false);
}

// Creates and returns a CommandLine object with specified filepath switches.
base::CommandLine CreateCommandLine(
const std::vector<std::string>& filepath_switches) {
base::CommandLine::StringVector cmd_vec = {"dummy_program"};
for (const std::string& filepath_switch : filepath_switches) {
cmd_vec.push_back(base::StringPrintf(
"--%s=%s", filepath_switch.c_str(),
GetFilepathForSwitch(filepath_switch).AsUTF8Unsafe().c_str()));
}
return base::CommandLine(cmd_vec);
}

base::FilePath GetFilepathForSwitch(
const std::string& filepath_switch) const {
return temp_dir_.path().Append(filepath_switch);
}

const std::vector<std::string> all_filepath_switches_ = {kClientTokenPath};

base::ScopedTempDir temp_dir_;
};

TEST_F(BlimpEngineConfigTest, ClientTokenCorrect) {
auto cmd_line = CreateCommandLine(all_filepath_switches_);
auto engine_config = BlimpEngineConfig::Create(cmd_line);
EXPECT_NE(nullptr, engine_config);
EXPECT_EQ(kTestClientToken, engine_config->client_token());
}

TEST_F(BlimpEngineConfigTest, ClientTokenEmpty) {
RemoveFileForSwitch(kClientTokenPath);
CreateFileForSwitch(kClientTokenPath, " ");
auto cmd_line = CreateCommandLine(all_filepath_switches_);
auto engine_config = BlimpEngineConfig::Create(cmd_line);
EXPECT_EQ(nullptr, engine_config);
}

} // namespace
} // namespace engine
} // namespace blimp
13 changes: 13 additions & 0 deletions blimp/engine/browser/switches.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "blimp/engine/browser/switches.h"

namespace blimp {
namespace engine {

const char kClientTokenPath[] = "blimp-client-token-path";

} // namespace engine
} // namespace blimp
17 changes: 17 additions & 0 deletions blimp/engine/browser/switches.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BLIMP_ENGINE_BROWSER_SWITCHES_H_
#define BLIMP_ENGINE_BROWSER_SWITCHES_H_

namespace blimp {
namespace engine {

// Path to the client token/shared secret between the engine and the client.
extern const char kClientTokenPath[];

} // namespace engine
} // namespace blimp

#endif // BLIMP_ENGINE_BROWSER_SWITCHES_H_

0 comments on commit 446acf6

Please sign in to comment.